-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Globals.pde
163 lines (139 loc) · 3.64 KB
/
A_Globals.pde
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
/*
* Globals are stored in this class to keep in one location
* Would run through static class, but is a bit tricky in Processing
*
* File name appended with A_ because Processing files are included in alphabetical order
* As such, globals here should have no dependencies on other custom classes
*
* Total grid is 500 x 500
* Grid is 25 x 25 with boxes of size 20
* Defined more in SnakeClass
*/
// --- CONSTANTS ---
final int BOARD_WIDTH = 500; // Need to modify here and directly in snake.pde if desired
final int BOARD_HEIGHT = 500;
final int PLAY_BUTTON_DIAM = 70;
final int PLAY_BUTTON_WIDTH = 100;
final int PLAY_BUTTON_HEIGHT = 70;
final int VIEW_BOARD_BUTTON_HEIGHT = 30;
final int SCORE_HEIGHT = 50;
final int BOARD_CENTER_X = BOARD_WIDTH / 2;
final int BOARD_CENTER_Y = (BOARD_HEIGHT - SCORE_HEIGHT) / 2 + SCORE_HEIGHT;
final color FOOD_COLOR = color(250, 50, 50);
final int BOARD_SIZE = 25;
final String[] SPEED_TEXT = {"Easy", "Medium", "Hard", "Sanic", "Neural", "Brute"};
final Integer[] SPEED_VALS = {10, 9, 8, 6, 5, 5};
final char[] DIRECTIONS = {'U', 'D', 'L', 'R'};
// Padding around each individual snake pixel
final int PADDING = 2;
// Size of each snake segment
final int SEG_SIZE = 20-(2*PADDING);
// Beginning length of the snake
final int START_LENGTH = 5;
// Amount the snake grows upon eating food
final int GROW_AMT = 3;
// --- VARIABLES ---
Snake snake = new Snake ();
Neural NAI = new Neural();
Brute BAI = new Brute();
SnakePoint food = randomFood();
Boolean playing = false;
Boolean snakeDied = false;
Boolean viewBoard = false;
Integer score = 0;
Integer highScore = 0;
HashMap<String, Integer> highScoreMap = initHighScoreMap();
Integer speed = -1;
String speedText = "";
// --- UTIL FUNCTIONS ---
SnakePoint randomFood () {
int x = 0;
int y = 0;
do {
x = int(random(25));
y = int(random(25));
} while (snake.hitBody(x, y));
return new SnakePoint(x, y, FOOD_COLOR);
}
void newFood() {
food = randomFood();
}
HashMap<String, Integer> initHighScoreMap () {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String s: SPEED_TEXT) {
map.put(s, new Integer(0));
}
return map;
}
// Get a random elem from a passed list. If the list is of size 0, return the passed default.
Object randomElem (ArrayList<?> selection, Object def) {
int randIndex = (int)floor(random(selection.size()));
return selection.size() > 0 ? selection.get(randIndex) : def;
}
char rightDir (char d) {
switch (d) {
case 'U':
return 'R';
case 'D':
return 'L';
case 'R':
return 'D';
case 'L':
return 'U';
default:
println("Oopsy whoopsy");
return d;
}
}
char leftDir (char d) {
switch (d) {
case 'U':
return 'L';
case 'D':
return 'R';
case 'R':
return 'U';
case 'L':
return 'D';
default:
println("Oopsy whoopsy");
return d;
}
}
Boolean isOpposite (char x, char y) {
return (x == 'U' && y == 'D') ||
(x == 'D' && y == 'U') ||
(x == 'R' && y == 'L') ||
(x == 'L' && y == 'R');
}
char getOpposite (char c) {
for (char dir : DIRECTIONS) {
if (isOpposite(c, dir)) {
return dir;
}
}
return '\0';
}
int[] getNextCoords (int x, int y, char dir) {
switch (dir) {
case 'U':
y -= 1;
break;
case 'D':
y += 1;
break;
case 'R':
x += 1;
break;
case 'L':
x -= 1;
break;
default:
println("Oopsy whoopsy");
}
return new int[] {x, y};
}
// Check if the given x and y will hit a wall
public Boolean hitWall (int x, int y) {
return x < 0 || x > (BOARD_SIZE-1) || y < 0 || y > (BOARD_SIZE-1);
}