void ExecuteMove(Tile3D startTile, Tile3D endTile) { if (endTile.currentPiece != null && endTile.currentPiece.type == ChessPiece.Type.KING) { if (board.currentMove == ChessPiece.Team.WHITE) { turnText.text = "White wins!!"; } else { turnText.text = "Black wins!!"; } StartCoroutine(LoadStartScene()); } Point4 startPosition = new Point4(startTile.x, startTile.y, startTile.z, startTile.w); Point4 newPosition = new Point4(endTile.x, endTile.y, endTile.z, endTile.w); board.MovePiece(board.GetMove(startPosition, newPosition)); if (board.currentMove == ChessPiece.Team.BLACK) { turnText.text = "Black's turn"; } else if (board.currentMove == ChessPiece.Team.WHITE) { turnText.text = "White's turn"; } attackers.ComputeAttackers(); }
void InitializeBoard() { board = ChessBoard.ClassicVariantBoard(); tiles = new Tile3D[board.size.x, board.size.y, board.size.z, board.size.w]; for (int x = 0; x < board.size.x; x++) { for (int y = 0; y < board.size.y; y++) { for (int z = 0; z < board.size.z; z++) { for (int w = 0; w < board.size.w; w++) { tiles[x, y, z, w] = GameObject.Instantiate(tilePrefab).GetComponent <Tile3D>(); tiles[x, y, z, w].Initialize(x, y, z, w, board); tiles[x, y, z, w].transform.parent = tileContainer.transform; tiles[x, y, z, w].transform.position = GetTilePosition(x, y, z, w); tiles[x, y, z, w].transform.rotation = Quaternion.LookRotation(Vector3.up); } } } } attackers = new ChessboardAttackerHelper(board); attackers.ComputeAttackers(); }
void ExecuteMove(Point4 startTile, Point4 endTile) { board.MovePiece(board.GetMove(startTile, endTile)); if (GameIsOver()) { endPopup.Show(GetWinner(), playerColor); } attackers.ComputeAttackers(); UpdateTileHighlights(); if (board.currentMove != playerColor && !GameIsOver()) { StartCoroutine(RunAI()); } }
public void Restart() { chessboardControllers = Transform.FindObjectsOfType <ChessboardController2D>(); board = ChessBoard.ClassicVariantBoard(); foreach (ChessboardController2D chessboardController in chessboardControllers) { chessboardController.Initialize(board); } attackers = new ChessboardAttackerHelper(board); attackers.ComputeAttackers(); selectedTile = Point4.NONE; destinationTile = Point4.NONE; attackedTile = Point4.NONE; }
// Use this for initialization void Start() { chessboardControllers = Transform.FindObjectsOfType <ChessboardController2D>(); board = ChessBoard.ClassicVariantBoard(); foreach (ChessboardController2D chessboardController in chessboardControllers) { chessboardController.Initialize(board); } attackers = new ChessboardAttackerHelper(board); attackers.ComputeAttackers(); introStartTime = Time.time; CreatePanGesture(); CreateScaleGesture(); CreateTapGesture(); }
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)); }