Package com.sgfj

Examples of com.sgfj.SGFNodeIterator


     * @throws SGFPropertyNotFoundException
     * @throws GameException
     */
    public DocumentedGame(SGFNode current) throws SGFInvalidDataRequestException, SGFPropertyNotFoundException, GameException {
        Stack moves = new Stack();
        SGFNodeIterator ni = current.iterator();
        while (ni.prev(false) != null) {
            moves.push(ni.current());
            ni.prev(true);
        }

        SGFNode head = ni.current();
        kifu = current.iterator();

        // Read size and init board
        int size;
        try {
            size = head.getProperty(SGFPropertyName.SZ).getInt();
        } catch (SGFPropertyNotFoundException e) {
            size = 19;
        }
        super.init(size);

        // Read handicap property, stones added with AW/AB
        try {
            handicap  = head.getProperty(SGFPropertyName.HA).getInt();
        } catch (SGFPropertyNotFoundException e) {
            handicap = 0;
        }

        playNodeProperties();

        // Find color to play for first nodes without move
        do {
            try {
                byte color = ni.current().getMoveProperty().color;
                if (color != Board.NONE) {
                    colorToPlay = color;
                    break;
                }
            } catch (SGFPropertyNotFoundException e) {
            }
        } while (ni.next(true) != null);

        // Play moves from head to provided node
        while (!moves.empty()) {
            SGFNode node = (SGFNode) moves.pop();
            try {
View Full Code Here


    /**
     * @return First node with move property.
     */
    public SGFNode kifuFirstMove(boolean skipPass) {
        SGFNodeIterator i = kifu.first(false).iterator();
        do {
            try {
                SGFMove move = i.current().getMoveProperty();
                if (!skipPass || move.x >= 0)
                    break;
            } catch (SGFPropertyNotFoundException e) {
            }
        } while (i.next(true) != null);
        return i.current();
    }
View Full Code Here

                getWit(sgfp).addMark(WIT.CIRCLE);
        }

        // mark next move (if not marked in props)
        if (showNextMove) {
            SGFNodeIterator i = lastMoveNode.iterator();
            if (i.next(true) != null) {
                try {
                    SGFMove m = i.current().getMoveProperty();
                    if (isInView(m) && (getWit(m).bits & WIT.PURE_MARK) == 0)
                        getWit(m).addMark(WIT.CIRCLE);
                } catch (SGFPropertyNotFoundException e) {
                    // DO NOTHING
                }
            }
        }

        // mark ko move (if not marked in props)
        if (game.koMove >= 0) {
            SGFPoint sgfp = game.board.xy(game.koMove);
            if (isInView(sgfp) && (getWit(sgfp).bits & WIT.PURE_MARK) == 0)
                getWit(sgfp).addMark(WIT.SQUARE);
        }

        // mark variations
        if (showVariants) {
            SGFNodeIterator i = lastMoveNode.iterator();
            i.firstVariant(true);
            do {
                SGFNode node = i.current();
                if (node != lastMoveNode)
                    try {
                        SGFMove m = node.getMoveProperty();
                        if (isInView(m))
                            getWit(m).addMark(WIT.VARIANT);
                    } catch (SGFPropertyNotFoundException e) {
                        // DO NOTHING
                    }
            } while (i.nextVariant(true) != null);
        }

        // mark hints
        if (showMoveHints) {
            SGFNodeIterator i = lastMoveNode.iterator();
            if (i.next(true) != null) {
                i.firstVariant(true);
                do {
                    SGFNode node = i.current();
                    try {
                        SGFMove m = node.getMoveProperty();
                        if (isInView(m)) {
                            if ((node.bits & SGFNode.GOOD_MOVE) != 0)
                                getWit(m).addMark(WIT.GOOD_MOVE);
                            else if ((node.bits & SGFNode.BAD_MOVE) != 0)
                                getWit(m).addMark(WIT.BAD_MOVE);
                            else
                                getWit(m).addMark(WIT.VARIANT);
                        }
                    } catch (SGFPropertyNotFoundException e) {
                        // DO NOTHING
                    }
                } while (i.nextVariant(true) != null);
            }
        }

        if (showMoyo) {
            bmap.setup(game.board);
View Full Code Here

            //    or no hints for next node
            //    or first fail node we want to see immediately
            // else show color to play indicator

            if (drawSolvedFailedIndicator) {
                SGFNodeIterator nextMoves = node.iterator();
                int bits = 0;
                if (nextMoves.next(true) != null) {
                    nextMoves.firstVariant(true);
                    do {
                        bits |= nextMoves.current().bits;
                    } while (nextMoves.nextVariant(true) != null);
                }

                //#ifdef debug
                System.out.println("Next move bits: " + bits);
                //#endif
View Full Code Here

        boolean doPlay() {
            try {
                if (!freePlay) {
                    // This code doesn't allow to play undocumented moves
                    //
                    SGFNodeIterator i = game.kifuLastMove().iterator();
                    if (i.next(true) == null)
                        return false;

                    // search for appropriate variant
                    SGFMove newMove = new SGFMove(cursorX, cursorY, game.colorToPlay);
                    SGFMove variantMove;
                    i.firstVariant(true);
                    do {
                        variantMove = i.current().getMoveProperty();
                    } while (!newMove.equals(variantMove) && i.nextVariant(true) != null);

                    if (!newMove.equals(variantMove))
                        return false;
                }

                game.play(cursorX, cursorY);
                snd.playStoneSound(game.colorToPlay == Board.WHITE);
                // show player's move
                notifyGameUpdated();

                if (!probStarted) {
                    if (probCurrent != null)
                        probCurrent.tried++;
                    probStarted = true;
                    statusScreen.setProblem(probCurrent, probEnum);
                }

                if (gobanCanvas.showMoveHints)
                    setCursorToNextGoodMove();
                else {
                    hasRespondMove = false;

                    SGFNodeIterator i = game.kifuLastMove().iterator();
                    if (i.next(true) != null) {
                        // play randomized response move

                        i.firstVariant(true);
                        int nGoodVariants = 0;
                        int nBadVariants = 0;

                        do {
                            if ((i.current().bits & SGFNode.GOOD_MOVE) != 0)
                                nGoodVariants++;
                            else if ((i.current().bits & SGFNode.BAD_MOVE) != 0)
                                nBadVariants++;
                        } while (i.nextVariant(true) != null);

                        // play response if there is good/bad hint
                        if (nGoodVariants > 0 || nBadVariants > 0) {
                            int k;
                            int mask;
                            Random rand = new Random();

                            if (nGoodVariants > 0) {
                                k = rand.nextInt(nGoodVariants);
                                mask = SGFNode.GOOD_MOVE;
                            } else {
                                k = rand.nextInt(nBadVariants);
                                mask = SGFNode.BAD_MOVE;
                            }
                            // k = 0 is valid good variant number, so in while it's >= 0
                            do {
                                if ((i.current().bits & mask) != 0)
                                    k--;
                            } while (k >= 0 && i.prevVariant(true) != null);

                            SGFMove variantMove = i.current().getMoveProperty();
                            game.play(variantMove.x, variantMove.y, variantMove.color);
                            snd.playStoneSound(game.colorToPlay == Board.WHITE);
                            // show response
                            notifyGameUpdated();

                            hasRespondMove = true;
                        }
                    }
                }

                if (!probFinished) {
                    SGFNodeIterator i = game.kifuLastMove().iterator();
                    boolean haveGoodMove = (i.current().bits & SGFNode.GOOD_MOVE) != 0;
                    probFinished = i.next(false) == null || !haveGoodMove;
                    if (probFinished) {
                        if (haveGoodMove) {
                            if (probCurrent != null)
                                probCurrent.solved++;
                            statusScreen.setProblem(probCurrent, probEnum);
View Full Code Here

        SGFNode n2v1 = new SGFNode();
        SGFNode n2v2 = new SGFNode();
        n1.add(n2v1);
        n1.add(n2v2);

        SGFNodeIterator iterator = n2v2.iterator();
        assertSame(n2v2, iterator.current());
        assertSame(n1, iterator.prev(true));
        assertSame(null, iterator.prev(true));
        assertSame(n2v2, iterator.next(true));
        assertSame(null, iterator.next(true));
        assertSame(n2v1, iterator.prevVariant(true));
        assertSame(null, iterator.prevVariant(true));
        assertSame(n1, iterator.prev(true));
        assertSame(n2v1, iterator.next(true));
        assertSame(n2v2, iterator.nextVariant(true));
        assertSame(null, iterator.nextVariant(true));
    }
View Full Code Here

                case Canvas.KEY_NUM6:
                    notify |= game.next();
                    break;
                case Canvas.KEY_NUM7:
                    {
                        SGFNodeIterator i = game.kifuLastMove().iterator();
                        // skip to node with some variants
                        while (i.prev(true) != null
                                && i.nextVariant(false) == null
                                && i.prevVariant(false) == null)
                            // DO NOTHING
                            ;
                        if (!(i.nextVariant(false) == null && i.prevVariant(false) == null)
                                && i.current() != game.kifuLastMove()) {
                            // play game to found node
                            while (i.current() != game.kifuLastMove())
                                game.prev();
                            notify = true;
                        }
                    }
                    break;
                case Canvas.KEY_NUM8:
                    notify |= game.nextVariant();
                    break;
                case Canvas.KEY_NUM9:
                    {
                        SGFNodeIterator i = game.kifuLastMove().iterator();
                        // skip to node with some variants
                        while (i.next(true) != null
                                && i.nextVariant(false) == null
                                && i.prevVariant(false) == null)
                            // DO NOTHING
                            ;
                        if (!(i.nextVariant(false) == null && i.prevVariant(false) == null)
                                && i.current() != game.kifuLastMove()) {
                            // play game to found node
                            while (i.current() != game.kifuLastMove())
                                game.next();
                            notify = true;
                        }
                    }
                    break;
View Full Code Here

        SGFNode n3 = new SGFNode();
        n1.add(n2v1);
        n1.add(n2v2);
        n2v1.add(n3);

        SGFNodeIterator it = n1.iterator();
        assertSame(n2v1, it.next(true));
        assertSame(n2v2, it.nextVariant(true));
        assertSame(n2v1, it.prevVariant(true));
        assertSame(n3, it.next(true));
    }
View Full Code Here

                getWit(sgfp).addMark(WIT.CIRCLE);
        }

        // mark next move (if not marked in props)
        if (showNextMove) {
            SGFNodeIterator i = lastMoveNode.iterator();
            if (i.next(true) != null) {
                try {
                    SGFMove m = i.current().getMoveProperty();
                    if (isInView(m) && (getWit(m).bits & WIT.PURE_MARK) == 0)
                        getWit(m).addMark(WIT.CIRCLE);
                } catch (SGFPropertyNotFoundException e) {
                    // DO NOTHING
                }
            }
        }

        // mark ko move (if not marked in props)
        if (game.koMove >= 0) {
            SGFPoint sgfp = game.board.xy(game.koMove);
            if (isInView(sgfp) && (getWit(sgfp).bits & WIT.PURE_MARK) == 0)
                getWit(sgfp).addMark(WIT.SQUARE);
        }

        // mark variations
        if (showVariants) {
            SGFNodeIterator i = lastMoveNode.iterator();
            i.firstVariant(true);
            do {
                SGFNode node = i.current();
                if (node != lastMoveNode)
                    try {
                        SGFMove m = node.getMoveProperty();
                        if (isInView(m))
                            getWit(m).addMark(WIT.VARIANT);
                    } catch (SGFPropertyNotFoundException e) {
                        // DO NOTHING
                    }
            } while (i.nextVariant(true) != null);
        }

        // mark hints
        if (showMoveHints) {
            SGFNodeIterator i = lastMoveNode.iterator();
            if (i.next(true) != null) {
                i.firstVariant(true);
                do {
                    SGFNode node = i.current();
                    try {
                        SGFMove m = node.getMoveProperty();
                        if (isInView(m)) {
                            if ((node.bits & SGFNode.GOOD_MOVE) != 0)
                                getWit(m).addMark(WIT.GOOD_MOVE);
                            else if ((node.bits & SGFNode.BAD_MOVE) != 0)
                                getWit(m).addMark(WIT.BAD_MOVE);
                            else
                                getWit(m).addMark(WIT.VARIANT);
                        }
                    } catch (SGFPropertyNotFoundException e) {
                        // DO NOTHING
                    }
                } while (i.nextVariant(true) != null);
            }
        }

        if (showMoyo) {
            bmap.setup(game.board);
View Full Code Here

            //    or no hints for next node
            //    or first fail node we want to see immediately
            // else show color to play indicator

            if (drawSolvedFailedIndicator) {
                SGFNodeIterator nextMoves = node.iterator();
                int bits = 0;
                if (nextMoves.next(true) != null) {
                    nextMoves.firstVariant(true);
                    do {
                        bits |= nextMoves.current().bits;
                    } while (nextMoves.nextVariant(true) != null);
                }

                //#ifdef debug
                System.out.println("Next move bits: " + bits);
                //#endif
View Full Code Here

TOP

Related Classes of com.sgfj.SGFNodeIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.