-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsnake.c
264 lines (232 loc) · 6.17 KB
/
snake.c
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
#include <stdlib.h>
#include <curses.h>
#include <signal.h>
#include <sys/select.h>
#include <stdio.h>
#include <time.h>
#define DESIRED_WIDTH 70
#define DESIRED_HEIGHT 25
WINDOW * g_mainwin;
int g_oldcur, g_score = 0, g_width, g_height;
typedef struct {
int x;
int y;
} pos;
pos fruit;
// 2D array of all spaces on the board.
bool *spaces;
// --------------------------------------------------------------------------
// Queue stuff
// Queue implemented as a doubly linked list
struct s_node
{
pos *position; // **TODO: make this a void pointer for generality.
struct s_node *prev;
struct s_node *next;
} *front=NULL, *back=NULL;
typedef struct s_node node;
// Returns the position at the front w/o dequeing
pos* peek( )
{
return front == NULL ? NULL : front->position;
}
// Returns the position at the front and dequeues
pos* dequeue( )
{
node *oldfront = front;
front = front->next;
return oldfront->position;
}
// Queues a position at the back
void enqueue( pos position )
{
pos *newpos = (pos*) malloc( sizeof( position ) );
node *newnode = (node*) malloc( sizeof( node ) );
newpos->x = position.x;
newpos->y = position.y;
newnode->position = newpos;
if( front == NULL && back == NULL )
front = back = newnode;
else
{
back->next = newnode;
newnode->prev = back;
back = newnode;
}
}
// --------------------------------------------------------------------------
// End Queue stuff
// --------------------------------------------------------------------------
// Snake stuff
// Writes text to a coordinate
void snake_write_text( int y, int x, char* str )
{
mvwaddstr( g_mainwin, y , x, str );
}
// Draws the borders
void snake_draw_board( )
{
int i;
for( i=0; i<g_height; i++ )
{
snake_write_text( i, 0, "X" );
snake_write_text( i, g_width-1, "X" );
}
for( i=1; i<g_width-1; i++ )
{
snake_write_text( 0, i, "X" );
snake_write_text( g_height-1, i, "X" );
}
snake_write_text( g_height+1, 2, "Score:" );
}
// Resets the terminal window and clears up the mem
void snake_game_over( )
{
free( spaces );
while( front )
{
node *n = front;
front = front->next;
free( n );
}
endwin();
exit(0);
}
// Is the current position in bounds?
bool snake_in_bounds( pos position )
{
return position.y < g_height - 1 && position.y > 0 && position.x < g_width - 1 && position.x > 0;
}
// 2D matrix of possible positions implemented with a 1D array. This maps
// the x,y coordinates to an index in the array.
int snake_cooridinate_to_index( pos position )
{
return g_width * position.y + position.x;
}
// Similarly this functions maps an index back to a position
pos snake_index_to_coordinate( int index )
{
int x = index % g_width;
int y = index / g_width;
return (pos) { x, y };
}
// Draw the fruit somewhere randomly on the board
void snake_draw_fruit( )
{
attrset( COLOR_PAIR( 3 ) );
int idx;
do
{
idx = rand( ) % ( g_width * g_height );
fruit = snake_index_to_coordinate( idx );
}
while( spaces[idx] || !snake_in_bounds( fruit ) );
snake_write_text( fruit.y, fruit.x, "F" );
}
// Handles moving the snake for each iteration
bool snake_move_player( pos head )
{
attrset( COLOR_PAIR( 1 ) ) ;
// Check if we ran into ourself
int idx = snake_cooridinate_to_index( head );
if( spaces[idx] )
snake_game_over( );
spaces[idx] = true; // Mark the space as occupied
enqueue( head );
g_score += 10;
// Check if we're eating the fruit
if( head.x == fruit.x && head.y == fruit.y )
{
snake_draw_fruit( );
g_score += 1000;
}
else
{
// Handle the tail
pos *tail = dequeue( );
spaces[snake_cooridinate_to_index( *tail )] = false;
snake_write_text( tail->y, tail->x, " " );
}
// Draw the new head
snake_write_text( head.y, head.x, "S" );
// Update scoreboard
char buffer[25];
sprintf( buffer, "%d", g_score );
attrset( COLOR_PAIR( 2 ) );
snake_write_text( g_height+1, 9, buffer );
}
int main( int argc, char *argv[] )
{
int key = KEY_RIGHT;
if( ( g_mainwin = initscr() ) == NULL ) {
perror( "error initialising ncurses" );
exit( EXIT_FAILURE );
}
// Set up
srand( time( NULL ) );
noecho( );
curs_set( 2 );
halfdelay( 1 );
keypad( g_mainwin, TRUE );
g_oldcur = curs_set( 0 );
start_color( );
init_pair( 1, COLOR_RED, COLOR_BLACK );
init_pair( 2, COLOR_GREEN, COLOR_BLACK );
init_pair( 3, COLOR_YELLOW, COLOR_BLACK );
init_pair( 4, COLOR_BLUE, COLOR_BLACK );
init_pair( 5, COLOR_CYAN, COLOR_BLACK );
init_pair( 6, COLOR_MAGENTA, COLOR_BLACK );
init_pair( 7, COLOR_WHITE, COLOR_BLACK );
getmaxyx( g_mainwin, g_height, g_width );
g_width = g_width < DESIRED_WIDTH ? g_width : DESIRED_WIDTH;
g_height = g_height < DESIRED_HEIGHT ? g_height : DESIRED_HEIGHT;
// Set up the 2D array of all spaces
spaces = (bool*) malloc( sizeof( bool ) * g_height * g_width );
snake_draw_board( );
snake_draw_fruit( );
pos head = { 5,5 };
enqueue( head );
// Event loop
while( 1 )
{
int in = getch( );
if( in != ERR )
key = in;
switch( key )
{
case KEY_DOWN:
case 'j':
case 'J':
case 's':
case 'S':
head.y++;
break;
case KEY_RIGHT:
case 'l':
case 'L':
case 'd':
case 'D':
head.x++;
break;
case KEY_UP:
case 'k':
case 'K':
case 'w':
case 'W':
head.y--;
break;
case KEY_LEFT:
case 'h':
case 'H':
case 'a':
case 'A':
head.x--;
break;
}
if( !snake_in_bounds( head ) )
snake_game_over( );
else
snake_move_player( head );
}
snake_game_over( );
}