Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Author: [redacted]
- *Date: 12/02/2019
- *Instructor: Dr. Thompson
- *Description: Anthony leads the army of Colony A in conquest of the other colonies.*/
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- const int SIZE = 10;
- //prints header
- void printHeader() {
- cout << "\n+------------------------------------------------------+\n"
- << "| Computer Science and Engineering |\n"
- << "| CSCE 1030 - Computer Science I |\n"
- << "| [redacted] |\n"
- << "+--------------------------------------------------------+\n" << endl;
- }
- //prints description of game
- void printIntro() {
- printf("\n%35s\n\n", "Welcome to Anthony's Battle!");
- printf("----------------------------------------------------------\n");
- cout << "Anthony the ant has decided that he wants to take over all\n"
- << "the adjacent ant colonies with his army of ants from colo-\n"
- << "ny \'A\'. To do this, Anthony's army will attempt to capture\n"
- << "ants from columns \'B\' through \'J\', with at least 1 ant re-\n"
- << "maining when the column \'J\' ant colony is found. Each col-\n"
- << "umn's ant colony is identified with a - in one of the rows\n"
- << "for that column, while patrols are represented by integers\n"
- << "between 1 and 10 for the number of ants in that particular\n"
- << "patrol. On the way to column \'J\' if Anthony's army encoun-\n"
- << "ters a patrol, the number in the patrol is subtracted from\n"
- << "Anthony's army total. Once his army finds the colony, how-\n"
- << "ever, any patrols not confronted will be added to his army\n"
- << "total. If Anthony makes really good decisions, then he can\n"
- << "take over all of the adjacent ant colonies; otherwise, his\n"
- << "army will be defeated!" << endl;
- printf("----------------------------------------------------------\n");
- }
- //initializes colonies matrix
- void populateColonies(int** colonies, int patrolSize) {
- srand(time(0)); //seeds rand
- int colPos; //will hold random int representing position of colony
- //iterates through each column in the colonies matrix
- for(int col = 0; col < SIZE; col++) {
- //iterates through each position in each column
- for(int row = 0; row < SIZE; row++) {
- //assigns each patrol's size a to a number between 1 and 10 (inc.)
- patrolSize = rand() % 10 + 1;
- colonies[row][col] = patrolSize;
- }
- //randomly determines position of colony and sets to 0
- colPos = rand() % 10;
- colonies[colPos][col] = 0;
- }
- }
- //displays matrix of colonies
- void displayColonies(int** colonies, int currentCol) {
- cout << " ";
- //print column headers as letters referring to colonies
- for(char i = 'A'; i <= 'J'; i++) {
- printf("%3c", i);
- }
- cout << "\n +-------------------------------+" << endl;
- //iterate through matrix and print successive rows
- for(int row = 0; row < SIZE; row++) {
- cout << row << "| ";
- //iterate up to but not including current column
- for(int col = 0; col < currentCol; col++) {
- if(colonies[row][col] == 1212) {
- printf("%2s ", "XX");
- }
- else {
- printf("%02d ", colonies[row][col]);
- }
- }
- //iterate from current column to end
- for(int col = currentCol; col < SIZE; col++) {
- if(colonies[row][col] == 1212) {
- printf("%2s ", "XX");
- }
- else {
- printf("%2s ", "**");
- }
- }
- cout << "|" << endl;
- }
- cout << " +-------------------------------+\n" << endl;
- }
- //checks status of row selection during game
- bool checkStatus(int row, int col, int armySize, int** colonies) {
- if(colonies[row][col] == 0) {
- return true;
- }
- else if(colonies[row][col] == 1212) {
- cout << "Ant patrol at this location has already been removed." << endl;
- }
- else {
- armySize -= colonies[row][col];
- cout << "Anthony's army suffered casualties and has " << armySize << "ants remaining." << endl;
- //1212 is a sentinel value indicating visit (XX)
- colonies[row][col] = 1212;
- return false;
- }
- }
- int main () {
- //prints header and introduction by calling corresponding functions
- printHeader();
- printIntro();
- //dynamic colonies matrix declared
- int** colonies = new int*[SIZE];
- //arrays declared iteratively within matrix to eliminate null pointers
- for(int col = 0; col < SIZE; col++) {
- colonies[col] = new int[SIZE];
- }
- int patrolSize; //int to hold number of ants in each cell
- //passes colony matrix by pointer to populate it
- populateColonies(colonies, patrolSize);
- //stores size of Anthony's army
- int armySize = 0;
- //finds sum of patrol sizes for Anthony's army (Colony A)
- for(int row = 0; row < SIZE; row++) {
- armySize += colonies[row][0];
- }
- //initial display at start of game
- cout << "Initializing board...\n" << endl;
- displayColonies(colonies, 1);
- cout << "Now let's begin... Anthony's army is " << armySize << "strong! \n" << endl;
- //loop to run game
- char currentCol = 'B';
- int row = 0;
- while(armySize >= 1 && currentCol <= 'J') {
- //ensures that row position is within range
- while(!(row >= 0 && row <= 9)) {
- printf("Enter row position in column %c to strike: ", currentCol);
- cin >> row;
- if(!(row >= 0 && row <= 9))
- cout << "Please enter a row position between 0 and 9 (inclusive)." << endl;
- }
- //col offset by 65 to convert from char to int
- if(checkStatus(row, currentCol - 65, armySize, colonies)) {
- //conquers remaining patrols and assimilates into army
- for(int row = 0; row < SIZE; row++) {
- if(colonies[row][currentCol - 65] != 1212)
- armySize += colonies[row][currentCol - 65];
- }
- currentCol++;
- }
- //subtract 65 to convert from char to int equivalent
- displayColonies(colonies, currentCol - 65);
- }
- //ending message (for victory or loss)
- if(armySize >= 1) { //indicates victory
- cout << "Congratulations! Anthony's army was victorious, with " << armySize << "ants remaining!" << endl;
- }
- else {
- cout << "Sorry, but Anthony's army has been defeated before taking over all ant colonies." << endl;
- //current column is set to 9 so that the entire board is printed
- displayColonies(colonies, 9);
- }
- //returns memory for colonies matrix back to freestore
- delete[] colonies;
- for(int col = 0; col < SIZE; col++) {
- delete[] colonies[col];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement