public bool PawnMove(ChessBoard board, Coords start, Coords end) { int direction; if (board.WhiteTurn()) { direction = -1; } else { direction = 1; } if (!this.getMoved() &&//first move 2 steps start.getX() == end.getX() && start.getY() + (2 * direction) == end.getY() && board.GetSoldierByPosition(new Coords(start.getY() + (2 * direction), end.getX())).getType() == " ") { firstMoveTwoSteps = true; return(true); } if (start.getX() == end.getX() &&//move 1 step start.getY() + direction == end.getY() && board.GetSoldierByPosition(new Coords(start.getY() + direction, end.getX())).getType() == " ") { return(true); } if ((start.getX() + 1 == end.getX() || start.getX() - 1 == end.getX()) &&//eating start.getY() + direction == end.getY() && board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor() && board.GetSoldierByPosition(end).getColor() != " ") { return(canIeat(board, start, end)); } if (board.validEnPassant(start, end))//en passant { return(true); } return(false); }
public void run() { bool valid = false; ChessBoard c = new ChessBoard(); c.initSoldiers(); // !c.checkMate(c.FindKing()) c.PrintBoard(); while (!valid) { Console.WriteLine("Please enter start axis Y, start axis X, end axis Y, end axis X"); Console.WriteLine("{0}", (c.WhiteTurn() ? "White its your turn:" : "Black its your turn:")); string input = Console.ReadLine().Trim(' '); if (input.Length == 4) { Coords start = new Coords(c.ConvertY(input[0]), c.ConvertX(input[1])); Coords end = new Coords(c.ConvertY(input[2]), c.ConvertX(input[3])); bool validTurn = false; if ((c.WhiteTurn() && c.GetSoldierByPosition(start).getColor() == "W")) { validTurn = true; } if (!(c.WhiteTurn()) && c.GetSoldierByPosition(start).getColor() == "B") { validTurn = true; } if (c.ValidInput(start, end) && validTurn && c.PossibleToCancelCheck(start, end)) { if (c.validCastling(start, end) && c.GetSoldierByPosition(start).validMove(c, start, end)) { c.doCastlingMove(start, end); c.nextTurn(); } else if (c.GetSoldierByPosition(start).validMove(c, start, end)) { if (c.GetSoldierByPosition(start).getType() == "P" || (c.GetSoldierByPosition(start).getColor() != c.GetSoldierByPosition(end).getColor() && c.GetSoldierByPosition(end).getColor() != " ") || c.inCheck(c.FindKing())) { c.setDrawCounter(0); } if (c.validPawnPromotion(start, end)) { c.doPromotion(start, end); c.nextTurn(); } else if (c.validEnPassant(start, end)) { c.doEnPassant(start, end); c.nextTurn(); } else { c.basicMove(start, end); c.nextTurn(); } } } } if (c.isDraw(c.FindKing())) { Console.WriteLine("Draw"); valid = true; break; } if (c.checkMate(c.FindKing())) { Console.WriteLine((!(c.WhiteTurn()) ? "White won" : "Black won")); valid = true; } } }