public class BreakthroughGame
extends java.lang.Object
Constructor and Description |
---|
BreakthroughGame() |
Modifier and Type | Method and Description |
---|---|
static void |
main(java.lang.String[] args)
Provide a text-based interface for playing the game of Breakthrough making use of the
BreakthroughState class. |
static java.lang.String |
stateToString(BreakthroughState state)
Given a
BreakthroughState object, return the String that represents the current game state. |
static int[] |
stringToMove(java.lang.String moveStr)
Given a move
String in the format "[a-h][1-8]-[a-h][1-8]", return an int array with the 0-based
row moved from, column moved from, row moved to, and column moved to. |
public static java.lang.String stateToString(BreakthroughState state)
BreakthroughState
object, return the String
that represents the current game state.
The WHITE
, empty (i.e. NONE
), and BLACK
positions are
represented by 'O', '-', and 'X', respectively. Rows are labeled on the left and right with row numbers descending from
8 to 1. (Note that this is one greater than the row number of the internal representation. Columns are labeled
on the top and bottom from left to right with column letters 'a' through 'h'. Columns and labels are space-separated.
Below the board, a single line indicate the player to play or the player that has won with one of the following messages:
String
corresponding to the initial game state would look like:
a b c d e f g h 8 X X X X X X X X 8 7 X X X X X X X X 7 6 - - - - - - - - 6 5 - - - - - - - - 5 4 - - - - - - - - 4 3 - - - - - - - - 3 2 O O O O O O O O 2 1 O O O O O O O O 1 a b c d e f g h O to play.
state
- - the game state to represent as a String
String
representation of the game statepublic static int[] stringToMove(java.lang.String moveStr)
String
in the format "[a-h][1-8]-[a-h][1-8]", return an int array with the 0-based
row moved from, column moved from, row moved to, and column moved to. For example, the String
"a2-a3" would result in the returned int array {0, 1, 0, 2}. An illegal move that does not match the
given move format will result in the return of an int array of length 0.moveStr
- a move String
in the format "[a-h][1-8]-[a-h][1-8]"String
does
not match the legal move format, return an int array of length 0.public static void main(java.lang.String[] args)
BreakthroughState
class.
Initially, print two lines with "BREAKTHROUGH" and "Please enter moves using the format [a-h][1-8]-[a-h][1-8]. Enter \"undo\" to undo a move.".
Then, for each turn of the game, print the board using stateToString
to generate the board String
.
Print a blank line, prompt the user for a move with "Move? ", and read the user input.
If the user input is "undo", either revert to the previous game state or, if there is none, print "Cannot undo."
Otherwise, if the move has an illegal input format, print the message "Illegal move format. Please enter moves using the format [a-h][1-8]-[a-h][1-8].".
If the move has a legal input format but is an illegal move, print "Illegal move.".
Otherwise, the move is taken. (Note: The return value of BreakthroughState
class makeMove
method provides illegal move feedback.)
Once the game ends, the board is printed one last time.args
- (unused)