-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController2048.java
More file actions
388 lines (326 loc) · 8.75 KB
/
Controller2048.java
File metadata and controls
388 lines (326 loc) · 8.75 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package game2048;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Controller2048 extends TimerTask implements KeyListener{
public static final int NUMBER_OF_STARTING_BLOCKS = 2;
public static int ARRAY_WIDTH = 7;
public static int ARRAY_HEIGHT = ARRAY_WIDTH;
public static int FRAME_WIDTH = 67*ARRAY_WIDTH;
public static int FRAME_HEIGHT = 67*ARRAY_HEIGHT + 60;
//Key_Codes for keyListener below
public static final int UP_ARROW = 38;
public static final int DOWN_ARROW = 40;
public static final int RIGHT_ARROW = 39;
public static final int LEFT_ARROW = 37;
public static final int NUMPAD_6 = 102;
public static final int NUMPAD_5 = 0;
public static final int NUMPAD_4 = 100;
public static final int NUMPAD_2 = 98;
public static final int NUMPAD_8 = 104;
public static final int KEY_W = 87;
public static final int KEY_D = 68;
public static final int KEY_S = 83;
public static final int KEY_A = 65;
public static final int KEY_SPACE = 32;
public static final int KEY_ESC = 27;
//These are tracking variables used in the game.
public static boolean gameIsReady;
public static int time;
public int timerTime = 0;
static GameBoard myGame;
public static boolean finished;
public static Graphics g;
public boolean inMainMenu;
public MainMenu menu;
public static JFrame menuFrame;
public static Dimension dim;
public static JLabel bar;
public static int barHeight;
public static final int TIME_BETWEEN_MOVES = 700;//Time = 1 second
public static final int MAX_TIME_TO_MOVE = 6000;//Time = 6 seconds.
public static long currentTime = System.currentTimeMillis();//This will be used to track max time between moves
//Keeps track of game type Original = 0, Russian = 1
public static final int RUSSIAN_GAME = 1;
public static final int ORIGINAL_GAME = 0;
public static int gameType = 1;
public static JFrame gameFrame2048;
private static Container contentPane2048;
private java.util.Timer universalGameTimer = new java.util.Timer();
public static int score = 0;
public Controller2048(String JFrameTitle, int locationX, int locationY, int windowWidth, int windowHeight) {
menu = new MainMenu();
menuFrame = menu.getFrame();
menuFrame.setVisible(true);
gameFrame2048 = new JFrame(JFrameTitle);
gameFrame2048.setVisible(false);
//Get screen size
dim = Toolkit.getDefaultToolkit().getScreenSize();
//Set gameFrame2048 to the middle of the screen
gameFrame2048.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame2048.setResizable(false);
//Create content pane to which the game is played
contentPane2048 = gameFrame2048.getContentPane();
contentPane2048.setLayout(null);
contentPane2048.setBackground(Color.GRAY);
universalGameTimer.schedule(this,0,TIME_BETWEEN_MOVES);
contentPane2048.addKeyListener(this);
contentPane2048.setFocusable(true);
((JComponent) contentPane2048).setOpaque(true);
bar = new JLabel();
contentPane2048.add(bar);
bar.setBackground(Color.BLUE);
bar.setOpaque(true);
bar.setVisible(true);
bar.setForeground(Color.WHITE);
bar.setFont(bar.getFont().deriveFont(32.0f));
barHeight = 60;
}
public static void newGame()
{
menuFrame.setVisible(false);
//Now to reset sizing variables
if(gameType==ORIGINAL_GAME && ARRAY_WIDTH == 4)
{
FRAME_WIDTH = 67*ARRAY_WIDTH + 5;
FRAME_HEIGHT = 67*ARRAY_HEIGHT + barHeight + 28;
}
else if(gameType==ORIGINAL_GAME && ARRAY_WIDTH == 8)
{
FRAME_WIDTH = 67*ARRAY_WIDTH + 5;
FRAME_HEIGHT = 67*ARRAY_HEIGHT + barHeight + 28;
}
else if(gameType== RUSSIAN_GAME && ARRAY_WIDTH == 5)
{
FRAME_WIDTH = 67*ARRAY_WIDTH + 5;
FRAME_HEIGHT = 67*ARRAY_HEIGHT + barHeight + 28;
}
else if(gameType== RUSSIAN_GAME && ARRAY_WIDTH == 7)
{
FRAME_WIDTH = 67*ARRAY_WIDTH + 5;
FRAME_HEIGHT = 67*ARRAY_HEIGHT + barHeight + 28;
}
gameFrame2048.getContentPane().setSize(FRAME_WIDTH, FRAME_HEIGHT - barHeight - 22);
gameFrame2048.setSize(FRAME_WIDTH, FRAME_HEIGHT);
gameFrame2048.setLocation(dim.width/2-FRAME_WIDTH/2, dim.height/2-FRAME_HEIGHT/2);
bar.setSize(FRAME_WIDTH, barHeight);
bar.setLocation(0,0);
//Create the new game
if(gameType == ORIGINAL_GAME)
{
if(myGame != null)
{
myGame = null;
}
gameFrame2048.getContentPane().removeAll();
myGame = new OriginalGameBoard(gameFrame2048, ARRAY_WIDTH, ARRAY_HEIGHT);
}
else
{
if(myGame != null)
{
myGame = null;
}
gameFrame2048.getContentPane().removeAll();
myGame = new RussianGameBoard(gameFrame2048,ARRAY_WIDTH, ARRAY_HEIGHT);
}
gameFrame2048.getContentPane().add(bar);
finished = false;
gameIsReady = true;
time = 0;
gameFrame2048.setVisible(true);
myGame.draw();
score = 0;
}
@Override
public void run()
{
if(gameType == ORIGINAL_GAME)
{
System.out.println("");
if(finished && gameIsReady)
{
gameIsReady = false;
System.out.println("Game Over");
gameOver();
}
else if(gameIsReady)
{
myGame.draw();
time++;
score = myGame.getScore();
bar.setText("Score: " + score);
if(time >= 10)
{
time = 0;
System.out.println(myGame.isFull());
if(!myGame.isFull())
{
myGame.populate();
}
((OriginalGameBoard) myGame).reduceScore(100);
}
if(myGame.isGameOver())
{
finished = true;
}
}
}
else //Russian 2048
{
if(finished && gameIsReady)
{
gameIsReady = false;
System.out.println("Game Over");
gameOver();
}
else if (gameIsReady)
{
myGame.draw();
time++;
score = myGame.getScore();
bar.setText("Score: " + score);
if(gameType == RUSSIAN_GAME)
{
if(myGame.isGameOver())
{
finished = true;
}
else
{
if(myGame.getNeedToPopulate()== true)
{
myGame.populate();
myGame.setNeedToPopulate(false);
}
else
{
myGame.fall();
}
}
}
}
}
}
@Override
public void keyPressed(KeyEvent e)
{
//if a key is pressed, the timer starts over.
time = 0;
//get the code of the pressed key
int keyPressed_Code = e.getKeyCode();
//use keyPressed_Code
if(gameType == ORIGINAL_GAME && !finished && gameIsReady)
{
switch(keyPressed_Code) {
case UP_ARROW:
case NUMPAD_8:
case KEY_W:
myGame.moveUp();
myGame.combineUp();
break;
case DOWN_ARROW:
case NUMPAD_2:
case KEY_S:
myGame.moveDown();
myGame.combineDown();
break;
case RIGHT_ARROW:
case NUMPAD_6:
case KEY_D:
myGame.moveRight();
myGame.combineRight();
break;
case LEFT_ARROW:
case NUMPAD_4:
case KEY_A:
myGame.moveLeft();
myGame.combineLeft();
break;
case KEY_SPACE:
break;
case KEY_ESC:
break;
}
}
//Russian Game Key
else if(!finished && gameIsReady)
{
switch(keyPressed_Code) {
case UP_ARROW:
case NUMPAD_8:
case KEY_W:
System.out.println("Up");
myGame.moveUp();
break;
case DOWN_ARROW:
case NUMPAD_2:
case KEY_S:
System.out.println("Down");
((RussianGameBoard) myGame).dropBlock();
break;
case RIGHT_ARROW:
case NUMPAD_6:
case KEY_D:
System.out.println("Right");
((RussianGameBoard) myGame).moveRight();
break;
case LEFT_ARROW:
case NUMPAD_4:
case KEY_A:
System.out.println("Left");
((RussianGameBoard) myGame).moveLeft();
break;
case KEY_SPACE:
break;
case KEY_ESC:
break;
}
}
myGame.draw();
}
@Override
public void keyReleased(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
Controller2048 myController = new Controller2048("2048", 50,50, FRAME_WIDTH, FRAME_HEIGHT);
}
public static void gameOver()
{
String[] buttons = {"Go to Main Menu", "Play Again!"};
System.out.println("Prompt");
int playOrMain = JOptionPane.showOptionDialog(null, "Your final score was " + score + "!\nWould you like to play again or return to the main menu?",
"Game Over", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0]);
System.out.println("Prompt2");
if(playOrMain == RUSSIAN_GAME)
{
System.out.println("New Game");
newGame();
gameFrame2048.setVisible(false);
gameFrame2048.setVisible(true);
}
else
{
System.out.println("Main Menu");
gameFrame2048.setVisible(false);
menuFrame.setVisible(true);
}
}
}