Пример #1
0
    static public TicTacState VictoryCheck(int[,] tiles, int playerNr)
    {
        TicTacState HState = CheckHorizontal(tiles, playerNr);

        if (HState != TicTacState.None)
        {
            Debug.Log(HState.ToString() + " Player: " + playerNr);
            //PrintBoard(tiles);
            return(HState);
        }
        TicTacState VState = CheckVertical(tiles, playerNr);

        if (VState != TicTacState.None)
        {
            Debug.Log(VState.ToString() + " Player: " + playerNr);
            // PrintBoard(tiles);
            return(VState);
        }
        TicTacState DState = CheckDiagonal(tiles, playerNr);

        if (DState != TicTacState.None)
        {
            Debug.Log(DState.ToString() + " Player: " + playerNr);
            //PrintBoard(tiles);
            return(DState);
        }

        if (TieCheck(tiles))
        {
            Debug.Log(TicTacState.Tie.ToString() + " Player: " + playerNr);
            return(TicTacState.Tie);
        }
        return(TicTacState.None);
    }
Пример #2
0
    static public TicTacState VictoryCheck(int[,] tiles, int depth, bool printS, int playerNr)
    {
        TicTacState HState = CheckHorizontal(tiles, playerNr);

        if (HState != TicTacState.None)
        {
            return(HState);
        }
        TicTacState VState = CheckVertical(tiles, playerNr);

        if (VState != TicTacState.None)
        {
            return(VState);
        }
        TicTacState DState = CheckDiagonal(tiles, playerNr);

        if (DState != TicTacState.None)
        {
            return(DState);
        }

        if (TieCheck(tiles))
        {
            return(TicTacState.Tie);
        }
        return(TicTacState.None);
    }
Пример #3
0
    int ScoreCheck(TicTacState state, int depth)
    {
        switch (state)
        {
        case TicTacState.Tie:
            return(0);

        case TicTacState.Lose:
            return(-1);

        case TicTacState.Win:
            return(1);

        default:
            return(0);
        }
    }
Пример #4
0
    int ResultScore(TicTacState state)
    {
        switch (state)
        {
        case TicTacState.Win:
            return(1);

        case TicTacState.Tie:
            return(0);

        case TicTacState.None:
            return(0);

        default:
            Debug.Log("Bad stuff happened");
            return(0);
        }
    }
Пример #5
0
    int MiniMax(int[,] dummyArray, int depth, bool maximizing, int alpha, int beta)
    {
        NodesTaken++;
        TicTacState currentState = TicTacToeFunctions.VictoryCheck(dummyArray, depth, false, playerNummber);

        if (currentState != TicTacState.None)
        {
            return(ScoreCheck(currentState, depth));
        }

        int score;

        if (maximizing)
        {
            //score = int.MinValue;
            for (int x = 0; x < dummyArray.GetLength(0); x++)
            {
                for (int y = 0; y < dummyArray.GetLength(1); y++)
                {
                    if (dummyArray[x, y] == 0)
                    {
                        dummyArray[x, y] = playerNummber;
                        score            = MiniMax(dummyArray, depth++, false, alpha, beta);
                        dummyArray[x, y] = 0;
                        // score = Math.Max(newScore, score);
                        alpha = Math.Max(alpha, score);
                        if (beta <= alpha)
                        {
                            break;
                        }
                    }
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
            }
            return(alpha);
        }
        else
        {
            // score = int.MaxValue;
            for (int x = 0; x < dummyArray.GetLength(0); x++)
            {
                for (int y = 0; y < dummyArray.GetLength(1); y++)
                {
                    if (dummyArray[x, y] == 0)
                    {
                        dummyArray[x, y] = 1;
                        score            = MiniMax(dummyArray, depth++, true, alpha, beta);
                        dummyArray[x, y] = 0;
                        //score = Math.Min(score, newScore);
                        beta = Math.Min(beta, score);
                        if (beta <= alpha)
                        {
                            break;
                        }
                    }
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
            }
            return(beta);
        }
    }