public void Run() { MatrixCoordinates kingCoordinates = new MatrixCoordinates(KingInitialRow, KingInitialColumn); King king = new King(kingCoordinates); MatrixCoordinates pawnACoordinates = new MatrixCoordinates(PawnAInitialRow, PawnAInitialColumn); Pawn pawnA = new Pawn('A', pawnACoordinates); MatrixCoordinates pawnBCoordinates = new MatrixCoordinates(PawnBInitialRow, PawnBInitialColumn); Pawn pawnB = new Pawn('B', pawnBCoordinates); MatrixCoordinates pawnCCoordinates = new MatrixCoordinates(PawnCInitialRow, PawnCInitialColumn); Pawn pawnC = new Pawn('C', pawnCCoordinates); MatrixCoordinates pawnDCoordinates = new MatrixCoordinates(PawnDInitialRow, PawnDInitialColumn); Pawn pawnD = new Pawn('D', pawnDCoordinates); bool endOfGame = false; int currentMove = 1; do { bool isValidMove = false; do { try { Console.Clear(); Console.WriteLine(this.board.GetImage(king, pawnA, pawnB, pawnC, pawnD)); isValidMove = this.IsValidMove(currentMove, king, pawnA, pawnB, pawnC, pawnD); } catch (KingSurvivalException kse) { Console.WriteLine(kse.Message); Console.ReadLine(); } } while (!isValidMove); endOfGame = this.HasGameEnded(currentMove, king, pawnA, pawnB, pawnC, pawnD); this.isKingWinner = this.HasKingWon(currentMove, endOfGame, king, pawnA, pawnB, pawnC, pawnD); currentMove++; } while (!endOfGame); if (endOfGame) { this.DisplayCurrentEndOnConsole(currentMove, king, pawnA, pawnB, pawnC, pawnD); } }
private bool HasGameEnded(int gameTurn, King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { bool isKingOnTurn = false; isKingOnTurn = (gameTurn % 2 == 1); if (isKingOnTurn && king.Coordinates.Row == 0) { return true; } else { if (!this.CanKingMove(king, pawnA, pawnB, pawnC, pawnD) || !this.CanAtLeastOnePawnMove(king, pawnA, pawnB, pawnC, pawnD)) { return true; } else { return false; } } }
private bool CanAtLeastOnePawnMove(King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { bool canPawnAMove = this.CanCurrentPawnMove( pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates, king.Coordinates); bool canPawnBMove = this.CanCurrentPawnMove( pawnB.Coordinates, pawnA.Coordinates, pawnC.Coordinates, pawnD.Coordinates, king.Coordinates); bool canPawnCMove = this.CanCurrentPawnMove( pawnC.Coordinates, pawnA.Coordinates, pawnB.Coordinates, pawnD.Coordinates, king.Coordinates); bool canPawnDMove = this.CanCurrentPawnMove( pawnD.Coordinates, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, king.Coordinates); bool canAtLeastOnePawnMove = canPawnAMove || canPawnBMove || canPawnCMove || canPawnDMove; return canAtLeastOnePawnMove; }
private bool IsValidPawnMove(string command, King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { switch (command) { case "adl": return this.HandleDownLeftPawnMove( pawnA, king.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "adr": return this.HandleDownRightPawnMove( pawnA, king.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "bdl": return this.HandleDownLeftPawnMove( pawnB, king.Coordinates, pawnA.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "bdr": return this.HandleDownRightPawnMove( pawnB, king.Coordinates, pawnA.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "cdl": return this.HandleDownLeftPawnMove( pawnC, king.Coordinates, pawnA.Coordinates, pawnB.Coordinates, pawnD.Coordinates); case "cdr": return this.HandleDownRightPawnMove( pawnC, king.Coordinates, pawnA.Coordinates, pawnB.Coordinates, pawnD.Coordinates); case "ddl": return this.HandleDownLeftPawnMove( pawnD, king.Coordinates, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates); case "ddr": return this.HandleDownRightPawnMove( pawnD, king.Coordinates, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates); default: throw new KingSurvivalException("Invalid move!"); } }
private bool IsValidMove(int turn, King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { bool isValid; string command; if (turn % 2 == 1) { Console.Write("King's turn: "); command = Console.ReadLine().ToLower(); isValid = this.IsValidKingMove(command, king, pawnA, pawnB, pawnC, pawnD); } else { Console.Write("Pawn's turn: "); command = Console.ReadLine().ToLower(); isValid = this.IsValidPawnMove(command, king, pawnA, pawnB, pawnC, pawnD); } return isValid; }
private bool IsValidKingMove(string command, King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { switch (command) { case "kul": return this.HandleUpLeftKingMove( king, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "kur": return this.HandleUpRightKingMove( king, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "kdl": return this.HandleDownLeftPawnMove( king, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); case "kdr": return this.HandleDownRightPawnMove( king, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); default: { throw new KingSurvivalException("Invalid move!"); } } }
private bool IsKingUpRightMovementAvailable(King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { if (king.Coordinates.Row == 0 || king.Coordinates.Column == BoardMaxColumn) { return false; } MatrixCoordinates newKingCoordinates = king.Coordinates + UpRightDirection; bool canKingGoUpRight = this.IsAvailableNextPosition( newKingCoordinates, pawnA.Coordinates, pawnB.Coordinates, pawnC.Coordinates, pawnD.Coordinates); return canKingGoUpRight; }
private bool HasKingWon(int gameTurn, bool gameCondition, King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { bool isGameEnded = gameCondition; bool isKingOnTurn = (gameTurn % 2 == 1); if (isGameEnded) { if (isKingOnTurn && king.Coordinates.Row == 0) { return true; } else { if (!this.CanKingMove(king, pawnA, pawnB, pawnC, pawnD)) { return false; } else if (!this.CanAtLeastOnePawnMove(king, pawnA, pawnB, pawnC, pawnD)) { return true; } else { return false; } } } else { return false; } }
private bool HandleUpRightKingMove(King king, params MatrixCoordinates[] otherPawnsCoordinates) { MatrixCoordinates newCoordinates = king.Coordinates + UpRightDirection; if (king.Coordinates.Row > 0 && king.Coordinates.Column < BoardMaxColumn && this.IsAvailableNextPosition(newCoordinates, otherPawnsCoordinates)) { king.Coordinates = newCoordinates; return true; } else { throw new KingSurvivalException("Invalid move!"); } }
private void DisplayCurrentEndOnConsole(int turn, King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { if (this.isKingWinner) { Console.Clear(); Console.WriteLine(this.board.GetImage(king, pawnA, pawnB, pawnC, pawnD)); Console.WriteLine("King wins in {0} turns!", turn / 2); } else { Console.Clear(); Console.WriteLine(this.board.GetImage(king, pawnA, pawnB, pawnC, pawnD)); Console.WriteLine("King loses in {0} turns...", turn / 2); } }
private bool CanKingMove(King king, Pawn pawnA, Pawn pawnB, Pawn pawnC, Pawn pawnD) { bool canKingGoUpLeft = this.IsKingUpLeftMovementAvailable(king, pawnA, pawnB, pawnC, pawnD); bool canKingGoDownLeft = this.IsKingDownLeftMovementAvailable(king, pawnA, pawnB, pawnC, pawnD); bool canKingGoUpRight = this.IsKingUpRightMovementAvailable(king, pawnA, pawnB, pawnC, pawnD); bool canKingGoDownRight = this.IsKingDownRightMovementAvailable(king, pawnA, pawnB, pawnC, pawnD); bool isAnyOfKingMovesAvaiable = canKingGoDownRight || canKingGoDownLeft || canKingGoUpLeft || canKingGoUpRight; return isAnyOfKingMovesAvaiable; }