public BoardSpace[][] updateBoard(BoardSpace[][] previousBoard, KeyValuePair <int, int> move, uint turnNumber)
    {
        BoardSpace[][] newBoard = previousBoard.Select(x => x.ToArray()).ToArray();
        if (turnNumber % 2 == 0)
        {
            newBoard[move.Key][move.Value] = BoardSpace.BLACK;
        }
        else
        {
            newBoard[move.Key][move.Value] = BoardSpace.WHITE;
        }
        List <KeyValuePair <int, int> > updatedMoves = BoardScript.GetPointsChangedFromMove(newBoard, turnNumber, move.Value, move.Key);

        foreach (KeyValuePair <int, int> action in updatedMoves)
        {
            if (turnNumber % 2 == 0)
            {
                newBoard[action.Key][action.Value] = BoardSpace.BLACK;
            }
            else
            {
                newBoard[action.Key][action.Value] = BoardSpace.WHITE;
            }
        }
        ++turnNumber;
        return(newBoard);
    }
    void OnMouseDown()
    {
        BoardScript boardScript = (BoardScript)board.GetComponent(typeof(BoardScript));

        boardScript.objectToBuild = building;
        source.PlayOneShot(click);
    }
Exemple #3
0
    void Awake()
    {
        source = GetComponent <AudioSource> ();
        GameObject board = GameObject.Find("Board");

        boardScript = (BoardScript)board.GetComponent(typeof(BoardScript));
    }
        public static bool isCheck(GameManager gameManager, BoardScript boardScript)
        {
            List <FigureBase>     figure          = gameManager.getFigureByColor(gameManager.nowTurn * -1);
            List <PositionToMove> positionToMoves = new List <PositionToMove>();

            foreach (var x in figure)
            {
                positionToMoves.AddRange(x.getPositionsToMove(boardScript.chessBoardString));
            }

            if (gameManager.nowTurn == 1)
            {
                foreach (var x in positionToMoves)
                {
                    if (x.posX == boardScript.whiteKing.posX && x.posY == boardScript.whiteKing.posY)
                    {
                        return(true);
                    }
                }
            }
            else
            {
                foreach (var x in positionToMoves)
                {
                    if (x.posX == boardScript.blackKing.posX && x.posY == boardScript.blackKing.posY)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
 private static void MakePseudoMove(int posXtoMove, int posYtoMove, BoardScript boardScript,
                                    GameManager gameManager)
 {
     boardScript.moveRecorder.recordMove(posXtoMove, posYtoMove);
     MoveFigure(posXtoMove, posYtoMove, boardScript, gameManager);
     gameManager.figureSelected = false;
 }
        private static bool checkIfCheckMate(BoardScript boardScript, GameManager gameManager)
        {
            bool result = true;

            foreach (var figureBase in gameManager.getFigureByColor(gameManager.nowTurn))
            {
                gameManager.cureentSelect = figureBase;
                List <PositionToMove> positionToMoves = figureBase.getPositionsToMove(boardScript.chessBoardString);
                foreach (var positionToMove in positionToMoves)
                {
                    MakePseudoMove(positionToMove.posX, positionToMove.posY, boardScript, gameManager);
                    if (!isCheck(gameManager, boardScript))
                    {
                        result = false;
                    }
                    undoMove(boardScript, gameManager);
                    if (!result)
                    {
                        break;
                    }
                }

                if (!result)
                {
                    break;
                }
            }

            gameManager.cureentSelect = null;
            return(result);
        }
Exemple #7
0
    // Casts a shadow of the piece on the board, indicating whether it can be played or not
    // Input: None, but it uses current
    // Output: True if the piece can be placed, false if not
    private bool castShadow(bool set = false)
    {
        RaycastHit hit;
        // Position
        Vector3 brickXYZ;

        // Position in the board matrix of the center piece
        int x;
        int y;
        // Board script
        BoardScript bs = (board.GetComponent("BoardScript") as BoardScript);

        brickXYZ = current.GetComponentInChildren <Renderer>().bounds.center;
        // TODO: Display shadow of where piece will place
        x = (int)-10;
        y = (int)-10;
        if (Physics.Raycast(brickXYZ, new Vector3(0, -1, 0), out hit) &&
            hit.collider.transform.parent != null &&
            hit.collider.transform.parent.tag == "Board")
        {
            // Calculate the board position on which this block lies.
            // If board can be somewhere other than origin, transform hit.point first
            x = Mathf.FloorToInt(hit.point.x) + bs.gridwidth / 2;
            y = Mathf.FloorToInt(hit.point.z) + bs.gridheight / 2;
            //debug.text = x + " " + y;
            //Debug.Log("Hit board");
        }

        //debug.text = rotation.ToString();
        // Switch statement on current tag to get piece shape
        //debug.text = "Trying to cast shadow " + hit.point.x.ToString() + " " + hit.point.z.ToString();
        return(checkPiece(current, rotation, x, y, set, true, false));
    }
        public static void makeMove(int posXtoMove, int posYtoMove, BoardScript boardScript, GameManager gameManager)
        {
            boardScript.moveRecorder.recordMove(posXtoMove, posYtoMove);
            MoveFigure(posXtoMove, posYtoMove, boardScript, gameManager);

            if (isCheck(gameManager, boardScript))
            {
                undoMove(boardScript, gameManager);
            }
            else
            {
                boardScript.UpdateMoveTracker();
                gameManager.endOfTurn();
            }

            if (isCheck(gameManager, boardScript))
            {
                if (checkIfCheckMate(boardScript, gameManager))
                {
                    Debug.Log("Mate");
                }
                else
                {
                    Debug.Log("Check");
                }
            }
        }
Exemple #9
0
    // Checks to see if there are any available moves given the pieces and board state
    // Input: None
    // Output: True if there is at least one move possible
    //         False if no moves are available
    private bool availableMoves()
    {
        BoardScript bs = (board.GetComponent("BoardScript") as BoardScript);

        // Step through all pieces available
        // Check if that piece can be placed with any rotation at any position
        foreach (GameObject piece in brickList)
        {
            for (int i = 0; i < bs.gridheight; i++)
            {
                for (int j = 0; j < bs.gridwidth; j++)
                {
                    for (int rot = 0; rot < 360; rot += 90)
                    {
                        if (checkPiece(piece, rot, j, i, false, false, true) == true)
                        {
                            return(true);
                        }
                    }
                }
            }
        }

        return(false);
    }
    int EvaluationFunction(BoardSpace[][] currentBoard, BoardSpace color, bool isGameCompleted)
    {
        int totalDifference = 0;

        foreach (BoardSpace[] row in currentBoard)
        {
            foreach (BoardSpace space in row)
            {
                if (space == color)
                {
                    totalDifference++;
                }
                else if (space != BoardSpace.EMPTY)
                {
                    totalDifference--;
                }
            }
        }
        if (isGameCompleted)
        {
            if (totalDifference > 0)
            {
                return(currentBoard.Length * currentBoard[0].Length);
            }
            if (totalDifference < 0)
            {
                return(currentBoard.Length * currentBoard[0].Length * -1);
            }
        }
        return(BoardScript.GetValidMoves(currentBoard, color == BoardSpace.BLACK ? 0u : 1u).Count);
    }
    // Use this for initialization
    void Start()
    {
        gameBoard = GetComponent <BoardScript>();
        textTurn  = Instantiate(textPrefab, new Vector3(0, 0, 0), Quaternion.identity, canvas.transform);
        textPass  = Instantiate(textPrefab, new Vector3(0, 0, 0), Quaternion.identity, canvas.transform);

        textTurn.text = turn1;
        //textTurn.fontSize = 40;
        textTurn.rectTransform.anchoredPosition = new Vector2(-100, 0);
        //textTurn.rectTransform.anchorMax = new Vector2(1, 0.5f);
        //textTurn.rectTransform.anchorMin = new Vector2(1, 0.5f);
        //textTurn.rectTransform.sizeDelta = new Vector2(textTurn.fontSize * textTurn.text.Length + 5, textTurn.fontSize + 5);
        textPass.text = pass;
        textPass.rectTransform.anchoredPosition = new Vector2(-100, 60);
        textPass.color = new Color(0, 0, 0, 0);

        buttonUnDo = Instantiate(buttonUnDoPrefab, new Vector3(0, 0, 0), Quaternion.identity, canvas.transform);
        buttonUnDo.GetComponent <RectTransform>().anchoredPosition = new Vector2(-150, -100);
        buttonUnDo.GetComponentInChildren <Text>().text            = "1手戻す";
        buttonUnDo.GetComponentInChildren <Text>().fontSize        = 40;
        buttonUnDo.onClick.AddListener(() => gameBoard.Undo());
        //ゲーム版の作成
        Instantiate(board);

        gameBoard.Init();
        gameBoard.GetReversePointList(gameBoard.turnFlag);
    }
Exemple #12
0
    public BoardSpace[][] createNewBoard(BoardSpace[][] board, KeyValuePair <int, int> move, bool isBlack)
    {
        BoardSpace[][] newBoard = new BoardSpace[8][];
        //Copy board
        for (int i = 0; i < 8; i++)
        {
            newBoard[i] = new BoardSpace[8];
            for (int j = 0; j < 8; j++)
            {
                newBoard[i][j] = board[i][j];
            }
        }

        newBoard[move.Key][move.Value] = isBlack ? BoardSpace.BLACK : BoardSpace.WHITE;

        //Update Moves
        List <KeyValuePair <int, int> > changedSpots = BoardScript.GetPointsChangedFromMove(newBoard, isBlack ? ((uint)2) : ((uint)1), move.Value, move.Key);

        foreach (KeyValuePair <int, int> change in changedSpots)
        {
            newBoard[change.Key][change.Value] = isBlack ? BoardSpace.BLACK : BoardSpace.WHITE;
        }

        return(newBoard);
    }
Exemple #13
0
 public static BoardScript instance()
 {
     if (instance_ == null)
     {
         instance_ = GameObject.FindObjectOfType <BoardScript>();
     }
     return(instance_);
 }
 void OnTriggerEnter(Collider other)        //当たり判定
 {
     PlayingScript.PlayingScript.currentCoinScore++;
     BoardScript.speedUp();
     scoreScript.setScore();
     //コイン取得時に音声を再生
     Destroy(this.gameObject);               //コインを消す
 }
    void HighlightGrid_RPC(int bigID, int smallID)
    {
        BoardScript   board     = GameObject.Find("Board").GetComponent <BoardScript>();
        BigGridScript bigGrid   = board.bigGrids[bigID].GetComponent <BigGridScript>();
        GridScript    smallGrid = bigGrid.grids[smallID].GetComponent <GridScript>();

        smallGrid.HighlightGrid();
    }
Exemple #16
0
    private int negamax(BoardSpace[][] node, uint depth, int color)
    {
        //Debug.Log("negamax function start");
        if (depth == maxDepth - 2)
        {
            int retVal = -1000000;
            List <KeyValuePair <int, int> > moves = BoardScript.GetValidMoves(node, BoardScript.GetTurnNumber() + maxDepth);

            foreach (KeyValuePair <int, int> n in moves)
            {
                int temp = rateMoveSelect(moves, n);
                if (temp > retVal)
                {
                    retVal = temp;
                }
            }
            return(retVal);
        }
        else
        {
            int value = -100000;
            //go through each valid move for this board state
            foreach (KeyValuePair <int, int> n in BoardScript.GetValidMoves(node, BoardScript.GetTurnNumber()))
            {
                BoardSpace[][] nodeCopy = new BoardSpace[8][];
                for (int x = 0; x < 8; x++)
                {
                    nodeCopy[x] = new BoardSpace[8];
                    System.Array.Copy(node[x], nodeCopy[x], 8);
                }
                if (color == -1)
                {
                    nodeCopy[n.Value][n.Key] = BoardSpace.BLACK;
                }
                else
                {
                    nodeCopy[n.Value][n.Key] = BoardSpace.WHITE;
                }

                //simulate the changes each move would result in
                List <KeyValuePair <int, int> > simulatedChanges = BoardScript.GetPointsChangedFromMove(nodeCopy, BoardScript.GetTurnNumber() + depth, n.Key, n.Value);
                foreach (KeyValuePair <int, int> spot in simulatedChanges)
                {
                    if (nodeCopy[spot.Value][spot.Key] == BoardSpace.BLACK)
                    {
                        nodeCopy[spot.Value][spot.Key] = BoardSpace.WHITE;
                    }
                    else
                    {
                        nodeCopy[spot.Value][spot.Key] = BoardSpace.BLACK;
                    }
                }
                //recurse
                value = Mathf.Max(value, -1 * negamax(nodeCopy, depth + 1, -1 * color));
            }
            return(value);
        }
    }
Exemple #17
0
    public override KeyValuePair <int, int> makeMove(List <KeyValuePair <int, int> > availableMoves, BoardSpace[][] currBoard)
    {
        board = currBoard;

        if (color == BoardSpace.BLACK)
        {
            colorNum = -1;
        }
        else
        {
            colorNum = 1;
        }
        KeyValuePair <int, int> best;
        int bestScore = -100000000;

        foreach (KeyValuePair <int, int> n in availableMoves)
        {
            Debug.Log("iteration start");
            BoardSpace[][] nodeCopy = new BoardSpace[8][];
            for (int x = 0; x < 8; x++)
            {
                nodeCopy[x] = new BoardSpace[8];
                System.Array.Copy(currBoard[x], nodeCopy[x], 8);
            }
            if (colorNum == -1)
            {
                nodeCopy[n.Value][n.Key] = BoardSpace.BLACK;
            }
            else
            {
                nodeCopy[n.Value][n.Key] = BoardSpace.WHITE;
            }

            //simulate the changes the move would result in
            List <KeyValuePair <int, int> > simulatedChanges = BoardScript.GetPointsChangedFromMove(nodeCopy, BoardScript.GetTurnNumber(), n.Key, n.Value);
            //Debug.Log(simulatedChanges);
            foreach (KeyValuePair <int, int> spot in simulatedChanges)
            {
                if (nodeCopy[n.Value][n.Key] == BoardSpace.BLACK)
                {
                    nodeCopy[n.Value][n.Key] = BoardSpace.WHITE;
                }
                else
                {
                    nodeCopy[n.Value][n.Key] = BoardSpace.BLACK;
                }
            }
            //Debug.Log("starting negamax traversal");
            int value = negamax(nodeCopy, 0, colorNum);
            if (value >= bestScore)
            {
                best = n;
            }
        }
        Debug.Log("best is x:" + best.Value + " y:" + best.Key);
        return(best);
    }
Exemple #18
0
    void ConfirmPlacement_RPC(int bigID, int smallID, Defines.TURN turn, float time)
    {
        BoardScript      board     = GameObject.Find("Board").GetComponent <BoardScript>();
        BigGridScript    bigGrid   = board.bigGrids[bigID].GetComponent <BigGridScript>();
        GridScript       smallGrid = bigGrid.grids[smallID].GetComponent <GridScript>();
        GUIManagerScript guiScript = GameObject.FindGameObjectsWithTag("GUIManager")[0].GetComponent <GUIManagerScript>();

        smallGrid.ConfirmPlacement();
        guiScript.SetTimer(turn, time);
    }
    //GameOver function stops all coroutines and calls DestroyAll Coroutine from BoardScript.
    //It is used for end game. It is called if bomb tick is reached 0 or there is a deadlock.
    public void GameOver()
    {
        BoardScript board = FindObjectOfType <BoardScript>();

        StopAllCoroutines();
        StartCoroutine(board.DestroyAll());
        inGameUI.SetActive(false);
        board.gameObject.SetActive(false);
        gameOverUI.SetActive(true);
        gameOverUI.transform.FindChild("ScoreText").GetComponent <Text>().text = FindObjectOfType <ScoreScript>().score.ToString();
    }
Exemple #20
0
    void Start()
    {
        rend  = GetComponent <Renderer>();
        board = GetComponentInParent <BoardScript>();


        if (!partOfPuzzle)
        {
            correct = true;
        }
    }
 void Start()
 {
     board         = FindObjectOfType <BoardScript>();
     matchManager  = FindObjectOfType <MatchScript>();
     inputManager  = FindObjectOfType <InputScript>();
     selectedTiles = new GameObject[3];
     isMatched     = false;
     isTurning     = false;
     needsToMove   = true;
     turn1         = 0;
     turn2         = 0;
 }
 void Start()
 {
     screenWidth  = Screen.width;
     screenHeight = Screen.height;
     aspectRatio  = screenWidth / screenHeight;
     board        = FindObjectOfType <BoardScript>();
     camOffSet    = -10f;
     if (board != null)
     {
         RepositionCamera(board.width - 1, board.height - 1);
     }
 }
    // Start is called before the first frame update
    void Start()
    {
        sprite.color = ColoreRandom();
        BoardScript boardscript = this.GetComponentInParent <BoardScript>();

        if (boardscript != null)
        {
            if (boardscript.colorcheck > 0)
            {
                boardscript.Invoke("CheckColor", 0.2f);
            }
        }
    }
    void Awake()
    {
        instance    = this;
        boardScript = GetComponent <BoardScript>();
        startCanvas = GameObject.Find("StartCanvas");
        endImage    = GameObject.Find("UI").transform.Find("EndImage").gameObject;

        intervaltime += interval;

        for (int x = 0; x < boardScript.columns; x++)
        {
            startPos.Add(x);
        }
    }
Exemple #25
0
    /// <summary>
    /// This shows how to override the abstract definition of makeMove. All this one
    /// does is stupidly a random, yet legal, move.
    /// </summary>
    /// <param name="availableMoves"></param>
    /// <param name="currentBoard"></param>
    /// <returns></returns>

    public override KeyValuePair <int, int> makeMove(List <KeyValuePair <int, int> > availableMoves, BoardSpace[][] currentBoard, uint turn_number)
    {
        //Debug.Log(string.Join(",", availableMoves));
        BoardSpace enemyColor = turn_number % 2 == 0 ? BoardSpace.WHITE : BoardSpace.BLACK;
        BoardSpace ourColor   = turn_number % 2 == 0 ? BoardSpace.BLACK : BoardSpace.WHITE;
        KeyValuePair <int, int>         result;
        List <KeyValuePair <int, int> > result_candidates = new List <KeyValuePair <int, int> >();
        float score = float.NegativeInfinity;

        foreach (KeyValuePair <int, int> move in availableMoves)
        {
            BoardSpace[][] newer_board = new BoardSpace[8][];
            for (int i = 0; i < 8; ++i)
            {
                newer_board[i] = new BoardSpace[8];
                for (int j = 0; j < 8; ++j)
                {
                    newer_board[i][j] = currentBoard[i][j];
                }
            }
            //Debug.Log(move.Key + "," + move.Value);
            newer_board[move.Key][move.Value] = ourColor;
            List <KeyValuePair <int, int> > changed = BoardScript.GetPointsChangedFromMove(newer_board, turn_number, move.Value, move.Key);
            //Debug.Log(string.Join(",", changed));
            foreach (KeyValuePair <int, int> change in changed)
            {
                newer_board[change.Key][change.Value] = ourColor;
            }
            float candidate = ABnegaMax(newer_board, 1, Maxdepth, turn_number + 1, float.NegativeInfinity, float.PositiveInfinity);
            //Debug.Log("candidate: " + candidate);
            if (candidate >= score)
            {
                if (candidate > score)
                {
                    result_candidates.Clear();
                    result = new KeyValuePair <int, int>(move.Key, move.Value);
                    score  = candidate;
                    result_candidates.Add(result);
                }
                else
                {
                    result_candidates.Add(result);
                }
            }
        }
        //Debug.Log("turn number: " + turn_number);
        //Debug.Log("final score: " + score);
        return(result_candidates[Random.Range(0, result_candidates.Count)]);
    }
Exemple #26
0
    private float ABnegaMax(BoardSpace[][] currentBoard, int current_depth, int Max_depth, uint turn_number, float alpha, float beta)
    {
        BoardSpace enemyColor     = turn_number % 2 == 0 ? BoardSpace.WHITE : BoardSpace.BLACK;
        BoardSpace ourColor       = turn_number % 2 == 0 ? BoardSpace.BLACK : BoardSpace.WHITE;
        uint       current_player = turn_number % 2;
        List <KeyValuePair <int, int> > possible_moves = BoardScript.GetValidMoves(currentBoard, turn_number);

        if (current_depth >= Max_depth || possible_moves.Count == 0)
        {
            return(Evaluation(currentBoard, turn_number));
        }
        List <float> score = new List <float>();

        //Debug.Log("current depth: " + current_depth + " move size: " + possible_moves.Count);
        foreach (KeyValuePair <int, int> move in possible_moves)
        {
            BoardSpace[][] newer_board = new BoardSpace[8][];
            for (int i = 0; i < 8; ++i)
            {
                newer_board[i] = new BoardSpace[8];
                for (int j = 0; j < 8; ++j)
                {
                    newer_board[i][j] = currentBoard[i][j];
                }
            }
            newer_board[move.Key][move.Value] = ourColor;
            List <KeyValuePair <int, int> > changed = BoardScript.GetPointsChangedFromMove(newer_board, turn_number, move.Value, move.Key);
            //Debug.Log("changed: " + string.Join(",", changed));
            foreach (KeyValuePair <int, int> change in changed)
            {
                newer_board[change.Key][change.Value] = ourColor;
            }
            //add a variable that stores alpha
            //instead of compiling scores, compare with beta. if >=, just return
            //negaMax w/ alpha and beta being -beta, -<variable>
            float value = (-ABnegaMax(newer_board, current_depth + 1, Max_depth, turn_number + 1, -beta, -alpha));
            score.Add(value);
            if (value >= beta)
            {
                break;
            }
            if (value >= alpha)
            {
                alpha = value;
            }
        }
        //Debug.Log("score: " + string.Join(",", score));
        return(score.Max());
    }
        public static void undoMove(BoardScript boardScript, GameManager gameManager)
        {
            MoveRecord moveRecord = boardScript.moveRecorder.getLastRecord();

            //moveFigueOnBoard
            gameManager.cureentSelect = gameManager.getFigureOnPosition(moveRecord.toX, moveRecord.toY);
            MoveFigure(moveRecord.fromX, moveRecord.fromY, boardScript, gameManager);
            gameManager.cureentSelect.firstMove = moveRecord.firstMove;
            //CreateFigureIfBeaten
            if (moveRecord.moveTo != " ")
            {
                boardScript.chessBoardString[moveRecord.toX, moveRecord.toY] = moveRecord.moveTo;
                boardScript.figurePlacer.figreSet(moveRecord.toX, moveRecord.toY, moveRecord.firstMove, boardScript.chessBoardString);
            }
            boardScript.moveRecorder.deleteLastRecord();
        }
Exemple #28
0
    public int Negamax(BoardSpace[][] board, int depth, bool isBlack)
    {
        //Generate List of possible moves
        List <KeyValuePair <int, int> > possibleMoves = BoardScript.GetValidMoves(board, isBlack ? ((uint)2) : ((uint)1));

        //If hit end, bubble up
        if (depth <= 0 || possibleMoves.Count == 0)
        {
            if (BoardScript.betterSEF)
            {
                return(BetterSEF(board));
            }
            else
            {
                return(UniformSEF(board));
            }
        }

        if (isBlack)
        {
            int maxScore = -9999;
            foreach (KeyValuePair <int, int> move in possibleMoves)
            {
                BoardSpace[][] newBoard     = createNewBoard(board, move, isBlack);
                int            currentScore = Negamax(newBoard, depth - 1, false);
                if (currentScore > maxScore)
                {
                    maxScore = currentScore;
                }
            }
            return(maxScore);
        }
        else
        {
            int minScore = 9999;
            foreach (KeyValuePair <int, int> move in possibleMoves)
            {
                BoardSpace[][] newBoard     = createNewBoard(board, move, isBlack);
                int            currentScore = Negamax(newBoard, depth - 1, true);
                if (currentScore < minScore)
                {
                    minScore = currentScore;
                }
            }
            return(minScore);
        }
    }
Exemple #29
0
    private KeyValuePair <float, KeyValuePair <int, int> > AlphaBetaNegamax(BoardSpace[][] curBoard, List <KeyValuePair <int, int> > availableMoves, int curDepth, float alpha, float beta, uint turnNumber)
    {
        turnNumber++;
        KeyValuePair <int, int> bestMove;
        float bestScore = int.MinValue;;
        KeyValuePair <float, KeyValuePair <int, int> > bestPair;
        List <KeyValuePair <int, int> > actions = BoardScript.GetValidMoves(curBoard, turnNumber);

        if (curDepth <= 0 || actions.Count == 0)
        {
            float score = betterEvaluationFunction(curBoard);
            //float score = bestEvaluationFunction(curBoard); // Doesn't work as I hoped :/

            if (score < bestScore)
            {
                return(new KeyValuePair <float, KeyValuePair <int, int> >(bestScore, bestMove));
            }
            else
            {
                KeyValuePair <int, int> lastAction = new KeyValuePair <int, int>(7, 7);
                return(new KeyValuePair <float, KeyValuePair <int, int> >(score, lastAction));
            }
        }
        foreach (KeyValuePair <int, int> action in actions)
        {
            BoardSpace[][] successor = updateBoard(curBoard, action, turnNumber);
            KeyValuePair <float, KeyValuePair <int, int> > currentMove = AlphaBetaNegamax(successor, availableMoves, curDepth - 1, -beta, -alpha, turnNumber);
            float value        = currentMove.Key;
            float currentScore = -value;

            if (currentScore >= bestScore)
            {
                bestScore = currentScore;
                bestMove  = action;
            }
            if (alpha < currentScore)
            {
                alpha = currentScore;
            }
            if (alpha >= beta)
            {
                break;
            }
        }
        bestPair = new KeyValuePair <float, KeyValuePair <int, int> >(bestScore, bestMove);
        return(bestPair);
    }
Exemple #30
0
    public override KeyValuePair <int, int> makeMove(List <KeyValuePair <int, int> > availableMoves, BoardSpace[][] currentBoard)
    {
        if (color == BoardSpace.BLACK)
        {
            colorNum = 1;
        }
        else
        {
            colorNum = -1;
        }
        KeyValuePair <int, int> best;
        int bestScore = -100000000;

        foreach (KeyValuePair <int, int> n in availableMoves)
        {
            BoardSpace[][] nodeCopy = (BoardSpace[][])currentBoard.Clone();
            if (colorNum == 1)
            {
                nodeCopy[n.Key][n.Value] = BoardSpace.BLACK;
            }
            else
            {
                nodeCopy[n.Key][n.Value] = BoardSpace.WHITE;
            }

            //simulate the changes the move would result in
            List <KeyValuePair <int, int> > simulatedChanges = BoardScript.GetPointsChangedFromMove(nodeCopy, BoardScript.GetTurnNumber(), n.Key, n.Value);
            foreach (KeyValuePair <int, int> spot in simulatedChanges)
            {
                if (nodeCopy[spot.Key][spot.Value] == BoardSpace.BLACK)
                {
                    nodeCopy[spot.Key][spot.Value] = BoardSpace.WHITE;
                }
                else
                {
                    nodeCopy[spot.Key][spot.Value] = BoardSpace.BLACK;
                }
            }
            int value = negamax(nodeCopy, maxDepth, colorNum);
            if (value >= bestScore)
            {
                best = n;
            }
        }
        return(best);
    }
    void Start()
    {
        homer = transform.Find("homer").gameObject;
        hammer = transform.Find("hammer").gameObject;
        boardScript = FindObjectOfType<BoardScript>();

        prevHomerId = -1;
        random = new System.Random(homerId * 10);
        active = false;
        transform.Find("HomerButton").GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
    }