⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sgfadapter2.java

📁 Java编写的小游戏扫雷代码,可以在多种环境下运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    if ((ab_pos2-ab_pos) < 4) {
                        break;
                    }
                    char xx = prop.charAt(ab_pos + 1);
                    char yy = prop.charAt(ab_pos + 2);
                    char tip = prop.charAt(ab_pos + 4);
                    move.addTip(xx-'a', yy-'a', tip);
//                    System.out.println("prop is " + prop);
//                    System.out.println("---------tip found");
                    ab_pos = ab_pos2 + 1;
                    if (ab_pos >= prop.length()) {
                        break;
                    }
                }                
            }
            else if (prop.startsWith("SQ[")) {
                int ab_pos = 2;
                
                while (true) {
                    int ab_pos2 = prop.indexOf(']', ab_pos);
                    if (ab_pos2 < 0) {
                        break;
                    }
                    if ((ab_pos2-ab_pos) != 3) {
                        break;
                    }
                    char xx = prop.charAt(ab_pos + 1);
                    char yy = prop.charAt(ab_pos + 2);
                    move.addTip(xx-'a', yy-'a', GoPoint.SQUARE);
                    
                    ab_pos = ab_pos2 + 1;
                    if (ab_pos >= prop.length()) {
                        break;
                    }
                }                
            }
            else if (prop.startsWith("TR[")) {
                int ab_pos = 2;
                
                while (true) {
                    int ab_pos2 = prop.indexOf(']', ab_pos);
                    if (ab_pos2 < 0) {
                        break;
                    }
                    if ((ab_pos2-ab_pos) != 3) {
                        break;
                    }
                    char xx = prop.charAt(ab_pos + 1);
                    char yy = prop.charAt(ab_pos + 2);
                    move.addTip(xx-'a', yy-'a', GoPoint.TRIANGLE);
                    
                    ab_pos = ab_pos2 + 1;
                    if (ab_pos >= prop.length()) {
                        break;
                    }
                }                
            }
            else if (prop.startsWith("SZ[")) {
                int ab_pos = prop.indexOf(']');
                match.boardSize = Integer.parseInt(prop.substring(3, ab_pos));
            }
            else if (prop.startsWith("PB[")) {
                int ab_pos = prop.indexOf(']');
                match.blackName = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("PW[")) {
                int ab_pos = prop.indexOf(']');
                match.whiteName = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("BR[")) {
                int ab_pos = prop.indexOf(']');
                match.blackRank = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("WR[")) {
                int ab_pos = prop.indexOf(']');
                match.whiteRank = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("EV[")) {
                int ab_pos = prop.indexOf(']');
                match.matchName = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("DT[")) {
                int ab_pos = prop.indexOf(']');
                match.matchDate = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("PC[")) {
                int ab_pos = prop.indexOf(']');
                match.place = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("RE[")) {
                int ab_pos = prop.indexOf(']');
                match.result = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("SO[")) {
                int ab_pos = prop.indexOf(']');
                match.source = prop.substring(3, ab_pos);
            }
            else if (prop.startsWith("TM[")) {
                int ab_pos = prop.indexOf(']');
                match.timeLen = Integer.parseInt(prop.substring(3, ab_pos));
            }
            
            t_pos = skipSpaces(source, t_pos);
            if (t_pos < 0) {
                break;
            }
            source = source.substring(t_pos);
        }
        
        
        return move;
    }
    
    
    private int getNextProperty(String source) {
//        System.out.println(source);
        int in_name = 0;
        int in_value = 1;
        int out_value = 2;
        
        int state = in_name;
        int t_pos = 0;
        
        char c = source.charAt(t_pos);
        while (true) {
            if (c == '[') {
                if (state == in_value) {
                    return -1;
                }
                state = in_value;
            }
            else if (c == ']') {
                if (state != in_value) {
                    return -1;
                }
                state = out_value;
            }
            else {
                if (state == out_value) {
                    break;
                }
            }
            t_pos++;
            if (t_pos >= source.length()) {
                break;
            }
            c = source.charAt(t_pos);
        }
        
        return t_pos;        
    }
    
    /**
     * @return:     null    no item found
     *              string  string for this item, like "B[as]LB[gh:c]C[...]"
     */
    private String getNextItem() {
        StringBuffer buf = new StringBuffer();
        char c;
        
        while(true) {
            pos = skipSpaces(src, pos);
            if (pos < 0) {
                return null;
            }
                                
            c = src.charAt(pos);
            
            if (c == '(') {
//                System.out.println("pushing one movement to stack!");
                forks.push(current);
                pos++;
            }
            else if (c == ')') {
                while(src.charAt(pos) == ')') {
//                System.out.println("poping one movement from stack!");
                    current = (Movement) forks.pop();
                    pos++;
                    if (pos >= src.length()) {
                        return null;
                    }
                }
            }
            else if (c == ';') {
                pos++;
                break;
            }
            else {
                return null;
            }
        }    
        pos = skipSpaces(src, pos);
        if (pos < 0) {
            return null;
        }
        
        c = src.charAt(pos);
        while ((c != ';') && (c != '(') && (c != ')')) {
            if (c == '[') {
                int end = src.indexOf(']', pos);
                if (end < pos) {
                    return null;
                }
                buf.append(src.substring(pos, end+1));
                pos = end;
            }
            else {
                buf.append(c);
            }
            pos++;
            if (pos >= src.length()) {
                break;
            }
            c = src.charAt(pos);
        }
        
        return buf.toString();
        
    }
    
    private int skipSpaces(String source, int position) {
        if (position >= source.length()) {
            return -1;
        }
        char c = source.charAt(position);
        while ((c == ' ') || (c == '\t') || (c == '\n')) {
            position++;
            if (position >= source.length()) {
                return -1;
            }
            c = source.charAt(position);
        }        
        return position;
    }

