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); }
private static void GenerateVesselsOnBattlefield(Battlefield battlefield) { var field = battlefield.Field; Random rndm = new Random(); //LOOP THROUGH PLACING THE 10 VESSELS (1 TANKER => 2 SUBMARINES => 3 CARRIERS => 4 BOATS) for (int vesselCount = 1; vesselCount <= 10; vesselCount++) { string vessel = string.Empty; // GET THE TYPE OF VESSEL BASED ON VESSELCOUNT switch (vesselCount) { case 1: vessel = BattlefieldElements.slotTanker; break; case 2: case 3: vessel = BattlefieldElements.slotSubmarine; break; case 4: case 5: case 6: vessel = BattlefieldElements.slotCarrier; break; case 7: case 8: case 9: case 10: vessel = BattlefieldElements.slotBoat; break; } // PLACE TANKER / SUBMARINE / CARRIER if (vesselCount >= 1 && vesselCount <= 6) { while (true) { int rowRandom = rndm.Next(0, 10); int colRandom = rndm.Next(0, 10); int horizontalOrVertical = rndm.Next(0, 2); // IS IT IN CORNER? if (BattlefieldValidator.CheckIfSlotIsInCorner(rowRandom, colRandom)) { continue; } // IS IT ON EDGE? else if (BattlefieldValidator.CheckIfSlotIsOnEdge(rowRandom, colRandom)) { if (CheckIfPlacingVesselOnEdgeIsPossible(field, rowRandom, colRandom, vessel)) { PlaceVesselOnEdge(field, rowRandom, colRandom, vessel); break; } } // IS IT IN THE MIDDLE? else { // HORIZONTAL if (horizontalOrVertical == 0) { if (CheckIfPlacingVesselInTheMiddleIsPossible(field, rowRandom, colRandom, horizontalOrVertical, vessel)) { PlaceVesselInTheMiddle(field, rowRandom, colRandom, horizontalOrVertical, vessel); break; } } // VERTICAL else { if (CheckIfPlacingVesselInTheMiddleIsPossible(field, rowRandom, colRandom, horizontalOrVertical, vessel)) { PlaceVesselInTheMiddle(field, rowRandom, colRandom, horizontalOrVertical, vessel); break; } } } } } // PLACE BOAT else { while (true) { int rowRandom = rndm.Next(0, 10); int colRandom = rndm.Next(0, 10); if (CheckIfAllSlotsAroundAreEmptyOrOccuppied(field, rowRandom, colRandom)) { PlaceBoat(field, rowRandom, colRandom); break; } } } } }