Search in sources :

Example 1 with Occupation

use of suite.weiqi.Weiqi.Occupation in project suite by stupidsing.

the class Board method play1.

/**
 * Plays a move on the Weiqi board. Uses group analysis which is slower.
 */
public void play1(Coordinate c, Occupation player) {
    Occupation current = get(c);
    Occupation opponent = player.opponent();
    if (current == Occupation.EMPTY) {
        set(c, player);
        GroupAnalysis ga = new GroupAnalysis(this);
        for (Coordinate neighbor : c.neighbors) if (get(neighbor) == opponent)
            killIfDead1(ga, neighbor);
        if (killIfDead1(ga, c))
            Fail.t("cannot perform suicide move");
    } else
        Fail.t("cannot move on occupied position");
}
Also used : Occupation(suite.weiqi.Weiqi.Occupation)

Example 2 with Occupation

use of suite.weiqi.Weiqi.Occupation in project suite by stupidsing.

the class Board method playIfSeemsPossible.

/**
 * Plays a move on the Weiqi board. Do not check for repeats in game state
 * history since Board do not have them. Use GameSet.moveIfPossible() for the
 * rule-accordance version.
 *
 * This method do not take use of GroupAnalysis for performance reasons.
 */
public MoveType playIfSeemsPossible(Coordinate c, Occupation player) {
    MoveType type;
    Occupation current = get(c);
    Occupation opponent = player.opponent();
    if (current == Occupation.EMPTY) {
        type = MoveType.PLACEMENT;
        set(c, player);
        for (Coordinate neighbor : c.neighbors) if (get(neighbor) == opponent && killIfDead(neighbor))
            type = MoveType.CAPTURE;
        if (!hasBreath(c)) {
            set(c, Occupation.EMPTY);
            type = MoveType.INVALID;
        }
    } else
        type = MoveType.INVALID;
    return type;
}
Also used : Occupation(suite.weiqi.Weiqi.Occupation)

Example 3 with Occupation

use of suite.weiqi.Weiqi.Occupation in project suite by stupidsing.

the class GameSet method playIfValid.

/**
 * Plays a move on the Weiqi board. Ensure no repeats in game state history.
 */
private boolean playIfValid(Move move, boolean rollBack) {
    Occupation opponent = nextPlayer.opponent();
    int i = 0;
    for (Coordinate c1 : move.position.neighbors) move.neighborColors[i++] = board.get(c1);
    move.type = board.playIfSeemsPossible(move.position, nextPlayer);
    boolean success = move.type != MoveType.INVALID;
    if (success) {
        int newHashCode = board.hashCode();
        success &= !previousStates.contains(newHashCode);
        if (success && !rollBack) {
            nextPlayer = opponent;
            previousStates.add(newHashCode);
        } else
            unplay_(move);
    }
    return success;
}
Also used : Occupation(suite.weiqi.Weiqi.Occupation)

Example 4 with Occupation

use of suite.weiqi.Weiqi.Occupation in project suite by stupidsing.

the class GroupAnalysis method assignGroups.

private void assignGroups() {
    int nGroups = 0;
    for (Coordinate c : Coordinate.all()) {
        Occupation color = board.get(c);
        // must be root
        Group group = null;
        for (Coordinate c1 : c.leftOrUp) if (board.get(c1) == color) {
            Group group1 = groupByCoord.get(c1).root();
            if (group != null) {
                if (group != group1)
                    group1.parent = group;
            } else
                group = group1;
        }
        if (group == null)
            group = new Group(nGroups++, color);
        groupByCoord.put(c, group);
    }
    for (Coordinate c : Coordinate.all()) {
        Group group = groupByCoord.get(c).root();
        groupByCoord.put(c, group);
        group.coords.add(c);
        groups.add(group);
    }
}
Also used : Occupation(suite.weiqi.Weiqi.Occupation)

Example 5 with Occupation

use of suite.weiqi.Weiqi.Occupation in project suite by stupidsing.

the class UctTest method testUctGame.

@Test
public void testUctGame() {
    new Profiler().profile(() -> {
        DecimalFormat df = new DecimalFormat("0.000");
        // 20000
        int nSimulations = 5000;
        int boundedTime = 300000;
        int seed = new Random().nextInt();
        System.out.println("RANDOM SEED = " + seed);
        ShuffleUtil.setSeed(seed);
        Board board = new Board();
        GameSet gameSet = new GameSet(board, Occupation.BLACK);
        while (true) {
            GameSet gameSet1 = new GameSet(gameSet);
            UctVisitor<Coordinate> visitor = UctWeiqi.newVisitor(gameSet1);
            UctSearch<Coordinate> search = new UctSearch<>(visitor);
            search.setNumberOfThreads(Constants.nThreads);
            search.setNumberOfSimulations(nSimulations);
            search.setBoundedTime(boundedTime);
            Stopwatch<Coordinate> timed = Stopwatch.of(search::search);
            Coordinate move = timed.result;
            if (move == null)
                break;
            Occupation player = gameSet.getNextPlayer();
            search.dumpPrincipalVariation();
            System.out.println(// 
            player + " " + // 
            move + " " + // 
            df.format(search.getWinningChance()) + " " + timed.duration + "ms");
            gameSet.play(move);
            UserInterface.display(gameSet);
        }
    });
}
Also used : DecimalFormat(java.text.DecimalFormat) UctSearch(suite.uct.UctSearch) Occupation(suite.weiqi.Weiqi.Occupation) Profiler(suite.sample.Profiler) Random(java.util.Random) Test(org.junit.Test)

Aggregations

Occupation (suite.weiqi.Weiqi.Occupation)13 HashSet (java.util.HashSet)5 Stack (java.util.Stack)2 Group (suite.weiqi.GroupAnalysis.Group)2 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 Set (java.util.Set)1 Test (org.junit.Test)1 Profiler (suite.sample.Profiler)1 UctSearch (suite.uct.UctSearch)1