Reputation: 65
I am very new to Java programming in general and have just started using 2D arrays. What I am trying to do is make a program that creates a chessboard 8x8, and asks the user to input strings (pieces) to be stored on the chessboard. For now, I dont want to check whether or not the piece is a valid chess piece, but I want to make sure that the placement is valid (i.e. when prompted to place a piece and the user chooses an invalid coordinate, the program will tell the user that its wrong). Otherwise, if the coordinate is right, it will store the string, which I will use another class to print the string(s) and output them, as well as where they are placed, to the user. Since it is a chess board, I am trying to convert 8x8 to 8 followed by a character from a-g.
Heres what I have so far:
public class ChessBoard {
public static void main(String[] args) {
char rows = 'a';
String spot;
int[][] grid = new int[8][8];
for (int i = 0; i < grid.length; i++, rows++) {
for (int col = 0; col < grid[i].length; col++) {
System.out.print(rows + "" + (col + 1) + " ");
}
System.out.println();
}
}
}
This prints the layout of the chessboard, but I am not sure how to ask the user for a string in a valid coordinate, say, e5, which can then be printed in another class.
I would want my expected output to be something like:
Enter a coordinate: a5
Enter a piece to be placed here: Queen
Continue until the user wishes to see what they have placed:
You have placed a Queen at a5
Any help would be appreciated, thanks!
Upvotes: 2
Views: 974
Reputation: 17945
To get the position from the user, you can use something like
// only once, to initialize the scanner
Scanner scanner = new Scanner(System.in);
// each time you need input from the user
String input = scanner.next();
To ask for a position, you can check whether the input matches what you expect, and request a new input otherwise:
String input = null; // will be changed to a valid position
boolean iLikeThisInput = false; // will be true if position is valid
while ( ! iLikeThisInput) {
System.out.println("Enter a coordinate (for example, a5): ");
input = scanner.next();
iLikeThisInput = input.matches("[a-g][1-8]"); // this is a regular expression
};
// now we now that the input is valid
int col = (int)(input.charAt(0) - 'a'); // 'a' is column 0: 'a' - 'a' = 0.
int row = (int)(input.charAt(1) - '0'); // '0' is row 0: same trick
Regular expressions are probably new for you (if you are new to programming), but are very powerful to check if text matches a certain pattern, and found across all major programming languages.
The trick of substracting character codes (last 2 lines) works because the characters codes for digits 0123456789
and those for the alphabet abcd
... are in numerical/alphabetical order, so that writing (int)('b'-'a')
is the same as writing 1
.
You will just be missing the code to get the piece. A regular expression for a lot of words can be built easily by using |
:
input.matches("Bob|Robert|Rob"); // matches only Bob or Robert or Rob
The final piece is to convert from integers to piece-names and vice-versa. I recommend writing two methods:
public static String pieceToName(int piece) { /**/ }
and
public static int nameToPiece(String name) { /**/ }
Using the switch
statement, you can quickly implement both with your preferred encoding:
switch(name) {
case "Queen": return 9;
case "Pawn": return 1;
...
}
Note that if you start to build an actual chess-playing program, you will have to distinguish between white and black pieces. One way of doing it is to use positive numbers for one color, and negative for the other.
Good luck with your program!
Upvotes: 2