Concentration

Game Description

Concentration (aka Memory) is a card game for two players. The cards are laid face down on the playing surface (i.e. board) in a rectangular pattern. The goal is to find as many matches as possible. Two cards match if there face values are the same and their suits are the same color (red or black).

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.

Design

The javadoc for all the classes.

Selected code fragments

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);

Code for entire program