/******************************************************************************************** Step6.cpp Jim Millard for CO311/Spring 2000 Simple program that displays a 4x4 Blue/Red checkerboard
Uses the ezwin API ********************************************************************************************/ #include <iostream> #include <string> using namespace std; #include "ezwin.h" #include "rect.h" void DrawCheckerboard(SimpleWindow& W); //required prototype int ApiMain() { //create the primary window SimpleWindow W("Exercise 3.22, Step 6 (complete)"); W.Open(); DrawCheckerboard(W); //wait for input prior to quitting char ch; cout << "Enter a character to continue"; cin >> ch; Terminate(); return 0; }
void DrawCheckerboard(SimpleWindow& W) { //A stack of useful constants that can later be replaced by function parameters const float height=2., //static height of all blocks width =2., //static width of all blocks xCenter =width/2., //horizontal center of each block yCenter =height/2.; //vertical center of each block const int BlocksAcross =4, //static width of checkerboard BlocksDown =4; //static height of checkerboard const color BaseColor = Blue, //static color of first block AltColor = Red; //static color of alternate block bool doAltColor; //keeps track of whether we should display in the alternate color color currentColor; //the color we're drawing now
//Draw by rows doAltColor = false; //start with the base color for(int row = 0; row < BlocksDown; row++) { //Draw the rectangles across the row for (int column = 0; column < BlocksAcross; column++) { //set the color if (doAltColor) currentColor = AltColor; else currentColor = BaseColor; doAltColor = !doAltColor; RectangleShape Rxy(W, xCenter + width*column, yCenter + row*height, currentColor, width, height); Rxy.Draw(); } //inner for loop //reverse the color flag if the number of columns is even if (!(BlocksAcross%2)) doAltColor = !doAltColor; } //outer for loop return; } //DrawCheckerboard