// Lab10-FoundationGameCode.cpp #include // for cin, cout #include // for string data type using namespace std; // needed because cin, cout, string are part of the std:: namespace #include // for _getche() #include // for time() void main() { srand(unsigned int(time(NULL))); const int nLimitX = 25; // a constant to represent the X-boundary of the game environment -- valid range 0-24 (inclusive) const int nLimitY = 15; // a constant to represent the Y-boundary of the game environment -- valid range 0-14 (inclusive) int nTreasureX = rand()%nLimitX; int nTreasureY = rand()%nLimitY; int nTreasureGold = 500; string sTreasureName = "Treasure Chest"; int nGodzillaX = rand()%nLimitX; int nGodzillaY = rand()%nLimitY; int nGodzillaHealthPoints = 40; int nGodzillaStrength = 2000; string sGodzillaName = "Godzilla"; int nPlayerX = rand()%nLimitX; int nPlayerY = rand()%nLimitY; int nPlayerHealthPoints = 80; int nPlayerStrength = 40; string sPlayerName; cout << "Welcome to Godzilla's Revenge!"; cout << "\nSo, what's your name? "; cin >> sPlayerName; unsigned char ucDirection = 'a'; while (ucDirection != 'q') { system("cls"); // clear the screen before redisplaying the location information and map // DISPLAY STATUS INFORMATION cout << sPlayerName << ", your location is " << nPlayerX << ", " << nPlayerY << endl; cout << sGodzillaName << ": " << nGodzillaX << ',' << nGodzillaY << endl; cout << sTreasureName << ": " << nTreasureX << ',' << nTreasureY << endl; // end DISPLAY STATUS INFORMATION // DISPLAY A VISUAL MAP // Most of the locations are dots to indicate // open territory through which the Player and Godzilla move. // As the loops cycle through each row, the column positions are // displayed one after the next (a single character at a time). // The if-else-if determines if the current position being displayed // is in fact occupied by either the Player, Godzilla or the Treasure. // In that case, a character is displayed to show the object's location. for (int nY=0; nY nGodzillaX) // determine the X direction to pursue the player nGodzillaX++; // player was to the east, move Godzilla east else if (nPlayerX < nGodzillaX) nGodzillaX--; // player was to the west, move Godzilla west // else // do not change the X position of Godzilla, since Godzilla is already on the same X column // end if move Godzilla on the X-axis if (nPlayerY > nGodzillaY) // determine the Y direction to pursue the player nGodzillaY++; // player was to the south, move Godzilla south else if (nPlayerY < nGodzillaY) nGodzillaY--; // player was to the north, move Godzilla north // else // do not change the Y position of Godzilla, since Godzilla is already on the same Y row // end if move Godzilla on the Y-axis } // end if (rand()%100 < 60) that is, end section that governs Godzilla's movement // When Godzilla lands on the player's location . . . it's game over if (nPlayerX == nGodzillaX && nPlayerY == nGodzillaY) { cout << endl << sPlayerName << ", you were quite tasty!\n"; break; // leave the while loop since player has been eaten } // end if Godzilla finds player // end HANDLE ENCOUNTERS } // end while system("pause"); } // end main