Esempio n. 1
0
 // Constructors.
 public MinMaxDecision(Object obj, MiniMax type)
     : base(obj)
 {
     value     = 0;
     this.type = type;
 }
Esempio n. 2
0
    // Use this for initialization
    void Start()
    {
        board = new PieceColor[, ] {
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.White, PieceColor.Black, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Black, PieceColor.White, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null },
            { PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null, PieceColor.Null }
        };

        boardObjects = new GameObject[, ] {
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null },
            { null, null, null, null, null, null, null, null }
        };



        for (int j = 0; j <= BOARD_SIZE; j++)
        {
            for (int i = 0; i <= BOARD_SIZE; i++)
            {
                if (board[i, j] == PieceColor.Black)
                {
                    boardObjects[i, j] = (GameObject)Instantiate(blackPiece, new Vector3(i, 1, j), Quaternion.identity);
                    blackScore++;
                    //print (i + ", " + j);
                }
                else if (board[i, j] == PieceColor.White)
                {
                    boardObjects[i, j] = (GameObject)Instantiate(whitePiece, new Vector3(i, 1, j), Quaternion.identity);
                    whiteScore++;
                }
            }
        }

        /*
         * for (int j = 0; j < BOARD_SIZE; j++) {
         *      for (int i = 0; i < BOARD_SIZE; i++) {
         *              if (isValidMove(i, j)) {
         *                      Instantiate(blackPiece, new Vector3(i, 1, j), Quaternion.identity);
         *              }
         *      }
         * }
         */
        currentTurn  = turn.player;
        currentState = gameState.Menu;



        playingCanvas.SetActive(false);
        endCanvas.SetActive(false);

        AIscript = GetComponent <MiniMax>();
    }
Esempio n. 3
0
        public MoveScenarios GetBestMove(Board board, int maxDepth, int secTimeout)
        {
            MiniMax       miniMax       = new MiniMax(maxDepth);
            WorkerManager workerManager = WorkerManager.getInstance();

            if (workerManager.getWorkersCount() == 0)
            {
                return(miniMax.getBestMove(board, secTimeout));
            }

            int         maxColor    = board.CurrentColor;
            MoveContext rootContext = new MoveContext(board, null, null, 0, null, true);

            Stack <MoveContext> mainStack   = new Stack <MoveContext>();
            Stack <MoveContext> childsStack = new Stack <MoveContext>();
            List <MoveContext>  contextList = new List <MoveContext>();

            int generatedChilds = miniMax.generateMoveContext(rootContext, workerManager.getWorkersCount(), contextList);

            if (generatedChilds == 0)
            {
                return(null);
            }
            else if (generatedChilds == 1)
            {
                rootContext = contextList[0];
                miniMax.resolve(contextList[0].ChildsContext, maxColor, maxDepth, secTimeout);
            }
            else if (generatedChilds <= -1)
            {
                foreach (MoveContext mc in contextList)
                {
                    mainStack.Push(mc);
                }

                miniMax.resolve(mainStack, maxColor, 2, -1);
            }
            else
            {
                int i = 0;

                for (; i < contextList.Count - generatedChilds; i++)
                {
                    mainStack.Push(contextList[i]);
                }

                for (; i < contextList.Count; i++)
                {
                    childsStack.Push(contextList[i]);
                }

                workerManager.setParameters(childsStack, mainStack, maxDepth, maxColor, secTimeout - 15);
                workerManager.runWorkers();
                workerManager.waitForWorkers();

                childsStack.Clear();

                miniMax.resolve(mainStack, maxColor, maxDepth, -1);
            }

            if (rootContext.BestValue != int.MinValue)
            {
                return(new MoveScenarios(rootContext.CurrPosition, rootContext.CurrMoveScenario));
            }

            return(new MiniMax(2).getBestMove(board, -1));
        }
Esempio n. 4
0
 // method to actually executing a move in the game, separated from AIMove since the X player can also call it from GridSpace
 public void MakeMove(string player, int move)
 {
     currentState = MiniMax.CalculateNewState(currentState, player, move);
 }
 public MultiThreadedAlgorithm(byte signInRowToWin)
 {
     this.signInRowToWin = signInRowToWin;
     algorithm           = new MiniMax(signInRowToWin);
 }
Esempio n. 6
0
        /// <summary>
        /// Private Constructor used to initialize child node
        /// </summary>
        /// <param name="currentColor"></param> ---> will switch color
        /// <param name="pegBoard"></param>
        /// <param name="diskBoard"></param>
        /// <param name="minimaxValue"></param> ---> will switch min/max
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <param name="levelInTree"></param>
        /// <param name="aiColor"></param>
        private TreeNode(Player.Color currentColor, PegBoard pegBoard, DiskBoard diskBoard, MiniMax minimaxValue, Pair to, Pair from, int levelInTree, Player.Color aiColor, int movesFromWin, int maxTreeDepth, double middlePegHeuristic)
        {
            this._pegBoard           = pegBoard;
            this._diskBoard          = diskBoard;
            this._moveMade           = new PieceMove();
            this._moveMade.to        = to;
            this._moveMade.from      = from;
            this._levelInTree        = levelInTree;
            this._aiColor            = aiColor;
            this._movesFromWin       = movesFromWin - 1;
            this._maxTreeDepth       = maxTreeDepth;
            this._middlePegHeuristic = middlePegHeuristic;

            if (maxTreeDepth == 1)
            {
                this._difficultyLevel = Difficulty.Easy;
            }
            else if (maxTreeDepth == 3)
            {
                this._difficultyLevel = Difficulty.Medium;
            }
            else
            {
                this._difficultyLevel = Difficulty.Hard;
            }

            // Switch colors
            if (currentColor == Player.Color.White)
            {
                this._currentColor = Player.Color.Black;
            }
            else
            {
                this._currentColor = Player.Color.White;
            }

            // Switch minimaxValue
            if (minimaxValue == MiniMax.Max)
            {
                this._minimaxValue = MiniMax.Min;
            }
            else
            {
                this._minimaxValue = MiniMax.Max;
            }
        }