/// <summary>Query if 'currentMatrix' is sorted.</summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <param name="currentMatrix" type="int[,]">The game field.</param>
        /// <returns>true if sorted, false if not.</returns>
        public bool IsSorted(int[,] currentMatrix)
        {
            if (currentMatrix == null)
            {
                throw new ArgumentNullException("The matrix cannot be null or empty");
            }

            int matrixSize = currentMatrix.GetLength(0);

            INumberGenerator numberGenerator = new NumberGenerator(matrixSize * matrixSize);
            IMatrixGenerator matrixGenerator = new MatrixGenerator(matrixSize, numberGenerator);

            int[,] sortedMatrix = matrixGenerator.GenerateMatrix();

            for (int i = 0; i < matrixSize; i++)
            {
                for (int j = 0; j < matrixSize; j++)
                {
                    if (currentMatrix[i, j] != sortedMatrix[i, j])
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>Starts new game.</summary>
        public void StartNewGame()
        {
            IMatrixGenerator matrixGenerator = new MatrixGenerator(CommonConstants.GAME_BOARD_SIZE, this.numberGenerator);

            int[,] currentMatrix = matrixGenerator.GenerateMatrix();
            IEqualMatrixChecker         equalMatrixChecker = new EqualMatrixChecker();
            MatrixEmptyCellRandomizator matrixRandomizator = new MatrixEmptyCellRandomizator();
            Point   emptyPoint = matrixRandomizator.Randomize(currentMatrix);
            Command currentCommand;

            this.renderer.PrintWelcome();

            // main algorithm
            int playerMoves = 0;

            gameEnd = false;
            string inputString = "";

            while (!gameEnd)
            {
                this.renderer.RenderMatrix(currentMatrix);
                if (equalMatrixChecker.IsSorted(currentMatrix))  // IsGameWon check
                {
                    this.renderer.PrintGameWon(playerMoves);

                    this.renderer.Print(CommonConstants.PLAYER_NAME);
                    string playerName = "";
                    while (true)
                    {
                        playerName = this.inputReader.Read();
                        if (!string.IsNullOrEmpty(playerName))
                        {
                            break;
                        }
                        this.renderer.Print(CommonConstants.NON_EMPTY_PLAYER_NAME);
                    }

                    Player currentPlayer = new Player(playerName, playerMoves);
                    scoreboard.AddPlayer(currentPlayer);

                    this.renderer.RenderScoreboard(scoreboard);
                    return;
                }

                this.renderer.Print(CommonConstants.NUMBER_TO_MOVE);
                inputString = this.inputReader.Read();

                switch (inputString)
                {
                case "exit":
                    currentCommand = new ExitCommand(this.renderer, this);
                    break;

                case "restart":
                    currentCommand = new RestartCommand(this);
                    break;

                case "top":
                    currentCommand = new ShowScoreboardCommand(this.renderer, scoreboard);
                    break;

                default:
                    currentCommand = new DefaultCommand(currentMatrix, this.renderer, emptyPoint, inputString);
                    break;
                }

                currentCommand.Execute();

                if (currentCommand is DefaultCommand && (currentCommand as DefaultCommand).IsPlayerMoved)
                {
                    playerMoves++;
                }
            }
        }