Skip to content

Commit 70d6913

Browse files
committed
Format code
1 parent 3d5aa45 commit 70d6913

6 files changed

Lines changed: 43 additions & 37 deletions

File tree

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _GETXYFROMARRAYS_H
22
#define _GETXYFROMARRAYS_H
3-
//only for char/byte
4-
#define GETX_CHAR(CoordinateOrigin,ElementCoordinate,Column) ((ElementCoordinate)-(CoordinateOrigin))/(Column)
5-
#define GETY_CHAR(CoordinateOrigin,ElementCoordinate,Column) ((ElementCoordinate)-(CoordinateOrigin))%(Column)
3+
// only for char/byte
4+
#define GETX_CHAR(CoordinateOrigin, ElementCoordinate, Column) \
5+
((ElementCoordinate) - (CoordinateOrigin)) / (Column)
6+
#define GETY_CHAR(CoordinateOrigin, ElementCoordinate, Column) \
7+
((ElementCoordinate) - (CoordinateOrigin)) % (Column)
68
#endif

c/Snake/gotoxy_version/GlobalVar.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
// char HEAD = '@'; // The shape of snake head
99
// char BODY = 'O'; // The shape of snake body
10-
#define HEAD '@' // The shape of snake head
10+
#define HEAD '@' // The shape of snake head
1111
#define HEAD_STRING "@"
12-
#define BODY 'O' // The shape of snake body
12+
#define BODY 'O' // The shape of snake body
1313
#define BODY_STRING "O"
1414
char a[HEIGHT][WIDTH] = {{BODY, BODY, BODY, HEAD}}; // The initial char is 0
1515
char *p[HEIGHT * WIDTH] = {&a[0][3], &a[0][2], &a[0][1],
1616
&a[0][0]}; // p[0] stand for snake head
1717

1818
int n = 3; // The length of snake body (without head)
1919
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)
20+
int direction = 1; // 1.right;2.up;3.left;4.down;-1.exit
21+
int delay = 200; // delay 0.2s(200ms)
2222
bool isPause = 0;
2323
#endif

c/Snake/gotoxy_version/Move.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
#define moveBody() \
77
{ \
88
*p[n] = 0; \
9-
PRINT_STRING_XY((GETX_CHAR(a[0], p[n], WIDTH) + 2), \
10-
(GETY_CHAR(a[0], p[n], WIDTH) * 2), "_") \
9+
PRINT_STRING_XY((GETX_CHAR(a[0], p[n], WIDTH) + 2), \
10+
(GETY_CHAR(a[0], p[n], WIDTH) * 2), "_") \
1111
for (i = n; i > 0; i--) \
1212
{ \
1313
p[i] = p[i - 1]; \
1414
/* per part goes to the address of the next part of body*/ \
1515
} \
1616
*p[0] = BODY; \
1717
/* The First part of snake body come to snake head*/ \
18-
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
19-
(GETY_CHAR(a[0], p[0], WIDTH) * 2), BODY_STRING) \
18+
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
19+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), BODY_STRING) \
2020
}
2121

2222
#define moveRight() \
@@ -25,31 +25,31 @@
2525
p[0] = p[0] + 1; /* Move snake head */ \
2626
*p[0] = HEAD; \
2727
/* change the char of new head(new address)'s shape to HEAD */ \
28-
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
29-
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
28+
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
29+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
3030
}
3131
#define moveLeft() \
3232
{ \
3333
moveBody(); \
3434
p[0] = p[0] - 1; \
3535
*p[0] = HEAD; \
36-
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
37-
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
36+
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
37+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
3838
}
3939
#define moveDown() \
4040
{ \
4141
moveBody(); \
4242
p[0] = p[0] + WIDTH; \
4343
*p[0] = HEAD; \
44-
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
45-
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
44+
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
45+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
4646
}
4747
#define moveUp() \
4848
{ \
4949
moveBody(); \
5050
p[0] = p[0] - WIDTH; \
5151
*p[0] = HEAD; \
52-
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
53-
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
52+
PRINT_STRING_XY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
53+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
5454
}
5555
#endif

