static void Main(string[] args) { // GamePlay.startGame("Battleships version 1.0 \n\nGame created by Andrzej && Mateusz","Let`s start"); int playerOcean1 = 1; int playerOcean2 = 2; Ocean ocean1 = new Ocean(playerOcean1); Ocean ocean2 = new Ocean(playerOcean2); placeShipsOnBoard(ocean1); placeShipsOnBoard(ocean2); Ocean currentOcean = ocean2; string currentPlayer = "Player1"; bool gameOver = false; while (!gameOver) { battle(currentOcean, currentPlayer); if (currentOcean.isGameOver()) { gameOver = true; System.Console.WriteLine(GamePlay.howWin(currentOcean)); Thread.Sleep(500); } currentOcean = (currentOcean == ocean2) ? currentOcean = ocean1 : currentOcean = ocean2; currentPlayer = (currentPlayer == "Player1") ? currentPlayer = "Player2" : currentPlayer = "Player1"; } }
private static void battle(Ocean currentOcean, string currentPlayer) { string hiddenOcean = currentOcean.makeHiddenOcean(currentOcean.playerOcean); GamePlay.displayOcean(hiddenOcean); int x, y; x = ReadCoordinate("X", currentPlayer); y = ReadCoordinate("Y", currentPlayer); try { if (currentOcean.Shoot(x, y)) { System.Console.WriteLine("TRAFIONY!"); Thread.Sleep(1000); } else { System.Console.WriteLine("PUDŁO!"); Thread.Sleep(1000); } } catch (ArgumentException e) { System.Console.WriteLine(e); } hiddenOcean = currentOcean.makeHiddenOcean(currentOcean.playerOcean); GamePlay.displayOcean(hiddenOcean.ToString()); if (currentOcean.hasShipSunk()) { GamePlay.DisplayMessage("hit and sunk"); } }
private static void placeShipsOnBoard(Ocean ocean) { int posX; int posY; int numberOfShipsBefore; foreach (KeyValuePair <string, int> ship in Ocean.shipsToLocate) { do { System.Console.WriteLine(ocean); numberOfShipsBefore = ocean.getNumberOfShips(); List <int> shipDetails = askPlayerForShipDetalis(new string[] { "Type X: ", "Type Y: " }, ship.Key); string orientation = askForShipOrientation("vertical [type v] or horizontal [type h]"); posX = shipDetails[0]; posY = shipDetails[1]; ocean.AddShip(ship.Value, posX, posY, orientation); // Thread.Sleep(1000); // Console.Clear(); } while (numberOfShipsBefore == ocean.getNumberOfShips()); } GamePlay.displayOcean(ocean.ToString()); }
internal static string howWin(Ocean currentOcean) { return(currentOcean.playerOcean == 1 ? "Player 2 wins" : "Player 1 wins"); }
private static string getNameOfOcean(Ocean ocean) { return(ocean.playerOcean == 1 ? "Player1 Ocean" : "Player 2 Ocean"); }