コード例 #1
0
ファイル: CheckersGame.cs プロジェクト: whuang02/AICheckers
        /// <summary>
        /// Function that handles a mouse click action
        /// </summary>
        /// <param name="mouseX">X position of the mouse</param>
        /// <param name="mouseY">Y position of the mouse</param>
        private void handleMouseAction(int mouseX, int mouseY)
        {
            //Make a rectangle for the mouse click size of 1 pixel
            Rectangle mouseClick = new Rectangle(mouseX, mouseY, 1, 1);

            //Below handles the event of when the black button is pressed
            if (mouseClick.Intersects(blackButton))
            {
                newGame(PieceColor.Black);
            }
            //Below handles the event of when the white button is pressed
            else if (mouseClick.Intersects(whiteButton))
            {
                newGame(PieceColor.White);
            }
            //Below handles any clicking of checkers pieces or board tiles
            else if (currentState.winner == PieceColor.None && currentState.turnColor != PieceColor.None)
            {
                bool clickablePlace = false;
                foreach (CheckersPiece gamePiece in currentState.moveablePieces)
                {
                    if (gamePiece.intersects(mouseClick) == true)
                    {
                        clickablePlace = true;
                        currentState.doGamePieceAction(gamePiece);
                        break;
                    }
                }
                BoardTile[,] tiles = currentState.gameBoard.getTiles();
                for (int x = 0; x < CheckersBoard.MAX_HORIZONTAL_TILES; x++)
                {
                    for (int y = 0; y < CheckersBoard.MAX_VERTICAL_TILES; y++)
                    {
                        if (tiles[x, y].intersects(mouseClick) == true)
                        {
                            clickablePlace = true;
                            if (currentState.doTileAction(tiles[x, y]) == true)
                            {
                                currentState.activeGamePiece.Update(currentState);
                                break;
                            }
                        }
                    }
                    if (clickablePlace == true)
                    {
                        break;
                    }
                }
                if (clickablePlace == false)
                {
                    currentState.gameBoard.clearMarkings();
                }
            }
        }
コード例 #2
0
ファイル: CheckersGame.cs プロジェクト: whuang02/AICheckers
        /// <summary>
        /// Max value search function
        /// </summary>
        /// <param name="state">The current state of the game</param>
        /// <param name="alpha">highest alpha found</param>
        /// <param name="beta">lowest beta found</param>
        /// <param name="depth">depth current state is at</param>
        /// <returns>The best max value</returns>
        public int MaxValue(CheckersGameState state, int alpha, int beta, int depth)
        {
            //increase depth
            depth++;
            maxDepth = (depth > maxDepth) ? depth : maxDepth;
            //Evaluate the state
            int utilityValue = Evaluate(state);

            //If the state is terminal or if the cuttoff is reached, return the value
            if (utilityValue == MAX_INT || utilityValue == MIN_INT ||
                depth == cutoff + iterativeDepth || (DateTime.Now - startTime).TotalSeconds > 55)
            {
                return(utilityValue);
            }
            //integer value used to determine alpha
            int value = MIN_INT;
            //A temporary gamestate to prevent anything in the actual game from changing
            CheckersGameState tempState;

            //The loop below finds every possible action from this state,
            //then using the temporary state, it will do the action,
            //and pass that state into the minValue function to find the largest possible value for alpha
            foreach (CheckersPiece piece in state.moveablePieces)
            {
                state.doGamePieceAction(piece);
                foreach (GameMove action in state.activeGamePiece.getPossibleMoves())
                {
                    tempState = new CheckersGameState(state);
                    nodes++;
                    if (tempState.doTileAction(tempState.gameBoard.getTileAt(action.destinationPosition)) == true)
                    {
                        tempState.activeGamePiece.Update(tempState);
                    }
                    value = Math.Max(value, MinValue(tempState, alpha, beta, depth));

                    //if alpha becomes greater than or equal to beta prune the tree
                    if (value >= beta)
                    {
                        maxPruned++;
                        return(value);
                    }

                    if (alpha < value)
                    {
                        alpha = value;
                        //if we are in the actual state of the game, that means we found a new best move
                        if (depth == 1)
                        {
                            bestMove = action;
                        }
                    }
                }
            }

            return(value);
        }