/* Chessboard[0,0,0] = King */ //this is actually the moveTo facility /// <summary> /// A helper method 2 the moveTo() method,this where the actual moving happens . /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="z"></param> /// <returns></returns> public AbstractPiece this[int x, int y, int z] { get { return(matrix[x, y, z]); } set { if (value == null) { matrix[x, y, z] = null; return; } if (((AbstractPiece)value).name == ChessNames.King) { int kingX = ((AbstractPiece)value).position.x; int kingY = ((AbstractPiece)value).position.y; int kingZ = ((AbstractPiece)value).position.z; if ((kingX - x == -2) && (kingX == 3 || kingX == 4)) { AbstractPiece rook = matrix[7, kingY, kingZ]; matrix[7, kingY, kingZ] = null; rook.position.x = kingX + 1; matrix[kingX + 1, kingY, kingZ] = rook; } if (kingX - x == 2 && (kingX == 3 || kingX == 4)) { AbstractPiece rook = matrix[0, kingY, kingZ]; matrix[0, kingY, kingZ] = null; rook.position.x = kingX - 1; matrix[kingX - 1, kingY, kingZ] = rook; } } if (((AbstractPiece)value).name == ChessNames.Pawn) { int oldPosX = ((AbstractPiece)value).position.x; int oldPosY = ((AbstractPiece)value).position.y; int oldPosZ = ((AbstractPiece)value).position.z; //handling first move (two blocks) if ((Math.Abs(oldPosY - y) == 2) || (Math.Abs(oldPosZ - z) == 2)) { //Do internal swap AbstractPiece temp1 = (AbstractPiece)value; //original position matrix[temp1.position.x, temp1.position.y, temp1.position.z] = null; //new position ((Pawn)temp1).hasMovedTwoBlocks = true; temp1.position.x = x; temp1.position.y = y; temp1.position.z = z; matrix[x, y, z] = value; return; } else { ((Pawn)((AbstractPiece)value)).hasMovedTwoBlocks = false; } //handle unpassant. here only we will handle capturing the piece //I'm not sure, but it looks something like this not the following: //if((Math.Abs(x-oldPosX) != 0) || (Math.Abs(z-oldPosZ) != 0)) //it has captured something //this is correct probably if (Math.Abs(x - oldPosX) != 0) { if (Chessboard.getReference()[x, y, z] == null) {//check if it has captured by unpassant move if ((value.player is Player2) && (z - oldPosZ < 0)) { Chessboard.getReference()[x, y, z + 1].IsCaptured = true; Chessboard.getReference()[x, y, z + 1] = null; } else { if ((value.player is Player1) && (z - oldPosZ > 0)) { Chessboard.getReference()[x, y, z - 1].IsCaptured = true; Chessboard.getReference()[x, y, z - 1] = null; } } } } } AbstractPiece temp = (AbstractPiece)value; //original position matrix[temp.position.x, temp.position.y, temp.position.z] = null; //new position temp.position.x = x; temp.position.y = y; temp.position.z = z; matrix[x, y, z] = value; } }
public override List <Position> getAllMoves() { int xIncrement = 1; int yIncrement = 1; int zIncrement = 1; moves.Clear(); Position piecePos = piece.position; if (piece.player is Player2) { xIncrement = -1; yIncrement = -1; zIncrement = -1; } Position possibleMove; //y increment possibleMove = new Position(piece.position.x, piece.position.y + yIncrement, piece.position.z); handleMove(possibleMove); //z increment possibleMove = new Position(piece.position.x, piece.position.y, piece.position.z + zIncrement); handleMove(possibleMove); //two steps forward on y if ((!piece.hasMoved) && (Chessboard.getReference()[piece.position.x, piece.position.y + yIncrement, piece.position.z] == null)) { possibleMove = new Position(piece.position.x, piece.position.y + 2 * yIncrement, piece.position.z); handleMove(possibleMove); } //two steps forward on z if ((!piece.hasMoved) && (Chessboard.getReference()[piece.position.x, piece.position.y, piece.position.z + zIncrement] == null)) { possibleMove = new Position(piece.position.x, piece.position.y, piece.position.z + 2 * zIncrement); handleMove(possibleMove); } //opponent moves possibleMove = new Position(piece.position.x + xIncrement, piece.position.y, piece.position.z + yIncrement); handleMoveOpponent(possibleMove); possibleMove = new Position(piece.position.x - xIncrement, piece.position.y, piece.position.z + yIncrement); handleMoveOpponent(possibleMove); possibleMove = new Position(piece.position.x, piece.position.y + yIncrement, piece.position.z + zIncrement); handleMoveOpponent(possibleMove); //En Passant moves حيوووووووووتي إت possibleMove = new Position(piece.position.x + xIncrement, piece.position.y, piece.position.z + yIncrement); handleMoveEnPassant(possibleMove, EnPassantedPosition.Right); possibleMove = new Position(piece.position.x - xIncrement, piece.position.y, piece.position.z + yIncrement); handleMoveEnPassant(possibleMove, EnPassantedPosition.Left); possibleMove = new Position(piece.position.x, piece.position.y + yIncrement, piece.position.z + zIncrement); handleMoveEnPassant(possibleMove, EnPassantedPosition.Up); possibleMove = new Position(piece.position.x, piece.position.y + yIncrement, piece.position.z - zIncrement); handleMoveEnPassant(possibleMove, EnPassantedPosition.Down); return(moves); // مع السلومة يا حمومة }
public void undo() { if (currentPhase < 0) { return; } //reverse camera and turn GameManager.getReference(null).toggleTurn(true); HistoryPhase currHP = history[currentPhase]; if (currHP.newPiece == null) //we've moved to a null position { //check castling if (currHP.oldPiece.GetType() == typeof(King)) { if (Math.Abs(currHP.oldPos.x - currHP.newPos.x) > 1) //i.e. castling happened { //just restore the Rook as well //we have to know where the rook is if (currHP.newPos.x < currHP.oldPos.x) //rook is left { AbstractPiece curRook = vchessboard.ModelAt(currHP.newPos.x + 1, currHP.newPos.y, currHP.newPos.z).LogicalPieceRef; Chessboard.getReference()[0, currHP.newPos.y, currHP.newPos.z] = curRook; Console.WriteLine(curRook); } else { AbstractPiece curRook = vchessboard.ModelAt(currHP.newPos.x - 1, currHP.newPos.y, currHP.newPos.z).LogicalPieceRef; Chessboard.getReference()[7, currHP.newPos.y, currHP.newPos.z] = curRook; } } } //check for unpassant move if (currHP is PawnHistoryPhase) { //upassant = changing direction on X axis if (Math.Abs(currHP.oldPos.x - currHP.newPos.x) == 1) { //Just restore the captured pawn, the rest of the code //restores this pawn AbstractPiece capturedPawn = vchessboard.LogicalModelAt(currHP.newPos.x, currHP.newPos.y, currHP.oldPos.z); Chessboard.getReference()[currHP.newPos.x, currHP.newPos.y, currHP.oldPos.z] = capturedPawn; capturedPawn.IsCaptured = false; ((Pawn)capturedPawn).hasMovedTwoBlocks = true; } } Chessboard.getReference()[currHP.oldPos.x, currHP.oldPos.y, currHP.oldPos.z] = currHP.oldPiece; currHP.oldPiece.position = currHP.oldPos; if (currHP is PawnHistoryPhase) { //restore previous state (just to check if it has moved two steps forward ((Pawn)currHP.oldPiece).hasMovedTwoBlocks = ((PawnHistoryPhase)currHP).hasMovesTwoBlocks; } currHP.oldPiece.hasMoved = currHP.oldHasMoved; currentPhase--; } else //we've captured something { Chessboard.getReference()[currHP.oldPos.x, currHP.oldPos.y, currHP.oldPos.z] = currHP.oldPiece; currHP.oldPiece.position = currHP.oldPos; currHP.oldPiece.isSelected = false; Chessboard.getReference()[currHP.newPos.x, currHP.newPos.y, currHP.newPos.z] = currHP.newPiece; currHP.newPiece.IsCaptured = false; //for rendering currHP.newPiece.isSelected = false; currentPhase--; } PanelHistory.getReference().addMessage("Undo: " + GameManager.getReference(null).curPlayer().ToString() + " moved " + currHP.oldPiece.ToString() + " back from " + currHP.oldPos.ToString() + " to " + currHP.newPos.ToString(), GameManager.getReference(null).isPlayer1Turn()); }
private bool castlingIsOk(CastleAllignment castleAllign) { int rookX = ((castleAllign == CastleAllignment.Left) ? 0 : 7); AbstractPiece temp = Chessboard.getReference()[rookX, piece.position.y, piece.position.z]; if ( (temp != null) && (temp is Rook) && (!temp.hasMoved) && (!piece.hasMoved) && (!((King)piece).isThreatend) ) { //check to see if the path is clear int kingX = piece.position.x; //check if there are any piece along the way //compare kingX with rookX to choose what pieces to check int xIncrement = (kingX > rookX ? -1 : 1); List <Position> freePieces = new List <Position>(); freePieces.Add(new Position(kingX + xIncrement, piece.position.y, piece.position.z)); freePieces.Add(new Position(kingX + (2 * xIncrement), piece.position.y, piece.position.z)); if (xIncrement == -1) { if (Chessboard.getReference()[kingX + (3 * xIncrement), piece.position.y, piece.position.z] != null) { return(false); } freePieces.Add(new Position(kingX + (3 * xIncrement), piece.position.y, piece.position.z)); } //get enemy pieces List <AbstractPiece> enemyPieces = Chessboard.getReference().getNotCapturedEnemies (Chessboard.getReference()[piece.position.x, piece.position.y, piece.position.z].player); foreach (Position freePiece in freePieces) { if (Chessboard.getReference()[freePiece.x, freePiece.y, freePiece.z] != null) { return(false); } foreach (AbstractPiece enemyPiece in enemyPieces) { List <Position> enemyAvailableMoves = enemyPiece.getAvailableMoves(); foreach (Position enemyAvailableMove in enemyAvailableMoves) { if (enemyAvailableMove.x == freePiece.x && enemyAvailableMove.y == freePiece.y && enemyAvailableMove.z == freePiece.z) { return(false); } } } } ; return(true); } return(false); }