Skip to content

Commit 3d5aa45

Browse files
committed
Separate two print method to two directory
1 parent 1cbd7b9 commit 3d5aa45

30 files changed

Lines changed: 647 additions & 0 deletions

c/Snake/clear_version/Move.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef _MOVE_H
2+
#define _MOVE_H
3+
#include "GlobalVar.h"
4+
#define moveBody() \
5+
{ \
6+
*p[n] = 0; \
7+
for (i = n; i > 0; i--) \
8+
{ \
9+
p[i] = p[i - 1]; \
10+
/* per part goes to the address of the next part of body*/ \
11+
} \
12+
*p[0] = BODY; \
13+
/* The First part of snake body come to snake head*/ \
14+
}
15+
16+
#define moveRight() \
17+
{ \
18+
moveBody(); \
19+
p[0] = p[0] + 1; /* Move snake head */ \
20+
*p[0] = HEAD; \
21+
/* change the char of new head(new address)'s shape to HEAD */ \
22+
}
23+
#define moveLeft() \
24+
{ \
25+
moveBody(); \
26+
p[0] = p[0] - 1; \
27+
*p[0] = HEAD; \
28+
}
29+
#define moveDown() \
30+
{ \
31+
moveBody(); \
32+
p[0] = p[0] + WIDTH; \
33+
*p[0] = HEAD; \
34+
}
35+
#define moveUp() \
36+
{ \
37+
moveBody(); \
38+
p[0] = p[0] - WIDTH; \
39+
*p[0] = HEAD; \
40+
}
41+
#endif

c/Snake/clear_version/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Under construction
2+
3+
- [x] Windows
4+
- [x] Linux

c/Snake/clear_version/ShowMap.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef _SHWOMAP_H
2+
#define _SHWOMAP_H
3+
#include <stdio.h>
4+
#include "GlobalVar.h"
5+
#define ShowMap() \
6+
{ \
7+
system("clear"); \
8+
printf("Your Score is:%d\n", n - 3); \
9+
for (i = 0; i < (WIDTH)*2; i++) \
10+
printf("_"); \
11+
printf("\n"); \
12+
for (i = 0; i < (HEIGHT); i++) \
13+
{ \
14+
for (j = 0; j < (WIDTH); j++) \
15+
{ \
16+
if (a[i][j] == 0) \
17+
printf("_|"); \
18+
else \
19+
printf("%c|", a[i][j]); \
20+
} \
21+
printf("\n"); \
22+
} \
23+
printf("\nw,s,a,d->Up Down Left Right;\nj,k->Speed " \
24+
"Up/Down;\nESC: Exit\n"); \
25+
}
26+
27+
#endif

c/Snake/clear_version/Sleep.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef _SLEEP_H
2+
#define _SLEEP_H
3+
#include "GlobalVar.h"
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <time.h>
7+
#include <unistd.h>
8+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
9+
#define SLEEPS(delay) Sleep(delay)
10+
#elif defined(__linux__) || defined(__gnu_linux__)
11+
#include <pthread.h>
12+
#define SLEEPS(delay) usleep((delay)*1000)
13+
#elif defined(__APPLE__)
14+
#endif
15+
#endif

c/Snake/clear_version/Snake.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "Snake.h"
2+
#include "GlobalVar.h"
3+
#include "Sleep.h"
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
int main()
7+
{
8+
KeyMonitor_Starter();
9+
ShowMap();
10+
RandomApple();
11+
while (1)
12+
{
13+
ShowMap();
14+
do
15+
{
16+
SLEEPS(delay);
17+
} while (isPause);
18+
19+
switch (isFail())
20+
{
21+
case 0:
22+
break;
23+
case 1:
24+
printf("Fail!Don't eat your body!\nYour Final Score is:%d\n",
25+
n - 3);
26+
return -1;
27+
break;
28+
case 2:
29+
printf("Fail!Don't eat your body!\nYour Final Score is:%d\n",
30+
n - 3);
31+
return -1;
32+
break;
33+
}
34+
35+
if (canEat())
36+
{
37+
n++; // length++
38+
p[n] = p[n - 1];
39+
RandomApple();
40+
}
41+
42+
switch (direction) // choose which direction to move
43+
{
44+
case 1: // Right
45+
{
46+
moveRight();
47+
break;
48+
}
49+
case 2: // Up
50+
{
51+
moveUp();
52+
break;
53+
}
54+
case 3: // Left
55+
{
56+
moveLeft();
57+
break;
58+
}
59+
case 4: // Down
60+
{
61+
moveDown();
62+
break;
63+
}
64+
}
65+
if (direction == -1)
66+
{
67+
break;
68+
}
69+
}
70+
KeyMonitor_Stoper();
71+
}

