示例#1
0
 private void RestartGame()
 {
     Console.WriteLine(Messages.Welcome);
     this.tiles          = MatrixGenerator.GenerateMatrix();
     this.tiles          = MatrixGenerator.ShuffleMatrix(this.tiles);
     this.isGameFinished = Gameplay.IsMatrixSolved(this.tiles);
     Gameplay.PrintMatrix(this.tiles);
     this.gameState = State.InGame;
 }
示例#2
0
        public void ShuffleMatrixTest()
        {
            List <ITile> tiles = new List <ITile>();

            for (int index = 1; index < 16; index++)
            {
                tiles.Add(new Tile(index.ToString(), index - 1));
            }
            tiles.Add(new Tile(string.Empty, 15));

            int           occurrencesCount = 0;
            IList <ITile> shuffledTiles    = MatrixGenerator.ShuffleMatrix(tiles);

            for (int index = 0; index < tiles.Count; index++)
            {
                if (tiles[index].Label == shuffledTiles[index].Label)
                {
                    occurrencesCount++;
                }
            }

            Assert.AreNotEqual(occurrencesCount, tiles.Count);
        }
        /// <summary>
        /// Main method of the program.
        /// </summary>
        public static void Main()
        {
            List <Tile> tilesMatrix    = new List <Tile>();
            int         movesCount     = 0;
            string      currentCommand = "restart";
            bool        isMatrixSolved = false;

            while (currentCommand != "exit")
            {
                if (!isMatrixSolved)
                {
                    switch (currentCommand)
                    {
                    case "restart":
                    {
                        string welcomeMessage = "Welcome to the game “15”. Please try to arrange the numbers sequentially." +
                                                "\nUse 'top' to view the top scoreboard, 'restart' to start a new game and 'exit'\nto quit the game.";
                        Console.WriteLine();
                        Console.WriteLine(welcomeMessage);
                        tilesMatrix    = MatrixGenerator.GenerateMatrix();
                        tilesMatrix    = MatrixGenerator.ShuffleMatrix(tilesMatrix);
                        isMatrixSolved = Gameplay.IsMatrixSolved(tilesMatrix);
                        Console.WriteLine(Gameplay.GetMatrixAsString(tilesMatrix));
                        break;
                    }

                    case "top":
                    {
                        Console.WriteLine(Scoreboard.PrintScoreboard());
                        break;
                    }
                    }

                    Console.Write("Enter a number to move: ");
                    currentCommand = Console.ReadLine();

                    int  tileLabel;
                    bool isMovingCommand = int.TryParse(currentCommand, out tileLabel);

                    if (isMovingCommand)
                    {
                        try
                        {
                            Gameplay.MoveTiles(tilesMatrix, tileLabel);
                            movesCount++;
                            Console.WriteLine(Gameplay.GetMatrixAsString(tilesMatrix));
                            isMatrixSolved = Gameplay.IsMatrixSolved(tilesMatrix);
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception.Message);
                        }
                    }
                    else
                    {
                        currentCommand = currentCommand.ToLower();
                        if (!Enum.GetNames(typeof(Command)).Any(x => x.ToLower().Equals(currentCommand)))
                        {
                            Console.WriteLine("Invalid command");
                        }
                    }
                }
                else
                {
                    if (movesCount == 0)
                    {
                        Console.WriteLine("Your matrix was solved by default :) Come on - NEXT try");
                    }
                    else
                    {
                        Console.WriteLine("Congratulations! You won the game in {0} moves.", movesCount);
                        if (Scoreboard.CheckPlayerScores(movesCount))
                        {
                            Console.Write("Please enter your name for the top scoreboard: ");
                            string playerName = Console.ReadLine();
                            Player player     = new Player(playerName, movesCount);
                            Scoreboard.AddPlayer(player);
                        }
                        else
                        {
                            Console.WriteLine("Your scores are not at top five.");
                        }

                        Console.WriteLine(Scoreboard.PrintScoreboard());
                    }

                    currentCommand = "restart";
                    isMatrixSolved = false;
                    movesCount     = 0;
                }
            }
        }