Beispiel #1
0
        static string SaveGame(BattleShips game)
        {
            var saveName = AskUserForSaveGameName(1, 30);
            BattleShipsSaveJson battleShips = new BattleShipsSaveJson()
            {
                Height      = game.Height,
                Width       = game.Width,
                Player1     = game.Player1,
                Player1Turn = game.Player1Turn,
                Player2     = game.Player2,
                SaveName    = saveName,
                GameType    = game.GameType
            };

            SaveTool.SaveGameToFile(battleShips);

            using var ctx = new AppDbContext();
            var battleShipsSave = game.CreateBattleShipsSave(saveName);

            if (ctx.BattleShipsSaves.Any(e => e.SaveName == saveName))
            {
                var save = ctx.BattleShipsSaves.FirstOrDefault(e => e.SaveName == saveName);
                game.UpdateBattleShipsSave(save !);
                ctx.Update(save);
            }
            else
            {
                ctx.Add(battleShipsSave);
            }
            ctx.SaveChanges();

            return("");
        }
Beispiel #2
0
        private static string AskUserForSaveGameName(int min, int max)
        {
            using var ctx = new AppDbContext();
            var userInput = "";

            while (true)
            {
                Console.WriteLine("Please enter the name for a save");
                Console.Write(">");
                userInput = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(userInput) || userInput.Length < min || userInput.Length > max)
                {
                    Console.WriteLine($"Save name has to be between {min} and {max} characters long.");
                    continue;
                }

                if (SaveTool.SaveGameExists(userInput) || ctx.BattleShipsSaves.Any(e => e.SaveName == userInput))
                {
                    Console.WriteLine($"A save '{userInput}' already exists. Do you wish to overwrite it? (Y/N)");
                    var userAnswer = Console.ReadLine()?.Trim().ToUpper();
                    if (userAnswer != null && userAnswer.Equals("Y"))
                    {
                        SaveTool.DeleteGameFromFile(userInput);
                        break;
                    }

                    continue;
                }

                break;
            }

            return(userInput);
        }
Beispiel #3
0
        private static string LoadGameFromJson()
        {
            var userChoice = "";
            var menu       = new Menu(MenuLevel.Level1);

            Console.Clear();
            var saveGames = SaveTool.LoadGamesFromFile();

            if (DisplaySaveGames(menu, saveGames, out string quitToMenu))
            {
                return(quitToMenu);
            }
            do
            {
                Console.WriteLine("Please enter the name of a save");
                Console.Write(">");
                var userInput = Console.ReadLine();
                if (userInput !.ToUpper().Trim() == "X" || userInput.ToUpper().Trim() == "M")
                {
                    break;
                }

                Console.WriteLine(userInput);
                if (saveGames.Any(e => e.SaveName.Equals(userInput)))
                {
                    var battleShipsSave = saveGames.First(e => e.SaveName.Equals(userInput));
                    var battleShipsGame = new BattleShips(battleShipsSave);

                    menu.AddNewMenuItem(new MenuItem("Save game", "S", null));

                    userChoice = HumanVsHumanMainGame(battleShipsGame, menu);
                    break;
                }

                Console.WriteLine("This save doesn't exist.");
            } while (true);

            return(userChoice);
        }