CSDN小伙伴们,大家好啊,这次我们来实现一个小游戏——三子棋)(井字游戏)
相信大家都玩过这个,玩法呢就是两个人轮流下“井”中的棋,直到一方三字连成线,或者棋盘占满
接下来,跟着我的思路一起实现它吧!
首先把基本框架写出
然后我们思考,游戏首先要有主页选项,并且每次结束游戏后会返回游戏主页,这样我们就可以先敲出这样的代码
#include
int main()
{
int input = 0;
do
{
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
主页我们简单设计一下
void menu()
{
printf("******************************\n");
printf("********* 1 . play *********\n");
printf("********* 0 . quit *********\n");
printf("******************************\n");
}
设计好自定义函数后,直接主函数引用它
#include
void menu()
{
printf("******************************\n");
printf("********* 1 . play *********\n");
printf("********* 0 . quit *********\n");
printf("******************************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
效果大概就是这样
主体大致就设计好了,接下来,该设计大量自定义函数了
自定义函数我们可以用多个源文件和头文件来时间连接
在主函数直接引用头文件“game.h”
//test.c
#include"game.h"
void menu()
{
printf("******************************\n");
printf("********* 1 . play *********\n");
printf("********* 0 . quit *********\n");
printf("******************************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
三子棋我们可以用数组的方式来实现,“井”字,三横三纵,可以用二维数组
//game.h
#include
#define POW 3
#define COL 3
//test.c
void game()
{
int board[POW][COL] = { 0 };
}
三子棋下棋前,都是空白,我们可以用空格代替
//test.c
void game()
{
int board[POW][COL] = { 0 };
arr_init(board, POW, COL);
}
//game.c
#include"game.h"
//数组初始化
void arr_init(int board[POW][COL], int pow, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < pow; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//game.h
#pragma once
#include
#define POW 3
#define COL 3
//数组初始化
void arr_init(int board[POW][COL], int pow, int col);
棋盘是“井”状的,它可以才分为“ |”和“---”反复打印实现
为了美观,我们把数组内容的左右分别加一个空格
//test.c
void game()
{
int board[POW][COL] = { 0 };
arr_init(board, POW, COL);
chess(board, POW, COL);
}
//game.c
void chess(int board[POW][COL], int pow, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < pow; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < pow - 1)
printf("|");
}
printf("\n");
if (i < col - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < pow - 1)
printf("|");
}
printf("\n");
}
}
}
//game.h
#pragma once
#include
#define POW 3
#define COL 3
//数组初始化
void arr_init(int board[POW][COL], int pow, int col);
//棋盘
void chess(int board[POW][COL], int pow, int col);
最后的效果大概是这样
接下来就是下棋了,下棋的顺序大概是:玩家下棋,判断胜负,电脑下棋,判断胜负的循环
输入坐标后既要判断坐标是否超纲也要判断坐标位置是否被占用
//game.c
//玩家下棋
void playmove(int board[POW][COL], int pow, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入棋子坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= pow && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else printf("该位置已占用,请重新输入\n");
}
else printf("请输入正确坐标\n");
}
}
电脑下棋位置是随机的,我们可以用随机数来实现,而随机数需要rand,rand需要一个时刻变化的数,所以使用时间戳是个不错的选择
rand需要引用头文件stdlib.h
时间戳需要引用头文件time.h
//game.c
//电脑下棋
void computermove(int board[POW][COL], int pow, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % pow;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//test.c
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;
}
//game.h
#pragma once
#include
#include
#include
既要判断输赢又要判断是否平局
//game.c
//判断输赢
char ful(int board[POW][COL], int pow, int col)
{
int i = 0;
//判断三横
for (i = 0; i < pow; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
//判断三纵
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
int j = 0;
for (i = 0; i < pow; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 'C';
}
}
return 'Q';
}
如果返回值为 *玩家赢 为#电脑赢 为Q平局 为C继续
再按照玩家下棋,判断胜负,电脑下棋,判断胜负的顺序进行循环
//test.c
void game()
{
int board[POW][COL] = { 0 };
arr_init(board, POW, COL);
char ret;
while (1)
{
chess(board, POW, COL);
playmove(board, POW, COL);
ret = ful(board, POW, COL);
if (ret != 'C')
break;
computermove(board, POW, COL);
ret = ful(board, POW, COL);
if (ret != 'C')
break;
}
if (ret == '*') printf("玩家胜利\n");
else if (ret == '#')
{
printf("电脑胜利\n");
chess(board, POW, COL);
}
else if (ret == 'Q')
{
printf("平局\n");
chess(board, POW, COL);
}
}
//game.h
#pragma once
#include
#include
#include
#define POW 3
#define COL 3
//数组初始化
void arr_init(int arr[POW][COL], int pow, int col);
//棋盘
void chess(int arr[POW][COL], int pow, int col);
//玩家下棋
void playmove(int arr[POW][COL], int pow, int col);
//电脑下棋
void computermove(int arr[POW][COL], int pow, int col);
//判断输赢
char ful(int arr[POW][COL], int pow, int col);
最后所有代码为下
//game.h
#pragma once
#include
#include
#include
#define POW 3
#define COL 3
//数组初始化
void arr_init(int arr[POW][COL], int pow, int col);
//棋盘
void chess(int arr[POW][COL], int pow, int col);
//玩家下棋
void playmove(int arr[POW][COL], int pow, int col);
//电脑下棋
void computermove(int arr[POW][COL], int pow, int col);
//判断输赢
char ful(int arr[POW][COL], int pow, int col);
//game.c
#include"game.h"
//数组初始化
void arr_init(int board[POW][COL], int pow, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < pow; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//棋盘
void chess(int board[POW][COL], int pow, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < pow; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < pow - 1)
printf("|");
}
printf("\n");
if (i < col - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < pow - 1)
printf("|");
}
printf("\n");
}
}
}
//玩家下棋
void playmove(int board[POW][COL], int pow, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入棋子坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= pow && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else printf("该位置已占用,请重新输入\n");
}
else printf("请输入正确坐标\n");
}
}
//电脑下棋
void computermove(int board[POW][COL], int pow, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % pow;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断输赢
char ful(int board[POW][COL], int pow, int col)
{
int i = 0;
for (i = 0; i < pow; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
int j = 0;
for (i = 0; i < pow; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 'C';
}
}
return 'Q';
}
//test.c
#include"game.h"
void menu()
{
printf("******************************\n");
printf("********* 1 . play *********\n");
printf("********* 0 . quit *********\n");
printf("******************************\n");
}
void game()
{
int board[POW][COL] = { 0 };
arr_init(board, POW, COL);
char ret;
while (1)
{
chess(board, POW, COL);
playmove(board, POW, COL);
ret = ful(board, POW, COL);
if (ret != 'C')
break;
computermove(board, POW, COL);
ret = ful(board, POW, COL);
if (ret != 'C')
break;
}
if (ret == '*') printf("玩家胜利\n");
else if (ret == '#')
{
printf("电脑胜利\n");
chess(board, POW, COL);
}
else if (ret == 'Q')
{
printf("平局\n");
chess(board, POW, COL);
}
}
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;
}
以上就是三子棋的全部内容,主要运用的是对图形的打印,数组的运用,函数的运用,条件语句和循环,感谢观看~