public void GameTestStart() { //test game! ConsoleDisplay.ShowPlayerGrid(cpuPlayer1); ConsoleDisplay.ShowPlayerGrid(cpuPlayer2); bool gameOver = false; for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { cpuPlayer1.Fire(y, x, cpuPlayer2.PlayerGrid); cpuPlayer2.Fire(y, x, cpuPlayer1.PlayerGrid); if (!cpuPlayer2.CheckFleet()) { Console.WriteLine("Game Over, P1 wins!"); gameOver = true; break; } if (!cpuPlayer1.CheckFleet()) { Console.WriteLine("Game Over, P2 wins!"); gameOver = true; break; } } if (gameOver) { break; } } ConsoleDisplay.ShowPlayerBattleGrid(cpuPlayer1); ConsoleDisplay.ShowPlayerBattleGrid(cpuPlayer2); ConsoleDisplay.ShowPlayerGrid(cpuPlayer1); ConsoleDisplay.ShowPlayerGrid(cpuPlayer2); }
private bool GameLoop() { bool gameOver = false; while (!gameOver) { Console.Clear(); int y; int x; ConsoleDisplay.ShowPlayerGrid(player); ConsoleDisplay.ShowPlayerBattleGrid(player); Console.WriteLine("Select a Coordinate to Attack: "); string coord = Console.ReadLine(); try { if (string.IsNullOrEmpty(coord)) { throw new ArgumentException(); } else if (coord.Count() > 2 || !char.IsLetter(coord[0]) || !char.IsDigit(coord[1])) { throw new ArgumentException(); } else { y = int.Parse(coord[1].ToString().ToUpper()); x = ConsoleDisplay.ALPHABET.IndexOf(coord[0].ToString().ToUpper()); if (y > 9 || x > 9) { Console.WriteLine("Coord outside boundaries. Please try again."); Console.ReadKey(); continue; } bool hit = false; try { hit = player.Fire(x, y, cpu.PlayerGrid); } catch (ArgumentException e) { Console.WriteLine("You've already fired at this location!"); } if (hit) { Console.WriteLine("HIT!"); if (player.BattleGrid.GetGridSquare(y, x).CheckStatus() == GridSquareStatus.SUNK) { Console.WriteLine("SUNK!"); } Console.ReadKey(); } else { Console.WriteLine("MISS!"); Console.ReadKey(); } x = random.Next(0, 10); y = random.Next(0, 10); try { hit = cpu.Fire(x, y, player.PlayerGrid); } catch (ArgumentException e) { //TODO: ideally we need to try a different coord, } if (hit) { Console.WriteLine("One of your Ships was HIT!"); if (player.PlayerGrid.GetGridSquare(y, x).CheckStatus() == GridSquareStatus.SUNK) { Console.WriteLine("One of your Ships SUNK!"); } Console.ReadKey(); } if (!player.CheckFleet()) { Console.WriteLine("Game Over, " + cpu.Name + " Wins!"); gameOver = true; } if (!cpu.CheckFleet()) { Console.WriteLine("Game Over, " + player.Name + " Wins!"); gameOver = true; } } } catch (ArgumentException e) { Console.WriteLine("Invalid Coord. Example: A1"); Console.ReadKey(); } } return(false); }