我正在用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;
}