-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGameLauncher.java
More file actions
54 lines (44 loc) · 1.56 KB
/
Copy pathSnakeGameLauncher.java
File metadata and controls
54 lines (44 loc) · 1.56 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
import java.awt.Point;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import core.Context;
public final class SnakeGameLauncher {
private SnakeGameLauncher() {
}
public static void launch() {
SwingUtilities.invokeLater(() -> {
SnakeWorld world = new SnakeWorld(20, 20);
Context context = GameContextFactory.create(world);
SnakeGamePanel panel = new SnakeGamePanel(context, world);
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
});
}
public static String runSmokeTest() {
SnakeWorld world = new SnakeWorld(12, 12, new Random(7L));
Context context = GameContextFactory.create(world);
context.queueCommand(new DirectionCommand(world.getSnake(), Direction.DOWN));
context.executeQueuedCommands();
for (int step = 0; step < 5; step++) {
context.queueCommand(world);
context.executeQueuedCommands();
}
Point head = world.getSnake().getHead();
return "Snake smoke test passed: head="
+ head.x
+ ","
+ head.y
+ " score="
+ world.getScore()
+ " length="
+ world.getSnake().getLength()
+ " gameOver="
+ world.isGameOver();
}
}