-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
204 lines (175 loc) · 4.65 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
204
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Node defination
typedef struct Node {
int x;
int y;
struct Node* next;
} Node;
// Create a new Node
Node* createNode() {
Node* newNode = (Node*)malloc(sizeof(Node));
return newNode;
}
// Print List
void printList(Node* head) {
printf("\n");
while(head != NULL) {
printf(" (%d, %d) ", head->x, head->y);
head = head->next;
}
printf("\n");
}
// Print the board
void displayBoard(char** board, int m, int n) {
for(int i = 0; i <= m; i++) printf("##");
for(int i = 0; i < m; i++) {
printf("\n#");
for(int j = 0; j < n; j++) {
printf("%c ", board[i][j]);
}
printf("#");
}
printf("\n");
for(int i = 0; i <= m; i++) printf("##");
}
// Check if present on snake or not. 0 - No, 1 - Yes.
int checkOnSnake(Node* head, int x, int y) {
while(head != NULL) {
if(head->x == x && head->y == y) {
return 1;
}
head = head->next;
}
return 0;
}
// Generate Fruit
void generateFruit(char** board, int m, int n, Node* head) {
time_t t;
srand((unsigned) time(&t));
int x = 0;
int y = 0;
do {
x = rand() % m;
y = rand() % n;
}while(checkOnSnake(head, x, y));
board[x][y] = 'O';
}
// Initalize the board.
void initBoard(char** board, int m, int n) {
for(int i = 0 ; i < m ; i++) {
*(board + i) = (char*)calloc(n, sizeof(char));
}
for(int i = 0 ; i < m ; i++) {
for(int j = 0 ; j < n ; j++) {
board[i][j] = ' ';
}
}
}
//Intialize the snake
void initSnake(char** board, Node* head, int length) {
head->x = 0;
head->y = 0;
board[0][0] = '@';
for(int i = 1 ; i < length; i++) {
head->next = createNode();
head = head-> next;
head->x = 0;
head->y = i;
if(i == length -1)
board[0][i] = '*';
else
board[0][i] = '=';
}
}
// Remove last node
void removeLastNode(Node* head, char** board) {
while(head->next->next != NULL) {
head= head->next;
}
board[head->next->x][head->next->y] = ' ';
board[head->x][head->y] = '*';
head->next = NULL;
}
// Move helper
void helper(char** board, Node* head, int x1 , int y1) {
board[head->x][head->y] = '=';
Node* newNode = createNode();
newNode->x = head->x;
newNode->y = head->y;
newNode->next = head-> next;
head->next = newNode;
head->x = x1;
head->y = y1;
}
// Move the snake
int moveSnake(char** board, int m, int n, char move, Node* head) {
int x = 0 , y = 0;
switch(move) {
case 'w':
x = head->x == 0 ? m - 1 : head->x - 1;
y = head->y;
break;
case 'a':
x = head->x;
y = head->y == 0 ? n - 1 : head->y - 1;
break;
case 's':
x = (head->x + 1) % m;
y = head->y;
break;
case 'd':
x = head->x;
y = (head->y + 1) % m;
break;
default:
return 0;
}
if(x == head->next->x && y == head->next->y) return 0;
helper(board,head, x ,y);
int score = board[x][y] == 'O' ? 1 : 0;
if(!score) removeLastNode(head, board);
else generateFruit(board, m, n, head);
board[x][y] = '@';
if(checkOnSnake(head->next, x, y)) return -1;
return score;
}
// Driver code
int main()
{
char restart = 'n';
do{
system("clear");
int m = 15, n = 15;
int score = 0;
char** board = (char**)calloc(m, sizeof(char*)); // Create Board..
initBoard(board, m, n);
Node* head = createNode();
initSnake(board, head, 4);
generateFruit(board, m, n, head);
char move = 'A';
do {
printf("\n@ -head, * - tail\n");
printf("\nBoundaires are safe! They teleport you :P\n\n");
displayBoard(board, m, n);
//printList(head);
printf("\n\nw - Up, a - Left, d - Right, s - Down, q - Quit\n");
scanf(" %c", &move);
system("clear");
if(move != 'q') {
int res = moveSnake(board, m, n, move, head);
if(res == -1) {
printf("Yikes! You Ate Yourself!\n");
break;
}
score += res;
}
}while(move != 'q');
if(move == 'q') printf("You Quit!\n");
printf("Your score is : %d ", score);
printf("\n\nWant to Play Again? press r to restart!\n");
scanf(" %c", &restart);
}while(restart == 'r');
return 0;
}