/**************************************************************************************
 *
 * private methods for write sgf file
    private String comment;
    private Vector tips;

    protected int col, row;
    protected int player;

    private Vector captives;
    private Vector forcePut; *
 **************************************************************************************/
    private String movementTreeToString(Movement root) {
        String output = "";
        System.out.println("saving movement!");

        output += movementToString(root);
        
        if (root.getChildCount() == 0) {
            output += ")\n";
            return output;
        }
        else if (root.getChildCount() == 1) {
            output += ";\n";
            output += movementTreeToString((Movement)root.getChildAt(0));
            return output;
        }
        else {
            for (int i = 0; i < root.getChildCount(); i++) {
                output += "\n(;";
                output += movementTreeToString((Movement)root.getChildAt(i));
            }
            output += ")\n";
        }
        System.out.println("movement string is " + output);
        return output;
    }

    private String movementToString(Movement move) {
        
        String output = "";
        
        // write move
        if ((move.col != -1) && (move.row != -1)) {
            if (move.player == GoPlayer.BLACK) {
                output += ("B[" + (char)(move.col+'a') + (char)(move.row+'a') + "]");
            }
            else {
                output += ("W[" + (char)(move.col+'a') + (char)(move.row+'a') + "]");
            }
        }
        
        // write comment
        if (move.getComment() != null) {
            output += ("C[" + move.getComment() + "]");
        }
        
        // write tips
        for (int i = 0; i < move.getTipCount(); i++) {
            Movement.HandTip t = move.tipAt(i);
            if (t.tip == GoPoint.TRIANGLE) {
                output += ("TR[" + (char)(t.col+'a') + (char)(t.row+'a') + "]");
            }
            else if (t.tip == GoPoint.CIRCLE) {
                output += ("CR[" + (char)(t.col+'a') + (char)(t.row+'a') + "]");
            }
            else if (t.tip == GoPoint.CROSS) {
                output += ("MA[" + (char)(t.col+'a') + (char)(t.row+'a') + "]");
            }
            else if (t.tip == GoPoint.SQUARE) {
                output += ("SQ[" + (char)(t.col+'a') + (char)(t.row+'a') + "]");
            }
            else {
                output += ("LB[" + (char)(t.col+'a') + (char)(t.row+'a') + ":" + (char)t.tip + "]");
            }
        }
        
        //write force put
        for (int i = 0; i < move.getForceCount(); i++) {
            Movement.HandTip t = move.forceAt(i);
            if (t.tip == GoPlayer.BLACK) {
                output += ("AB[" + (char)(t.col+'a') + (char)(t.row+'a') + "]");
            }
            else {
                output += ("AW[" + (char)(t.col+'a') + (char)(t.row+'a') + "]");
            }
        }        
        
        return output;
    }
        
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -