争取练习到15分钟能打完一个:
one:snake.c
#include
#include
#include
#define APPLE
#define SNAKEHEAD '+'
#define SNAKETAIL '-'
#define SIZE 10
typedef struct {
int row;
int col;
} Pos;
typedef struct {
Pos head;
Pos tail;
} Snake;
Snake snake;
Pos apple;
bool is_same(const Pos* pFirstPos, const Pos* pSecondPos) {
if ((pFirstPos->row == pSecondPos->row) && (pFirstPos->col == pSecondPos->col)) {
return true;
}
else {
return false;
}
}
void init_snake() {
int row = rand() % (SIZE - 2);
int col = rand() % (SIZE - 2);
int direction = rand() % 4;
snake.head.row = row;
snake.head.col = col;
switch(direction) {
case 0:
//尾巴在头上边
snake.tail.row = row - 1;
snake.tail.col = col;
break;
case 1:
//尾巴在头下面
snake.tail.row = row + 1;
snake.tail.col = col;
break;
case 2:
//尾巴在头左边
snake.tail.row = row;
snake.tail.col = col - 1;
break;
default:
//尾巴在头右边
snake.tail.row = row;
snake.tail.col = col + 1;
break;
}
}
void plant_apple() {
do {
apple.row = rand() % (SIZE - 2);
apple.col = rand() % (SIZE - 2);
} while ((is_same(&apple, &(snake.head))) || (is_same(&apple, &(snake.tail))));
}
void show_map() {
int row = 0;
int col = 0;
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++) {
if ((row == apple.row) && (col == apple.col)) {
printf("%c", APPLE);
}
else if ((row == snake.head.row) && (col == snake.head.col)) {
printf("%c", SNAKEHEAD);
}
else if ((row == snake.tail.row) && (col == snake.tail.col)) {
printf("%c", SNAKETAIL);
}
else {
printf(" ");
}
}
printf("\n");
}
}
void move_snake() {
int direction = 0;
int row = 0;
int col = 0;
printf("请输入移动的方向:(0表示向上,1表示向左,2表示向下,3表示向右)");
scanf("%d", &direction);
switch(direction) {
case 0:
if ((snake.head.row - 1) >= 0) {
snake.tail.row = snake.head.row;
snake.tail.col = snake.head.col;
snake.head.row--;
}
break;
case 1:
if ((snake.head.col - 1) >= 0) {
snake.tail.row = snake.head.row;
snake.tail.col = snake.head.col;
snake.head.col--;
}
break;
case 2:
if ((snake.head.row + 1) < SIZE){
snake.tail.row = snake.head.row;
snake.tail.col = snake.head.col;
snake.head.row++;
}
break;
default:
if ((snake.head.col + 1) < SIZE) {
snake.tail.row = snake.head.row;
snake.tail.col = snake.head.col;
snake.head.col++;
}
break;
}
if (is_same(&apple, &(snake.head)) || (is_same(&apple, &(snake.tail)))) {
plant_apple();
}
}
int main() {
srand(time(0));
init_snake();
plant_apple();
show_map();
while (1) {
move_snake();
show_map();
}
return 0;
}
two: tictactoe.c
#include
char g_Map[3][3];
void init() {
int row;
int col;
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
g_Map[row][col] = ' ';
}
}
}
void show_map() {
printf("%c | %c | %c\n", g_Map[0][0], g_Map[0][1], g_Map[0][2]);
printf("--+---+--\n");
printf("%c | %c | %c\n", g_Map[1][0], g_Map[1][1], g_Map[1][2]);
printf("--+---+--\n");
printf("%c | %c | %c\n", g_Map[2][0], g_Map[2][1], g_Map[2][2]);
}
int place_chess(char player) {
while(1) {
int row;
int col;
printf("请输入行号:\n");
scanf("%d", &row);
if ((row < 1) || (row > 3)) {
printf("输入错误,请再次输入\n");
continue;
}
printf("请输入列号:\n");
scanf("%d", &col);
if ((col < 1) || (col > 3)) {
printf("输入错误,请再次输入\n");
continue;
}
row--;
col--;
if (' ' != g_Map[row][col]) {
printf("输入错误,请再次输入\n");
continue;
}
g_Map[row][col] = player;
break;
}
}
char wins() {
if ((g_Map[0][0] == g_Map[0][1]) &&
(g_Map[0][1] == g_Map[0][2]) &&
(' ' != g_Map[0][1])) {
return g_Map[0][0];
}
if ((g_Map[1][0] == g_Map[1][1]) &&
(g_Map[1][1] == g_Map[1][2]) &&
(' ' != g_Map[1][1])) {
return g_Map[1][0];
}
if ((g_Map[2][0] == g_Map[2][1]) &&
(g_Map[2][1] == g_Map[2][2]) &&
(' ' != g_Map[2][1])) {
return g_Map[2][0];
}
if ((g_Map[0][0] == g_Map[1][0]) &&
(g_Map[1][0] == g_Map[2][0]) &&
(' ' != g_Map[1][0])) {
return g_Map[0][0];
}
if ((g_Map[0][1] == g_Map[1][1]) &&
(g_Map[1][1] == g_Map[2][1]) &&
(' ' != g_Map[1][1])) {
return g_Map[0][1];
}
if ((g_Map[0][2] == g_Map[1][2]) &&
(g_Map[1][2] == g_Map[2][2]) &&
(' ' != g_Map[1][2])) {
return g_Map[0][2];
}
if ((g_Map[0][0] == g_Map[1][1]) &&
(g_Map[1][1] == g_Map[2][2]) &&
(' ' != g_Map[0][0])) {
return g_Map[0][0];
}
if ((g_Map[2][0] == g_Map[1][1]) &&
(g_Map[1][1] == g_Map[0][2]) &&
(' ' != g_Map[1][1])) {
return g_Map[1][1];
}
return ' ';
}
int main() {
int again;
do {
char player = 'O';
init();
show_map();
int loop;
for (loop = 0; loop < 9; loop++) {
place_chess(player);
show_map();
char result = wins();
if (('*' == result) || ('O' == result)) {
printf("%c wins!!\n", result);
break;
}
else {
if ('O' == player) {
player = '*';
}
else {
player = 'O';
}
}
}
if (9 == loop) {
printf("和棋!!\n");
}
printf("是否开始新的游戏?(1 is Yes, 0 is No)\n");
scanf("%d", &again);
} while(0 != again);
return 0;
}