Reputation: 3
I am working on a board style game in java. currently the board is initialized in a 2d array. A player can make a move by enter the color of his chip plus his move so for example by entering: "W c 3" W = the color of the chip/player c is the letter corresponding to the column and 6 is the row. I need to be able to get the values from the string and update the board's row and column. So " a 1" should be row =1 col = 1. "b 1" should be row = 1 col = 2 "e 5" would be row = 5 col = 5 as an example.
How would I go about doing something like that?
here is my code in my move.java class if this helps: The method i'm working on is the Move (String str) method.
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
***Keep in mind that the string variable (str) is being populated by this code:
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;
}
Upvotes: 0
Views: 999
Reputation: 2548
You can do it this way (comments explain it):
// 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]);
Upvotes: 0
Reputation: 1047
Do it in the following order:
Assuming you have an input of the form:
String input = new String("W c 3");
1.Parse the input with split():
String color = input.split(" ")[0];
String column = input.split(" ")[1];
2.For the int variable, call the method parseInt()
int row = Integer.parseInt(input.split(" ")[2]);
Upvotes: 0
Reputation: 234875
If your string is strictly of the form "a b" where a is in the range a a - z, and b is a number in the range 0 - 9 then do something like
/* s is your string */
int row = s.charAt(0) - 'a' + 1;
int col = s.charAt(2) - '0' + 1;
where I've exploited the ASCII character number values and the fact that 'a' is a byte data type.
Of course, in production, you ought to pre-validate s
(check its length, whether or not the second character is a space etc. You could even use a regular expression check via Java's String.matches
method). All you'd have to do is check if
s.matches("[a-z] [0-9]")
is true. My method is a throwback to the good old C days.
Upvotes: 1
Reputation: 4855
I would do this:
Upvotes: 0