-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
203 lines (190 loc) · 5.01 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define MAX 20
#define MAXCALDA 399
void setUp();
void map_draw();
void input();
void logic();
int gameOver = 0; // loop controller
int x, y; // position of snake head
int score; // player score
int encX, encY; // value for increment the player position
int comX, comY; // food position
int calX[MAXCALDA], calY[MAXCALDA], caldaT; // array for tail in x and y, size of tail
int wallCollision;
int record; // For the height score
FILE *data; // file data
// Function for setup everything
void setUp(){
data = fopen("data", "rb");
if(data == NULL){
printf("Fail to open the score file!");
exit(1);
}
/*record = 10;
fwrite(&record,sizeof(int),1,data);*/
fread(&record, sizeof(int), 1, data);
fclose(data);
srand(time(NULL));
x = MAX / 2;
y = MAX / 2;
encX = 0;
encY = 0;
comX = rand() % (MAX - 1);
comY = rand() % (MAX - 1);
score = 0;
caldaT = 0;
wallCollision = 0; // Enable wall collision
}
// Draw the map
void map_draw(){
int i, j, k;
for(i = 0; i < MAX + 2; i++){
printf("#");
}
printf("\n");
for(i = 0; i < MAX; i++){
for(j = 0; j < MAX; j++){
if(j == 0){
printf("#");
}
if(j == x && i == y){
printf("O");
}
else if(j == comX && i == comY){
printf("T");
}else{
int rend_space = 0;
for(k = 0; k < caldaT; k++){
if(calX[k] == j && calY[k] == i){
printf("o");
rend_space = 1;
}
}
if(rend_space != 1){
printf(" ");
}
}
if(j == MAX - 1){
printf("#");
}
}
printf("\n");
}
for(i = 0; i < MAX + 2; i++){
printf("#");
}
printf("\nScore: %d", score);
printf("\nRecord: %d", record);
}
// input of keyboard
void input(){
char comando;
while(_kbhit()){
switch(comando = getch()){
case 'w':
encX = 0;
encY = -1;
break;
case 's':
encX = 0;
encY = 1;
break;
case 'a':
encX = -1;
encY = 0;
break;
case 'd':
encX = 1;
encY = 0;
break;
case 'z':
gameOver = 1;
break;
}
}
}
// Game logic
void logic(){
int i;
int posCalX, posCalY; // the position of the tail behind the head
int posSeguiCalX, posSeguiCalY; // help variable for change the positions
posCalX = x;
posCalY = y;
for(i = 0; i < caldaT; i++){ // like a "bubbleSort" for tail caldas
posSeguiCalX = posCalX;
posSeguiCalY = posCalY;
posCalX = calX[i];
posCalY = calY[i];
calX[i] = posSeguiCalX;
calY[i] = posSeguiCalY;
}
x += encX;
y += encY;
if(!wallCollision){
if (x < 0)
x = MAX - 1;
if(x > MAX - 1)
x = 0;
if(y < 0)
y = MAX - 1;
if(y > MAX - 1)
y = 0;
}else{
if ((x < 0) || (x > MAX - 1) || (y < 0) || (y > MAX - 1))
gameOver = 1;
}
if(x == comX && y == comY){
score += 10;
comX = rand() % (MAX - 1);
comY = rand() % (MAX - 1);
caldaT++;
}
for(i = 0; i < caldaT; i++){
if (calX[i] == x && calY[i] == y){
gameOver = 1;
}
}
}
// main function
int main()
{
setUp();
// Game loop
while(gameOver != 1){
input();
logic();
map_draw();
Sleep(80);
system("cls");
}
if(score > record){
data = fopen("data", "wb");
if(data == NULL) {
printf("Fail in save the new RECORD!\n");
exit(1);
}
fwrite(&score, sizeof(int), 1, data);
fclose(data);
printf("********************\n");
printf("*** NEW RECORD ***\n");
printf("********************\n");
printf("NEW RECORD: %d\n\n", score);
system("pause");
}
else{
printf("!!!!!!!!!!!!!!!!!\n");
printf("!!! GAME OVER !!!\n");
printf("!!!!!!!!!!!!!!!!!\n");
printf("SCORE: %d\n", score);
if(score != record){
printf("RECORD: %d\n", record);
}
system("pause");
}
return 0;
}