Ejemplo n.º 1
0
 public TileSet(TileState ts)
 {
     size  = 1;
     high  = low = HexGridUtil.getHexIndexForPlayer(ts.currentState, ts);
     state = ts.currentState;
     root  = null;
 }
Ejemplo n.º 2
0
    private void OnPlayFinished(TileState ts)
    {
        ts.setTileState(this.currentTurn);
        this.player_tiles[this.currentTurn].Add(ts);
        availableTiles.Remove(ts);
        //int go = CheckGameOver();
        int go = HexGridUtil.CheckGameOver(0, player_tiles[0], player_tiles[1]);

        if (go != -1)
        {
            //Debug.Log("GameOver: " + go);
            uiGameOver.SetActive(true);
            if (this.currentTurn == 0)
            {
                textCurrentPlayer.text = "Winner: " + players[0].name;
            }
            else
            {
                textCurrentPlayer.text = "Winner: " + players[1].name;
            }
            Stats.RecordGame(players[0].gameObject.name, players[1].gameObject.name, tossWon, this.currentTurn);
            Stats.Print();
            //Restart();
            return;
        }
        //ts.updateTileSet();
        //Debug.LogError("ts chain length: " + ts.tileSet.GetRoot().chainLength);
        TestEvaluationFunction();
        this.currentTurn = (this.currentTurn + 1) % 2;
        // this is necessary to switch to next turn after each turn is finished
        isCurrentTurnUpdated = true;
    }
Ejemplo n.º 3
0
    public int GetTotalScore(int playerID, bool Player, int depth)
    {
        AlphaBeta.TotalNodesEvaluated++;
        int totalScore = 0;

        if (this.winner != -1)
        {
            //Debug.Log(this.playerID + " winning node: " + this.winner);
            if (this.winner == playerID)
            {
                return(1000 * (depth + 1));
            }
            else
            {
                return(-1000 * (depth + 1));
            }
        }
        totalScore = HexGridUtil.evaluate(playerMaxTiles, playerID) - HexGridUtil.evaluate(playerMinTiles, 1 - playerID);

        //totalScore = playerScores[playerID] - playerScores[1 - playerID];
        //totalScore = HexGridUtil.evaluate(playerMaxTiles, playerID) - HexGridUtil.evaluate(playerMinTiles, 1 - playerID);

        //if (totalScore != totalScore1)
        //    Debug.LogError("Score Mismatch!!");

        //totalScore = evaluate(playerMaxTiles, playerID) - evaluate(playerMinTiles, 1 - playerID);
        //totalScore = evaluate(playerMaxTiles, playerID);
        //totalScore = HexGridUtil.evaluate(playerMaxTiles, playerID) - HexGridUtil.evaluate(playerMinTiles, 1 - playerID);
        //totalScore = HexGridUtil.evaluate(grid, playerMaxTiles, playerID);
        //Debug.Log("TotalScore: " + totalScore);
        return(totalScore);
    }
Ejemplo n.º 4
0
    public TileState highlightNeighbour(int direction, Dictionary <string, Tile> grid)
    {
        Hex       h  = Hex.Neighbor(new Hex(tile.index.x, tile.index.y, tile.index.z), direction);
        Tile      t  = HexGridUtil.TileAt(grid, h.q, h.r, h.s);
        TileState ts = t.GetComponent <TileState>();

        ts.highlight();
        return(ts);
    }
