【发布时间】:2013-10-10 19:40:14
【问题描述】:
我正在用 java 开发一个棋盘风格的游戏。目前该板已在二维数组中初始化。玩家可以通过输入他的筹码颜色加上他的移动来进行移动,例如输入:“W c 3” W = 筹码的颜色/玩家 c 是对应于列的字母,6 是行。我需要能够从字符串中获取值并更新板的行和列。因此,“a 1”应该是 row = 1 col = 1。例如,“b 1”应该是 row = 1 col = 2 “e 5”应该是 row = 5 col = 5。
我将如何去做这样的事情?
如果这有帮助,这是我在 move.java 类中的代码:我正在处理的方法是 Move (String str) 方法。
public class Move implements Comparable<Move>{
static final int PASS_VALUE = 0;
int row, col;
boolean movement;
int pass;
int pos;
Board board;
/**
*
* Default constructor that initializes a pass move
*/
Move(){
row = PASS_VALUE;
col = PASS_VALUE;
}//Move default contructor
/**
*
* @param rowValue
* @param colValue
*/
Move(int rowValue, int colValue){
row = rowValue;
col = colValue;
}//Move constructor
/**
*
* @param oldMove -- Move to be copied
*/
Move(Move oldMove){
row = oldMove.row;
col = oldMove.col;
}//Move clone constructor
Move(String str) {
//int i = Integer.parseInt( str );
} //Move String constructor
Move (int positions) {
}//Move Positions constructor
/**
*
* @return string value of Move
*/
@Override
public String toString(){
String result ="";
String headers = " abcdefgh";
char colLetter = headers.charAt(col);
result = colLetter + " " + row;
return result;
}//toString
/**
*
* @param otherMove -- move to be compared
* @return
* -1 if this move precedes otherMove
* 0 if this move equals otherMove
* 1 if this move succeeds otherMove
*/
@Override
public int compareTo(Move otherMove){
return 0;
}//compareTo
boolean isAMove() {
return movement;
}
boolean isAPass(){
return row == PASS_VALUE;
}
}//Move
***请记住,此代码正在填充字符串变量 (str):
Move getOpponentMove(BufferedReader keyboard) throws IOException {
OthelloOut.printComment("Please enter your move");
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
String initializeStr = keyboard.readLine();
Move opponentMove = new Move(initializeStr);
return opponentMove;
}
【问题讨论】:
-
Integer.parseInt 可能是一个开始的地方..
-
W c 3...6 is the row? -
做什么?在游戏上工作?解析用户输入?重新绘制游戏板?
-
@Fareed Warrad;您可能想要改写您的标题,因为您所做的不仅仅是将字符串转换为整数
-
您可以使用空格作为分隔符进行拆分,有一个字符串“abcdef...yz”并从中获取索引以获取与您的字母对应的数字,并使用 parseInt 转换字符串到数字的整数。
标签: java