Exemple #1
0
        private static string PLAYER2 = "Player_2";             // Second Player Name

        public static void RunConsoleGame(Setting?setting, GameSave?save = null, GAME_TYPE gameType = GAME_TYPE.HU_VS_HU)
        {
            try
            {
                hasOutcome = false;
                lastAction = null;
                if (setting == null)
                {
                    setting = new Setting();
                }
                if (gameType == GAME_TYPE.HU_VS_HU)
                {
                    PLAYER1_CONTROLLER = Selector;
                    PLAYER2_CONTROLLER = Selector;
                }
                else if (gameType == GAME_TYPE.HU_VS_AI)
                {
                    PLAYER1_CONTROLLER = Selector;
                    PLAYER2_CONTROLLER = AISelector;
                }
                if (save == null)
                {
                    restart = true;
                    game    = new Battleships(setting);
                }
                else
                {
                    restart = false;
                    game    = new Battleships(save);
                }
                PLAYER1 = game.Player_1;
                PLAYER2 = game.Player_2;
                bool           loop = true;
                ConsoleKeyInfo pressedKey;
                Init();
                if (save == null)
                {
                    if (setting.PlaceType == PlaceShips.Select)
                    {
                        SetShips();
                    }
                    else
                    {
                        game.PlaceRandom();
                    }
                    Console.SetCursorPosition(0, BoardRow);
                    DrawBoard();
                    Console.SetCursorPosition(firstCellCol, firstCellRow);
                    System.Threading.Thread.Sleep(100);
                    if (setting.PlaceType == PlaceShips.Select && gameType == GAME_TYPE.HU_VS_HU)
                    {
                        SetShips();
                    }
                    else
                    {
                        game.PlaceRandom();
                    }
                    Console.SetCursorPosition(firstCellCol, firstCellRow);
                }
                Console.SetCursorPosition(0, BoardRow);
                DrawBoard();
                Console.SetCursorPosition(firstCellCol, firstCellRow);
                do
                {
                    if (hasOutcome)
                    {
                        pressedKey = Selector();
                    }
                    else
                    {
                        pressedKey = !game.Turn ? PLAYER1_CONTROLLER() : PLAYER2_CONTROLLER();
                    }
                    if (pressedKey.Modifiers == CTRL)
                    {
                        if (pressedKey.Key == EXIT)
                        {
                            loop = false;
                        }
                        if (pressedKey.Key == NEW)
                        {
                            restart = true;
                            Init();
                            if (setting.PlaceType == PlaceShips.Select)
                            {
                                SetShips();
                            }
                            else
                            {
                                game.PlaceRandom();
                            }
                            Console.SetCursorPosition(0, BoardRow);
                            DrawBoard();
                            Console.SetCursorPosition(firstCellCol, firstCellRow);
                            System.Threading.Thread.Sleep(100);
                            if (setting.PlaceType == PlaceShips.Select && gameType == GAME_TYPE.HU_VS_HU)
                            {
                                SetShips();
                            }
                            else
                            {
                                game.PlaceRandom();
                            }
                            Console.SetCursorPosition(firstCellCol, firstCellRow);
                            Console.SetCursorPosition(0, BoardRow);
                            DrawBoard();
                            Console.SetCursorPosition(firstCellCol, firstCellRow);
                        }
                        //if (pressedKey.Key == UNDO)
                        //    UpdateGameState(false);
                        if (pressedKey.Key == SAVE && gameType == GAME_TYPE.HU_VS_HU)
                        {
                            SaveGame();
                        }
                    }
                    else if (pressedKey.Key == ENTER || pressedKey.Key == ENTER_ALT)
                    {
                        UpdateGameState(true, (Console.CursorTop - firstCellRow) / 2, (Console.CursorLeft - firstCellCol) / (ROW_SEP.Length + CROSS_POINT.Length));
                    }
                } while (loop);

                Console.Clear();
            }
            catch (Exception)
            {
                return;
            }
        }