Ejemplo n.º 5
0
    public void updateTileSet()
    {
        int layer = HexGridUtil.getHexIndexForPlayer(this.currentState, this);

        //Debug.Log("tile layer: " + layer);
        foreach (Tile t in this.tile.neighbours)
        {
            TileState n = t.tileState;
            if (n.tileSet == null)
            {
                n.tileSet = new TileSet(n);
            }
            if (n.currentState == this.currentState)
            {
                if (this.tileSet == null)
                {
                    n.tileSet.size++;
                    //n.tileSet.set.Add(this);
                    this.tileSet = n.tileSet;
                    if (tileSet.high < layer)
                    {
                        tileSet.high = layer;
                    }
                    if (tileSet.low > layer)
                    {
                        tileSet.low = layer;
                    }
                }
                else
                {
                    var r  = tileSet.GetRoot();
                    var nr = n.tileSet.GetRoot();
                    if (r == nr)
                    {
                        // nothing need be done as they are currently in the same set
                    }
                    else
                    {
                        // unify two tileSets
                        if (r.size > nr.size)
                        {
                            r.unify(nr);
                        }
                        else
                        {
                            nr.unify(r);
                        }
                    }
                }
            }
        }
        if (this.tileSet == null) // no neighbours is of same color
        {
            initTileSet();
        }
        //Debug.Log(this.currentState + ": chainLength: " + tileSet.chainLength);
    }
Ejemplo n.º 6
0
 public void InitTileSet(TileState ts)
 {
     root = null;
     size = 1;
     //set.Clear();
     //set.Add(ts);
     high  = low = HexGridUtil.getHexIndexForPlayer(ts.currentState, ts);
     state = ts.currentState;
 }
Ejemplo n.º 7
0
    /*
     * default = 0
     * max number of simulations == max visits = 1
     * max number of wins = TBI = 2
     * highest win rate = TBI just player's win rate num_win / visits = 3
     * highest win rate and highest number of simulations associated with the move  = 4
     *
     */

    private double DefaultPolicyEval(MCTS_Node v)
    {
        bool isTerminal = !isNonTerminal(v);

        // have to handle win weight according to v.depth possibly
        if (v.winner != -1)
        {
            if (v.winner == v.myPlayerID)
            {
                //Debug.Log(v.myPlayerID + " Me Winner depth: " + v.depth);
                return(1000);
            }
            else
            {
                //Debug.Log(v.myPlayerID + " Opponent Winner depth: " + v.depth);
                return(-1000);
            }
        }

        int score = 0;
        List <TileState> changed = new List <TileState>();

        foreach (TileState ts in v.myTiles)
        {
            if (ts.currentState == -1)
            {
                ts.currentState = v.myPlayerID;
                changed.Add(ts);
            }
        }
        foreach (TileState ts in v.opponentTiles)
        {
            if (ts.currentState == -1)
            {
                ts.currentState = 1 - v.myPlayerID;
                changed.Add(ts);
            }
        }
        score = HexGridUtil.evaluate(v.myTiles, v.myPlayerID) - HexGridUtil.evaluate(v.opponentTiles, 1 - v.myPlayerID);
        foreach (TileState ts in changed)
        {
            ts.currentState = -1;
        }

        return(score);

        //TimeRecorder.Instance.startTimer("MCTS_AlphaBeta");
        ////Debug.Log("calling alpha beta");
        //AlphaBeta ab = new AlphaBeta(v.myPlayerID);
        //ab.budget = 120;
        //ab.depthBudget = 2;
        //Node n = ab.NextMove(this.grid, v.myTiles, v.opponentTiles, v.possibleMoves, null);
        //TimeRecorder.Instance.stopTimer("MCTS_AlphaBeta");
        //return n.score;
        //return HexGridUtil.evaluate(v.myTiles, v.myPlayerID);
    }
Ejemplo n.º 8
0
    public bool IsTerminal()
    {
        bool terminalNode = false;

        // Game over?
        this.winner = HexGridUtil.CheckGameOver(this.playerID, this.playerMaxTiles, this.playerMinTiles);
        //this.winner = HexGridUtil.CheckGameOver(this.grid);
        terminalNode = this.winner != -1;
        return(terminalNode);
    }
