using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bombardment { class Program { private Tools _Tools = new Tools(); private bool _Running; private bool _GameOver; private int[] _YourGrid = new int[25] { 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 }; private int[] _EnemyGrid = new int[25] { 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 }; private int[] _Outposts = new int[2] { 0, 0 }; private void DisplayRules() { Console.WriteLine("YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU"); Console.WriteLine("HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED."); Console.WriteLine("YOU CAN ONLY PLACE ONE PLATOON AT ANY ONE OUTPOST."); Console.WriteLine("THE COMPUTER DOES THE SAME WITH ITS FOUR PLATOONS.\r\n"); Console.WriteLine("THE OBJECT OF THE GAME IS TO FIRE MISSILES AT THE"); Console.WriteLine("OUTPOSTS OF THE COMPUTER. IT WILL DO THE SAME TO YOU."); Console.WriteLine("THE ONE WHO DESTROYS ALL FOUR OF THE ENEMY'S PLATOONS"); Console.WriteLine("FIRST IS THE WINNER.\r\n"); Console.WriteLine("GOOD LUCK...AND TELL US WHERE YOU WANT THE BODIES SENT.\r\n"); } private int CheckWinner() { if (_Outposts[0] == 0) return 2; if (_Outposts[1] == 0) return 1; return 0; } private void DisplayYourGrid() { Console.WriteLine("======================="); Console.WriteLine("YOUR GRID"); Console.WriteLine("======================="); Console.WriteLine("X | 1 | 2 | 3 | 4 | 5 |"); Console.WriteLine("Y |---+---+---+---+---|"); for (int x = 1; x < 6; x++) { int gStart = (x - 1) * 5; int gEnd = x * 5; string displayLine = $"{x} |"; for (int g = gStart; g < gEnd; g++) { if (_YourGrid[g] == -1) displayLine += " X |"; else if (_YourGrid[g] == 1) displayLine += " O |"; else displayLine += " |"; } Console.WriteLine(displayLine); Console.WriteLine(" |---+---+---+---+---|"); } } private void DisplayEnemyGrid() { Console.WriteLine("======================="); Console.WriteLine("ENEMY GRID"); Console.WriteLine("======================="); Console.WriteLine("X | 1 | 2 | 3 | 4 | 5 |"); Console.WriteLine("Y |---+---+---+---+---|"); for (int x = 1; x < 6; x++) { int gStart = (x - 1) * 5; int gEnd = x * 5; string displayLine = $"{x} |"; for (int g = gStart; g < gEnd; g++) { if (_EnemyGrid[g] == -1) displayLine += " X |"; else displayLine += " |"; } Console.WriteLine(displayLine); Console.WriteLine(" |---+---+---+---+---|"); } } private void ClearGrids() { for (int g = 0; g < 25; g++) _YourGrid[g] = _EnemyGrid[g] = 0; } private void SetOutposts() { _Outposts[0] = 4; for(int o = 0; o < 4; o ++) { DisplayYourGrid(); bool outpostSet = false; while (!outpostSet) { Console.WriteLine($"PLACE YOUR OUTPOST #{o + 1}"); int x = _Tools.GetIntInput("X"); if (x < 1 || x > 5) Console.WriteLine("X MUST BE BETWEEN 1 AND 5."); else { int y = _Tools.GetIntInput("Y"); if (y < 1 || y > 5) Console.WriteLine("Y MUST BE BETWEEN 1 AND 5."); else { int outpost = ((y - 1) * 5) + (x - 1); if (_YourGrid[outpost] == 0) { outpostSet = true; _YourGrid[outpost] = 1; } else Console.WriteLine("YOU ALREADY HAVE AN OUTPOST THERE."); } } } } } private void ComputerSetOutposts() { _Outposts[1] = 4; for (int o = 0; o < 4; o++) { bool outpostSet = false; while (!outpostSet) { int x = _Tools.GenerateRandomNumber(1, 5, false); int y = _Tools.GenerateRandomNumber(1, 5, false); int outpost = ((y - 1) * 5) + (x - 1); if (_EnemyGrid[outpost] == 0) { outpostSet = true; _EnemyGrid[outpost] = 1; } } } } private void ComputerTurn() { Console.WriteLine("MY TURN ..."); int loc = 0, x = 0, y = 0; bool gotLoc = false; while (!gotLoc) { x = _Tools.GenerateRandomNumber(1, 5, false); y = _Tools.GenerateRandomNumber(1, 5, false); loc = ((y - 1) * 5) + (x - 1); if (_YourGrid[loc] > -1) gotLoc = true; } Console.WriteLine("..........BOOM!"); if(_YourGrid[loc] == 0) { Console.WriteLine("I MISSED YOU, YOU DIRTY RAT."); Console.WriteLine($"I PICKED LOCATION ({x},{y})."); } else { Console.WriteLine("I GOT YOU. IT WON'T BE LONG NOW!"); Console.WriteLine($"OUTPOST AT ({x},{y}) WAS HIT."); _YourGrid[loc] = -1; _Outposts[0]--; if (_Outposts[0] == 3) Console.WriteLine("YOU ONLY HAVE THREE OUTPOSTS LEFT."); else if (_Outposts[0] == 2) Console.WriteLine("YOU ONLY HAVE TWO OUTPOSTS LEFT."); else if (_Outposts[0] == 1) Console.WriteLine("YOU ONLY HAVE ONE OUTPOST LEFT."); else Console.WriteLine("YOU ARE DEAD! YOU HAVE NO OUTPOSTS LEFT."); } } private void FireMissile() { Console.WriteLine("WHERE DO YOU WANT TO FIRE YOUR MISSILE?"); int x = 0, y = 0; bool gotLoc = false; while (!gotLoc) { x = _Tools.GetIntInput("X"); if (x < 1 || x > 5) Console.WriteLine("PLEASE ENTER A NUMBER BETWEEN 1 AND 5."); else gotLoc = true; } gotLoc = false; while (!gotLoc) { y = _Tools.GetIntInput("Y"); if (y < 1 || y > 5) Console.WriteLine("PLEASE ENTER A NUMBER BETWEEN 1 AND 5."); else gotLoc = true; } int loc = ((y - 1) * 5) + (x - 1); Console.WriteLine("..........BOOM!"); if (_EnemyGrid[loc] == -1) Console.WriteLine("YOU FOOL, YOU ALREADY ATTACKED THERE!"); else if (_EnemyGrid[loc] == 0) Console.WriteLine("HA HA, YOU MISSED!"); else { Console.WriteLine("YOU GOT ONE OF MY OUTPOSTS!"); _EnemyGrid[loc] = -1; _Outposts[1]--; if (_Outposts[1] == 3) Console.WriteLine("ONE DOWN, THREE TO GO."); else if (_Outposts[1] == 2) Console.WriteLine("TWO DOWN, TWO TO GO."); else if (_Outposts[1] == 1) Console.WriteLine("THREE DOWN, ONE TO GO."); else Console.WriteLine("YOU GOT THEM ALL."); } } public void Run() { Console.WriteLine("*** BOMBARDMENT : C# ***"); _Running = true; if (_Tools.AskToSeeTheRules()) DisplayRules(); while (_Running) { int turn = 0; ClearGrids(); ComputerSetOutposts(); SetOutposts(); _GameOver = false; Console.WriteLine("--------- LET THE WAR BEGIN ----------"); while (!_GameOver) { Console.WriteLine("-----------------------"); Console.WriteLine($"TURN #{turn + 1}"); Console.WriteLine("-----------------------"); if(turn % 2 == 0) { DisplayYourGrid(); DisplayEnemyGrid(); FireMissile(); } else ComputerTurn(); int winner = CheckWinner(); if (winner == 1) { DisplayYourGrid(); DisplayEnemyGrid(); Console.WriteLine("YOU WIN!!!"); _GameOver = true; } else if (winner == 2) { DisplayYourGrid(); DisplayEnemyGrid(); Console.WriteLine("TOO BAD, BETTER LUCK NEXT TIME."); _GameOver = true; } else turn++; } Console.WriteLine("*** GAME OVER ***"); _Running = _Tools.AskToPlayAgain(); } Console.WriteLine("THANKS FOR PLAYING!"); } static void Main(string[] args) { Program p = new Program(); p.Run(); Console.ReadLine(); } } }