Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Objective: Draw an 8-by-8, red and white checkers board
- #include "std_lib_facilities.h"
- #include <iostream>
- #include "Simple_window.h"
- int main(){
- try{
- // create a window in the center of the screen wiht size: 600x600
- int wWidth = 660;
- int wHeigth = 660;
- Point centerScreen(x_max()/2 - wWidth/2, y_max()/2 - wHeigth/2);
- Simple_window sw(centerScreen, 660, 660, "Chapter 12 Exercise 3");
- // starting upper left coordinates of the window + 10 pixels frame
- int tlx = sw.x_max() - 650;
- int tly = sw.y_max() - 650;
- // Point topLeftCheckers(tlx, tly);
- // sqrSize == step of drawing
- int sqrSize = 80;
- // vector holding all (pointes to) rectangles
- vector<Graph_lib::Rectangle*> rects;
- int numOfSquares = 64;
- rects.reserve(numOfSquares);
- // instantiate all the squares as rectangle objects with: heigth = width
- int numOfColumns = 8;
- int numOfRows = 8;
- for(size_t i=0; i < numOfColumns; ++i){
- for(size_t j=0; j < numOfColumns; ++j){
- // create a 64 conjugate squares with size = 80,
- Graph_lib::Rectangle* r = new Graph_lib::Rectangle(Point(tlx + sqrSize*i, tly + sqrSize*j), sqrSize, sqrSize);
- // fill the consequtive squares with white or red color
- if ((i+j) % 2 == 0) r->set_fill_color(Color::red);
- else r->set_color(Color::white);
- // save object in vector
- rects.push_back(r);
- }
- }
- // attach all the rectangle objects to the window object
- for(size_t k=0; k < rects.size(); ++k) sw.attach(*rects[k]);
- sw.wait_for_button();
- }catch(exception& e){
- cerr << e.what() << endl;
- getchar();
- }catch(...){
- cerr <<"Default exception!"<< endl;
- getchar();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement