Ejemplo n.º 1
0
        public IActionResult OnPost(string[] coordinates, string submit)
        {
            var save = GameSaveRead.LoadSaveFromDb(Index);

            Player   = save[0];
            Computer = (Computer)save[1];

            if (submit.Equals("Continue"))
            {
                Player.GamingBoard.RemoveNeighbours(Player.GamingBoard.BoardSize);
                GameSaveRead.OverwriteSave(Index, Player, Computer);
                return(RedirectToPage("PlayGame", "FirstEntry", new { index = Index }));
            }

            if (coordinates.Length > 5 || coordinates.Length <= 1 && string.IsNullOrWhiteSpace(coordinates[0]))
            {
                GetShipsThatCanBePlaced();
                ModelState.AddModelError("Error", "Invalid coordinates");
                return(Page());
            }
            if (!PlaceShipsOnBoard(coordinates))
            {
                ModelState.AddModelError("Error", "All ships with that size have already been placed");
            }
            GetShipsThatCanBePlaced();
            GameSaveRead.OverwriteSave(Index, Player, Computer);
            return(Page());
        }
Ejemplo n.º 2
0
        public IActionResult OnPost(string[] radio, string[] indexRadio)
        {
            SaveHeaders = GameSaveRead.GetGameInformation();
            if (radio.Length == 0 || indexRadio.Length == 0)
            {
                return(Page());
            }
            var choice = radio[0];

            if (choice.Equals("Continue"))
            {
                var chosenSave = GameSaveRead.LoadSaveFromDb(int.Parse(indexRadio[0]));
                if (chosenSave[0].HitPoints > 0 && chosenSave[1].HitPoints > 0)
                {
                    return(RedirectToPage("PlayGame", "FirstEntry", new { index = int.Parse(indexRadio[0]) }));
                }
                ModelState.AddModelError("Error", "Game has already been won.");
                return(Page());
            }

            if (choice.Equals("Delete"))
            {
                GameSaveRead.DeleteSave(int.Parse(indexRadio[0]));
            }
            // just in case
            SaveHeaders = GameSaveRead.GetGameInformation();
            return(Page());
        }
Ejemplo n.º 3
0
        public IActionResult OnGet(int index)
        {
            Index = index;
            var save = GameSaveRead.LoadSaveFromDb(Index);

            Player   = save[0];
            Computer = (Computer)save[1];
            GetShipsThatCanBePlaced();
            return(Page());
        }
Ejemplo n.º 4
0
        private static void AskForGameSave(Player player, Computer computer)
        {
            Console.WriteLine("Would you like to save current game? Y/N");
            var saveGame = Console.ReadLine();

            if (saveGame == null || !saveGame.ToLower().Equals("y"))
            {
                return;
            }
            GameSaveRead.Save(player, computer);
            Console.WriteLine("\nGame has been saved.");
        }
Ejemplo n.º 5
0
        public IActionResult OnPost(string submit)
        {
            var player   = new Player(Name);
            var computer = new Computer();

            computer.GenerateRandomBoardWithShips();

            if (submit.Equals("Random"))
            {
                player.GenerateRandomBoardWithShips();
                return(RedirectToPage("PlayGame", "FirstEntry", new { index = GameSaveRead.Save(player, computer) }));
            }
            return(RedirectToPage("PlaceShips", new { index = GameSaveRead.Save(player, computer) }));
        }
Ejemplo n.º 6
0
 public IActionResult OnGet()
 {
     SaveHeaders = GameSaveRead.GetGameInformation();
     return(Page());
 }
Ejemplo n.º 7
0
        public static void GameLoop(Player player, Computer computer, bool isSave = false, int saveIndex = -1)
        {
            var boardPrintingUi = new BoardPrintingUI();
            var done            = false;
            var computerWin     = false;

            if (player.HitPoints == 0 || computer.HitPoints == 0)
            {
                Console.WriteLine("The game has already been won.");
                HelperMethods.WaitForUser();
                return;
            }

            // game moves
            while (!done)
            {
                Console.Clear();
                //Console.WriteLine(Environment.NewLine + boardPrintingUi.GetBoardsAsString(player.GamingBoard, player.TrackingBoard));
                //Console.WriteLine("----------------------------------");
                //Console.WriteLine(boardPrintingUi.GetSingleBoard(computer.GamingBoard));

                if (computer.SunkShips.Count > 0)
                {
                    foreach (var sunkShip in computer.SunkShips)
                    {
                        foreach (var coordinate in sunkShip.ShipCoordinates)
                        {
                            player.TrackingBoard[coordinate.GetX(), coordinate.GetY()] = BoardSquareState.Dead;
                        }
                    }
                }

                Console.WriteLine(Environment.NewLine + boardPrintingUi.GetBoardsAsString(player.GamingBoard, player.TrackingBoard));
                Console.WriteLine($"Ships that you have sunk this session: {string.Join(", ", computer.SunkShips)}\n");
                Console.WriteLine("player hp " + player.HitPoints + " computer hp " + computer.HitPoints);
                //Player move
                Console.WriteLine($"{player}, pick x coordinate (number):");
                var xCord = Console.ReadLine();
                Console.WriteLine($"{player}, pick y coordinate (letter):");
                var yCord = Console.ReadLine();

                if (!Coordinates.ValidateCoordinates(x: xCord, y: yCord))
                {
                    Console.WriteLine("Invalid coordinates. Try again.");
                    continue;
                }

                var playerShotCoordinates = new Coordinates(Convert.ToInt32(xCord), yCord);
                Console.WriteLine($"\nYou shot at x: {playerShotCoordinates.GetX()} y: {playerShotCoordinates.YCord}");

                //ParseShot(Coordinates, attacker, defender)
                if (Boards.ParseShot(playerShotCoordinates, player, computer))
                {
                    Console.WriteLine("You hit!");
                    if (computer.HitPoints <= 0)
                    {
                        done = true;
                    }
                }
                else
                {
                    Console.WriteLine("You missed :(");
                } // end player move

                //Computer move
                if (!done)
                {
                    var computerShotCoordinates = computer.MakeMove();
                    var shotResultComputer      = Boards.ParseShot(computerShotCoordinates, computer, player);

                    Console.WriteLine($"\n{computer.UserName} shot at x: {computerShotCoordinates.GetX()} y: {computerShotCoordinates.YCord}");

                    if (shotResultComputer)
                    {
                        Console.WriteLine($"{computer.UserName} hit!");
                        if (player.HitPoints <= 0)
                        {
                            done        = true;
                            computerWin = true;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"{computer.UserName} missed :(\n");
                    } // end computer move
                }

                Console.WriteLine("Press 'Q' to quit or any other key to continue.");
                var userChoice = Console.ReadLine();

                if (userChoice != null && userChoice.ToLower().Equals("q") || isSave && done)
                {
                    if (isSave)
                    {
                        while (true)
                        {
                            Console.WriteLine("Game has been won!");
                            Console.WriteLine("1 - Overwrite current save | 2 - create new save | Any key to exit");
                            var saveAction = Console.ReadLine();
                            if (saveAction != null && saveAction.ToLower().Equals("1"))
                            {
                                GameSaveRead.OverwriteSave(saveIndex, player, computer);
                                Console.WriteLine("Save overwritten.");
                                return;
                            }

                            if (saveAction != null && !saveAction.ToLower().Equals("2"))
                            {
                                return;
                            }
                            GameSaveRead.Save(player, computer);
                            Console.WriteLine("Game saved.");
                            HelperMethods.WaitForUser();
                            return;
                        }
                    }

                    AskForGameSave(player, computer);
                    HelperMethods.WaitForUser();
                    return;
                }
            }

            Console.WriteLine(computerWin ? $"\n{computer.UserName} won this game." : "\nYou won this game!!");
            Console.WriteLine("Thank you for playing!\n" +
                              "You can press Q to view opponent's board.");
            var showOpponentBoard = Console.ReadLine()?.ToLower();

            if (showOpponentBoard != null && showOpponentBoard.Equals("q"))
            {
                Console.WriteLine(boardPrintingUi.GetSingleBoard(computer.GamingBoard));
            }

            AskForGameSave(player, computer);
            HelperMethods.WaitForUser();
        }
Ejemplo n.º 8
0
        public static void SelectGameAndStart()
        {
            Console.Clear();
            Console.WriteLine("Fetching saves ..");
            var listOfHeaders = GameSaveRead.GetGameInformation();

            Console.Clear();
            // no saves
            if (listOfHeaders.Count == 0)
            {
                Console.WriteLine("No saves found.");
                HelperMethods.WaitForUser();
                return;
            }
            // print header
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"\n{"Index", -10} {"Save Date",-25} {"Winner",-15} {"Player",-5}");
            Console.ResetColor();
            // print saves
            var availableIndexes = new List <int>();

            foreach (var temp in listOfHeaders)
            {
                availableIndexes.Add(int.Parse(temp[0]));
                Console.Write($"{temp[0], -10} {temp[1],-25} {temp[2],-15} {temp[3],-5} \n");
            }
            // choosing a save and validation
            Console.WriteLine("\nChoose a save by index. Or press 'Q' to quit.");
            int userChoice;

            while (true)
            {
                var userInput = Console.ReadLine();
                if (userInput != null && userInput.ToLower().Equals("q"))
                {
                    return;
                }
                if (!int.TryParse(userInput, out userChoice) || !availableIndexes.Contains(userChoice))
                {
                    Console.WriteLine("Invalid input. Try again.");
                    continue;
                }
                break;
            }
            // actions with save
            Console.WriteLine($"\nChosen save: {userChoice}\nAvailable actions:");
            Console.WriteLine("1 Continue | 2 Watch replay | 3 Delete");
            int saveAction;

            while (true)
            {
                var userInput = Console.ReadLine();
                if (!int.TryParse(userInput, out saveAction) || saveAction != 1 && saveAction != 2 && saveAction != 3)
                {
                    Console.WriteLine("Choose again.");
                    continue;
                }

                break;
            }

            switch (saveAction)
            {
            case 1:
                var temp = GameSaveRead.LoadSaveFromDb(userChoice);
                GameLoop(temp[0], (Computer)temp[1], true, userChoice);
                break;

            case 2:
                ReplayGame(GameSaveRead.LoadSaveFromDb(userChoice));
                break;

            case 3:
                GameSaveRead.DeleteSave(userChoice);
                Console.WriteLine("Save deleted.");
                HelperMethods.WaitForUser();
                break;

            default:
                Console.WriteLine("No such option");
                break;
            }
        }