-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameFieldPanel.java
More file actions
228 lines (190 loc) · 6.95 KB
/
GameFieldPanel.java
File metadata and controls
228 lines (190 loc) · 6.95 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Lead Author:
* Ian Kligman; 5550139764
*
* References:
* Gaddis, T. (2015). Starting Out With Java Myprogramming Lab
* From Control Structures Through Objects. (6th ed.). Addison-Wesley.
*
*
* Version: 12/12/2022
*/
/**
* |-----------------------------------------------------------|
* | GameFieldPanel |
* |-----------------------------------------------------------|
* | Responsibilities | Collaborating Classes |
* |-------------------------------+---------------------------|
* | Create the game tiles | TreasureButtonListener |
* | | TreasureGameView |
* | | EmptyButtonListener |
* | | GameFieldPanel |
* | Randomly assign Treasure | GameInfoPanel |
* | | TreasureButton |
* | | EmptyButton |
* | | |
* |-------------------------------+---------------------------|
*/
// Required library imports for GUI and event listeners.
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class GameFieldPanel extends JPanel
{
private GameInfoPanel info;
private LastMovePanel lastMove;
private final int NUMBER_OF_TILES = 25;
private EmptyButton[] buttons = new EmptyButton[25];
private int[] treasurePositionArray = new int[10];
private int[] trollPositionArray = new int[5];
private int tries = 20;
private int foundTreasures = 0;
private int remainingTreasures = 10;
private int totalPoints = 0;
// Purpose: Checks the inputted array for the inputed number. Returns true if the array has the number.
public static boolean contains(int[] inputArray, int number)
{
for (int index = 0; index < inputArray.length; index++)
{
if (inputArray[index] == number)
return true;
}
return false;
}
// Purpose: Returns a random array of 10 random numbers from 1-25.
public int[] makeTreasureArray()
{
int[] randomArray = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int random;
for (int index = 0; index < randomArray.length; index++)
{
do
{
random = new Random().nextInt(NUMBER_OF_TILES);
}
while (contains(randomArray, random));
randomArray[index] = random;
}
return randomArray;
}
/* Purpose: Returns a random array of 5 random numbers from 1-25 that aren't already in the treasure
* position array.
*/
public int[] makeTrollArray()
{
int[] randomArray = {-1,-1,-1,-1,-1};
int random;
for (int index = 0; index < randomArray.length; index++)
{
do
{
random = new Random().nextInt(NUMBER_OF_TILES);
}
while (contains(randomArray, random) || contains(treasurePositionArray, random));
randomArray[index] = random;
}
return randomArray;
}
/* Purpose: Constructor sets the layout and assigns random positions to the 10 treasure buttons. Each button
* is added to an array of buttons as it gets created. Constructor takes a LastMovePanel and GameInfoPanel.
*/
public GameFieldPanel(GameInfoPanel inputInfo, LastMovePanel inputLastMove)
{
info = inputInfo;
lastMove = inputLastMove;
setLayout(new GridLayout(5,5));
treasurePositionArray = makeTreasureArray();
trollPositionArray = makeTrollArray();
for(int i=0; i<NUMBER_OF_TILES; i++)
{
if (contains(treasurePositionArray, i))
{
TreasureButton treasure = new TreasureButton(info);
treasure.addActionListener(new TreasureButtonListener(treasure,this,lastMove));
add(treasure);
addButton(treasure, i);
}
else if (contains(trollPositionArray, i))
{
TrollButton troll = new TrollButton(info);
troll.addActionListener(new TrollButtonListener(troll,this,lastMove));
add(troll);
addButton(troll, i);
}
else
{
EmptyButton empty = new EmptyButton(info);
empty.addActionListener(new EmptyButtonListener(empty,this,lastMove));
add(empty);
addButton(empty, i);
}
}
}
// Purpose: Allows for the creation of the JButton array. Used in the constructor.
public void addButton(EmptyButton inputButton, int index)
{
buttons[index] = inputButton;
}
// Purpose: At the end of the game, disable all buttons and use new icons to reveal any missed treasure.
public void endButtons()
{
for(int i = 0; i<25; i++)
{
if(buttons[i].isEnabled())
buttons[i].reveal();
buttons[i].setEnabled(false);
}
}
// Purpose: Decrement the number of tries with input validation and check for both endgame conditions.
public void reduceNumberOfTries()
{
if(tries > 0)
{
tries--;
info.setTries("Tries left: ", tries);
}
if(foundTreasures == 10)
{
endGame();
}
if(tries == 0)
{
endGame();
}
}
// Purpose: Adjust counters on the info panel when a treasure is found with input validation.
public void foundTreasure(TreasureButton currentButton)
{
if(remainingTreasures > 0)
remainingTreasures--;
info.setRemaining("Treasures left: ", remainingTreasures);
if(foundTreasures < 10)
foundTreasures++;
info.setFound("Treasures found: ", foundTreasures);
totalPoints += currentButton.getPointsValue();
info.setPoints("Points: ", totalPoints);
reduceNumberOfTries();
}
// Purpose: Set found treasures to 0 when a troll is encountered and update the tries and info panel.
public void foundTroll()
{
foundTreasures = 0;
totalPoints = 0;
info.setFound("Treasures found: ", foundTreasures);
info.setPoints("Points :", totalPoints);
reduceNumberOfTries();
}
// Purpose: Disable buttons for the end game and determine if the result was a win or a loss.
public void endGame()
{
endButtons();
if(foundTreasures == 10)
{
lastMove.youWin();
}
else
{
lastMove.gameOver();
}
}
}