-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
143 lines (130 loc) · 3.49 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
#include <stdio.h>
#include <stdlib.h>
#include "snake.h"
snakeb *insert(snakeb *c)
{
snakeb *new_node;
new_node = (snakeb *) malloc(sizeof(snakeb));
if(new_node==NULL)
{
printf("%s\n","Not available memory!" );
exit(1);
}
new_node->next = c;
return new_node;
}
void insert_end()
{
//It adds at the end of the snake a new node!
snakeb *new_node,*tmp;
if((new_node = (snakeb *) malloc(sizeof(snakeb)))==NULL)
{
printf("%s\n","Not available memory!" );
exit(1);
}
tmp=head;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=new_node;
new_node->y=0;
new_node->x=0;
new_node->part='x';
new_node->next=NULL;
}
void delTail()
{
//It deletes the last node of the tail!
snakeb *tmp,*ptr;
length-=2;
ptr=head;
while(ptr->next->next!=NULL)
ptr=ptr->next;
tmp=ptr->next;
table[tmp->y][tmp->x]='.';
free(tmp);
ptr->next=NULL;
}
void createSnake()
{
//It creates the snake.It places it in a random position and with a random direction!
snakeb *tmp;
int i,j;
head = insert(head);
head = insert(head);
head = insert(head);
head->y = rand() % n;
head->x = rand() % m;
head->part = 'O';
while(1)
{
if(head->y>=2 && head->x>=2 && head->y < n-2 && head->x<m-2)
{
if(table[head->y][head->x]!='#' && table[head->y][head->x]!=fruit )
{
if(table[head->y+2][head->x]!='#' && table[head->y+2][head->x]!=fruit && table[head->y+1][head->x]!='#' && table[head->y+1][head->x]!=fruit)
{
table[head->y][head->x] = head->part;
head->next->y=head->y+1;
head->next->x=head->x;
head->next->part='x';
table[head->y+1][head->x] = head->next->part ;
head->next->next->y=head->y+2;
head->next->next->x=head->x;
head->next->next->part='x';
table[head->y+2][head->x] = 'x' ;
break;
}
else if(table[head->y-2][head->x]!='#' && table[head->y-2][head->x]!=fruit && table[head->y-1][head->x]!='#' && table[head->y-1][head->x]!=fruit)
{
table[head->y][head->x] = head->part;
head->next->y=head->y-1;
head->next->x=head->x;
head->next->part='x';
table[head->y-1][head->x] = head->next->part ;
head->next->next->y=head->y-2;
head->next->next->x=head->x;
head->next->next->part='x';
table[head->y-2][head->x] = 'x' ;
break;
}
else if(table[head->y][head->x+2]!='#' && table[head->y][head->x+2]!=fruit && table[head->y][head->x+1]!='#' && table[head->y][head->x+1]!=fruit)
{
table[head->y][head->x] = head->part;
head->next->y=head->y;
head->next->x=head->x+1;
head->next->part='x';
table[head->y][head->x+1] = head->next->part ;
head->next->next->y=head->y;
head->next->next->x=head->x+2;
head->next->next->part='x';
table[head->y][head->x+2] = 'x' ;
break;
}
else if(table[head->y][head->x-2]!='#' && table[head->y][head->x-2]!=fruit && table[head->y][head->x-1]!='#' && table[head->y][head->x-1]!=fruit)
{
table[head->y][head->x] = head->part;
head->next->y=head->y;
head->next->x=head->x-1;
head->next->part='x';
table[head->y][head->x-1] = head->next->part ;
head->next->next->y=head->y;
head->next->next->x=head->x-2;
head->next->next->part='x';
table[head->y][head->x-2] = 'x' ;
break;
}
}
}
head->x = rand() % n;
head->y = rand() % m;
}
}
void freeSnake()
{
snakeb *tmp;
while(head!=NULL){
tmp=head;
head=head->next;
free(tmp);
}
}