/* A simple program written in the style of a first assignment to exercise some of the features of the Graphics package and introduce procedural decomposition, loops, and graphics object member functions. */ #include #include #include #include #include "127GraphicsSettings.h" // defines do_console parameters #include "127Graphics.h" // the old graphics package #include "Graphics.h" // the new wrapper #include "apmatrix.h" #include "apvector.h" #include "apstring.h" void playerColor(int playerNum, apmatrix colors, bool useColors, int &rrgb, int &rggb, int &rgbb); // global window object window W; int main() { srand(time(0)); int width = 10, height = 10, countX, countY, dispX = 100, dispY = 100, cWidth = 20, cHeight = 20; int numP, count, whoseTurn, x, y, rrgb, rggb, rgbb, sumTotal, temp, maximum, countRot, debugLevel, x2, y2; int turn, numTurns = 24; apmatrix field; apmatrix colors; char key; float waiting; apvector names; apvector moves(8,0); apvector moveX(8,0); apvector moveY(8,0); apvector numCells; moveX[0] = -1; moveX[1] = 0; moveX[2] = 1; moveX[3] = 1; moveX[4] = 1; moveX[5] = 0; moveX[6] = -1; moveX[7] = -1; moveY[0] = -1; moveY[1] = -1; moveY[2] = -1; moveY[3] = 0; moveY[4] = 1; moveY[5] = 1; moveY[6] = 1; moveY[7] = 0; bool done = false, validMove, crazy = false, art = false, rotate = true, useColors = false, hiyas; Point p; // The following statement must come before any code! do_console(); // sets parameters for console window cout << "Choose level of setting parameters:\n1: Quick Start\n2: Customizable\n3: Fully Customizable\n"; cout << "Enter your choice now: "; cin >> debugLevel; if (debugLevel == 3) { cout << "What is the cell width (20 recommended)? "; cin >> cWidth; cout << "And what is the cell height (20 recommended)? "; cin >> cHeight; } if (debugLevel >= 2) { cout << "Enter the width of the playing field: "; cin >> width; cout << "And what is the height: "; cin >> height; } if (debugLevel == 3) { cout << "What is the wait time (0-10, 2 recommended)? "; cin >> waiting; cout << "Use rotation (y/n, y default)? "; cin >> key; if (key == 'n' || key == 'N') rotate = false; cout << "Play in (C)rAzY mode (automatic moves), (A)rT mode (full automatic), or (N)ormal? "; cin >> key; if (key == 'y' || key == 'Y') crazy = true; else if (key == 'a' || key == 'A') { art = true; crazy = true; } else if (key == 'c' || key == 'C') crazy = true; } cout << "Number of players (2-100): "; cin >> numP; if (debugLevel >= 2) { cout << "Do you wish to select your colors (y/n, default n)? "; cin >> key; if (key == 'y' || key == 'Y') { useColors = true; colors.resize(numP, 3); cout << "Pick your red and blue components from the matrix on the left. The vector on the right contains\n"; cout << "the green component, and will dynamically change to show which color you are currently using.\n"; for (x = 0; x <= 400; x++) { for (y = 0; y <= 400; y++) { W.SetBrush(int(x * 65535/400), int(y * 65535/400), 0); W.DrawPixel(x, y); } } for (x = 0; x < numP; x++) { cout << "Player " << x + 1 << " pick your color now.\n"; done = false; hiyas = false; while (!done) { while (!FoundAClick(&p) && !hiyas) { for (y = 0; y <= 400; y++) { W.SetBrush(int(x2 * 65535/400), int(y2 * 65535/400), int(y * 65535/400)); W.DrawLine(425, y, 450, y); } W.GetMouseCoord(x2, y2); } x2 = p.h; y2 = p.v; if (x2 >= 0 && x2 <= 400 && y2 >= 0 && y2 <= 400) { colors[x][0] = int(x2 * 65535/400); colors[x][1] = int(y2 * 65535/400); hiyas = true; } if (x2 >= 425 && x2 <= 450 && y2 >= 0 && y2 <= 400) { colors[x][2] = int(y2 * 65535/400); done = true; } } } } } numCells.resize(numP); for (x = 0; x < numP; x++) numCells[x] = 0; names.resize(numP); cin.ignore(100, '\n'); if (!art) { cout << "Enter the names now:\n"; for (count = 0; count <= numP - 1; count++) { cout << "Player " << count + 1 << ": "; getline(cin, names[count]); } } cout << "Click anywhere to begin!\n"; W.SetBrush(65535, 65535, 65535); W.DrawRectangle(0, 0, 2000, 2000); field.resize(width, height); for (countX = 0; countX < width; countX++) for (countY = 0; countY < height; countY++) field[countX][countY] = 999999; if (!art) { W.SetPen(2); W.SetBrush(20000, 0, 40000); for (countX = dispX; countX <= dispX + width * cWidth; countX += cWidth) W.DrawLine(countX, dispY, countX, dispY + height * cHeight); for (countY = dispY; countY <= dispY + height * cHeight; countY += cHeight) W.DrawLine(dispX, countY, dispX + width * cWidth, countY); } if (!art) { W.SetBrush(0, 40000, 20000); W.DrawRectangle(dispX, dispY + 4, dispX + width * cWidth + 4, dispY); W.DrawRectangle(dispX, dispY + cHeight * height, dispX + width * cWidth + 4, dispY + cHeight * height + 4); W.DrawRectangle(dispX, dispY, dispX + 4, dispY + cHeight * height); W.DrawRectangle(dispX + cWidth * width, dispY, dispX + 4 + cWidth * width, dispY + cHeight * height); } whoseTurn = 0; key = (char)60; done = false; while (!done) { playerColor(whoseTurn, colors, useColors, rrgb, rggb, rgbb); W.SetBrush(rrgb, rggb, rgbb); W.SetFont(40, 3, 2); W.SetBrush(65535, 65535, 65535); W.DrawRectangle(0, 0, 400, 100); for (temp = 0; temp < numP; temp++) W.DrawRectangle(600 + 40 * temp, 25, 600 + 40 * (temp + 1), 55); W.SetBrush(rrgb, rggb, rgbb); W.DrawString(10,40,names[whoseTurn]); for (count = 0; count <= 7; count++) { moves[count] = rand()%2; } W.SetBrush(55000, 55000, 55000); W.DrawRectangle(380, 25, 440, 85); W.SetBrush(1000, 55000, 1000); W.DrawRectangle(400, 45, 420, 65); W.SetBrush(55000, 1000, 1000); for (count = 0; count <= 7; count++) { if (moves[count] == 1) W.DrawRectangle(400 + 20 * moveX[count], 45 + 20 * moveY[count], 420 + 20 * moveX[count], 65 + 20 * moveY[count]); } W.SetBrush(0, 0, 65535); W.DrawRectangle(450, 10, 500, 60); W.SetBrush(65535, 0, 0); W.SetFont(14, 13, 2); W.DrawString(452, 37, "QUIT"); W.SetFont(14, 0, 0); for (temp = 0; temp < numP; temp++) { playerColor(temp, colors, useColors, rrgb, rggb, rgbb); W.SetBrush(rrgb, rggb, rgbb); W.DrawRectangle(603 + 40 * temp, 30, 610 + 40 * temp, 40); W.SetBrush(0, 0, 0); W.DrawInteger(612 + 40 * temp, 42, numCells[temp]); } playerColor(whoseTurn, colors, useColors, rrgb, rggb, rgbb); W.SetBrush(rrgb, rggb, rgbb); validMove = false; countRot = 0; turn = 1; while (!validMove && !done && turn <= numTurns) { validMove = false; while (!FoundAClick(&p) && !art && turn <= numTurns && !crazy) { if (countRot == 700 && rotate) { W.SetBrush(65535, 65535, 65535); W.DrawRectangle(800, 10, 1000, 70); W.SetBrush(65535, 0, 0); W.SetFont(18, 2, 3); W.DrawInteger(800, 60, turn); turn++; countRot = 0; temp = moves[0]; moves[0] = moves[1]; moves[1] = moves[2]; moves[2] = moves[3]; moves[3] = moves[4]; moves[4] = moves[5]; moves[5] = moves[6]; moves[6] = moves[7]; moves[7] = temp; W.SetBrush(55000, 55000, 55000); W.DrawRectangle(380, 25, 440, 85); W.SetBrush(1000, 55000, 1000); W.DrawRectangle(400, 45, 420, 65); W.SetBrush(55000, 1000, 1000); for (count = 0; count <= 7; count++) { if (moves[count] == 1) W.DrawRectangle(400 + 20 * moveX[count], 45 + 20 * moveY[count], 420 + 20 * moveX[count], 65 + 20 * moveY[count]); } } countRot++; } if (!crazy || art) { x = p.h; y = p.v; } else { W.GetMouseCoord(x, y); } if (turn <= numTurns) { validMove = true; if (x < dispX || y < dispY || x > dispX + cWidth * width || y > dispY + cHeight * height) validMove = false; if (x >= 450 && x <= 500 && y >= 10 && y <= 60) { done = true; } else { if (!art) { x = int((x - dispX) / cWidth); y = int((y - dispY) / cHeight); } else { x = rand()%width; y = rand()%height; } if (x < 0 || x >= field.numrows() || y < 0 || y >= field.numcols()) validMove = false; else if (field[x][y] != 999999) validMove = false; } countRot++; } } if (turn > numTurns) { W.SetBrush(65535, 65535, 65535); W.DrawRectangle(800, 10, 1000, 70); W.SetBrush(65535, 0, 0); W.SetFont(12, 2, 3); W.DrawString(800, 60, "Turn over, click to continue."); W.WaitMouseClick(x, y); } if (!done && turn <= numTurns) { playerColor(whoseTurn, colors, useColors, rrgb, rggb, rgbb); W.SetBrush(rrgb, rggb, rgbb); field[x][y] = whoseTurn; W.DrawRectangle(x * cWidth + dispX, y * cHeight + dispY, (x+1) * cWidth + dispX, (y+1) * cHeight + dispY); numCells[whoseTurn]++; for (count = 0; count <= 7; count++) { if ((x + moveX[count] >= 0) && (x + moveX[count] < width) && (y + moveY[count] >= 0) && (y + moveY[count] < height)) { if (moves[count] == 1 && int(field[x + moveX[count]][y + moveY[count]]) != whoseTurn) { numCells[whoseTurn]++; if (field[x + moveX[count]][y + moveY[count]] < numP) { numCells[int(field[x + moveX[count]][y + moveY[count]])]--; field[x + moveX[count]][y + moveY[count]] = field[x + moveX[count]][y + moveY[count]] - int(field[x + moveX[count]][y + moveY[count]]) + .01 + whoseTurn; } else field[x + moveX[count]][y + moveY[count]] = whoseTurn + .01; clock_t waitTime = clock(); while (clock() - waitTime <= waiting) {} W.DrawRectangle((x + moveX[count]) * cWidth + dispX, (y + moveY[count]) * cHeight + dispY, (x + moveX[count] + 1) * cWidth + dispX, (y + moveY[count]+1) * cHeight + dispY); } } } } whoseTurn++; if (whoseTurn >= numP) whoseTurn = 0; sumTotal = 0; for (x = 0; x < numP; x++) sumTotal += numCells[x]; if (sumTotal >= width * height) done = true; } W.SetBrush(65535, 65535, 65535); for (temp = 0; temp < numP; temp++) W.DrawRectangle(600 + 40 * temp, 25, 600 + 40 * (temp + 1), 55); W.SetFont(14, 0, 0); for (temp = 0; temp < numP; temp++) { playerColor(temp, colors, useColors, rrgb, rggb, rgbb); W.SetBrush(rrgb, rggb, rgbb); W.DrawRectangle(603 + 40 * temp, 30, 610 + 40 * temp, 40); W.SetBrush(0, 0, 0); W.DrawInteger(612 + 40 * temp, 42, numCells[temp]); } // End the program maximum = 0; y = 999999; for (x = 0; x < numP; x++) { if (maximum < numCells[x]) { y = x; maximum = numCells[x]; } else if (maximum == numCells[x]) { y = 999999; } } if (!art) { playerColor(y, colors, useColors, rrgb, rggb, rgbb); W.SetBrush(65535, 65535, 65535); W.DrawRectangle(125, 125, 800, 250, FILLED, 50, 50); W.SetBrush(rrgb, rggb, rgbb); W.SetFont(100, 7, 3); if (y < 999999) W.DrawString(135, 220, names[y] + " Wins!!!"); else W.DrawString(135, 220, "TIE!!!!!"); } cout << "\nThis program is OVER!\n"; return 0; } void playerColor(int playerNum, apmatrix colors, bool useColors, int &rrgb, int &rggb, int &rgbb) { if (useColors) { rrgb = colors[playerNum][0]; rggb = colors[playerNum][1]; rgbb = colors[playerNum][2]; } else { rrgb = (playerNum*53082)%65535; rggb = (playerNum*29826)%65535; rgbb = (playerNum*12742)%65535; } return; }