コード例 #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
        private int[] GenerateResourceNumbers(BoardOptions template)
        {
            //new resource numbers
            switch (template)
            {
            case BoardOptions.tutorial:
                return(new int[] { 11, 12, 9, 4, 6, 5, 10, 7, 3, 11, 4, 8, 8, 10, 9, 3, 5, 2, 6 });
            }

            int[] result = new int[] { 2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12, 7 };
            result = (int[])Randomize(result);
            for (int i = 0; i < HexGridImgPath.Length; i++)
            {
                if (HexGridImgPath[i].Contains("Desert"))
                {
                    Random random = new Random();
                    int    swap   = 19;
                    while (swap != i)
                    {
                        swap = random.Next(0, 18);
                    }
                    result[swap] = result[i];
                    result[i]    = 7;
                }
            }
            return(result);
        }
コード例 #3
0
        public void Setup()
        {
            _logger = new Logger();

            var user = new User();

            var boardPrinter = new ConsoleBoardPrinter();
            var scoreManager = new ScoreManager();

            _actionParser   = new ActionParser();
            _boardManager   = new BoardManager();
            _boardGenerator = new BoardGenerator();

            // Delete all the scores at the start of each test.
            scoreManager.DeleteAll();

            //Setups the game
            _gameManager = new GameManager(boardPrinter, _boardManager, _boardGenerator, _actionParser, scoreManager, user);

            _boardOptions = new BoardOptions(new Vector2(5, 5), 5);

            _board = _boardGenerator.GenerateBoard(_boardOptions);

            // Generate all positions file
            FileGenerator.GenerateAllPositions(_board);
        }
コード例 #4
0
 public DataService(ILoggerFactory loggerFactory, IOptions <BoardOptions> options)
 {
     Logger         = loggerFactory.CreateLogger <DataService>();
     _loggerFactory = loggerFactory;
     _dict          = new Dictionary <string, DataHolder>();
     Instance       = this;
     Fetchers       = new List <HdojFetcher>();
     Options        = options.Value;
 }
コード例 #5
0
        public void TestCorrectMineNumber()
        {
            var boardSize    = new Vector2(10, 10);
            var boardOptions = new BoardOptions(boardSize, 10);

            var board = _boardGenerator.GenerateBoard(boardOptions);

            Assert.AreEqual(10, board.MineNumber);
        }
コード例 #6
0
        public void TestCorrectBoardSize()
        {
            var boardSize    = new Vector2(10, 10);
            var boardOptions = new BoardOptions(boardSize, 10, 10);

            var board = _boardGenerator.GenerateBoard(boardOptions);

            Assert.AreEqual(boardSize, board.Size);
        }
コード例 #7
0
 public BoardState(BoardOptions template)
 {
     HexGridImgPath = GenerateHexPaths(template);
     ResourceNumber = GenerateResourceNumbers(template);
     Road           = new Player[72];
     Settlement     = new Player[53];
     City           = new Player[53];
     Thief          = FindDesert();
 }
コード例 #8
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();
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: Adrian-MorenoG/MineSweeper
        private static void Main(string[] args)
        {
            var user           = new User();
            var boardPrinter   = new ConsoleBoardPrinter();
            var boardManager   = new BoardManager();
            var boardGenerator = new BoardGenerator();
            var actionParser   = new ActionParser();
            var scoreManager   = new ScoreManager();
            var gameManager    = new GameManager(boardPrinter, boardManager, boardGenerator, actionParser, scoreManager, user);
            var boardOptions   = new BoardOptions(new Vector2(5, 5), 5);

            gameManager.Start(boardOptions);
        }
コード例 #10
0
        public void UncoveringMineShowsMessageBox()
        {
            var fakeMineLocationGenerator = A.Fake <IMineLocationGenerator>();
            var fakeDialogService         = A.Fake <IDialogService>();
            var mainWindowViewModel       = new MainWindowViewModel(fakeMineLocationGenerator, fakeDialogService);
            var mineCoords   = new Coords(0, 0);
            var boardOptions = new BoardOptions {
                NumRows = 5, NumCols = 5, NumMines = 1
            };

            A.CallTo(() => fakeMineLocationGenerator.GenerateMineLocations(
                         boardOptions.NumRows,
                         boardOptions.NumCols,
                         boardOptions.NumMines)).Returns(new[] { mineCoords });
            mainWindowViewModel.SetBoardOptions(boardOptions);
            mainWindowViewModel.UncoverSquare(mineCoords);
            A.CallTo(() => fakeDialogService.ShowMessageBox(A <string> ._)).MustHaveHappened();
            A.CallTo(() => fakeDialogService.ShowMessageBox("You lost!")).MustHaveHappened();
        }
コード例 #11
0
        public void LoseInOneMoveAllPositions()
        {
            _boardOptions = new BoardOptions(new Vector2(5, 5), 24);

            using StreamReader sr = File.OpenText(FILENAMEALLPOSITIONS);
            string instruction = string.Empty;

            while ((instruction = sr.ReadLine()) != null)
            {
                _board = _boardGenerator.GenerateBoard(_boardOptions);

                // All the positions of the board are mines.
                foreach (BoardCell boardCell in _board.Cells)
                {
                    boardCell.IsMine = false;
                }

                // Parse the action
                SelectCellAction action = (SelectCellAction)_actionParser.ParseAction(instruction);
                BoardCell        cell   = _board.Cells.FirstOrDefault(x => x.Position == action.CellPosition);

                //The required cell is not a mine
                cell.IsMine = true;

                try
                {
                    _boardManager.SelectCell(_board, action.CellPosition);
                }
                catch (System.Exception ex)
                {
                    try
                    {
                        Assert.AreEqual(typeof(MineFoundException), ex.GetType());
                        _logger.Log(string.Format("We lose the game in one move at {0}", instruction), AutomationTestType.LoseInOneMoveAllPositions);
                    }
                    catch (System.Exception)
                    {
                        _logger.Log(string.Format("ASSERT ERROR: We don't lose the game in one move at {0}", instruction), AutomationTestType.LoseInOneMoveAllPositions);
                    }
                }
            }
        }
コード例 #12
0
        public void WinInOneMoveAllPositions()
        {
            _boardOptions = new BoardOptions(new Vector2(5, 5), 24);

            using StreamReader sr = File.OpenText(FILENAMEALLPOSITIONS);
            string instruction = string.Empty;

            while ((instruction = sr.ReadLine()) != null)
            {
                _board = _boardGenerator.GenerateBoard(_boardOptions);

                // All the positions of the board are mines.
                foreach (BoardCell boardCell in _board.Cells)
                {
                    boardCell.IsMine = true;
                }

                // Parse the action
                SelectCellAction action = (SelectCellAction)_actionParser.ParseAction(instruction);
                BoardCell        cell   = _board.Cells.FirstOrDefault(x => x.Position == action.CellPosition);

                //The required cell is not a mine
                cell.IsMine = false;

                _boardManager.SelectCell(_board, action.CellPosition);

                bool result = _board.Cells.Count(c => !c.IsMine && c.Status == CellStatus.VISIBLE) ==
                              _board.Cells.Length - _board.MineNumber;

                try
                {
                    Assert.AreEqual(true, result);
                    _logger.Log(string.Format("Win the game in one move {0}", instruction), AutomationTestType.WinInOneMoveAllPositions);
                }
                catch (System.Exception)
                {
                    _logger.Log(string.Format("ASSERT ERROR: We don't win the game in one move {0}", instruction), AutomationTestType.WinInOneMoveAllPositions);
                }
            }
        }
コード例 #13
0
        public Board GenerateBoard(BoardOptions options)
        {
            var maxX = (int)options.Size.X;
            var maxY = (int)options.Size.Y;

            var cellsNum = (int)(options.Size.X * options.Size.Y);
            var cells    = new BoardCell[cellsNum];

            var posRandomGenerator = new Random(options.Seed);
            var minePositions      = new List <Vector2>();

            while (minePositions.Count < options.Mines)
            {
                var x      = posRandomGenerator.Next(0, maxX);
                var y      = posRandomGenerator.Next(0, maxY);
                var newPos = new Vector2(x, y);

                if (!minePositions.Contains(newPos))
                {
                    minePositions.Add(newPos);
                }
            }

            for (var y = 0; y < maxY; y++)
            {
                for (var x = 0; x < maxX; x++)
                {
                    var position = new Vector2(x, y);
                    var cell     = new BoardCell
                    {
                        Position          = position,
                        IsMine            = minePositions.Contains(position),
                        NeighbouringCells = 0,
                        Status            = CellStatus.HIDDEN
                    };

                    cells[x + y * maxX] = cell;
                }
            }

            var validNeighbourPositions = new[]
            {
                new Vector2(-1, -1),
                new Vector2(0, -1),
                new Vector2(1, -1),
                new Vector2(-1, 0),
                new Vector2(1, 0),
                new Vector2(-1, 1),
                new Vector2(0, 1),
                new Vector2(1, 1)
            };

            foreach (var minePosition in minePositions)
            {
                foreach (var validNeighbourPosition in validNeighbourPositions)
                {
                    var currentPosition = minePosition + validNeighbourPosition;

                    if (currentPosition.X >= 0 &&
                        currentPosition.Y >= 0 &&
                        currentPosition.X < maxX &&
                        currentPosition.Y < maxY)
                    {
                        cells[(int)(currentPosition.X + currentPosition.Y * maxX)].NeighbouringCells++;
                    }
                }
            }

            return(new Board(options.Mines, options.Size, cells));;
        }
コード例 #14
0
        public static string[] GenerateBoardPicturePath(BoardOptions choosenOption)
        {
            int maxDesert   = 1;
            int maxBrick    = 3;
            int maxFeild    = 4;
            int maxForest   = 4;
            int maxHill     = 3;
            int maxMountain = 3;

            string desert   = @"/Images/hex/Desert.png";
            string brick    = @"/Images/hex/Brick.png";
            string feild    = @"/Images/hex/Field.png";
            string forest   = @"/Images/hex/Forest.png";
            string hill     = @"/Images/hex/Hill.png";
            string mountain = @"/Images/hex/Mountain.png";

            string[] pathToHexpicture = new string[18];
            switch (choosenOption)
            {
            case BoardOptions.tutorial:
                pathToHexpicture[0]  = brick;
                pathToHexpicture[1]  = feild;
                pathToHexpicture[2]  = forest;
                pathToHexpicture[3]  = hill;
                pathToHexpicture[4]  = mountain;
                pathToHexpicture[5]  = brick;
                pathToHexpicture[6]  = feild;
                pathToHexpicture[7]  = forest;
                pathToHexpicture[8]  = hill;
                pathToHexpicture[9]  = desert;
                pathToHexpicture[10] = mountain;
                pathToHexpicture[11] = brick;
                pathToHexpicture[12] = feild;
                pathToHexpicture[13] = forest;
                pathToHexpicture[14] = hill;
                pathToHexpicture[15] = forest;
                pathToHexpicture[16] = mountain;
                pathToHexpicture[17] = feild;

                break;

            case BoardOptions.random:
                pathToHexpicture = PlacePicturePath(brick, maxBrick, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(feild, maxFeild, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(forest, maxForest, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(hill, maxHill, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(mountain, maxMountain, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(desert, maxDesert, pathToHexpicture);

                break;

            case BoardOptions.center:
                //Always desert on CenterTile.
                pathToHexpicture[9] = desert;
                pathToHexpicture    = PlacePicturePath(brick, maxBrick, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(feild, maxFeild, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(forest, maxForest, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(hill, maxHill, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(mountain, maxMountain, pathToHexpicture);
                break;
            }
            return(pathToHexpicture);
        }
コード例 #15
0
        private string[] GenerateHexPaths(BoardOptions choosenOption)
        {
            int maxDesert   = 1;
            int maxHill     = 3;
            int maxFeild    = 4;
            int maxForest   = 4;
            int maxPasture  = 4;
            int maxMountain = 3;

            string desert   = @"/Content/Images/Hex/Desert.png";
            string hill     = @"/Content/Images/Hex/Hill.png";
            string field    = @"/Content/Images/Hex/Field.png";
            string forest   = @"/Content/Images/Hex/Forest.png";
            string pasture  = @"/Content/Images/Hex/Pasture.png";
            string mountain = @"/Content/Images/Hex/Mountain.png";

            string[] pathToHexpicture = new string[19];
            switch (choosenOption)
            {
            case BoardOptions.tutorial:
                pathToHexpicture[0]  = hill;
                pathToHexpicture[1]  = field;
                pathToHexpicture[2]  = forest;
                pathToHexpicture[3]  = pasture;
                pathToHexpicture[4]  = mountain;
                pathToHexpicture[5]  = hill;
                pathToHexpicture[6]  = field;
                pathToHexpicture[7]  = forest;
                pathToHexpicture[8]  = pasture;
                pathToHexpicture[9]  = desert;
                pathToHexpicture[10] = mountain;
                pathToHexpicture[11] = hill;
                pathToHexpicture[12] = field;
                pathToHexpicture[13] = forest;
                pathToHexpicture[14] = pasture;
                pathToHexpicture[15] = forest;
                pathToHexpicture[16] = mountain;
                pathToHexpicture[17] = field;
                pathToHexpicture[18] = pasture;

                break;

            case BoardOptions.random:
                pathToHexpicture = PlacePicturePath(hill, maxHill, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(field, maxFeild, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(forest, maxForest, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(pasture, maxPasture, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(mountain, maxMountain, pathToHexpicture);
                pathToHexpicture = PlacePicturePath(desert, maxDesert, pathToHexpicture);

                break;

            case BoardOptions.center:
                //Always desert on CenterTile.
                pathToHexpicture[9] = desert;
                pathToHexpicture    = PlacePicturePath(hill, maxHill, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(field, maxFeild, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(forest, maxForest, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(pasture, maxPasture, pathToHexpicture);
                pathToHexpicture    = PlacePicturePath(mountain, maxMountain, pathToHexpicture);
                break;
            }
            return(pathToHexpicture);
        }