【C语言 三子棋游戏】犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述C语言 三子棋游戏相关的知识,希望能为你提供帮助。
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()//游戏菜单
{
printf("**********************\\n");
printf("*****1.play*****\\n");
printf("*****0.exit*****\\n");
printf("**********************\\n");
}void game()
{
int set = 0;
char board[ROW][COL];
//3*3的棋盘空间
Initboard(board, ROW, COL);
//初始化棋盘
Displayboard(board, ROW, COL);
//显示棋盘
while (1)
{
Playermove(board, ROW, COL);
//玩家下棋
//1为继续
//0为平局
//*为玩家获胜
//#为电脑获胜
set=Who_win(board, ROW, COL);
//判断谁赢了
if (set != 1)//判断是否继续
break;
Computermove(board, ROW, COL);
//电脑下棋
set = Who_win(board, ROW, COL);
if (set != 1)
break;
}
if (set == \'*\')
printf("玩家获胜\\n");
if (set == \'#\')
printf("电脑获胜\\n");
if (set == 0)
printf("打成平局\\n");
}int main()
{
int input = 0;
srand((unsigned int)time(NULL));
//生成随机数
do
{
menu();
//进入游戏游戏菜单
printf("请选择:");
scanf("%d", &
input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出成功");
break;
default:
printf("输入错误,请重新输入:\\n");
break;
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[ROW][COL], int row, int col)//初始化棋盘
{
int x = 0;
int y = 0;
for (x = 0;
x <
row;
x++)
{
for (y = 0;
y <
col;
y++)
{
board[x][y] = \' \';
}
}
}void Displayboard(char board[ROW][COL], int row, int col)//显示棋盘
{
int x = 0;
int y = 0;
for (x = 0;
x <
row;
x++)
{
for (y = 0;
y <
col;
y++)
{
printf(" %c ", board[x][y]);
if (y <
col - 1)
printf("|");
}
printf("\\n");
if (x <
row - 1)
{
for (y = 0;
y <
col;
y++)
{
printf("---");
if (y <
col - 1)
printf("|");
}
printf("\\n");
}
}
}void Playermove(char board[ROW][COL], int row, int col)//玩家下棋
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下棋:");
scanf("%d%d", &
x, &
y);
if (x >
= 1 &
&
x <
= row &
&
y >
= 1 &
&
y <
= col)
{
if (board[x - 1][y - 1] == \' \')
{
board[x - 1][y - 1] = \'*\';
break;
}
else
printf("已被占用,请重新输入\\n");
}
else
printf("输入不在范围内,请重新输入\\n");
}
Displayboard(board, ROW, COL);
}void Computermove(char board[ROW][COL], int row, int col)//电脑下棋
{
printf("电脑下棋:\\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == \' \')
{
board[x][y] = \'#\';
break;
}
}
Displayboard(board, ROW, COL);
}int Who_win(char board[ROW][COL], int row, int col)//判断谁赢了
{
int x = 0;
int y = 0;
for (x = 0;
x <
row;
x++)
{
if (board[x][0] == board[x][1] &
&
board[x][0] == board[x][2] &
&
board[x][0] != \' \')//判断三行是否相等
return board[x][0];
}
for (y = 0;
y <
col;
y++)
{
if (board[0][y] == board[1][y] &
&
board[0][y] == board[2][y] &
&
board[0][y] != \' \')//判断三列是否相等
return board[0][y];
}
if (board[0][0] == board[1][1] &
&
board[1][1] == board[2][2] &
&
board[1][1] != \' \')//判断对角是否相等
return board[1][1];
if (board[0][2] == board[1][1] &
&
board[1][1] == board[2][0] &
&
board[1][1] != \' \')
return board[1][1];
for (y = 0;
y <
col;
y++)
{
for (x = 0;
x <
row;
x++)//判断是否继续游戏
{
if (board[x][y] == \' \')
return 1;
}
}
return 0;
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1#include<
stdio.h>
#include<
time.h>
#include<
stdlib.h>
// 符号定义
#define ROW 3
#define COL 3//函数的声明
void Initboard(char board[ROW][COL],int row,int col);
//初始化棋盘
void Displayboard(char board[ROW][COL], int row, int col);
//显示棋盘
void Playermove(char board[ROW][COL], int row, int col);
//玩家下棋
void Computermove(char board[ROW][COL], int row, int col);
//电脑下棋
int Who_win(char board[ROW][COL], int row, int col);
//判断谁赢了
运行结果:
文章图片
推荐阅读
- MySQL自定义数据库连接池和开源数据库连接池的使用
- Java ASM系列((066)Exception处理)
- Thymeleaf模板使用
- centos7网络配置一些问题
- Spring(IOC是啥(如何推导和理解?))
- Python实现批量压缩文件/文件夹——zipfile
- centos7网络配置一些问题_李孟_新浪博客
- SpringCloud升级之路2020.0.x版-23.订制Spring Cloud LoadBal
- 如何优化 node 项目的 docker 镜像(像老板压榨员工一样压榨镜像)