/******************************************************************************************** 03-B.cpp Jim Millard for CO311 Uses the ezwin API ********************************************************************************************/ #include <iostream> #include <string> using namespace std; #include "ezwin.h" #include "rect.h" void DrawCheckerboard(SimpleWindow& W, const color, const color); void BlockPosition(int row, int column, float blockHeight, float blockWidth, float& windowX, float& windowY); color SetColor(int row, int column, color, color); int ApiMain() { cout << "This program will display a checkerboard using two colors that you choose." << endl; << "Please choose two colors from the following list: " << endl << "0-Black, 1-White, 2-Red, 3-Green, 4-Blue, 5-Yellow, 6-Cyan, 7-Magenta" << endl; int BaseColor, AltColor; cout << "Choose the first color: "; cin >> BaseColor; cout << "Choose the second color: "; cin >> AltColor; //create the primary window SimpleWindow W("Practical 3, Problem B"); W.Open(); DrawCheckerboard(W, (color)BaseColor, (color)AltColor); //wait for input prior to quitting char ch; cout << "Enter a character to continue"; cin >> ch; Terminate(); return 0; } // //this function draws the checkerboard // void DrawCheckerboard(SimpleWindow& W, const color BaseColor, const color AltColor) { //A stack of useful constants that can later be replaced by function parameters const float height=1., //static height of all blocks width =1.; //static width of all blocks const int BlocksAcross=8, //static width of checkerboard BlocksDown =8; //static height of checkerboard color currentColor; //the color we're drawing now float absX, absY; //the position of the block in the window //Draw by rows for(int row = 0; row < BlocksDown; row++) { //Draw the rectangles across the row for (int column = 0; column < BlocksAcross; column++) { //set the color based on its position currentColor = SetColor(row, column, BaseColor, AltColor); //calculate the position of the block in the display window BlockPosition(row, column, height, width, absX, absY); //create the block with relevant parameters RectangleShape Rxy(W, absX, absY, currentColor, width, height); //dray the block Rxy.Draw(); } //inner for loop } //outer for loop return; } //DrawCheckerboard // //this function automates the discovery of the EXACT position where the block will be placed //based on the row/column "general" placement, and the block's height and width. //This routine assumes that all blocks will ultimately be of identical height and that //the first row/column is (0,0) and the origin (0,0) is in the upper-left corner. // void BlockPosition(int row, int column, float blockHeight, float blockWidth, float& windowX, float& windowY) { windowX = column*blockWidth + blockWidth/2; windowY = row*blockHeight + blockHeight/2; } // //this function sets the color of the square based on it's position in the checkerboard //it takes advantage of the coincicence between the sum of row & column for the block and //whether that sum is even or odd. // color SetColor(int row, int column, color color_1, color color_2) { switch ((row + column)%2) { case 1: return color_2; default: return color_1; } }