#include "Tools.h" #include #include #include using std::istringstream; using std::cout; using std::cin; using std::endl; using std::string; int YourGrid[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; int EnemyGrid[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; int Outposts[] = { 0,0 }; bool Running, GameOver; void DisplayRules() { cout << "YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU" << endl; cout << "HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED." << endl; cout << "YOU CAN ONLY PLACE ONE PLATOON AT ANY ONE OUTPOST." << endl; cout << "THE COMPUTER DOES THE SAME WITH ITS FOUR PLATOONS." << endl << endl; cout << "THE OBJECT OF THE GAME IS TO FIRE MISSILES AT THE" << endl; cout << "OUTPOSTS OF THE COMPUTER. IT WILL DO THE SAME TO YOU." << endl; cout << "THE ONE WHO DESTROYS ALL FOUR OF THE ENEMY'S PLATOONS" << endl; cout << "FIRST IS THE WINNER." << endl << endl; cout << "GOOD LUCK...AND TELL US WHERE YOU WANT THE BODIES SENT." << endl << endl; } int CheckWinner() { if (Outposts[0] == 0) return 2; if (Outposts[1] == 0) return 1; return 0; } void DisplayYourGrid() { cout << "=======================" << endl; cout << "YOUR GRID" << endl; cout << "=======================" << endl; cout << "X | 1 | 2 | 3 | 4 | 5 |" << endl; cout << "Y |---+---+---+---+---|" << endl; for (int x = 1; x < 6; x++) { int gStart = (x - 1) * 5; int gEnd = x * 5; cout << x << " |"; for (int g = gStart; g < gEnd; g++) { if (YourGrid[g] == -1) cout << " X |"; else if (YourGrid[g] == 1) cout << " O |"; else cout << " |"; } cout << endl; cout << " |---+---+---+---+---|" << endl; } } void DisplayEnemyGrid() { cout << "=======================" << endl; cout << "ENEMY GRID" << endl; cout << "=======================" << endl; cout << "X | 1 | 2 | 3 | 4 | 5 |" << endl; cout << "Y |---+---+---+---+---|" << endl; for (int x = 1; x < 6; x++) { int gStart = (x - 1) * 5; int gEnd = x * 5; cout << x << " |"; for (int g = gStart; g < gEnd; g++) { if (EnemyGrid[g] == -1) cout << " X |"; else cout << " |"; } cout << endl; cout << " |---+---+---+---+---|" << endl; } } void ClearGrids() { for (int g = 0; g < 25; g++) { YourGrid[g] = 0; EnemyGrid[g] = 0; } } void SetOutposts() { Outposts[0] = 4; int outpost = 0; for (int o = 0; o < 4; o++) { DisplayYourGrid(); bool outpostSet = false; while (!outpostSet) { cout << "PLACE YOUR OUTPOST #" << (o + 1) << endl; int x = GetIntInput("X"); if (x < 1 || x > 5) cout << "X MUST BE BETWEEN 1 AND 5." << endl; else { int y = GetIntInput("Y"); if (y < 1 || y > 5) cout << "Y MUST BE BETWEEN 1 AND 5." << endl; else { outpost = ((y - 1) * 5) + (x - 1); if (YourGrid[outpost] == 0) outpostSet = true; else cout << "YOU ALREADY HAVE AN OUTPOST THERE." << endl; } } } YourGrid[outpost] = 1; } } void ComputerSetOutposts() { Outposts[1] = 4; int outpost = 0; for (int o = 0; o < 4; o++) { bool outpostSet = false; while (!outpostSet) { int x = GenerateRandomNumber(1, 5, false); int y = GenerateRandomNumber(1, 5, false); outpost = ((y - 1) * 5) + (x - 1); if (EnemyGrid[outpost] == 0) outpostSet = true; } EnemyGrid[outpost] = 1; } } void ComputerTurn() { cout << "MY TURN..." << endl; int loc = 0, x = 0, y = 0; bool gotLoc = false; while (!gotLoc) { x = GenerateRandomNumber(1, 5, false); y = GenerateRandomNumber(1, 5, false); loc = ((y - 1) * 5) + (x - 1); if (YourGrid[loc] > -1)gotLoc = true; } cout << "..........BOOM!" << endl; if (YourGrid[loc] == 0) { cout << "I MISSED YOU, YOU DIRTY RAT." << endl; cout << "I PICKED LOCATION (" << x << "," << y << ")." << endl; } else { cout << "I GOT YOU. IT WON'T BE LONG NOW!" << endl; cout << "OUTPOST AT (" << x << "," << y << ") WAS HIT." << endl; YourGrid[loc] = -1; Outposts[0]--; if (Outposts[0] == 3) cout << "YOU ONLY HAVE THREE OUTPOSTS LEFT." << endl; else if (Outposts[0] == 2) cout << "YOU ONLY HAVE TWO OUTPOSTS LEFT." << endl; else if (Outposts[0] == 1) cout << "YOU ONLY HAVE ONE OUTPOST LEFT." << endl; else cout << "YOU ARE DEAD! YOU HAVE NO OUTPOSTS LEFT." << endl; } } void FireMissile() { int loc = 0, x = 0, y = 0; bool gotLoc = false; cout << "WHERE DO YOU WANT TO FIRE YOUR MISSILE?" << endl; while (!gotLoc) { x = GetIntInput("X"); if (x < 1 || x > 5) cout << "X MUST BE BETWEEN 1 AND 5." << endl; else gotLoc = true; } gotLoc = false; while (!gotLoc) { y = GetIntInput("Y"); if (y < 1 || y > 5) cout << "Y MUST BE BETWEEN 1 AND 5." << endl; else gotLoc = true; } loc = ((y - 1) * 5) + (x - 1); cout << "..........BOOM!" << endl; if (EnemyGrid[loc] == -1) cout << "YOU FOOL, YOU ALREADY ATTACKED THERE!" << endl; else if (EnemyGrid[loc] == 0) cout << "HA HA, YOU MISSED!" << endl; else { cout << "YOU GOT ONE OF MY OUTPOSTS!" << endl; EnemyGrid[loc] = -1; Outposts[1]--; if (Outposts[1] == 3) cout << "ONE DOWN, THREE TO GO." << endl; else if (Outposts[1] == 2) cout << "TWO DOWN, TWO TO GO." << endl; else if (Outposts[1] == 1) cout << "THREE DOWN, ONE TO GO." << endl; else cout << "YOU GOT THEM ALL." << endl; } } int main() { srand(time(NULL)); cout << "*** BOMBARDMENT : C\\C++ ***" << endl; if (AskToSeeTheRules())DisplayRules(); Running = true; while (Running) { int turn = 0; ClearGrids(); ComputerSetOutposts(); SetOutposts(); GameOver = false; cout << "--------- LET THE WAR BEGIN ----------" << endl; while (!GameOver) { cout << "-----------------------" << endl; cout << "TURN #" << (turn + 1) << endl; cout << "-----------------------" << endl; if (turn % 2 == 0) { DisplayYourGrid(); DisplayEnemyGrid(); FireMissile(); } else ComputerTurn(); int winner = CheckWinner(); if (winner == 1) { DisplayYourGrid(); DisplayEnemyGrid(); cout << "YOU WIN!!!" << endl; GameOver = true; } else if (winner == 2) { DisplayYourGrid(); DisplayEnemyGrid(); cout << "TOO BAD, BETTER LUCK NEXT TIME." << endl; GameOver = true; } else turn++; } cout << "*** GAME OVER ***" << endl; Running = AskToPlayAgain(); } cout << "THANKS FOR PLAYING!" << endl; }