Exemple #1
0
    public void removeChess(int index)
    {
        int playerKey = (redTurn ? 1 : 2);

        if (countdown >= dropTime && BoardUtility.canRemove(gameGrid, index, playerKey))
        {
            for (int i = 0; i < all_chess.Count; i++)
            {
                if (all_chess[i].x == index && all_chess[i].y != 5)
                {
                    all_chess[i].x++;
                }
                else if (all_chess[i].x == index && all_chess[i].y == 5)
                {
                    Chess chs = all_chess[i];
                    all_chess.Remove(chs);
                    Destroy(chs.gameObject);
                }
            }

            redTurn = !redTurn;

            //remove it from the gamegrid
            gameGrid = BoardUtility.removeFromGrid(gameGrid, index, 5);

            checkWinner();
        }
    }
    //minimax algorithm that will expend its search and return the heuristic value for different result
    //with remove option
    public static Node minimaxWithRemove(int[] gameGrid, int depth, int alpha, int beta, bool maximizingPlayer, int playerKey, int index, int h)
    {
        if (depth == 0)
        {
            Node n = new Node();
            n.value = heristic(gameGrid, playerKey, h);
            n.index = index;
            return(n);
        }
        else
        {
            if (maximizingPlayer)
            {
                Node n = new Node();
                n.value = -9999999;
                int j = -1;
                for (int i = 0; i < 14; i++)
                {
                    if (i < 7 && !BoardUtility.canInsert(gameGrid, i))
                    {
                        break;
                    }
                    else if (i >= 7 && !BoardUtility.canRemove(gameGrid, i - 7, playerKey))
                    {
                        break;
                    }

                    int[] newGrid = BoardUtility.copyGameGrid(gameGrid);

                    if (i < 7)
                    {
                        BoardUtility.insertToGrid(newGrid, BoardUtility.insertingPosition(newGrid, i), playerKey);
                    }
                    else
                    {
                        BoardUtility.removeFromGrid(newGrid, i - 7, 5);
                    }

                    Node n1 = minimax(newGrid, depth - 1, alpha, beta, false, playerKey, i, h);
                    if (n1.value >= n.value)
                    {
                        n = n1;
                        j = i;
                    }
                    else if (n1.value == n.value)
                    {
                        if (Random.Range(0, 2) == 1)
                        {
                            n = n1;
                            j = i;
                        }
                    }
                    if (alphaBeta && n1.value >= alpha)
                    {
                        alpha = n1.value;
                    }
                    if (alphaBeta && beta <= alpha)
                    {
                        break;
                    }
                }
                n.index = j;
                return(n);
            }
            else
            {
                Node n = new Node();
                n.value = 99999999;
                int key = (playerKey + 1) % 2;
                int j   = -1;
                for (int i = 0; i < 7; i++)
                {
                    if (i < 7 && !BoardUtility.canInsert(gameGrid, i))
                    {
                        break;
                    }
                    else if (i >= 7 && !BoardUtility.canRemove(gameGrid, i - 7, key))
                    {
                        break;
                    }

                    int[] newGrid = BoardUtility.copyGameGrid(gameGrid);

                    if (i < 7)
                    {
                        BoardUtility.insertToGrid(newGrid, BoardUtility.insertingPosition(newGrid, i), key);
                    }
                    else
                    {
                        BoardUtility.removeFromGrid(newGrid, i - 7, 5);
                    }

                    Node n1 = minimax(newGrid, depth - 1, alpha, beta, true, playerKey, i, h);
                    if (n1.value <= n.value)
                    {
                        n = n1;
                        j = i;
                    }
                    else if (n1.value == n.value)
                    {
                        if (Random.Range(0, 2) == 1)
                        {
                            n = n1;
                            j = i;
                        }
                    }
                    if (alphaBeta && beta >= n1.value)
                    {
                        beta = n1.value;
                    }
                    if (alphaBeta && beta <= alpha)
                    {
                        break;
                    }
                }
                n.index = j;
                return(n);
            }
        }
    }