using loops for drawing a checkers board

Hello all,

This is the text of exercise 4 of chapter 12 of PPP;
Draw a checkers board: 8-by-8 alternating white and red squares.

Is there a way to use the loop for doing that code to minimize the size of the code?
Last edited on
I presume you're using a GUI? What OS and what IDE are you using?
My OS is Windows 7 and my IDE is visual studio 2012? Have you read the PPP and especially that chapter?
Have you read the PPP and especially that chapter?


No.

Perhaps you could either provide the block of code in question or a link to it, if it is available online.

Thinking logically, you could have a function to draw a coloured square at the required screen coordinates. Then you just need to iterate through a matrix calling the function with the x and y coordinates for where the square is to be drawn, the size of a square side, and colour.
This is the block I should write for that problem;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <Graph.h>
#include <Simple_window.h>

int main()
{
	Point t(100,100);
	Simple_window win(t, 600,400, "My window");

	Graph_lib::Rectangle r1(Point(20,20),20,20);
	 r1.set_fill_color(Color::red);
	 win.attach(r1);

	win.wait_for_button();
}

//************************* 


The code written in line 9 through 11 is for only one square. And for doing the exercise I should repeat that snip code for 64 times! and that was the reason that I thought about a loop.
Go for it...
I imagine it would look something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <deque>

#include <Graph.h>
#include <Simple_window.h>

typedef Graph_lib::Rectangle Rect ;

int main()
{
    Simple_window win(Point(100,100), 600, 400, "My window") ;

    std::deque<Rect> rectangles ;

    for (int x = 0; x < 8; ++x)
    {
         for (int y=0; y < 8; ++y)
         {
             Rect rect(Point(x*20, y*20), 20, 20);
             if ( y % 2 )
                  rect.set_fill_color( x % 2 ? Color::red : Color::black ) ;
             else
                  rect.set_fill_color( x % 2 ? Color::black : Color::red ) ;
             rectangles.push_back(rect) ;
             win.attach(&rectangles.back()) ;
         }
    }
    
    win.wait_for_button() ;
}
Thank you "cire",
I haven't learned about #include <deque> or deque<Rect> rectangles ; until now in this book, so I can't use of them. But anyway, I appreciate your answer.
I have a hard time believing you weren't introduced to containers of some sort by the time you hit that point in the book, although I can well believe you weren't introduced to the the deque.

You may treat it almost the same as a vector, although it avoids a problem in this code that a vector would not. (Mainly that a push_back may cause reallocation and therefore relocation of elements so that the address fed to win.attach() is no longer valid.)
I have a hard time believing you weren't introduced to containers of some sort by the time you hit that point in the book


Yeah, I weren't introduced to that facility until now in this book!

When I recalled the vectors I immediately thought about some code like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() 
{
	Point t(100,100);
	Simple_window win(t, 600,400, "My window");

	vector<Graph_lib::Rectangle> VR(64);
	VR[0].(Point(20,20),20,20);
	 VR[0].set_fill_color(Color::red);
	 win.attach(VR[0]);
	 // some more codes.

	win.wait_for_button();
}


But it also faces errors in line 7.
Last edited on
Topic archived. No new replies allowed.