Java 如何解析字符串21点游戏结果
我正在开发我的新java应用程序。这是21点游戏分析器。 服务器将21点游戏结果发送给用户,我的应用程序对其进行解析,然后将游戏描述显示给用户。游戏结果如下所示:Java 如何解析字符串21点游戏结果,java,parsing,Java,Parsing,我正在开发我的新java应用程序。这是21点游戏分析器。 服务器将21点游戏结果发送给用户,我的应用程序对其进行解析,然后将游戏描述显示给用户。游戏结果如下所示: #N145。20(9♥️,6.♠️,J♠️,Q♦️) - 19(8♦️,A.♠️) 这里145是游戏号码,20是赌场的分数,19是玩家的分数。 这一行应该被解析为游戏对象。下面是游戏类: //import public class Game { private final int gameNumber; privat
#N145。20(9♥️,6.♠️,J♠️,Q♦️) - 19(8♦️,A.♠️)代码>
这里145是游戏号码,20是赌场的分数,19是玩家的分数。
这一行应该被解析为游戏对象。下面是游戏类:
//import
public class Game {
private final int gameNumber;
private final boolean casinoWon;
private final Set<Card> gamerDeck;
private final Set<Card> casinoDeck;
private final int gamerScore;
private final int casinoScore;
public Game(int gameNumber, boolean casinoWon,
Set<Card> gamerDeck, Set<Card> casinoDeck,
int gamerScore, int casinoScore) {
this.gameNumber = gameNumber;
this.casinoWon = casinoWon;
this.gamerDeck = gamerDeck;
this.casinoDeck = casinoDeck;
this.gamerScore = gamerScore;
this.casinoScore = casinoScore;
}
//getters
}
//导入
公开课游戏{
私人最终整数游戏号码;
私有最终布尔casinoWon;
私人终场游戏机;
私人最终设置赌场;
私人最终智力玩家核心;
私人赌场;
公共游戏(整数游戏号、布尔赌场、,
设置玩家标识,设置玩家标识,
int玩家核心,int赌场核心){
this.gameNumber=游戏编号;
this.casinoWon=casinoWon;
this.gamerDeck=gamerDeck;
this.casinoDeck=casinoDeck;
this.gamerScore=gamerScore;
this.casinoScore=casinoScore;
}
//吸气剂
}
我已经开始编写解析器,但我认为我做得不对。下面是一组代码:
//import
public class StringParser {
public Game parseStringToGame(String inputString) throws ParseException {
int gameNumber = -1;
boolean isCasinoWon = false;
int gamerScore = -1;
int casinoScore = -1;
String stringGamerDeck = "";
String stringCasinoDeck = "";
Iterator<Character> iterator = stringToCharacterList(inputString).iterator();
while(iterator.hasNext()) {
StringBuilder currentObject = new StringBuilder();
Character c = iterator.next();
if (c == '#')
c = iterator.next();
else
throw new ParseException();
if (c == 'N')
c = iterator.next();
else
throw new ParseException();
while(Character.isDigit(c)) {
currentObject.append(c);
c = iterator.next();
}
gameNumber = Integer.parseInt(currentObject.toString());
currentObject = new StringBuilder();
//c=='.'
if (c == '.')
c = iterator.next();
else
throw new ParseException();
//c==' '
if (c == ' ')
c = iterator.next();
else
throw new ParseException();
//to be continued
}
}
private List<Character> stringToCharacterList(String s) {
List<Character> characters = new LinkedList<>();
for (char c : s.toCharArray())
characters.add(c);
return characters;
}
}
//进口
公共类字符串分析器{
公共游戏ParseStringToName(字符串输入字符串)引发ParseException{
int gameNumber=-1;
布尔值IsCasinown=false;
int gamerScore=-1;
int casinoScore=-1;
字符串stringGamerDeck=“”;
字符串stringCasinoDeck=“”;
迭代器迭代器=stringToCharacterList(inputString).Iterator();
while(iterator.hasNext()){
StringBuilder currentObject=新建StringBuilder();
字符c=迭代器.next();
如果(c='#')
c=迭代器.next();
其他的
抛出新的ParseException();
如果(c='N')
c=迭代器.next();
其他的
抛出新的ParseException();
while(Character.isDigit(c)){
currentObject.append(c);
c=迭代器.next();
}
gameNumber=Integer.parseInt(currentObject.toString());
currentObject=新的StringBuilder();
//c=='。'
如果(c=='。)
c=迭代器.next();
其他的
抛出新的ParseException();
//c==''
如果(c='')
c=迭代器.next();
其他的
抛出新的ParseException();
//待续
}
}
私有列表stringToCharacterList(字符串s){
列表字符=新的LinkedList();
for(char c:s.toCharArray())
字符。添加(c);
返回字符;
}
}
如您所见,它看起来非常恶心。还有其他“高级”方法来解析字符串吗?如果游戏的输出总是固定的,您可以使用regexp来完成此任务
Pattern p = Pattern.compile("^#N(\\d*).\\s+(\\d*)(.+)\\s+-\\s+(\\d*)(.+)");
Matcher m = p.matcher(s);
m.find();
int gameNumber = Integer.valueOf(m.group(1));
int gamerScore = Integer.valueOf(m.group(2));
int casinoScore = Integer.valueOf(m.group(4));
boolean casinoWon = casinoScore - gamerScore > 0;
Set<Card> gamerDeck = parseDeck(m.group(3));
Set<Card> casinoDeck = parseDeck(m.group(5));
Pattern p=Pattern.compile(“^ N(\\d*)。\\s+(\\d*)(.+)\\s+-\\s+(\\d*)(.+)”;
匹配器m=匹配器p;
m、 查找();
int gameNumber=整数.valueOf(m.group(1));
int gamerScore=Integer.valueOf(m.group(2));
int casinoScore=Integer.valueOf(m.group(4));
布尔casinoWon=casinoScore-gamerScore>0;
Set gamerDeck=parseDeck(m.group(3));
Set casinoDeck=parseDeck(m.group(5));
您只需要实现parseDeck方法。字符串真的包含数字(Unicode字符?)还是纯文本字符串?如果可能的话,我会研究正则表达式,正确的话,您可以用一个表达式完成很多解析。如果我错了,请纠正我,但这不是9♥️, 6.♠️, J♠️, Q♦️代码>35而不是20?我知道,在21点规则中,J等于2,Q等于3,K等于4。Joakim Danielson,是的,这个字符串只包含Unicode字符。你说的“一个表达式”是什么意思?有趣的是,这里10,J,Q和K都有10的值。a是11或1