c/Snake/gotoxy_version/Snake.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include "GlobalVar.h"
55
#include "GotoXY.h"
66
#include "KeyMonitor.h"
7-
#include "ShowMap.h"
87
#include "Move.h"
98
#include "Random.h"
9+
#include "ShowMap.h"
1010
#include "Sleep.h"
1111
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
1212
#include <conio.h>
@@ -20,7 +20,7 @@
2020
#endif
2121

2222
/* Print String At (x,y) and make Cursor go to another place */
23-
#define PRINT_STRING_XY(x, y, content) \
23+
#define PRINT_STRING_XY(x, y, content) \
2424
{ \
2525
gotoxy((x), (y)); \
2626
printf("%s", (content)); \
@@ -38,8 +38,11 @@
3838
/* if random location is 0 ->*;else find again and again*/ \
3939
} while (a[i][j] != 0); \
4040
a[i][j] = '*'; \
41-
PRINT_STRING_XY(((GETX_CHAR((a[0]), (&a[i][j]), (WIDTH))) + 2), \
42-
((GETY_CHAR((a[0]), (&a[i][j]), (WIDTH))) * 2), "*"); \
41+
PRINT_STRING_XY(((GETX_CHAR((a[0]), (&a[i][j]), (WIDTH))) + 2), \
42+
((GETY_CHAR((a[0]), (&a[i][j]), (WIDTH))) * 2), "*"); \
43+
gotoxy(0, 62); \
44+
printf("Food is at (%02d,%02d)", i, j); \
45+
gotoxy(HEIGHT + 7, 40); \
4346
}
4447

4548
// exec when(before) moving

c/Snake/snake.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
// char HEAD = '@'; // The shape of snake head
2222
// char BODY = 'O'; // The shape of snake body
23-
#define HEAD '@' // The shape of snake head
24-
#define BODY 'O' // The shape of snake body
23+
#define HEAD '@' // The shape of snake head
24+
#define BODY 'O' // The shape of snake body
2525
char a[HEIGHT][WIDTH] = {{BODY, BODY, BODY, HEAD}}; // The initial char is 0
2626
char *p[HEIGHT * WIDTH] = {&a[0][3], &a[0][2], &a[0][1],
27-
&a[0][0]}; // p[0] stand for snake head
27+
&a[0][0]}; // p[0] stand for snake head
2828

2929
int n = 3; // The length of snake body (without head)
3030
int i, j;
@@ -295,7 +295,8 @@ int main()
295295
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
296296
// set pthread_attr to detached
297297
pthread_t tid;
298-
pthread_create(&tid, &attr, KeyMonitor, NULL); // Create pthread to capture input
298+
pthread_create(&tid, &attr, KeyMonitor,
299+
NULL); // Create pthread to capture input
299300
randomApple();
300301
while (1)
301302
{
@@ -305,7 +306,7 @@ int main()
305306
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
306307
Sleep(delay);
307308
#elif defined(__linux__) || defined(__gnu_linux__)
308-
usleep(delay * 1000);
309+
usleep(delay * 1000);
309310
#elif defined(__APPLE__)
310311
#endif
311312
} while (isPause);

c/Snake/test copy.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#include<conio.h>
2-
#include<iostream>
3-
1+
#include <conio.h>
2+
#include <iostream>
3+
44
using namespace std;
5-
5+
66
int main()
77
{
8-
while(!kbhit()) //当没有键按下
8+
while (!kbhit()) //当没有键按下
99
{
10-
cout << "无键按下" << endl;
10+
cout << "无键按下" << endl;
1111
}
12-
cout << "有键按下" << endl;
13-
system("pause");
14-
return 0;
12+
cout << "有键按下" << endl;
13+
system("pause");
14+
return 0;
1515
}

0 commit comments

Comments
 (0)