📄 diceparser.java
字号:
/*JDice: Java Dice Rolling ProgramCopyright (C) 2006 Andrew D. Hilton (adhilton@cis.upenn.edu)J2ME version modifications by Teemu Vaattovaara (t8vate00@students.oamk.fi) This program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package J2MEDice;import java.util.*;import java.io.*;public class DiceParser { /* this is a helper class to manage the input "stream"*/ private static class StringStream { StringBuffer buff; public StringStream(String s) { buff = new StringBuffer(s); } private void munchWhiteSpace() { int index = 0; char curr; while (index < buff.length()) { curr = buff.charAt(index); if (curr != 0x09 && curr != 0x0A && curr != 0x0D && curr != 0x20) { break; } index++; } buff = buff.delete(0, index); } public boolean isEmpty() { munchWhiteSpace(); return buff.toString().equals(""); } public Integer getInt() { return readInt(); } public Integer readInt() { int index = 0; char curr; munchWhiteSpace(); while (index < buff.length()) { curr = buff.charAt(index); if (!Character.isDigit(curr)) { break; } index++; } try { String stringBuffer = buff.toString(); Integer ans = new Integer(Integer.parseInt((String) stringBuffer.substring(0, index))); buff = buff.delete(0, index); return ans; } catch (Exception e) { return null; } } public Integer readSgnInt() { munchWhiteSpace(); StringStream state = save(); if (checkAndEat("+")) { Integer ans = readInt(); if (ans != null) { return ans; } restore(state); return null; } if (checkAndEat("-")) { Integer ans = readInt(); if (ans != null) { return new Integer( 0 - ans.intValue()); } restore(state); return null; } return readInt(); } public boolean checkAndEat(String s) { munchWhiteSpace(); String tempBuff = new String(buff); if (tempBuff.indexOf(s) == 0) { buff = buff.delete(0, s.length()); return true; } return false; } public StringStream save() { return new StringStream(buff.toString()); } public void restore(StringStream ss) { this.buff = new StringBuffer(ss.buff.toString()); } public String toString() { return buff.toString(); } } /** roll::= ndice ; roll | ndice xdice::= dice | N X dice dice::= die bonus? dtail XXXX| FA(die,bonus,N) dtail dtail::= & dice | <nothing> die::= (N)? dN bonus::= + N | -N **/ public static Vector parseRoll(String s) { StringStream ss = new StringStream(s.toLowerCase()); Vector v = parseRollInner(ss, new Vector()); if (ss.isEmpty()) { return v; } return null; } private static Vector parseRollInner(StringStream ss, Vector v) { Vector r = parseXDice(ss); if (r == null) { return null; } Vector tempV = DiceUtils.appendVectors(v, r); if (ss.checkAndEat(";")) { return parseRollInner(ss, tempV); } return tempV; } private static Vector parseXDice(StringStream ss) { StringStream saved = ss.save(); Integer x = ss.getInt(); int num; if (x == null) { num = 1; } else { if (ss.checkAndEat("x")) { num = x.intValue(); } else { num = 1; ss.restore(saved); } } DieRoll dr = parseDice(ss); if (dr == null) { return null; } Vector ans = new Vector(); for (int i = 0; i < num; i++) { ans.addElement(dr); } return ans; } /** * dice::= die (bonus?) dtail * XXXX| FA(die,bonus,N) dtail */ private static DieRoll parseDice(StringStream ss) { return parseDTail(parseDiceInner(ss), ss); } private static DieRoll parseDiceInner(StringStream ss) { /*if(checkAndEat("FA(")) { DieRoll d=parseFA(ss); if(d==null) return null; return parseDTail(d,ss); }*/ Integer num = ss.getInt(); int dsides; int ndice; if (num == null) { ndice = 1; } else { ndice = num.intValue(); } if (ss.checkAndEat("d")) { num = ss.getInt(); if (num == null) { return null; } dsides = num.intValue(); } else { return null; } num = ss.readSgnInt(); int bonus; if (num == null) { bonus = 0; } else { bonus = num.intValue(); } return new DieRoll(ndice, dsides, bonus); } private static DieRoll parseDTail(DieRoll r1, StringStream ss) { if (r1 == null) { return null; } if (ss.checkAndEat("&")) { DieRoll d2 = parseDice(ss); return parseDTail(new DiceSum(r1, d2), ss); } else { return r1; } } private static void test(String s) { Vector v = parseRoll(s); int i; if (v == null) { System.out.println("Failure:" + s); } else { System.out.println("Results for " + s + ":"); for (i = 0; i < v.size(); i++) { DieRoll dr = (DieRoll) v.elementAt(i); System.out.print(v.elementAt(i)); System.out.print(": "); System.out.println(dr.makeRoll()); } } } public static void main(String[] args) { test("d6"); test("2d6"); test("d6+5"); test("4X3d8-5"); test("12d10+5 & 4d6+2"); test("d6 ; 2d4+3"); test("4d6+3 ; 8d12 -15 ; 9d10 & 3d6 & 4d12 +17"); test("4d6 + xyzzy"); test("hi"); test("4d4d4"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -