-
Notifications
You must be signed in to change notification settings - Fork 1
/
Concentration.java
executable file
·154 lines (129 loc) · 4.76 KB
/
Concentration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.Random;
/**
* The game of Concentration (also called Memory or Match Match)
*
* Create a gameboard of tiles. Each tile contains a card that has exactly
* one match on the board. Cards are originally show "face down" on the board.
*
* Player chooses two random cards from the board. The chosen cards
* are temporarily shown face up. If the cards match, they are removed from board.
*
* Play continues, matching two cards at a time, until all cards have been matched.
*/
public class Concentration extends Board
{
// create the game board
private Tile[][] gameboard = makeBoard();
/**
* The constructor for the game. Creates the 2-dim gameboard
* by populating it with tiles.
*/
public Concentration() {
gameboard = makeBoard();
String[] cards = getCards();
// Fisher–Yates shuffle
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
Random rand = new Random();
for (int swapA = cards.length - 1; swapA > 0; swapA--) {
int swapB = rand.nextInt(swapA + 1);
String cardA = cards[swapA];
String cardB = cards[swapB];
cards[swapA] = cardB;
cards[swapB] = cardA;
}
// place on board
int cardIndex = 0;
for (int row = 0; row < gameboard.length; row++) {
for (int col = 0; col < gameboard[0].length; col++) {
gameboard[row][col] = new Tile(cards[cardIndex]);
cardIndex++;
}
}
}
/**
* Determine if the board is full of cards that have all been matched,
* indicating the game is over
*
* Precondition: gameboard is populated with tiles
*
* @return true if all pairs of cards have been matched, false otherwse
*/
public boolean allTilesMatch() {
for (int row = 0; row < gameboard.length; row++) {
for (int col = 0; col < gameboard[0].length; col++) {
Tile t = gameboard[row][col];
if (!t.matched()) {
return false;
}
}
}
return true;
}
/**
* Check for a match between the cards in the two tile locations.
* For matched cards, remove from the board. For unmatched cares, them face down again.
*
* Precondition: gameboard is populated with tiles,
* row values (i values) must be in the range of 0 to gameboard.length,
* column values (j values) must be in the range of 0 to gameboard[0].length
*
* @param row1 the row value of Tile 1
* @param column1 the column value of Tile 1
* @param row2 the row value of Tile 2
* @param column2 the column vlue of Tile 2
* @return a message indicating whether or not a match occured
*/
public String checkForMatch(int row1, int column1, int row2, int column2) {
Tile tile1 = gameboard[row1][column1];
Tile tile2 = gameboard[row2][column2];
if (tile1.equals(tile2)) {
tile1.foundMatch();
tile2.foundMatch();
return "MATCH";
} else {
tile1.faceUp(false);
tile2.faceUp(false);
return "no match sorry";
}
}
/**
* Set tile to show its card in the face up state
*
* Precondition: gameboard is populated with tiles,
* row values (i values) must be in the range of 0 to gameboard.length,
* column values (j values) must be in the range of 0 to gameboard[0].length
*
* @param row the row value of Tile
* @param column the column value of Tile
*/
public void showFaceUp (int row, int column) {
gameboard[row][column].faceUp(true);
}
/**
* Returns a string representation of the board. A tab is placed between tiles,
* and a newline is placed at the end of a row
*
* Precondition: gameboard is populated with tiles
*
* @return a string represetation of the board
*/
public String toString() {
String boardString = "";
for (int row = 0; row < gameboard.length; row++) {
int numColumns = gameboard[row].length;
for (int col = 0; col < numColumns; col++) {
Tile t = gameboard[row][col];
if (t.isFaceUp()) {
boardString += t.getFace();
} else {
boardString += t.getBack();
}
if (col < numColumns - 1) {
boardString += "\t";
}
}
boardString += "\n";
}
return boardString;
}
}