public static string GetMenuCommand() { bool isTrue = true; while (isTrue) { Console.Write($" Press key: "); switch (Console.ReadKey(true).Key) { case ConsoleKey.Escape: return("EXIT"); case ConsoleKey.Spacebar: return("PLAY"); case ConsoleKey.I: return("INSTRUCTIONS"); case ConsoleKey.S: return("STATISTICS"); default: Console.WriteLine(GameElements.GetInvalidMessage()); break; } } return(null); }
public static void PlayGame() { int stage = 1; Player player = new Player(); Player opponent = new Player(); while (true) { while (true) { PrintGameplayUI(player, opponent, stage); Random myRandom = new Random(); int rowRandom; int colRandom; while (true) { rowRandom = myRandom.Next(0, 10); colRandom = myRandom.Next(0, 10); if (opponent.OpponentBattlefield.Field[rowRandom, colRandom] == BattlefieldElements.slotHidden) { break; } } string playerAttackedSlotResult = player.GetAttacked(rowRandom, colRandom); opponent.BotAttack(rowRandom, colRandom, playerAttackedSlotResult); Console.WriteLine($" Opponent" + opponent.GetAttackMessage(playerAttackedSlotResult)); Thread.Sleep(1000); if (opponent.CheckIfWinner()) { break; } if (BattlefieldElements.slotsVessels.Contains(playerAttackedSlotResult)) { if (BattlefieldValidator.CheckIfSlotIsOnEdge(rowRandom, colRandom)) { player.MarkShipOnEdgeAsDestroyed(rowRandom, colRandom, playerAttackedSlotResult); } continue; } else { break; } } // OPPONENT ATTACKS, PLAYER GETS ATTACKED if (opponent.CheckIfWinner()) { break; } //GAME ENDS - YOU WIN while (true) { PrintGameplayUI(player, opponent, stage); int row = -1; int col = -1; while (true) { Console.Write($" Attack (i.e. A0/a0): "); string command = Console.ReadLine(); if (command.Length != 2) { Console.WriteLine(GameElements.GetInvalidMessage()); continue; } else { char colChar = command[0]; if (colChar >= 'A' && colChar <= 'J') { col = colChar - 'A'; } else if (colChar >= 'a' && colChar <= 'j') { col = colChar - 'a'; } else { Console.WriteLine(GameElements.GetInvalidMessage()); continue; } char rowChar = command[1]; if (rowChar - '0' >= 0 && rowChar - '0' <= 9) { row = rowChar - '0'; } else { Console.WriteLine(GameElements.GetInvalidMessage()); continue; } break; } } // GET CORRECT INPUT string opponentAttackedSlotResult = opponent.GetAttacked(row, col); player.Attack(row, col, opponentAttackedSlotResult); Console.WriteLine($" You" + player.GetAttackMessage(opponentAttackedSlotResult)); Thread.Sleep(1000); if (player.CheckIfWinner()) { break; } if (BattlefieldElements.slotsVessels.Contains(opponentAttackedSlotResult)) { continue; } else { break; } } // PLAYER ATTACKS, OPPONENT GETS ATTACKED if (player.CheckIfWinner()) { break; } stage++; // END OF STAGE } PrintGameplayUI(player, opponent, stage); bool isWinner = false; if (player.CheckIfWinner()) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\n ~CONGRATULATIONS! YOU WON!~"); isWinner = true; } // YOU WON else if (opponent.CheckIfWinner()) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("\n ~CONDOLENCES... YOU LOST!~"); } // YOU LOST Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"\n The game took {stage} stages."); Thread.Sleep(5000); Console.Clear(); UpdateDatabaseStatistics(isWinner); }