public void MovePiece(Move move) { ChessPiece piece = move.pieceMoved; Point4 newPosition = move.endPosition; ChessPiece capturedPiece = pieces[piece.x, piece.y, piece.z, piece.w]; Point4 movedFrom = move.startPosition; SetPiece(newPosition, piece); SetPiece(movedFrom, null); /*if (moveHistory.Count > 0) * { * lastMove = moveHistory[moveHistory.Count - 1]; * } * * if (lastMove != null && lastMove.pieceMoved.type == ChessPiece.Type.PAWN && * piece.type == ChessPiece.Type.PAWN && lastMove.startPosition == lastMove.pieceMoved.startPosition && * (newPosition == lastMove.pieceMoved.currentPosition - lastMove.pieceMoved.forwardW || newPosition == lastMove.pieceMoved.currentPosition - lastMove.pieceMoved.forwardZ)) ||{ || SetPiece(lastMove.pieceMoved.currentPosition, null); ||}*/ if (piece.type == ChessPiece.Type.PAWN) { if ((piece.team == ChessPiece.Team.WHITE && piece.z == size.z - 1 && piece.w == size.w - 1) || (piece.team == ChessPiece.Team.BLACK && piece.z == 0 && piece.w == 0)) { ChessPiece promotedPiece = piece.DeepCopy(); promotedPiece.type = ChessPiece.Type.QUEEN; SetPiece(newPosition, promotedPiece); } } moveHistory.Add(move); /*if (capturedPiece.type == ChessPiece.Type.KING) * { * if(currentMove == ChessPiece.Team.WHITE) * { * turnText.text = "White wins!!"; * } * else * { * turnText.text = "Black wins!!"; * } * StartCoroutine(LoadStartScene()); * }*/ if (currentMove == ChessPiece.Team.WHITE) { currentMove = ChessPiece.Team.BLACK; } else { currentMove = ChessPiece.Team.WHITE; } OnBoardUpdate(); }
public void Undo() { Move lastMove = moveHistory[moveHistory.Count - 1]; SetPiece(lastMove.startPosition, lastMove.pieceMoved); //Be explicit because en passant and promotion SetPiece(lastMove.endPosition, null); if (lastMove.pieceCaptured != null) { SetPiece(lastMove.pieceCaptured.currentPosition, lastMove.pieceCaptured); } moveHistory.RemoveAt(moveHistory.Count - 1); if (currentMove == ChessPiece.Team.WHITE) { currentMove = ChessPiece.Team.BLACK; } else { currentMove = ChessPiece.Team.WHITE; } OnBoardUpdate(); }
// Use this for initialization public void Show(ChessPiece.Team winner, ChessPiece.Team player) { gameObject.SetActive(true); if (winner == player) { undoButton.gameObject.SetActive(false); restartButton.gameObject.SetActive(true); } else { undoButton.gameObject.SetActive(true); restartButton.gameObject.SetActive(false); } string winnerName = ""; if (winner == ChessPiece.Team.WHITE) { winnerName = "White"; } else { winnerName = "Black"; } title.text = winnerName + " wins!"; body.text = winnerName + " took the enemy king"; }
public void AddChessPiece(int x, int y, int z, int w, ChessPiece.Type type, ChessPiece.Team team) { Point4 position = new Point4(x, y, z, w); SetPiece(position, new ChessPiece(type, team, position, this)); }
public float EvaluateBoard(ChessBoard board) { ChessPiece.Team enemyTeam = board.currentMove; scoresComputed++; float enemyScore = 0; float alliedScore = 0; attackHelper.ComputeAttackers(); foreach (ChessPiece piece in board.pieces) { if (piece == null) { continue; } if (piece.type == ChessPiece.Type.KING) { bool inCheck = false; ChessPiece checkingPiece = null; foreach (ChessPiece attacker in attackHelper.GetAttackers(piece.currentPosition)) { if (attacker.team != piece.team) { inCheck = true; checkingPiece = piece; } } bool uncontestedCheck = false; if (checkingPiece != null) { foreach (ChessPiece attacker in attackHelper.GetAttackers(checkingPiece.currentPosition)) { if (attacker.team != checkingPiece.team) { uncontestedCheck = true; } } } int flightSquareCount = 0; int flightSquareTotal = 0; foreach (Point4 move in piece.GetValidMoves()) { bool tileAttacked = false; //Check king flight square and -1 point per square attacked foreach (ChessPiece attacker in attackHelper.GetAttackers(move)) { if (attacker.team != piece.team) { tileAttacked = true; break; } } if (!tileAttacked) { flightSquareCount++; } flightSquareTotal++; } int flightSquaresCovered = flightSquareTotal - flightSquareCount; if (inCheck && board.currentMove != piece.team) { return(1000000); } else { // Add/remove score depending on the king position if (piece.team == enemyTeam) { enemyScore += 1000000; enemyScore -= inCheck ? KING_CHECKED_SCORE : 0; enemyScore -= flightSquaresCovered * KING_FLIGHT_COVERED_SCORE; enemyScore -= uncontestedCheck ? KING_UNCONTESTED_CHECKED_SCORE : 0; if (flightSquareCount == 0) { enemyScore -= KING_NO_FLIGHT_SCORE; } } if (piece.team != enemyTeam) { alliedScore += 1000000; alliedScore -= inCheck ? OUR_KING_CHECKED_SCORE : 0; alliedScore -= flightSquaresCovered * KING_FLIGHT_COVERED_SCORE; alliedScore -= uncontestedCheck ? KING_UNCONTESTED_CHECKED_SCORE : 0; if (flightSquareCount == 0) { alliedScore -= KING_NO_FLIGHT_SCORE; } } } continue; } float pieceScore = GetUnitValue(piece); float teamScore = pieceScore; List <ChessPiece> attackers = attackHelper.GetAttackers(piece.currentPosition); int friendlyAttacks = 0; int enemyAttacks = 0; foreach (ChessPiece attacker in attackers) { if (attacker.team == piece.team) { friendlyAttacks++; } else if (attacker.team != piece.team) { enemyAttacks++; } } if (friendlyAttacks == 0) { teamScore -= pieceScore * UNCONNECTED_MULT; } if (enemyAttacks >= 1) { if (friendlyAttacks < enemyAttacks) { teamScore -= pieceScore * ATTACK_BALANCE_THREATENED_MULT; } else { teamScore -= pieceScore * THREATENED_MULT; } } if (piece.type == ChessPiece.Type.PAWN) { //Points for pawn push int pushAmount = 0; if (piece.forwardW.w > 0) { pushAmount += piece.w; } else { pushAmount += board.size.w - 1 - piece.w; } if (piece.forwardZ.z > 0) { pushAmount += piece.z; } else { pushAmount += board.size.z - 1 - piece.z; } pushAmount = (int)Mathf.Max(pushAmount - ADVANCED_PAWN_MIN, 0); teamScore += pushAmount * ADVANCED_PAWN_MULT; } else { //Points for pawn push int pushAmount = 0; if (piece.forwardW.w > 0) { pushAmount += piece.w; } else { pushAmount += board.size.w - 1 - piece.w; } teamScore += pushAmount * ADVANCED_UNIT_MULT; } //Lower score if on the edge of the board if (piece.currentPosition.x == 0 || piece.currentPosition.x == board.size.x - 1) { teamScore -= pieceScore * BOARD_EDGE_DETRIMENT; } if (piece.currentPosition.y == 0 || piece.currentPosition.y == board.size.y - 1) { teamScore -= pieceScore * BOARD_EDGE_DETRIMENT; } if (piece.currentPosition.z == 0 || piece.currentPosition.z == board.size.z - 1) { teamScore -= pieceScore * BOARD_EDGE_DETRIMENT; } if (piece.currentPosition.w == 0 || piece.currentPosition.w == board.size.w - 1) { teamScore -= pieceScore * BOARD_EDGE_DETRIMENT; } if (piece.team == enemyTeam) { enemyScore += teamScore; } else { alliedScore += teamScore; } } return(alliedScore - enemyScore + Random.Range(-0.1f, 0.1f)); }