コード例 #1
0
    private void GameInit()
    {
        //start Game Init
        GameBoard = new Board();
        GameBoard.Init();


        //finish Game Init
        if (p1.Type != PlayerType.NULL || p2.Type != PlayerType.NULL)
        {
            currentPlayer    = (p1.Index == 1) ? p1 : p2;
            currentGameState = GameState.IN_PROGRESS;
            ChangeMenu();
        }
        else
        {
            Debug.Log("FATAL ERROR: Still Haven't Assigned Players in Game Model Class");
        }

        if (p1.Type == PlayerType.AI || p2.Type == PlayerType.AI)
        {
            AI = new MinimaxAI(p1, p2);
            //Check for AI here to avoid UNITY Crash $HACK$
            if (p1.Type == PlayerType.AI && p1.Index == 1)
            {
                CheckforAI();
            }
            if (p2.Type == PlayerType.AI && p2.Index == 1)
            {
                CheckforAI();
            }
        }
    }
コード例 #2
0
        private void onSetAI(object param)
        {
            Int32 Param = Int32.Parse((String)param);

            switch (Param)
            {
            case 0:
                model.removeAI();
                break;

            case 1:
                AlphaBetaAI abAI = new AlphaBetaAI();
                model.setAI(abAI);
                break;

            case 2:
                GeneralAI genAI = new GeneralAI();
                model.setAI(genAI);
                break;

            case 3:
                MinimaxAI mmAI = new MinimaxAI();
                model.setAI(mmAI);
                break;
            }
        }
コード例 #3
0
 private void AIplay(bool miniMax)
 {
     if (!gameWon)
     {
         if (AIvsAI)
         {
             if (miniMax)
             {
                 if (playedMoves1 == 6)
                 {
                     playedMoves1 = 0;
                     if (Repeating(firstpreviousMoveResults))
                     {
                         AIRandomMove1 = true;
                     }
                 }
                 else if (playedMoves2 == 6)
                 {
                     playedMoves2 = 0;
                     if (Repeating(secondpreviousMoveResults))
                     {
                         AIRandomMove2 = true;;
                     }
                 }
                 if (blackIsPlaying)
                 {
                     MinimaxAI minimax = new MinimaxAI(new PiecesCountGameEvaluation());
                     if (AIRandomMove1)
                     {
                         Move moveResult = minimax.AIRandomMove(board, blackIsPlaying);
                         AIturnToPlay(moveResult);
                         AIRandomMove1 = false;
                     }
                     else
                     {
                         Move moveResult = minimax.AIMinimaxMove(board, blackIsPlaying);
                         firstpreviousMoveResults[playedMoves1] = moveResult;
                         AIturnToPlay(moveResult);
                         playedMoves1++;
                     }
                 }
                 else
                 {
                     AlfaBetaAI alfaBeta = new AlfaBetaAI(new PiecesCountGameEvaluation());
                     if (AIRandomMove2)
                     {
                         Move moveResult = alfaBeta.AIRandomMove(board, blackIsPlaying);
                         AIturnToPlay(moveResult);
                         AIRandomMove2 = false;
                     }
                     else
                     {
                         Move moveResult = alfaBeta.AIAlfaBetaMove(board, blackIsPlaying);
                         secondpreviousMoveResults[playedMoves2] = moveResult;
                         AIturnToPlay(moveResult);
                         playedMoves2++;
                     }
                 }
             }
             else
             {
                 if (AB)
                 {
                     AlfaBetaAI alfaBeta   = new AlfaBetaAI(new PiecesCountGameEvaluation());
                     Move       moveResult = alfaBeta.AIAlfaBetaMove(board, blackIsPlaying);
                     AIturnToPlay(moveResult);
                 }
                 else
                 {
                     MinimaxAI minimax    = new MinimaxAI(new PiecesCountGameEvaluation());
                     Move      moveResult = minimax.AIMinimaxMove(board, blackIsPlaying);
                     AIturnToPlay(moveResult);
                 }
             }
         }
         else
         {
             if (miniMax)
             {
                 MinimaxAI minimax    = new MinimaxAI(new PiecesCountGameEvaluation());
                 Move      moveResult = minimax.AIMinimaxMove(board, blackIsPlaying);
                 AIturnToPlay(moveResult);
             }
             else if (miniMax == false)
             {
                 AlfaBetaAI alfaBeta   = new AlfaBetaAI(new PiecesCountGameEvaluation());
                 Move       moveResult = alfaBeta.AIAlfaBetaMove(board, blackIsPlaying);
                 AIturnToPlay(moveResult);
             }
         }
     }
     else
     {
         GameWon(blackIsPlaying);
     }
 }
