Players alternate turns. During a turn a player flips over two cards, one at a time. If the cards match, the player removes them from the board and flips over two more cards. The player's turn continues as long as the player keeps finding matches. When the player has flipped over two cards that don't match, the player's turn ends and the non-matching cards are flipped back (so the face value and suit is no longer visible).
The game ends when all the cards have been removed from the board. The player with the most number of matches wins.
Card and Deck classes, if possible.Card class to the GameCard classGameCards to represent the
board.toString methods for instantiable
classes so the board may be printed easily.BufferedReader object to get the
players' names and choices for which cards to flip over.The javadoc for all the classes.
Given:
BufferedReader stdin; stdin = new BufferedReader(new InputStreamReader(System.in)); Board board = new Board();
Read in the players' names:
String player1Name, player2Name;
System.out.print("Enter player 1's name: ");
player1Name = stdin.readLine();
System.out.print("Enter player 2's name: ");
player2Name = stdin.readLine();
Let a player choose the first card to flip over:
int row1, col1;
GameCard card1 = null;
boolean askAgain = true;
do {
System.out.print("\tChoose card: row ");
row1 = Integer.parseInt(stdin.readLine()) - 1;
System.out.print("\t column ");
col1 = Integer.parseInt(stdin.readLine()) - 1;
if (!board.invalidBoardPosition(row1, col1)) {
card1 = board.getCard(row1, col1);
askAgain = false;
}
else {
System.out.println("ERROR: invalid board position");
}
} while (askAgain);
card1.flip();
System.out.print(board);