今天分享一下写的小游戏啦
头文件game.h
#include
#include
#include
#define H 3
#define L 3
void InitBoard(char Board[H][L], int h, int l);
void DisplayBoard(char Board[H][L], int h, int l);
void playermove(char Board[H][L], int h, int l);
void computermove(char Board[H][L], int h, int l);
char iswin(char Board[H][L], int h, int l);
源文件game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void InitBoard(char Board[H][L], int h, int l)
{
int i = 0;
int j = 0;
for (i = 0; i < H; i++)
{
for (j = 0; j < L; j++)
{
Board[i][j] = ' ';
}
}
}
void DisplayBoard(char Board[H][L], int h, int l)
{
int i = 0;
for (i = 0; i < H; i++)
{
int j = 0;
/*printf(" %c | %c | %c \n", Board[i][0], Board[i][1], Board[i][2]);*/
for (j = 0; j < L; j++)
{
printf(" %c ",Board[i][j]);
if (j
}
printf("\n");
if (i < H - 1)
{
for (j = 0; j < L ; j++)
{
printf("---");
if (j < L - 1)
printf("|");
}
}
printf("\n");
}
}
void playermove(char Board[H][L], int h, int l)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
while (1)
{
printf("请输入坐标-->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= H&&y >= 1 && y <= L)
{
if (Board[x - 1][y - 1] == ' ')
{
Board[x - 1][y - 1] = '*';
break;
}
else
{
printf("被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
void computermove(char Board[H][L], int h, int l)
{
int x = 0;
int y = 0;
printf("电脑下棋:>\n");
while (1)
{
x = rand() % H;
y = rand() % L;
if (Board[x][y] == ' ')
{
Board[x][y] = '#';
break;
}
}
}
isfull(char Board[H][L], int h, int l)
{
int i = 0;
int j = 0;
for (i = 0; i < H; i++)
{
for (j = 0; j < L; j++)
{
if (Board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char iswin(char Board[H][L], int h, int l)
{
int i = 0;
for (i = 0; i < H; i++)
{
if (Board[i][0] == Board[i][1] && Board[i][1] == Board[i][2] && Board[i][1] != ' ')
{
return Board[i][1];
}
}
int j = 0;
for (j = 0; j < L; j++)
{
if (Board[0][j] == Board[1][j] && Board[1][j] == Board[2][j] && Board[1][j] != ' ')
{
return Board[1][j];
}
}
if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[1][1] != ' ')
{
return Board[1][1];
}
if (Board[0
学习.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("************************************\n");
printf("******** 1.三子棋 ********\n");
printf("******** 0.退出 ********\n");
printf("************************************\n");
}
void game()
{
int ret = 0;
char Board[H][L] = { 0 };
//初始化函数
InitBoard(Board, H, L);
DisplayBoard(Board, H, L);
while (1)
{
playermove(Board, H, L);
ret = iswin(Board, H, L);
if (ret != 'c')
{
break;
}
DisplayBoard(Board, H, L);
computermove(Board, H, L);
ret = iswin(Board, H, L);
if (ret != 'c')
{
break;
}
DisplayBoard(Board, H, L);
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请输入-->");
scanf("%d", &input);
switch (input)
{
case 1:
{
printf("三子棋\n");
game();
break;
}
case 0: printf("退出游戏\n");
break;
default:printf("输入错误\n");
break;
}
} while (input);
{
}
return 0;
}
编写思路是先初始化棋盘 打印棋盘 然后玩家移动 接着电脑移动 最后判断胜负就好啦
当我们开始运行程序 并且输入1 就会出现以下结果
然后我们可以开始愉快的三子棋啦
当我们赢了笨笨的机器人 就可以获得胜利!反之亦然 当然也有平局哦
当我们不想玩的时候 就可以输入0来退出游戏
大家有兴趣的话可以玩一下呀 看看能不能战胜机器人 获得胜利