コード例 #4
0
ファイル: Main.cs プロジェクト: CE2018-AI/Checker-Minimax
    // Use this for initialization
    void Start()
    {
        multipleRoundsTest = total > 1;
        //Player one = new Player("Player 1", Player.Side.BLACK);
        //Player two = new Player("Player 2", Player.Side.WHITE);

        MinimaxAI one = new MinimaxAI(Player.Side.BLACK, 3);
        MinimaxAI two = new MinimaxAI(Player.Side.WHITE, 4);

        //RandomAI one = new RandomAI(Player.Side.BLACK);
        //RandomAI two = new RandomAI(Player.Side.WHITE);

        //one goes first if true;
        bool turn = true;

        //System.out.println(board.toString());

        //Scanner sc = new Scanner(System.in);

        int blackWin = 0;
        int whiteWin = 0;
        int c        = 0;

        for (int t = 0; t < total; t++)
        {
            Board board = new Board();

            Player current = one;
            if (!turn)
            {
                current = two;
            }
            c = 0;
            println(board.toString());
            while (c < 10)
            {
                //Thread.sleep(500);
                c++;
                print(current.toString() + "'s turn: ");

                //Board.Decision decision = null;
                Board.Decision decision = Board.Decision.FAILED_INVALID_DESTINATION;
                //current instanceof AI
                if (current.GetType() == typeof(MinimaxAI))
                {
                    // decision = ((AI)current).makeMove(board);
                    decision = ((MinimaxAI)current).makeMove(board); //------------*
                }
                else
                {
                    /*string text = sc.nextLine();
                     * if (text.equals("board"))
                     * {
                     *   println(board.toString());
                     * }
                     * if (text.equals("rand"))
                     * {
                     *   decision = current.makeRandomMove(board);
                     * }
                     * else
                     * {
                     *   string[] split = text.split(" ");
                     *   Move m;
                     *   if (split.Length == 1)
                     *   {
                     *       m = new Move(Integer.parseInt(text.charAt(0) + ""), Integer.parseInt(text.charAt(1) + ""),
                     *               Integer.parseInt(text.charAt(2) + ""), Integer.parseInt(text.charAt(3) + ""));
                     *   }
                     *   else
                     *   {
                     *       int[] s = new int[split.length];
                     *       for (int i = 0; i < split.length; i++)
                     *       {
                     *           s[i] = Integer.parseInt(split[i]);
                     *       }
                     *       m = new Move(s[0], s[1], s[2], s[3]);
                     *
                     *
                     *   }
                     *   decision = current.makeMove(m, board);
                     * }*/
                }
                //System.out.println("Decision: " + decision);
                if (decision == Board.Decision.FAILED_INVALID_DESTINATION || decision == Board.Decision.FAILED_MOVING_INVALID_PIECE)
                {
                    println("Move Failed");
                    // don't update anything
                }
                else if (decision == Board.Decision.COMPLETED)
                {
                    println(board.toString());
                    if (board.getNumBlackPieces() == 0)
                    {
                        println("White wins with " + board.getNumWhitePieces() + " pieces left");
                        whiteWin++;
                        break;
                    }
                    if (board.getNumWhitePieces() == 0)
                    {
                        println("Black wins with " + board.getNumBlackPieces() + " pieces left");
                        blackWin++;
                        break;
                    }
                    if (turn)
                    {
                        current = two;
                    }
                    else
                    {
                        current = one;
                    }
                    turn = !turn;
                }
                else if (decision == Board.Decision.ADDITIONAL_MOVE)
                {
                    println("Additional Move");
                }
                else if (decision == Board.Decision.GAME_ENDED)
                {
                    //current player cannot move
                    if (current.getSide() == Player.Side.BLACK)
                    {
                        println("White wins");
                        whiteWin++;
                    }
                    else
                    {
                        println("Black wins");
                        blackWin++;
                    }
                    break;
                }
            }
            Debug.Log("Game finished after: " + c + " rounds");
            //one instanceof MinimaxAI
            if (one.GetType() == typeof(MinimaxAI))
            {
                Debug.Log("Avg time per move: " + ((MinimaxAI)one).getAverageTimePerMove());
            }
        }
        Debug.Log("Black won " + (blackWin / total * 100) + "%" + ", White won " + (whiteWin / total * 100) + "%");
        Debug.Log("Count move = " + c);
    }
コード例 #5
0
 void Awake()
 {
     minimaxAI = this;
 }