Ejemplo n.º 9
0
    public bool isNonTerminal(MCTS_Node node)
    {
        TimeRecorder.Instance.startTimer("isNonTerminal");
        if (node.isTerminal)
        {
            return(false);
        }
        if (node.myTiles.Count < 8)
        {
            return(true);
        }


        List <TileState> changed = new List <TileState>();

        foreach (TileState ts in node.myTiles)
        {
            if (ts.currentState == -1)
            {
                ts.currentState = node.myPlayerID;
                changed.Add(ts);
            }
        }
        foreach (TileState ts in node.opponentTiles)
        {
            if (ts.currentState == -1)
            {
                ts.currentState = 1 - node.myPlayerID;
                changed.Add(ts);
            }
        }
        //TimeRecorder.Instance.startTimer("isNonTerminal - 1");
        //node.winner = HexGridUtil.CheckGameOver(grid);
        node.winner = HexGridUtil.CheckGameOver(node.myPlayerID, node.myTiles, node.opponentTiles);
        if (node.winner != -1)
        {
            node.isTerminal = true;
        }
        //TimeRecorder.Instance.stopTimer("isNonTerminal - 1");
        foreach (TileState ts in changed)
        {
            ts.currentState = -1;
        }
        //TimeRecorder.Instance.stopTimer("isNonTerminal");
        return(node.winner == -1);
    }
Ejemplo n.º 10
0
    public static int CheckGameOver(int playerID, List <TileState> myTiles, List <TileState> opponentTiles)
    {
        int winner      = -1;
        int chainLength = HexGridUtil.evaluate(myTiles, playerID);

        if (chainLength >= 7)
        {
            winner = playerID;
        }
        else
        {
            chainLength = HexGridUtil.evaluate(opponentTiles, 1 - playerID);
            if (chainLength >= 7)
            {
                winner = 1 - playerID;
            }
        }
        return(winner);
    }
Ejemplo n.º 11
0
    public static TileState BridgeTowardsGoal(int playerID, List <TileState> playerMaxTiles, List <TileState> playerMinTiles, Dictionary <string, Tile> grid, List <TileState> ignore, bool closest)
    {
        List <Hex>       lineStart, lineGoal;
        List <TileState> playerTiles;

        if (playerID == 0)
        {
            playerTiles = playerMinTiles;
            lineStart   = FractionalHex.HexLinedraw(new Hex(0, 0, 0), new Hex(0, 7, -7));
            lineGoal    = FractionalHex.HexLinedraw(new Hex(7, 0, -7), new Hex(7, 7, -14));
        }
        else
        {
            playerTiles = playerMaxTiles;
            lineStart   = FractionalHex.HexLinedraw(new Hex(0, 0, 0), new Hex(7, 0, -7));
            lineGoal    = FractionalHex.HexLinedraw(new Hex(0, 7, -7), new Hex(7, 7, -14));
        }

        //Debug.Log("PlayerID: " + playerID + " isMax: " + (playerTiles == playerMaxTiles )) ;

        int       minD = -1;
        TileState ts   = null;
        Tile      tile = null;

        foreach (TileState t in playerTiles)
        {
            List <Tile> nn = HexGridUtil.Neighbours(grid, t.tile);
            foreach (Tile n in nn)
            {
                if (n.GetComponent <TileState>().currentState != -1)
                {
                    continue;
                }
                if (ignore != null && ignore.Count > 0 && ignore.Contains(n.GetComponent <TileState>()))
                {
                    continue;
                }
                foreach (Hex h in lineStart)
                {
                    foreach (Hex g in lineGoal)
                    {
                        int d = Hex.Distance(new Hex(n.index.x, n.index.y, n.index.z), g);
                        if (closest)
                        {
                            if (minD == -1 || minD > d)
                            {
                                minD = d;
                                tile = n;
                            }
                        }
                        else
                        {
                            if (minD == -1 || minD < d)
                            {
                                minD = d;
                                tile = n;
                            }
                        }
                    }
                }
                foreach (Hex h in lineGoal)
                {
                    foreach (Hex g in lineGoal)
                    {
                        int d = Hex.Distance(new Hex(n.index.x, n.index.y, n.index.z), new Hex(0, 0, 0));
                        if (closest)
                        {
                            if (minD == -1 || minD > d)
                            {
                                minD = d;
                                tile = n;
                            }
                        }
                        else
                        {
                            if (minD == -1 || minD < d)
                            {
                                minD = d;
                                tile = n;
                            }
                        }
                    }
                }
            }
        }
        if (tile != null)
        {
            ts = tile.GetComponent <TileState>();
        }
        return(ts);
    }