-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
160 lines (148 loc) · 4.22 KB
/
Copy pathBoard.java
File metadata and controls
160 lines (148 loc) · 4.22 KB
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
155
156
157
158
159
160
/**
* A game board of NxM board of tiles.
*
* @author PLTW
* @version 2.0
*/
/**
* A Board class for concentration
*/
public class Board
{
private static String[] tileValues = {"lion", "lion",
"penguin", "penguin",
"dolphin", "dolphin",
"fox", "fox",
"monkey", "monkey",
"turtle", "turtle"};
private Tile[][] gameboard = new Tile[3][4];
/**
* Constructor for the game. Creates the 2D gameboard
* by populating it with card values
*
*/
public Board()
{
String temp;
int rand;
for (int i = 0; i < tileValues.length; i++) {
rand = (int)(Math.random()*tileValues.length);
temp = tileValues[rand];
tileValues[rand] = tileValues[i];
tileValues[i] = temp;
}
int val = 0;
for(int r=0; r<gameboard.length; r++){
for(int c=0; c<gameboard[0].length; c++){
gameboard[r][c] = new Tile(tileValues[val]);
val++;
}
// System.out.println(java.util.Arrays.toString(gameboard[r]));
}
}
/**
* Returns a string representation of the board, getting the state of
* each tile. If the tile is showing, displays its value,
* otherwise displays it as hidden.
*
* Precondition: gameboard is populated with tiles
*
* @return a string represetation of the board
*/
public String toString()
{
String str = "";
for(Tile[] row : gameboard){
for(Tile card : row){
if(card.isShowingValue()){
str += card + "\t";
}
else{
str += card.getHidden() + "\t";
}
}
str += "\n";
}
return str;
}
/**
* Determines if the board is full of tiles that have all been matched,
* indicating the game is over.
*
* Precondition: gameboard is populated with tiles
*
* @return true if all tiles have been matched, false otherwse
*/
public boolean allTilesMatch()
{
for(Tile[] row : gameboard){
for(Tile card : row){
if(!card.matched())
return false;
}
}
return true;
}
/**
* Sets the tile to show its value (like a playing card face up)
*
* Preconditions:
* gameboard is populated with tiles,
* row values must be in the range of 0 to gameboard.length,
* column 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 showValue (int row, int column)
{
gameboard[row][column].show();
}
/**
* Checks if the Tiles in the two locations match.
*
* If Tiles match, show Tiles in matched state and return a "matched" message
* If Tiles do not match, re-hide Tiles (turn face down).
*
* Preconditions:
* gameboard is populated with Tiles,
* row values must be in the range of 0 to gameboard.length,
* column values must be in the range of 0 to gameboard[0].length
*
* @param row1 the row value of Tile 1
* @param col1 the column value of Tile 1
* @param row2 the row value of Tile 2
* @param col2 the column vlue of Tile 2
* @return a message indicating whether or not a match occured
*/
public String checkForMatch(int row1, int col1, int row2, int col2)
{
String msg = "";
if(gameboard[row1][col1].equals(gameboard[row2][col2])){
msg = "You got a match!";
gameboard[row1][col1].foundMatch();
gameboard[row2][col2].foundMatch();
}
else{
msg = "Not a match. Try again!";
gameboard[row1][col1].hide();
gameboard[row2][col2].hide();
}
return msg;
}
/**
* Checks the provided values fall within the range of the gameboard's dimension
* and that the tile has not been previously matched
*
* @param row the row value of Tile
* @param col the column value of Tile
* @return true if row and col fall on the board and the row,col tile is unmatched, false otherwise
*/
public boolean validateSelection(int row, int col)
{
if(gameboard.length-1 < row || gameboard[0].length-1 < col || gameboard[row][col].matched()){
return false;
}
return true;
}
}