Exemple #1
0
        private void DisplayGameStatus(GameState state)
        {
            Console.SetCursorPosition(BoardDistanceFromLeft, GameStatusDistanceFrmTop);

            switch (state.Status)
            {
                case GameStatus.InProgress:
                {
                    Console.Write("Use the arrow keys to move numbers around");
                    break;
                }

                case GameStatus.Lost:
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("Game Over");
                    Console.ResetColor();
                    break;
                }

                case GameStatus.Won:
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("You won!");
                    Console.ResetColor();
                    break;
                }

                case GameStatus.Quit:
                {
                    Console.Write("Goodbye!");
                    break;
                }
            }
        }
        public GameState GenerateInitialBoard()
        {
            var state = new GameState
            {
                Status = GameStatus.InProgress
            };

            AddTileToBoard(state);
            AddTileToBoard(state);

            return state;
        }
Exemple #3
0
        public void DisplayGame(GameState state, int seed)
        {
            if (state == null)
                throw new ArgumentNullException("state");

            Console.Clear();

            DisplayBoard(state.Board);
            DisplayScore(state.Score);
            DisplayLastActions(state.Actions);
            DisplaySeed(seed);
            DisplayGameStatus(state);

            Console.SetCursorPosition(0, GameStatusDistanceFrmTop + 2);
        }
        public void RunGameAction(GameState state, GameAction action)
        {
            MoveStatistics statistics = null;
            state.Actions.AddLast(action);

            var actionsTriggeringBoardStore = new List<GameAction>()
            {
                GameAction.Up,
                GameAction.Right,
                GameAction.Left,
                GameAction.Down
            };

            if (actionsTriggeringBoardStore.Contains(action))
                StoreCurrentBoard(state);

            switch (action)
            {
                case GameAction.Quit:
                {
                    state.Status = GameStatus.Quit;
                    break;
                }

                case GameAction.Undo:
                {
                    LoadPreviousBoard(state);
                    break;
                }

                // To simplify the calculations, rotate the board so we are always
                // sliding to the left, then rotate it back
                case GameAction.Left:
                {
                    statistics = SlideTilesToLeft(state.Board);
                    break;
                }

                case GameAction.Up:
                {
                    MatrixHelper.RotateNegative90Degrees(state.Board);
                    statistics = SlideTilesToLeft(state.Board);
                    MatrixHelper.Rotate90Degrees(state.Board);
                    break;
                }

                case GameAction.Right:
                {
                    MatrixHelper.Rotate180Degrees(state.Board);
                    statistics = SlideTilesToLeft(state.Board);
                    MatrixHelper.Rotate180Degrees(state.Board);
                    break;
                }

                case GameAction.Down:
                {
                    MatrixHelper.Rotate90Degrees(state.Board);
                    statistics = SlideTilesToLeft(state.Board);
                    MatrixHelper.RotateNegative90Degrees(state.Board);
                    break;
                }
            }

            if (statistics != null && statistics.MoveOccurred)
            {
                AddTileToBoard(state);

                if (!AnyMovesPossible(state.Board))
                    state.Status = GameStatus.Lost;

                if (HasWon(state.Board))
                    state.Status = GameStatus.Won;

                foreach (var mergeResult in statistics.MergeResults)
                {
                    state.Score += mergeResult;
                }
            }
        }
 private void StoreCurrentBoard(GameState state)
 {
     var clonedBoard = (int[,])state.Board.Clone();
     state.PreviousBoards.Push(clonedBoard);
 }
        private void LoadPreviousBoard(GameState state)
        {
            if (!state.PreviousBoards.Any())
                return;

            var previousBoard = state.PreviousBoards.Pop();
            state.Board = previousBoard;
        }
        private void AddTileToBoard(GameState state)
        {
            // Create a list of all empty tiles and pick one from random
            var emptyCoordinates = new List<Coordinate>();

            for (int row = 0; row < state.Board.GetLength(0); row++)
            {
                for (int col = 0; col < state.Board.GetLength(1); col++)
                {
                    if (state.Board[row, col] == 0)
                        emptyCoordinates.Add(new Coordinate(row, col));
                }
            }

            if (emptyCoordinates.Any())
            {
                int indexToAdd = _random.Next(0, emptyCoordinates.Count);
                var coords = emptyCoordinates[indexToAdd];
                state.Board[coords.Row, coords.Column] = 2;
            }
        }