Thursday, 19 March 2020

Chapter 12 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 4

Draw a 3-by-3 tic-tac-toe board of alternating white and red squares.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%204

I got confused on this just because I started over-complicating it. I started thinking of creating it as 8 lines that intersect but pondered about how to colour the parts in-between. Instead I decided to create a function that creates and fills a rectangle with just the top left co-ordinate given. 

I also don't like how set_color() means set the line colour. To me it's quite ambiguous and alludes to the fill colour more.

Another thing, you can't return any object that is a type of Shape as its copy constructor has been explicitly deleted. MSDN says people do this to prevent behaviour that they don't want but in this case I can't see why creating a rectangle and then returning it would be so heinous. Clearly it was done for a reason, however, I decided to be naughty and commented out the line in Shape (in my files it's lines 176 and 177).

After a frustrating 20 minutes of nothing appearing I was ready to just give up and do it the long way, however, when stepping through the code, the rectangles were being created and attached to the window in the for loop, but, on the last rectangle all the values got cleared in the window and therefore nothing was drawn. For some reason a range-based for loop 'resets' the window's shapes. I think it has something to do with Shape's default destructor being called as r will only be in scope during the loop. Anyway, a standard for loop works fine and we don't have to repeat rectangle code constantly.

(16/08/2021 - Note from future me...it didn't work because you allowed it to use the copy constructor which had been deleted...creating a copy that goes out of scope.....)


Chapter 12 // Exercise 4 - Principles & Practice Using C++

(This is very umbrella looking.....)

No comments:

Post a Comment