Skip to content

Commit 54e114e

Browse files
author
Angel Jumbo
committed
bug fix: food does not spawns the snake body anymore
1 parent a7aec95 commit 54e114e

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ The classic snake game for the terminal that can plays itself and be use like a
33
[![asciicast](https://asciinema.org/a/477685.svg)](https://asciinema.org/a/477685)
44
## Installation
55

6-
(RN its only for linux)
76

87
```
98
make
109
make install
1110
```
1211

12+
ncurses is required.
13+
1314
## Usage
1415

1516
By default it will run like a normal snake game that you can control with wasd, hjkl(vi keys) or the arrow keys.
@@ -26,10 +27,7 @@ sssnake -S -s 15 -j 5
2627
```
2728
press Q to quit.
2829
Use the -h options to see the option details.
29-
30-
## Known bugs
31-
32-
- Sometimes the food spawns in the snake body.
30+
3331

3432
## Contributing
3533

main.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int main(int argc, char *argv[]) {
6161

6262
do { // This loop goes forever if the option "screensaver" is given.
6363

64-
// Used to know where the body of the snake and junk are,
64+
// Used to know where the body of the snake and the junk are,
6565
// blocksTaken is just a matrix of 2x2 but It can change its dimesions
6666
// dinamically if I ever decide to change them by detecting changes in the
6767
// dimensions of the terminal.
@@ -157,6 +157,14 @@ int main(int argc, char *argv[]) {
157157
direction = get_direction(c);
158158
update_position(snake, blocksTaken, &food, direction, maxX, maxY);
159159
}
160+
// xymap_print_log(blocksTaken, snake->head->x, snake->head->y,
161+
// snake->tail->x, snake->tail->y);
162+
163+
// FILE *fp;
164+
// fp = fopen("log.txt", "a+");
165+
166+
// fprintf(fp, "Food (%i,%i) \n", food.x, food.y);
167+
// fclose(fp);
160168

161169
if (snake->head->x == food.x && snake->head->y == food.y)
162170
rand_pos_food(&food, blocksTaken, maxX, maxY);

snake.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ static void move_body(Snake *snake, XYMap *blocksTaken, Point *food, int prevx,
4444
int prevy, int maxX, int maxY) {
4545
int prevx2, prevy2;
4646

47-
xymap_mark(blocksTaken, snake->head->x, snake->head->y, SBODY);
4847
// xymap_unmark(blocksTaken, snake->tail->x, snake->tail->y);
4948

5049
SnakePart *snakePart = snake->head->next;
@@ -74,8 +73,10 @@ static void move_body(Snake *snake, XYMap *blocksTaken, Point *food, int prevx,
7473
// xymap_mark(blocksTaken, snake->tail->x, snake->tail->y, SBODY);
7574
// rand_pos_food(food, blocksTaken, maxX, maxY);
7675
} else {
76+
// only unmark if the head haven't take the place of the tail
7777
xymap_unmark(blocksTaken, prevx, prevy);
7878
}
79+
xymap_mark(blocksTaken, snake->head->x, snake->head->y, SBODY);
7980
}
8081

8182
void update_position(Snake *snake, XYMap *blocksTaken, Point *food, int dir,

xymap.c

+36
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,39 @@ void xymap_print(XYMap *map) {
5151
printf("\n");
5252
}
5353
}
54+
55+
void xymap_print_log(XYMap *map, int headx, int heady, int tailx, int taily) {
56+
FILE *fp;
57+
58+
fp = fopen("log.txt", "a+");
59+
fprintf(fp, " ");
60+
for (int i = 0; i < map->maxX; i++)
61+
fprintf(fp, "%i ", i);
62+
63+
fprintf(fp, "\n");
64+
for (int i = 0; i < map->maxX; i++)
65+
fprintf(fp, "--");
66+
fprintf(fp, "\n");
67+
for (int j = 0; j < map->maxY; j++) {
68+
69+
fprintf(fp, "%i| ", j);
70+
for (int i = 0; i < map->maxX; i++) {
71+
if (map->arr[i + map->maxX * j] != 0) {
72+
if (i == headx && j == heady) {
73+
fprintf(fp, "h ");
74+
} else if (i == tailx && j == taily) {
75+
fprintf(fp, "t ");
76+
} else {
77+
fprintf(fp, "O ");
78+
}
79+
} else {
80+
fprintf(fp, " ");
81+
}
82+
// fprintf(fp, "%i ", map->arr[i + map->maxX * j]);
83+
}
84+
fprintf(fp, "\n");
85+
}
86+
87+
fprintf(fp, "\n");
88+
fclose(fp);
89+
}

xymap.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ void xymap_unmark(XYMap *map, int x, int y);
2222
int xymap_marked(XYMap *map, int x, int y);
2323
void xymap_print(XYMap *map);
2424

25+
void xymap_print_log(XYMap *map, int headx, int heady, int tailx, int taily);
2526
XYMap *xymap_copy(XYMap *map);
2627
#endif

0 commit comments

Comments
 (0)