示例#1
0
    void AIChooseMove()
    {
        int highest = -999999;
        List <PotentialMove> chosenMoves = new List <PotentialMove>();

        foreach (PotentialMove move in potentialMoves)
        {
            if (move.ResultingBoard.totalBoardValue > highest)
            {
                highest = move.ResultingBoard.totalBoardValue;
            }
        }
        foreach (PotentialMove move in potentialMoves)
        {
            if (move.ResultingBoard.totalBoardValue == highest)
            {
                chosenMoves.Add(move);
            }
        }
        int num = Random.Range(0, chosenMoves.Count - 1);

        if (chosenMoves[num].tile.piece != null)
        {
            TakePiece(chosenMoves[num].tile.piece);
        }
        UnitInfo info = chosenMoves[num].piece.GetComponent <UnitInfo>();

        info.targetPosition = chosenMoves[num].tile.position;
        info.moved          = true;
        AIChosenPiece       = chosenMoves[num].piece;
        chosenMoves.Clear();
        potentialMoves.Clear();
        aiTurnState = AITurnState.CHOSEN;
    }
示例#2
0
    void SwitchTurn()
    {
        bool blackFound = false;
        bool whiteFound = false;

        foreach (GameObject piece in gameBoard.black)
        {
            UnitInfo info = piece.GetComponent <UnitInfo>();
            if (info.type == UnitInfo.unitType.KING)
            {
                blackFound = true;
                break;
            }
        }
        foreach (GameObject piece in gameBoard.white)
        {
            UnitInfo info = piece.GetComponent <UnitInfo>();
            if (info.type == UnitInfo.unitType.KING)
            {
                whiteFound = true;
                break;
            }
        }
        if (!blackFound)
        {
            gameOver = true;
            winText.SetActive(true);
            winText.GetComponent <Text>().text = "White win!";
        }
        else if (!whiteFound)
        {
            gameOver = true;
            winText.SetActive(true);
            winText.GetComponent <Text>().text = "Black win!";
        }
        CheckPawn();
        turnCount++;
        currentlySelectedPiece = null;
        turnState   = TurnState.PASSIVE;
        aiTurnState = AITurnState.PASSIVE;
        CheckPiece();
    }
示例#3
0
    void AIFindMoves()
    {
        List <GameObject> piecesToUse = new List <GameObject>();

        if (currentTurn == 0)
        {
            piecesToUse = gameBoard.white;
        }
        else
        {
            piecesToUse = gameBoard.black;
        }
        foreach (GameObject piece in piecesToUse)
        {
            UnitInfo info = piece.GetComponent <UnitInfo>();
            Tile     tile = new Tile();
            for (int x = 0; x < boardLength; x++)
            {
                for (int y = 0; y < boardLength; y++)
                {
                    if (tiles[x, y].piece == piece)
                    {
                        tile = tiles[x, y];
                    }
                }
            }

            switch (info.type)
            {
            case UnitInfo.unitType.PAWN:
                PawnMove(piece, tile);
                break;

            case UnitInfo.unitType.KNIGHT:
                KnightMove(piece, tile);
                break;

            case UnitInfo.unitType.QUEEN:
                Move(piece, tile, boardLength, 1, 1);
                Move(piece, tile, boardLength, 0, 1);
                Move(piece, tile, boardLength, 1, 0);
                Move(piece, tile, boardLength, -1, -1);
                Move(piece, tile, boardLength, 0, -1);
                Move(piece, tile, boardLength, -1, 0);
                Move(piece, tile, boardLength, -1, 1);
                Move(piece, tile, boardLength, 1, -1);
                break;

            case UnitInfo.unitType.KING:
                Move(piece, tile, 1, 1, 1);
                Move(piece, tile, 1, 0, 1);
                Move(piece, tile, 1, 1, 0);
                Move(piece, tile, 1, -1, -1);
                Move(piece, tile, 1, 0, -1);
                Move(piece, tile, 1, -1, 0);
                Move(piece, tile, 1, -1, 1);
                Move(piece, tile, 1, 1, -1);
                break;

            case UnitInfo.unitType.BISHOP:
                Move(piece, tile, boardLength, 1, 1);
                Move(piece, tile, boardLength, -1, -1);
                Move(piece, tile, boardLength, -1, 1);
                Move(piece, tile, boardLength, 1, -1);
                break;

            case UnitInfo.unitType.ROOK:
                Move(piece, tile, boardLength, 0, 1);
                Move(piece, tile, boardLength, 1, 0);
                Move(piece, tile, boardLength, -1, 0);
                Move(piece, tile, boardLength, 0, -1);
                break;

            default:
                break;
            }
        }
        aiTurnState = AITurnState.DECIDING;
    }