public void redo() { if (currentPhase == history.Count - 1) { return; } //reverse camera and turn GameManager.getReference(null).toggleTurn(true); currentPhase++; HistoryPhase currHP = history[currentPhase]; if (currHP.newPiece == null) //we've moved to a null position { Chessboard.getReference()[currHP.newPos.x, currHP.newPos.y, currHP.newPos.z] = currHP.oldPiece; currHP.oldPiece.position = currHP.newPos; } else //we've captured something { //check castling and enpassant later Chessboard.getReference()[currHP.newPos.x, currHP.newPos.y, currHP.newPos.z] = currHP.oldPiece; currHP.oldPiece.position = currHP.newPos; currHP.oldPiece.isSelected = false; currHP.newPiece.IsCaptured = true; //for rendering currHP.newPiece.isSelected = false; } PanelHistory.getReference().addMessage("Redo: " + GameManager.getReference(null).curPlayer().ToString() + " moved " + currHP.oldPiece.ToString() + " again from " + currHP.oldPos.ToString() + " to " + currHP.newPos.ToString(), GameManager.getReference(null).isPlayer1Turn()); }
/// <summary> /// Moves the current AbstractPiece to the new passed Position . /// </summary> /// <param name="newPosition"></param> public void moveTo(Position newPosition) { HistoryPhase hp; if (this is Pawn) { hp = new PawnHistoryPhase( (Pawn)this, this.position, Chessboard.getReference()[newPosition.x, newPosition.y, newPosition.z], newPosition ); } else { hp = new HistoryPhase( this, this.position, Chessboard.getReference()[newPosition.x, newPosition.y, newPosition.z], newPosition ); } History.getReference().pushPhase(hp); this.hasMoved = true; Chessboard.getReference()[newPosition.x, newPosition.y, newPosition.z] = this; }
private void handleMoveEnPassant(Position newMovePos, EnPassantedPosition opponentPos) { int xIncrement = 1; //int yIncrement = 1; int zIncrement = 1; if (piece.player is Player2) { xIncrement = -1; //yIncrement = -1; zIncrement = -1; } if (positionIsValid(newMovePos)) { /*important: handle changing coords according to the player*/ AbstractPiece opponent = null; switch (opponentPos) { case EnPassantedPosition.Left: opponent = Chessboard.getReference()[piece.position.x - xIncrement, piece.position.y, piece.position.z]; break; case EnPassantedPosition.Right: opponent = Chessboard.getReference()[piece.position.x + xIncrement, piece.position.y, piece.position.z]; break; case EnPassantedPosition.Up: opponent = Chessboard.getReference()[piece.position.x, piece.position.y + zIncrement, piece.position.z]; break; case EnPassantedPosition.Down: opponent = Chessboard.getReference()[piece.position.x, piece.position.y - zIncrement, piece.position.z]; break; } if (opponent == null) { return; } if ((opponent.player == piece.player) || (opponent.name != ChessNames.Pawn)) { return; } if (!((Pawn)opponent).hasMovedTwoBlocks) { return; } if (!History.getReference().peakPhase().oldPiece.Equals(opponent)) { return; } //Stack operations to check if it's the last piece moved!! //ATTENTION! //It's all ok! add it to available moves //opponent.isCapturable = true; //IMPORTANT FOR A.I. opponent.isCapturable = true; moves.Add(newMovePos); } }
new private void handleMove(Position newMovePos) { if (positionIsValid(newMovePos)) { if (Chessboard.getReference()[newMovePos.x, newMovePos.y, newMovePos.z] == null) //check if position is empty { moves.Add(newMovePos); } } }
/// <summary> /// Returns a list of Positions of the opponent's pieces that can capture the passed Position . /// </summary> /// <param name="pos"></param> /// <param name="player"></param> /// <returns></returns> public List <Position> getThreatningPiece(Position pos, AbstractPlayer player) { //get all enemy active pieces List <AbstractPiece> enemyPieces = Chessboard.getReference().getNotCapturedEnemies(player); //Now for each enemy piece, check if it's threatning us, if yes, add it to the list List <Position> threatningEnemiesPos = new List <Position>(); foreach (AbstractPiece enemyPiece in enemyPieces) { if (enemyPiece is Pawn) { if (GameManager.getReference(null).curPlayer() is Player2) //pawns up and increasing z { if ((enemyPiece.position.z + 1 == pos.z) && (Math.Abs(enemyPiece.position.x - pos.x) == 1) && (pos.y == enemyPiece.position.y)) { threatningEnemiesPos.Add(enemyPiece.position); } if ((pos.x == enemyPiece.position.x) && (pos.y - enemyPiece.position.y == 1) && (pos.z - enemyPiece.position.z == 1)) { threatningEnemiesPos.Add(enemyPiece.position); } } else //pawns down and decreasing z { if ((enemyPiece.position.z - pos.z == 1) && (enemyPiece.position.x + 1 == pos.x || enemyPiece.position.x - 1 == pos.x) && (pos.y == enemyPiece.position.y)) { threatningEnemiesPos.Add(enemyPiece.position); } if ((pos.x == enemyPiece.position.x) && (enemyPiece.position.y - pos.y == 1) && (enemyPiece.position.z - pos.z == 1)) { threatningEnemiesPos.Add(enemyPiece.position); } } } else { List <Position> enemyPositions = enemyPiece.getAvailableMoves(); foreach (Position availablePosition in enemyPositions) { if (availablePosition == pos) { threatningEnemiesPos.Add(enemyPiece.position); break; } } } } return(threatningEnemiesPos); }
private void handleMoveOpponent(Position newMovePos) { if (positionIsValid(newMovePos)) { if (Chessboard.getReference()[newMovePos.x, newMovePos.y, newMovePos.z] != null) { if (piece.player != Chessboard.getReference()[newMovePos.x, newMovePos.y, newMovePos.z].player) //the piece is enemy { moves.Add(newMovePos); } } } }
/// <summary> /// adds the passed position to the List of moves only if the position is valid and has no piece of the same player . /// </summary> /// <param name="newMovePos"></param> protected void handleMove(Position newMovePos) { if (positionIsValid(newMovePos)) { if (Chessboard.getReference()[newMovePos.x, newMovePos.y, newMovePos.z] == null) //check if position is empty { moves.Add(newMovePos); } else { if (piece.player != Chessboard.getReference()[newMovePos.x, newMovePos.y, newMovePos.z].player) //the piece is enemy { moves.Add(newMovePos); } } } }
public override List <Position> getAllMoves() { moves.Clear(); Position piecePos = piece.position; int reflex = 1; for (int xIncrement = piecePos.x + 1; ; xIncrement += reflex) { if (xIncrement < 0) { break; } if (xIncrement > 7) { xIncrement = piecePos.x; reflex = -1; continue; } Position possibleMove = new Position(xIncrement, piecePos.y, piecePos.z); if (Chessboard.getReference()[xIncrement, piecePos.y, piecePos.z] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[xIncrement, piecePos.y, piecePos.z].player) //the piece is enemy { moves.Add(possibleMove); } if (reflex == -1) //break this path anyway { break; } else { xIncrement = piecePos.x; reflex = -1; continue; } } } reflex = 1; for (int yIncrement = piecePos.y + 1; ; yIncrement += reflex) { if (yIncrement < 0) { break; } if (yIncrement > 7) { yIncrement = piecePos.y; reflex = -1; continue; } Position possibleMove = new Position(piecePos.x, yIncrement, piecePos.z); if (Chessboard.getReference()[piecePos.x, yIncrement, piecePos.z] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[piecePos.x, yIncrement, piecePos.z].player) //the piece is enemy { moves.Add(possibleMove); } if (reflex == -1) //break this path anyway { break; } else { yIncrement = piecePos.y; reflex = -1; continue; } } } reflex = 1; for (int zIncrement = piecePos.z + 1; ; zIncrement += reflex) { if (zIncrement < 0) { break; } if (zIncrement > 7) { zIncrement = piecePos.z; reflex = -1; continue; } Position possibleMove = new Position(piecePos.x, piecePos.y, zIncrement); if (Chessboard.getReference()[piecePos.x, piecePos.y, zIncrement] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[piecePos.x, piecePos.y, zIncrement].player) //the piece is enemy { moves.Add(possibleMove); } if (reflex == -1) //break this path anyway { break; } else { zIncrement = piecePos.z; reflex = -1; continue; } } } return(moves); }
public override List <Position> getAllMoves() { moves.Clear(); Position piecePos = piece.position; int xreflex = 1; int yreflex = 1; for (int xIncrement = piecePos.x + 1, yIncrement = piecePos.y + 1; ; xIncrement += xreflex, yIncrement += yreflex) { if (xIncrement < 0 || yIncrement < 0) { break; } if (xIncrement > 7 || yIncrement > 7) { if (xreflex == -1) //break this path anyway if the path was visited twice { break; } xIncrement = piecePos.x; yIncrement = piecePos.y; xreflex = -1; yreflex = -1; continue; } Position possibleMove = new Position(xIncrement, yIncrement, piecePos.z); if (Chessboard.getReference()[xIncrement, yIncrement, piecePos.z] == null) { //before adding, check if this move leads to check mate! moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[xIncrement, yIncrement, piecePos.z].player) //the piece is enemy { moves.Add(possibleMove); } if (xreflex == -1) //break this path anyway if the path was visited twice { break; } else { xIncrement = piecePos.x; yIncrement = piecePos.y; xreflex = -1; yreflex = -1; continue; } } } xreflex = -1; yreflex = 1; for (int xIncrement = piecePos.x - 1, yIncrement = piecePos.y + 1; ; xIncrement += xreflex, yIncrement += yreflex) { if (xIncrement < 0 || yIncrement < 0) { if (xreflex == 1) //break this path anyway if the path was visited twice { break; } xIncrement = piecePos.x; yIncrement = piecePos.y; xreflex = 1; yreflex = -1; continue; } if (xIncrement > 7 || yIncrement > 7) { if (xreflex == 1) //break this path anyway if the path was visited twice { break; } xIncrement = piecePos.x; yIncrement = piecePos.y; xreflex = 1; yreflex = -1; continue; } Position possibleMove = new Position(xIncrement, yIncrement, piecePos.z); if (Chessboard.getReference()[xIncrement, yIncrement, piecePos.z] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[xIncrement, yIncrement, piecePos.z].player) //the piece is enemy { moves.Add(possibleMove); } if (xreflex == 1) //break this path anyway if the path was visited twice { break; } else { xIncrement = piecePos.x; yIncrement = piecePos.y; xreflex = 1; yreflex = -1; continue; } } } yreflex = 1; int zreflex = 1; for (int yIncrement = piecePos.y + 1, zIncrement = piecePos.z + 1; ; yIncrement += yreflex, zIncrement += zreflex) { if (yIncrement < 0 || zIncrement < 0) { break; } if (yIncrement > 7 || zIncrement > 7) { if (yreflex == -1) { break; } yIncrement = piecePos.y; zIncrement = piecePos.z; yreflex = -1; zreflex = -1; continue; } Position possibleMove = new Position(piecePos.x, yIncrement, zIncrement); if (Chessboard.getReference()[piecePos.x, yIncrement, zIncrement] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[piecePos.x, yIncrement, zIncrement].player) //the piece is enemy { moves.Add(possibleMove); } if (yreflex == -1) //break this path anyway if the path was visited twice { break; } else { yIncrement = piecePos.y; zIncrement = piecePos.z; yreflex = -1; zreflex = -1; continue; } } } yreflex = 1; zreflex = -1; for (int yIncrement = piecePos.y + 1, zIncrement = piecePos.z - 1; ; yIncrement += yreflex, zIncrement += zreflex) { if (yIncrement < 0 || zIncrement < 0) { if (yreflex == -1) { break; } yIncrement = piecePos.y; zIncrement = piecePos.z; yreflex = -1; zreflex = 1; continue; } if (yIncrement > 7 || zIncrement > 7) { if (yreflex == -1) { break; } yIncrement = piecePos.y; zIncrement = piecePos.z; yreflex = -1; zreflex = 1; continue; } Position possibleMove = new Position(piecePos.x, yIncrement, zIncrement); if (Chessboard.getReference()[piecePos.x, yIncrement, zIncrement] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[piecePos.x, yIncrement, zIncrement].player) //the piece is enemy { moves.Add(possibleMove); } if (yreflex == -1) //break this path anyway if the path was visited twice { break; } else { yIncrement = piecePos.y; zIncrement = piecePos.z; yreflex = -1; zreflex = 1; continue; } } } //testing x-z allignment xreflex = 1; zreflex = 1; for (int xIncrement = piecePos.x + 1, zIncrement = piecePos.z + 1; ; xIncrement += xreflex, zIncrement += zreflex) { if (xIncrement < 0 || zIncrement < 0) { break; } if (xIncrement > 7 || zIncrement > 7) { if (xreflex == -1) { break; } xIncrement = piecePos.x; zIncrement = piecePos.z; xreflex = -1; zreflex = -1; continue; } Position possibleMove = new Position(xIncrement, piecePos.y, zIncrement); if (Chessboard.getReference()[xIncrement, piecePos.y, zIncrement] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[xIncrement, piecePos.y, zIncrement].player) //the piece is enemy { moves.Add(possibleMove); } if (xreflex == -1) //break this path anyway if the path was visited twice { break; } else { xIncrement = piecePos.x; zIncrement = piecePos.z; xreflex = -1; zreflex = -1; continue; } } } xreflex = -1; zreflex = 1; for (int xIncrement = piecePos.x - 1, zIncrement = piecePos.z + 1; ; xIncrement += xreflex, zIncrement += zreflex) { if (xIncrement < 0 || zIncrement < 0) { if (xreflex == 1) { break; } xIncrement = piecePos.x; zIncrement = piecePos.z; xreflex = 1; zreflex = -1; continue; } if (xIncrement > 7 || zIncrement > 7) { if (xreflex == 1) { break; } xIncrement = piecePos.x; zIncrement = piecePos.z; xreflex = 1; zreflex = -1; continue; } Position possibleMove = new Position(xIncrement, piecePos.y, zIncrement); if (Chessboard.getReference()[xIncrement, piecePos.y, zIncrement] == null) { moves.Add(possibleMove); } else { if (piece.player != Chessboard.getReference()[xIncrement, piecePos.y, zIncrement].player) //the piece is enemy { moves.Add(possibleMove); } if (xreflex == 1) //break this path anyway if the path was visited twice { break; } else { xIncrement = piecePos.x; zIncrement = piecePos.z; xreflex = 1; zreflex = -1; continue; } } } return(moves); }
// /// <summary> /// Returns a list of the positions of the threatning enemies /// </summary> /// <returns></returns> public List <Position> getPositionOfThreatningEnemies() { return(Chessboard.getReference().getThreatningPiece(this.position, this.player)); }
public static void fillChessboard() { Axis axisToDelpoy; Position pawn1 = positions[0]; Position rook1 = positions[1]; Position knight1 = positions[2]; Position pawn2 = positions[3]; Position rook2 = positions[4]; Position knight2 = positions[5]; //First check the different axis between the first rook and bishop if ((rook1.x ^ knight1.x) != 0) { axisToDelpoy = Axis.XAxis; } else if ((rook1.y ^ knight1.y) != 0) { axisToDelpoy = Axis.YAxis; } else { axisToDelpoy = Axis.ZAxis; } int[] increment = { 0 /*x*/, 0 /*y*/, 0 /*z*/ }; switch (axisToDelpoy) { case Axis.XAxis: { if (rook1.x == 7) { increment[0] = -1; } else { increment[0] = 1; } } break; case Axis.YAxis: { if (rook1.y == 7) { increment[1] = -1; } else { increment[1] = 1; } } break; case Axis.ZAxis: { if (rook1.z == 7) { increment[2] = -1; } else { increment[2] = 1; } } break; } //start with rook positions Chessboard.getReference()[rook1.x, rook1.y, rook1.z] = new Rook(rook1, GameManager.getReference(null).getPlayerOne()); int temp = rook1.y + (increment[1] * 7); Chessboard.getReference()[rook1.x + (increment[0] * 7), temp, rook1.z + (increment[2] * 7)] = new Rook(rook1.x + (increment[0] * 7), temp, rook1.z + (increment[2] * 7), GameManager.getReference(null).getPlayerOne()); Chessboard.getReference()[rook1.x + increment[0], rook1.y + increment[1], rook1.z + increment[2]] = new Knight(rook1.x + increment[0], rook1.y + increment[1], rook1.z + increment[2], GameManager.getReference(null).getPlayerOne()); Chessboard.getReference()[ rook1.x + (increment[0] * 7) - increment[0], rook1.y + (increment[1] * 7) - increment[1], rook1.z + (increment[2] * 7) - increment[2]] = new Knight( rook1.x + (increment[0] * 7) - increment[0], rook1.y + (increment[1] * 7) - increment[1], rook1.z + (increment[2] * 7) - increment[2], GameManager.getReference(null).getPlayerOne()); Chessboard.getReference()[ rook1.x + (2 * increment[0]), rook1.y + (2 * increment[1]), rook1.z + (2 * increment[2])] = new Bishop( rook1.x + (2 * increment[0]), rook1.y + (2 * increment[1]), rook1.z + (2 * increment[2]), GameManager.getReference(null).getPlayerOne() ); Chessboard.getReference()[ rook1.x + (increment[0] * 7) - (2 * increment[0]), rook1.y + (increment[1] * 7) - (2 * increment[1]), rook1.z + (increment[2] * 7) - (2 * increment[2])] = new Bishop( rook1.x + (increment[0] * 7) - (2 * increment[0]), rook1.y + (increment[1] * 7) - (2 * increment[1]), rook1.z + (increment[2] * 7) - (2 * increment[2]), GameManager.getReference(null).getPlayerOne() ); //Hint: Swapping the Queen and the King for the next team must occur Chessboard.getReference() [rook1.x + (3 * increment[0]), rook1.y + (3 * increment[1]), rook1.z + (3 * increment[2])] = new Queen( rook1.x + (3 * increment[0]), rook1.y + (3 * increment[1]), rook1.z + (3 * increment[2]), GameManager.getReference(null).getPlayerOne() ); Chessboard.getReference() [rook1.x + (4 * increment[0]), rook1.y + (4 * increment[1]), rook1.z + (4 * increment[2])] = new King( rook1.x + (4 * increment[0]), rook1.y + (4 * increment[1]), rook1.z + (4 * increment[2]), GameManager.getReference(null).getPlayerOne()); //Now we'll align the soldiers. increment[0] = 0; increment[1] = 0; increment[2] = 0; switch (axisToDelpoy) { case Axis.XAxis: { if (pawn1.x == 7) { increment[0] = -1; } else { increment[0] = 1; } } break; case Axis.YAxis: { if (pawn1.y == 7) { increment[1] = -1; } else { increment[1] = 1; } } break; case Axis.ZAxis: { if (pawn1.z == 7) { increment[2] = -1; } else { increment[2] = 1; } } break; } for (int soldierCnt = 0; soldierCnt < 8; soldierCnt++) { Chessboard.getReference() [pawn1.x + (soldierCnt * increment[0]), pawn1.y + (soldierCnt * increment[1]), pawn1.z + (soldierCnt * increment[2])] = new Pawn( pawn1.x + (soldierCnt * increment[0]), pawn1.y + (soldierCnt * increment[1]), pawn1.z + (soldierCnt * increment[2]), GameManager.getReference(null).getPlayerOne()); } //Now, do the same thing for the second team (paste from the above code, DA!) //First check the different axis between the second rook and bishop if ((rook2.x ^ knight2.x) != 0) { axisToDelpoy = Axis.XAxis; } else if ((rook2.y ^ knight2.y) != 0) { axisToDelpoy = Axis.YAxis; } else { axisToDelpoy = Axis.ZAxis; } increment[0] = 0; increment[1] = 0; increment[2] = 0; switch (axisToDelpoy) { case Axis.XAxis: { if (rook2.x == 7) { increment[0] = -1; } else { increment[0] = 1; } } break; case Axis.YAxis: { if (rook2.y == 7) { increment[1] = -1; } else { increment[1] = 1; } } break; case Axis.ZAxis: { if (rook2.z == 7) { increment[2] = -1; } else { increment[2] = 1; } } break; } //start with rook positions Chessboard.getReference()[rook2.x, rook2.y, rook2.z] = new Rook(rook2, GameManager.getReference(null).getPlayerTwo()); Chessboard.getReference()[ rook2.x + (increment[0] * 7), rook2.y + (increment[1] * 7), rook2.z + (increment[2] * 7)] = new Rook( rook2.x + (increment[0] * 7), rook2.y + (increment[1] * 7), rook2.z + (increment[2] * 7), GameManager.getReference(null).getPlayerTwo()); Chessboard.getReference()[ rook2.x + increment[0], rook2.y + increment[1], rook2.z + increment[2]] = new Knight( rook2.x + increment[0], rook2.y + increment[1], rook2.z + increment[2], GameManager.getReference(null).getPlayerTwo()); Chessboard.getReference()[ rook2.x + (increment[0] * 7) - increment[0], rook2.y + (increment[1] * 7) - increment[1], rook2.z + (increment[2] * 7) - increment[2]] = new Knight( rook2.x + (increment[0] * 7) - increment[0], rook2.y + (increment[1] * 7) - increment[1], rook2.z + (increment[2] * 7) - increment[2], GameManager.getReference(null).getPlayerTwo()); Chessboard.getReference()[ rook2.x + (2 * increment[0]), rook2.y + (2 * increment[1]), rook2.z + (2 * increment[2])] = new Bishop( rook2.x + (2 * increment[0]), rook2.y + (2 * increment[1]), rook2.z + (2 * increment[2]), GameManager.getReference(null).getPlayerTwo()); Chessboard.getReference()[ rook2.x + (increment[0] * 7) - (2 * increment[0]), rook2.y + (increment[1] * 7) - (2 * increment[1]), rook2.z + (increment[2] * 7) - (2 * increment[2])] = new Bishop( rook2.x + (increment[0] * 7) - (2 * increment[0]), rook2.y + (increment[1] * 7) - (2 * increment[1]), rook2.z + (increment[2] * 7) - (2 * increment[2]), GameManager.getReference(null).getPlayerTwo()); //Hint: Swapping the Queen and the King for the next team must occur Chessboard.getReference()[rook2.x + (3 * increment[0]), rook2.y + (3 * increment[1]), rook2.z + (3 * increment[2])] = new Queen( rook2.x + (3 * increment[0]), rook2.y + (3 * increment[1]), rook2.z + (3 * increment[2]), GameManager.getReference(null).getPlayerTwo() ); Chessboard.getReference()[rook2.x + (4 * increment[0]), rook2.y + (4 * increment[1]), rook2.z + (4 * increment[2])] = new King( rook2.x + (4 * increment[0]), rook2.y + (4 * increment[1]), rook2.z + (4 * increment[2]), GameManager.getReference(null).getPlayerTwo() ); //Now we'll align the soldiers. increment[0] = 0; increment[1] = 0; increment[2] = 0; switch (axisToDelpoy) { case Axis.XAxis: { if (pawn2.x == 7) { increment[0] = -1; } else { increment[0] = 1; } } break; case Axis.YAxis: { if (pawn2.y == 7) { increment[1] = -1; } else { increment[1] = 1; } } break; case Axis.ZAxis: { if (pawn2.z == 7) { increment[2] = -1; } else { increment[2] = 1; } } break; } for (int soldierCnt = 0; soldierCnt < 8; soldierCnt++) { Chessboard.getReference()[pawn2.x + (soldierCnt * increment[0]), pawn2.y + (soldierCnt * increment[1]), pawn2.z + (soldierCnt * increment[2])] = new Pawn( pawn2.x + (soldierCnt * increment[0]), pawn2.y + (soldierCnt * increment[1]), pawn2.z + (soldierCnt * increment[2]), GameManager.getReference(null).getPlayerTwo() ); } }
/* 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 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()); }
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); // مع السلومة يا حمومة }
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); }