コード例 #1
0
        public void TestSelectMine()
        {
            /*
             * [1][1][░][░][░]
             * [*][1][░][1][1]
             * [1][1][░][1][*]
             * [░][░][░][2][2]
             * [░][░][░][1][*]
             */

            // EXPECTED:
            var expected     = File.ReadAllText("game_manager_output_3.txt").Replace("\r", "");
            var boardOptions = new BoardOptions(new Vector2(5, 5), 3, 170023000);

            // All the commands that the user will send.
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("Test User");
            stringBuilder.AppendLine("S 0 1");

            var input = new StringReader(stringBuilder.ToString());

            // All the output that the console prints.
            var output        = new StringWriter();
            var mockedConsole = new MockConsole(input, output);

            MineSweeperConsole.SetConsoleWrapper(mockedConsole);

            _gameManager.Start(boardOptions);

            Console.WriteLine(output.ToString().Replace("\r", ""));

            Assert.AreEqual(expected, output.ToString().Replace("\r", ""));
            Assert.False(_gameManager.IsRunning());
        }
コード例 #2
0
        public void ProcessAction(Action action)
        {
            switch (action)
            {
            case FinishGameAction _:
                _gameFinished = true;
                break;

            case SelectCellAction selectCellAction:
                _boardManager.SelectCell(_board, selectCellAction.CellPosition);

                // All the cells that are not mines has been selected. We WON.
                if (UserWon())
                {
                    _gameFinished = true;
                    _boardPrinter.PrintBoardWithCoords(_board);
                    MineSweeperConsole.WriteLine("YOU WON");
                }

                break;

            case FlagCellAction flagCellAction:
                _boardManager.FlagCell(_board, flagCellAction.CellPosition);
                break;
            }
        }
コード例 #3
0
 public void PrintScore()
 {
     foreach (var score in _scores)
     {
         MineSweeperConsole.WriteLine(score.ToString());
     }
 }
コード例 #4
0
        public void Start(BoardOptions boardOptions)
        {
            _gameFinished = false;
            _board        = _boardGenerator.GenerateBoard(boardOptions);
            _user.ReadUserName();

            var gameStart = DateTime.UtcNow;

            while (!_gameFinished)
            {
                try
                {
                    // Clear the screen
                    MineSweeperConsole.Clear();

                    // Print the board
                    _boardPrinter.PrintBoardWithCoords(_board);

                    // Print the available options
                    MineSweeperConsole.WriteLine(_menuOptions);

                    // Get the user input
                    var input = _user.ReadUserAction();
                    MineSweeperConsole.WriteLine(input);

                    // Parse the action
                    var action = _actionParser.ParseAction(input);

                    // Process the action
                    ProcessAction(action);
                }
                catch (MineFoundException)
                {
                    _gameFinished = true;

                    // Clear the screen
                    MineSweeperConsole.Clear();

                    // Print the board
                    _boardPrinter.PrintBoardWithCoords(_board);
                    MineSweeperConsole.WriteLine("YOU LOOSE");
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            var elapsedTime = DateTime.UtcNow - gameStart;
            var score       = Score.GenerateScore(TimeSpan.FromSeconds(elapsedTime.TotalSeconds), UserWon(), _user, _board);

            _scoreManager.AddRow(score);
            _scoreManager.PrintScore();
        }
コード例 #5
0
        public void TestCustomMap()
        {
            string map = "[░][░][░]\n" +
                         "[1][1][░]\n" +
                         $"[█][1][░]\n{Clr}\n";


            var cells = new[]
            {
                new BoardCell {
                    Position = new Vector2(0, 0), IsMine = false, NeighbouringCells = 0, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(1, 0), IsMine = false, NeighbouringCells = 0, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(2, 0), IsMine = false, NeighbouringCells = 0, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(0, 1), IsMine = false, NeighbouringCells = 1, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(1, 1), IsMine = false, NeighbouringCells = 1, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(2, 1), IsMine = false, NeighbouringCells = 0, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(0, 2), IsMine = true, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(1, 2), IsMine = false, NeighbouringCells = 1, Status = CellStatus.VISIBLE
                },
                new BoardCell {
                    Position = new Vector2(2, 2), IsMine = false, NeighbouringCells = 0, Status = CellStatus.VISIBLE
                },
            };

            var board = new Board(1, new Vector2(3, 3), cells);

            using var output = new StringWriter();
            using var input  = new StringReader("");
            MineSweeperConsole.SetConsoleWrapper(new MockConsole(input, output));

            _printer.PrintBoard(board);

            Assert.AreEqual(map, output.ToString());
        }
コード例 #6
0
        public void TestHiddenMapWithCoords()
        {
            string map = "    0  1  2 \n" +
                         "   ---------\n" +
                         "0| [█][█][█]\n" +
                         "1| [█][█][█]\n" +
                         $"2| [█][█][█]\n{Clr}\n";

            var cells = new[]
            {
                new BoardCell {
                    Position = new Vector2(0, 0), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(1, 0), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(2, 0), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(0, 1), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(1, 1), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(2, 1), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(0, 2), IsMine = false, NeighbouringCells = 1, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(1, 2), IsMine = false, NeighbouringCells = 1, Status = CellStatus.HIDDEN
                },
                new BoardCell {
                    Position = new Vector2(2, 2), IsMine = false, NeighbouringCells = 0, Status = CellStatus.HIDDEN
                },
            };

            var board = new Board(1, new Vector2(3, 3), cells);

            using var output = new StringWriter();
            using var input  = new StringReader("");
            MineSweeperConsole.SetConsoleWrapper(new MockConsole(input, output));

            _printer.PrintBoardWithCoords(board);
            Assert.AreEqual(map, output.ToString());
        }
コード例 #7
0
        public void PrintScore()
        {
            if (!File.Exists(_path))
            {
                return;
            }
            using var sr = File.OpenText(_path);

            MineSweeperConsole.WriteLine("\n\n::: SCOREBOARD :::");
            string s;

            while ((s = sr.ReadLine()) != null)
            {
                MineSweeperConsole.WriteLine(s);
            }
        }
コード例 #8
0
        public void TestAddScoreSuccess()
        {
            var user  = new MockUser("Test User");
            var board = new Board(1, Vector2.One, new BoardCell[2]);

            var input       = new StringReader("");
            var output      = new StringWriter();
            var consoleMock = new MockConsole(input, output);

            MineSweeperConsole.SetConsoleWrapper(consoleMock);
            var scoreManager = new ScoreManager();

            // Test when file does not exists.
            scoreManager.AddRow(Score.GenerateScore(TimeSpan.Zero, false, user, board));

            // Test when file already exists.
            scoreManager.AddRow(Score.GenerateScore(TimeSpan.FromSeconds(10), true, user, board));

            Assert.DoesNotThrow(() => scoreManager.PrintScore());
            Assert.AreEqual($"\n\n::: SCOREBOARD :::{Clr}\nTest User [BoardSize: 1*1; #Mines: 1; Win: False; Time: 0s]{Clr}\n" +
                            $"Test User [BoardSize: 1*1; #Mines: 1; Win: True; Time: 10s]{Clr}\n", output.ToString());
        }
コード例 #9
0
ファイル: User.cs プロジェクト: Adrian-MorenoG/MineSweeper
 public string ReadUserAction()
 {
     return(MineSweeperConsole.ReadLine());
 }
コード例 #10
0
ファイル: User.cs プロジェクト: Adrian-MorenoG/MineSweeper
 public void ReadUserName()
 {
     MineSweeperConsole.WriteLine("Put your name");
     Name = MineSweeperConsole.ReadLine();
 }