//The bishop moves to any square (except as limited by Article 4.2) on the diagonals on which it stands. override public MoveTypesE canMove(Point p) { MoveTypesE mt = base.canMove(p); if (mt == MoveTypesE.ILLEGAL) { return(MoveTypesE.ILLEGAL); } int dy = p.getY() - loc.getY(); int dx = p.getX() - loc.getX(); if (System.Math.Abs(dy) == System.Math.Abs(dx)) { int signFactorX = (dx * System.Math.Abs(dx) > 0) ? 1 : -1; int signFactorY = (dy * System.Math.Abs(dy) > 0) ? 1 : -1; for (int i = 1; i < System.Math.Abs(dx); i++) { if (gameBoard.pieceAt(loc.getX() + signFactorX * i, loc.getY() + signFactorY * i) != null) { return(MoveTypesE.ILLEGAL); } } } return(mt); }
/// <summary> /// Similar tryToMove as the default, but if castling, moves the correct king to the correct position as well. /// </summary> /// <param name="p"></param> public override void tryToMove(Point p) { MoveTypesE mt = canMove(p); if (mt != MoveTypesE.ILLEGAL) { if (mt == MoveTypesE.CASTLE) { Point tmploc = loc; if (loc.getY() > p.getY()) { gameBoard.Move(new Point(p.getX(), 0), new Point(p.getX(), p.getY() + 1)); } else { gameBoard.Move(new Point(p.getX(), 7), new Point(p.getX(), p.getY() - 1)); } gameBoard.Move(loc, p); gameBoard.switchTurn(); } else { gameBoard.Move(loc, p); } hasMoved = true; } }
//Same basic functionality as normal move but keeps track of setting enpassant if the pawn doublesteps and promotes the pawn if it reaches the end public override void tryToMove(Point p) { MoveTypesE mt = canMove(p); if (mt != MoveTypesE.ILLEGAL) { if (mt == MoveTypesE.DOUBLESTEP) { gameBoard.Move(loc, p, new Point((loc.getX() + p.getX()) / 2, (loc.getY() + p.getY()) / 2)); } else { if (mt == MoveTypesE.ENPASSANT) { gameBoard.killEnPassant(new Point(loc.getX(), p.getY())); } if (mt == MoveTypesE.PROMOTE) { gameBoard.promotePawn(loc); } gameBoard.Move(loc, p); } hasMoved = true; } }
//The default tryToMove function, and only should be overwritten if piece has special rules // Pawn: ENPASSANT, DOUBLESTEP, PROMOTE // King: CASTLE // Uses canMove to deterine if move is ILLEGAL, and if not, makes the move public virtual void tryToMove(Point p) { MoveTypesE mt = canMove(p); if (mt != MoveTypesE.ILLEGAL) { gameBoard.Move(loc, p); hasMoved = true; } }
/// <summary> /// The knight's move is composed of two different steps; first, it makes one step of one single square along its rank or file, and then, /// still moving away from the square of departure, one step of one single square on a diagonal. It does not matter if the square of the /// first step is occupied. /// </summary> /// <param name="p">The point the knight is trying to move to</param> /// <returns>The move type for the piece trying to make that move</returns> override public MoveTypesE canMove(Point p) { MoveTypesE mt = base.canMove(p); if (mt == MoveTypesE.ILLEGAL) { return(MoveTypesE.ILLEGAL); } int dy = p.getY() - loc.getY(); int dx = p.getX() - loc.getX(); if ((System.Math.Abs(dy) == 2 && System.Math.Abs(dx) == 1) || (System.Math.Abs(dy) == 1 && System.Math.Abs(dx) == 2)) { return(mt); } return(MoveTypesE.ILLEGAL); }
/// <summary> /// (a) Except when castling, the king moves to any adjoining square that is not attacked by an opponent's piece. /// (b) Castling is a move of the king and either rook, counting as a single move of the king and executed as follows: the king is transferred from /// its original square two squares toward either rook on the same rank; then that rook is transferred over the king to the square the king has just crossed. /// (e) Castling is [permanently] illegal: /// (i) if the king has already been moved; or /// (ii) with a rook that has already been moved. /// (f) Castling is prevented for the time being: /// (i) if the king's original square, or the square which the king must pass over, or that which it is to occupy, is attacked by an opponent's piece; or /// (ii) if there is any piece between the king and the rook with which castling is to be effected[i.e.castling may still be legal even if the rook is attacked or, /// when castling queenside, passes over an attacked square] . /// </summary> /// <param name="p">The point the pawn is trying to move to</param> /// <returns>The move type for the piece trying to make that move</returns> override public MoveTypesE canMove(Point p) { MoveTypesE mt = base.canMove(p); if (mt == MoveTypesE.ILLEGAL) { return(MoveTypesE.ILLEGAL); } int dy = p.getY() - loc.getY(); int dx = p.getX() - loc.getX(); if ((System.Math.Abs(dx) <= 1 && System.Math.Abs(dy) <= 1)) { return(mt); } if (!hasMoved && System.Math.Abs(dy) == 2 && dx == 0) { //Debug.Log("Seeing if can castle"); if (gameBoard.inCheck(loc)) { return(MoveTypesE.ILLEGAL); } if (dy > 0) { for (int i = loc.getY() + 1; i < 7; i++) { if (gameBoard.pieceAt(loc.getX(), i) != null) { //Debug.Log("Piece in the way"); return(MoveTypesE.ILLEGAL); } } GameObject rookMaybe = gameBoard.pieceAt(loc.getX(), 7); if (rookMaybe == null || ((Piece)rookMaybe.GetComponent("Piece")).getHasMoved()) { //Debug.Log("Rook Has Moved"); return(MoveTypesE.ILLEGAL); } if (gameBoard.inCheck(gameObject, rookMaybe, loc.getX(), loc.getY() + 1) || gameBoard.inCheck(gameObject, rookMaybe, loc.getX(), loc.getY() + 2)) { //Debug.Log("In Check On Way"); return(MoveTypesE.ILLEGAL); } } else { for (int i = loc.getY() - 1; i > 0; i--) { if (gameBoard.pieceAt(loc.getX(), i) != null) { return(MoveTypesE.ILLEGAL); } } GameObject rookMaybe = gameBoard.pieceAt(loc.getX(), 0); if (rookMaybe == null || ((Piece)rookMaybe.GetComponent("Piece")).getHasMoved()) { // Debug.Log("Rook Has Moved"); return(MoveTypesE.ILLEGAL); } if (gameBoard.inCheck(gameObject, rookMaybe, loc.getX(), loc.getY() - 1) || gameBoard.inCheck(gameObject, rookMaybe, loc.getX(), loc.getY() - 2)) { // Debug.Log("In Check On Way"); return(MoveTypesE.ILLEGAL); } } return(MoveTypesE.CASTLE); } return(MoveTypesE.ILLEGAL); }