Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. // Objective: Draw an 8-by-8, red and white checkers board
  2.  
  3. #include "std_lib_facilities.h"
  4. #include <iostream>
  5. #include "Simple_window.h"
  6.  
  7. int main(){
  8. try{
  9. // create a window in the center of the screen wiht size: 600x600
  10. int wWidth = 660;
  11. int wHeigth = 660;
  12. Point centerScreen(x_max()/2 - wWidth/2, y_max()/2 - wHeigth/2);
  13. Simple_window sw(centerScreen, 660, 660, "Chapter 12 Exercise 3");
  14.  
  15. // starting upper left coordinates of the window + 10 pixels frame
  16. int tlx = sw.x_max() - 650;
  17. int tly = sw.y_max() - 650;
  18. // Point topLeftCheckers(tlx, tly);
  19. // sqrSize == step of drawing
  20. int sqrSize = 80;
  21.  
  22. // vector holding all (pointes to) rectangles
  23. vector<Graph_lib::Rectangle*> rects;
  24. int numOfSquares = 64;
  25. rects.reserve(numOfSquares);
  26.  
  27. // instantiate all the squares as rectangle objects with: heigth = width
  28. int numOfColumns = 8;
  29. int numOfRows = 8;
  30. for(size_t i=0; i < numOfColumns; ++i){
  31. for(size_t j=0; j < numOfColumns; ++j){
  32. // create a 64 conjugate squares with size = 80,
  33. Graph_lib::Rectangle* r = new Graph_lib::Rectangle(Point(tlx + sqrSize*i, tly + sqrSize*j), sqrSize, sqrSize);
  34. // fill the consequtive squares with white or red color
  35. if ((i+j) % 2 == 0) r->set_fill_color(Color::red);
  36. else r->set_color(Color::white);
  37. // save object in vector
  38. rects.push_back(r);
  39. }
  40. }
  41. // attach all the rectangle objects to the window object
  42. for(size_t k=0; k < rects.size(); ++k) sw.attach(*rects[k]);
  43.  
  44. sw.wait_for_button();
  45.  
  46. }catch(exception& e){
  47. cerr << e.what() << endl;
  48. getchar();
  49.  
  50. }catch(...){
  51. cerr <<"Default exception!"<< endl;
  52. getchar();
  53. }
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement