コード例 #1
0
 public void ReduceScoreShouldReduceTheNumberOfOpenPlayingFields()
 {
     PlayingField sampleField = new PlayingField(3, 4, 1);
     sampleField.OpenCell(0, 0);
     int scoreAfterFirstMove = sampleField.OpenCellsCounter;
     sampleField.ReduceScore(1);
     Assert.AreNotEqual(scoreAfterFirstMove, sampleField.OpenCellsCounter);
 }
コード例 #2
0
        /// <summary>
        /// Prints the playing field on each turn.
        /// </summary>
        /// <param name="playingField">Current playing field.</param>
        /// <param name="gamestatus">Status of the game, currently being played.</param>
        internal void PrintPlayingField(PlayingField playingField, GameStatus gamestatus)
        {
            MenuPrinter menuPrinter = new MenuPrinter();
            menuPrinter.ConsoleSetUp();
            menuPrinter.PrintBackground();

            int rows = playingField.Field.GetLength(0);
            int columns = playingField.Field.GetLength(1);

            string formatString = new string(' ', (Console.WindowWidth - (columns + (columns * 2))) / 2);

            this.sb.AppendLine();
            this.sb.Append(formatString);

            for (int i = 0; i < columns; i++)
            {
                this.sb.Append(string.Format("{0}", i).PadLeft(2, ' ') + " ");
            }

            this.sb.AppendLine();
            this.PrintLine(columns);

            for (int i = 0; i < rows; i++)
            {
                this.sb.Append(new string(' ', ((Console.WindowWidth - (columns + (columns * 2))) / 2) - 4));
                this.sb.Append(string.Format("{0}", i).PadLeft(2, ' '));
                this.sb.Append(" | ");

                for (int j = 0; j < columns; j++)
                {
                    Cell currentField = playingField.Field[i, j];

                    if (gamestatus == GameStatus.GameOn)
                    {
                        if (currentField.Status == CellStatus.Opened)
                        {
                            this.sb.Append(playingField.Field[i, j].Value);
                            this.sb.Append("  ");
                        }
                        else if (currentField.Status == CellStatus.Flagged)
                        {
                            this.sb.Append(Flag);
                        }
                        else
                        {
                            this.sb.Append(CellClosed);
                        }
                    }
                    else if (gamestatus == GameStatus.GameOver)
                    {
                        if (currentField.IsMine)
                        {
                            this.sb.Append(MineOpened);
                        }
                        else
                        {
                            this.sb.Append(playingField.Field[i, j].Value + "  ");
                        }
                    }
                    else
                    {
                        if (currentField.IsMine == true || currentField.Status == CellStatus.Flagged)
                        {
                            this.sb.Append(Flag);
                        }
                        else
                        {
                            this.sb.Append(playingField.Field[i, j].Value + "  ");
                        }
                    }
                }

                this.sb.Append("|");
                this.sb.AppendLine();
            }

            this.PrintLine(columns);
            Console.WriteLine(this.sb.ToString());
            this.sb.Clear();
        }
コード例 #3
0
        /// <summary>
        /// Method which takes care of anything the user inputs on the console during a minesweeper game.
        /// </summary>
        /// <param name="playingField">Method gets the current playing field.</param>
        public void HandleInput(PlayingField playingField)
        {
            int row;
            int col;
            int fieldRows = playingField.Field.GetLength(0);
            int fieldCows = playingField.Field.GetLength(1);

            bool isValid = false;
            while (!isValid)
            {
                Console.Write(EnterCordinates);
                string inputCommand = Console.ReadLine();
                this.InputCoordinates = inputCommand.Split(' ');

                if (this.InputCoordinates.Length == 1)
                {
                    switch (this.InputCoordinates[0])
                    {
                        case "restart":
                            Game.Instance().RestartGame();
                            isValid = true;
                            break;
                        case "exit":
                            Game.Instance().ChangeToGameOver();
                            isValid = true;
                            break;
                        case "undo":
                            List<Coordinates> lastTurnCells = Game.Instance().Memento.GetLastCells();

                            if (lastTurnCells == null)
                            {
                                Console.Beep(700, 400);
                                Console.Beep(700, 400);
                            }
                            else
                            {
                                int howManyFieldsOpenedLast = 0;
                                foreach (var cell in lastTurnCells)
                                {
                                    if (playingField.Field[cell.Row, cell.Col].Status == CellStatus.Closed)
                                    {
                                        playingField.Field[cell.Row, cell.Col].Status = CellStatus.Flagged;
                                        Game.Instance().NumberOfFlags += 1;
                                    }
                                    else if (playingField.Field[cell.Row, cell.Col].Status == CellStatus.Opened)
                                    {
                                        playingField.Field[cell.Row, cell.Col].Status = CellStatus.Closed;
                                        howManyFieldsOpenedLast++;
                                    }
                                    else
                                    {
                                        playingField.Field[cell.Row, cell.Col].Status = CellStatus.Closed;
                                        Game.Instance().NumberOfFlags -= 1;
                                    }
                                }

                                Game.Instance().Memento.RemoveCells();
                                playingField.ReduceScore(howManyFieldsOpenedLast);
                            }

                            isValid = true;
                            break;

                        default:
                            Console.WriteLine(InvalidCoordinatesText);
                            break;
                    }
                }
                else
                {
                    bool isValidIntRow = int.TryParse(this.InputCoordinates[0], out row) && row < fieldRows && row >= 0;
                    bool isValidIntCol = int.TryParse(this.InputCoordinates[1], out col) && col < fieldCows && col >= 0;

                    if (isValidIntRow && isValidIntCol && this.InputCoordinates.Length > 2)
                    {
                        if (this.InputCoordinates[2].ToLower() == "f")
                        {
                            playingField.SetFlag(row, col);
                            break;
                        }
                        else if (this.InputCoordinates[2].ToLower() == "r")
                        {
                            playingField.RemoveFlag(row, col);
                            break;
                        }
                        else
                        {
                            Console.WriteLine(InvalidCoordinatesText);
                        }
                    }
                    else if (isValidIntRow && isValidIntCol)
                    {
                        playingField.OpenCell(row, col);
                        break;
                    }
                    else
                    {
                        Console.WriteLine(InvalidCoordinatesText);
                    }
                }
            }
        }
コード例 #4
0
 public void WhenNewFieldIsCreatedOpenCellsCounterShouldBeZero()
 {
     PlayingField sampleField = new PlayingField(3, 4, 1);
     Assert.AreEqual(0, sampleField.OpenCellsCounter);
 }
コード例 #5
0
 public void OpeningCellsShouldIncreaseTheOpenCellCounter()
 {
     PlayingField sampleField = new PlayingField(3, 4, 1);
     sampleField.OpenCell(0, 0);
     Assert.AreNotEqual(0, sampleField.OpenCellsCounter);
 }
コード例 #6
0
 public void CreatingPlayingFieldShouldCreateMinesweeperPlayingField()
 {
     PlayingField sampleField = new PlayingField(3, 4, 1);
     Assert.AreEqual(3, sampleField.Field.GetLength(0));
     Assert.AreEqual(4, sampleField.Field.GetLength(1));
 }