Esempio n. 1
0
        /// <summary>
        /// Makes or erases a game move
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="action">false if you want to erase a move and true if you want to make one</param>
        private static void UpdateGameState(bool action, int x = -1, int y = -1)
        {
            if (game == null)
            {
                return;
            }
            GameMove?lastMove = action ? game.MakeMove(x, y) : game.UndoMove();

            if (lastMove == null)
            {
                return;
            }
            lastAction = $"{(action ? "" : "Undone: ")}{((action ? game.Turn : !game.Turn) ? PLAYER1 : PLAYER2)} {lastMove}";

            int lastRow = Console.CursorTop;
            int lastCol = Console.CursorLeft;

            if (lastMove.res == CellState.SHOT_EMPTY)
            {
                Console.SetCursorPosition(0, BoardRow);
                DrawBoard(true);
                PostLastAction("Press Any Key to start next players turn.");
                Console.ReadKey(true);
            }

            Console.SetCursorPosition(0, playerRow);
            WriteLine(CLEAR_LINE);
            Console.SetCursorPosition(0, playerRow);
            WriteLine($"{(game.Turn ? PLAYER2 : PLAYER1)}'s turn\n", ConsoleColor.Cyan);

            Console.SetCursorPosition(0, BoardRow);
            DrawBoard();

            PostLastAction(lastAction);

            Console.SetCursorPosition(0, lastLogRow);
            if (action)
            {
                WriteLine($"{(game.Turn ? PLAYER1 : PLAYER2)} {lastMove}");
                lastLogRow++;
            }
            else
            {
                Console.CursorTop--;
                WriteLine(CLEAR_LINE ?? "");
                lastLogRow--;
            }

            if (game.Check)
            {
                PostLastAction($"{(game.Turn ? PLAYER2 : PLAYER1)} has Won!!! Congratulations!!!!!");
                hasOutcome = true;
            }
            if (!action)
            {
                hasOutcome = false;
            }

            Console.SetCursorPosition(0, 0);
            Console.SetCursorPosition(lastCol, lastRow);
        }