private static void ExecuteComand(string inputString, ref int moves) { MatrixGenerator matrixGenerator = new MatrixGenerator(GAME_BOARD_SIZE); IMatrixRenderer matrixRenderer = new MatrixRenderer(); int matrixLength = currentMatrix.GetLength(0); IEqualMatrixChecker equalMatrixChecker = new EqualMatrixChecker(matrixLength, new MatrixGenerator(matrixLength)); switch (inputString) { case "restart": moves = 0; emptyPoint = new Point(GAME_BOARD_SIZE - 1, GAME_BOARD_SIZE - 1); currentMatrix = matrixGenerator.GenerateMatrix(); MatrixEmptyCellRandomizator matrixRandomizator = new MatrixEmptyCellRandomizator(); emptyPoint = matrixRandomizator.Randomize(currentMatrix); PrintWelcome(); matrixRenderer.Render(currentMatrix); break; case "top": pe4at(); matrixRenderer.Render(currentMatrix); break; default: int number = 0; bool isNumber = int.TryParse(inputString, out number); if (!isNumber) { Console.WriteLine("Invalid comand!"); break; } if (number < 16 && number > 0) { Point newPoint = new Point(0, 0); for (int i = 0; i < 4; i++) { newPoint.Row = emptyPoint.Row + Directions.GetDirection(i).Row; newPoint.Col = emptyPoint.Col + Directions.GetDirection(i).Col; if (OutOfMatrixChecker.CheckIfOutOfMatrix(newPoint, matrixLength)) { if (i == 3) { Console.WriteLine("Invalid move"); } continue; } if (currentMatrix[newPoint.Row, newPoint.Col] == number) { EmptyCellMover.MoveEmptyCell(emptyPoint, new Point(newPoint.Row, newPoint.Col), currentMatrix); moves++; matrixRenderer.Render(currentMatrix); break; } if (i == 3) { Console.WriteLine("Invalid move"); } } } else { Console.WriteLine("Invalid move"); break; } break; } }
public void Start() { MatrixGenerator matrixGenerator = new MatrixGenerator(GAME_BOARD_SIZE); IMatrixRenderer matrixRenderer = new MatrixRenderer(); currentMatrix = matrixGenerator.GenerateMatrix(); MatrixEmptyCellRandomizator matrixRandomizator = new MatrixEmptyCellRandomizator(); emptyPoint = matrixRandomizator.Randomize(currentMatrix); int matrixLength = currentMatrix.GetLength(0); IEqualMatrixChecker equalMatrixChecker = new EqualMatrixChecker(matrixLength, new MatrixGenerator(matrixLength)); //GenerateMatrix(); PrintWelcome(); matrixRenderer.Render(currentMatrix); // main algorithm int moves = 0; Console.Write("Enter a number to move: "); string inputString = Console.ReadLine(); while (inputString.CompareTo("exit") != 0) { ExecuteComand(inputString, ref moves); if (equalMatrixChecker.CheckMatrix(currentMatrix)) { GameWon(moves); pe4at(); emptyPoint = new Point(GAME_BOARD_SIZE - 1, GAME_BOARD_SIZE - 1); currentMatrix = matrixGenerator.GenerateMatrix(); emptyPoint = matrixRandomizator.Randomize(currentMatrix); PrintWelcome(); matrixRenderer.Render(currentMatrix); moves = 0; } Console.Write("Enter a number to move: "); inputString = Console.ReadLine(); } Console.WriteLine("Good bye!"); }