0

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

4 回答 4

1

如果您的字符串严格采用“a b”形式,其中 a 在 aa - z 范围内,b 是 0 - 9 范围内的数字,则执行类似的操作

/* s is your string */
int row = s.charAt(0) - 'a' + 1;
int col = s.charAt(2) - '0' + 1;

我利用了 ASCII 字符数字值以及“a”是字节数据类型这一事实。

当然,在生产中,你应该预先验证s(检查它的长度,第二个字符是否是空格等。你甚至可以通过Java的String.matches方法使用正则表达式检查)。您所要做的就是检查是否

s.matches("[a-z] [0-9]")

是真的。我的方法是回到过去的 C 时代。

于 2013-10-10T19:47:25.187 回答
0

我会这样做:

  • 使用空格作为分隔符拆分
  • “abcdefg....xyz”中的 indexOf(字母)
  • Integer.parseInt(数字)
于 2013-10-10T19:45:08.630 回答
0

请按以下顺序进行:

假设您输入了以下表单:

String input = new String("W c 3");

1.使用 split() 解析输入:

String color = input.split(" ")[0];
String column = input.split(" ")[1];

2.对于int变量,调用方法parseInt()

int row = Integer.parseInt(input.split(" ")[2]);
于 2013-10-10T19:46:20.660 回答
0

你可以这样做(评论解释它):

// create an array of the alphabet from a - z
public String[] alpha = {"a", "b", "c", "d", "e"}; //...etc.

// here is your value
String input = "e 5";

// split it at the space
String[] split = input.split(" ");

// find it in the array and add 1 to get your row (because arrays start at 0)
int row = Arrays.asList(alpha).indexOf(split[0]) + 1;

// get the column as well
int column = Integer.parseInt(split[1]);
于 2013-10-10T19:50:50.243 回答