c/Snake/clear_version/Snake.h

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#ifndef _SNAKE_H
2+
#define _SNAKE_H
3+
#include "GlobalVar.h"
4+
#include "KeyMonitor.h"
5+
#include "Move.h"
6+
#include "ShowMap.h"
7+
#include "Sleep.h"
8+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
9+
#include <conio.h>
10+
#include <handleapi.h>
11+
#include <processthreadsapi.h>
12+
#include <windows.h>
13+
#elif defined(__linux__) || defined(__gnu_linux__)
14+
#include <pthread.h>
15+
#elif defined(__APPLE__)
16+
#endif
17+
#endif
18+
19+
/* Print String At (x,y) and make Cursor go to another place */
20+
21+
/* Random Food */
22+
#define RandomApple() \
23+
{ \
24+
srand(time(NULL)); \
25+
do \
26+
{ \
27+
i = rand() % HEIGHT; \
28+
j = rand() % WIDTH; \
29+
/* if random location is 0 ->*;else find again and again*/ \
30+
} while (a[i][j] != 0); \
31+
a[i][j] = '*'; \
32+
}
33+
34+
// exec when(before) moving
35+
_Bool canEat()
36+
{
37+
switch (direction)
38+
{
39+
// Right
40+
case 1: {
41+
if (*(p[0] + 1) == '*')
42+
{
43+
return 1;
44+
}
45+
break;
46+
}
47+
// Up
48+
case 2: {
49+
if (*(p[0] - WIDTH) == '*')
50+
{
51+
return 1;
52+
}
53+
break;
54+
}
55+
// Left
56+
case 3: {
57+
if (*(p[0] - 1) == '*')
58+
{
59+
return 1;
60+
}
61+
break;
62+
}
63+
// Down
64+
case 4: {
65+
if (*(p[0] + WIDTH) == '*')
66+
{
67+
return 1;
68+
}
69+
break;
70+
}
71+
}
72+
return 0;
73+
}
74+
75+
// exec when(before) moving
76+
int isFail()
77+
{
78+
if (p[0] < &a[0][0] ||
79+
p[0] > &a[HEIGHT - 1][WIDTH - 1]) // snake is not in the matrix
80+
{
81+
// gotoxy(27, 0);
82+
// printf("Fail!\nDon't hit the wall!\n");
83+
direction = -1;
84+
return 1;
85+
}
86+
else
87+
{
88+
switch (direction)
89+
{
90+
// Right
91+
case 1: {
92+
{
93+
for (i = n; i > 0; i--)
94+
{
95+
if ((p[0] + 1) == p[i]) // Right of the head is body
96+
{
97+
// gotoxy(27, 0);
98+
// printf("Fail!\nDon't eat your body!\n");
99+
direction = -1;
100+
return 2;
101+
}
102+
}
103+
break;
104+
}
105+
}
106+
// Up
107+
case 2: {
108+
{
109+
for (i = n; i > 0; i--)
110+
{
111+
if ((p[0] - WIDTH) == p[i]) // Up of the head is body
112+
{
113+
// gotoxy(27, 0);
114+
// printf("Fail!\nDon't eat your body!\n");
115+
direction = -1;
116+
return 2;
117+
}
118+
}
119+
break;
120+
}
121+
}
122+
// Left
123+
case 3: {
124+
{
125+
for (i = n; i > 0; i--)
126+
{
127+
if ((p[0] - 1) == p[i]) // Left of the head is body
128+
{
129+
// gotoxy(27, 0);
130+
// printf("Fail!\nDon't eat your body!\n");
131+
direction = -1;
132+
return 2;
133+
}
134+
}
135+
break;
136+
}
137+
}
138+
// Down
139+
case 4: {
140+
{
141+
for (i = n; i > 0; i--)
142+
{
143+
if ((p[0] + WIDTH) == p[i]) // Down of the head is body
144+
{
145+
// printf("Fail!\nDon't eat your body!\n");
146+
direction = -1;
147+
return 2;
148+
}
149+
}
150+
break;
151+
}
152+
}
153+
}
154+
}
155+
return 0;
156+
}

c/Snake/gotoxy_version/GlobalVar.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _GLOBALVAR_H
2+
#define _GLOBALVAR_H
3+
4+
#include <stdbool.h>
5+
#define WIDTH 40
6+
#define HEIGHT 20
7+
8+
// char HEAD = '@'; // The shape of snake head
9+
// char BODY = 'O'; // The shape of snake body
10+
#define HEAD '@' // The shape of snake head
11+
#define HEAD_STRING "@"
12+
#define BODY 'O' // The shape of snake body
13+
#define BODY_STRING "O"
14+
char a[HEIGHT][WIDTH] = {{BODY, BODY, BODY, HEAD}}; // The initial char is 0
15+
char *p[HEIGHT * WIDTH] = {&a[0][3], &a[0][2], &a[0][1],
16+
&a[0][0]}; // p[0] stand for snake head
17+
18+
int n = 3; // The length of snake body (without head)
19+
int i, j;
20+
int direction = 1; // 1.right;2.up;3.left;4.down;-1.exit
21+
int delay = 200; // delay 0.2s(200ms)
22+
bool isPause = 0;
23+
#endif

0 commit comments

Comments
 (0)