当前位置:网站首页>【C语言】三子棋(经典解法+一览图)
【C语言】三子棋(经典解法+一览图)
2022-07-31 02:45:00 【rookieﻬ°】
文章目录
什么是三子棋?
三子棋就是在3*3的棋盘中,双方进行围堵和想法,有一方先达到连着3颗棋子就算胜利;
三子棋需要掌握到C语言的哪个地方?
据笔者完成三子棋的过程来看,三子棋只需要掌握到二维数组即可。
如何实现三子棋?
1.头文件(函数声明)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define COL 3
void menu();
void Init_board(char board[ROW][COL],int row,int col);//初始化数组为空格
void display_board(char board[ROW][COL],int row, int col);//展示数组
void Player(char board[ROW][COL], int row, int col);//玩家下棋
void Computer(char board[ROW][COL],int row,int col);//电脑下棋
char Iswin(char board[ROW][COL],int row,int col);//判断输赢
2.game.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 Init_board(char board[ROW][COL], int row, int col)//初始化数组为空格
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void display_board(char board[ROW][COL], int row, int col)//展示棋盘
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ",board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void Player(char board[ROW][COL], int row, int col)//玩家下棋
{
int x, y;
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");
}
}
}
void Computer(char board[ROW][COL], int row, int col)//电脑下棋
{
printf("电脑下:>\n");
int x, y;
while (1)
{
x = rand() % row;
y = rand() % col;
//判断是否已有棋子
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
else
{
;
}
}
}
char Iswin(char board[ROW][COL], int row, int col)//判断输赢
{
//玩家赢
int i;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*')
{
return '*';
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == '*')
{
return '*';
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == '*')
{
return '*';
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == '*')
{
return '*';
}
//电脑赢
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
{
return '#';
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == '#')
{
return '#';
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == '#')
{
return '#';
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == '#')
{
return '#';
}
//平局
int flag = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ');
flag++;
}
}
if (flag == 0)
{
return 'Q';
}
else
return 'C';
}
3.text.c(三子棋的测试)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
char ret;
char board[ROW][COL] = {
"0"};//创建数组
Init_board(board, ROW, COL);//初始化数组为空格
display_board(board, ROW, COL);//展示棋盘
while (1)
{
Player(board, ROW, COL);//玩家下棋
ret = Iswin(board, ROW, COL);//判断输赢
if (ret != 'C')
break;
display_board(board, ROW, COL);//打印棋盘
Computer(board, ROW, COL);//电脑下棋
ret = Iswin(board, ROW, COL);//判断输赢
if (ret != 'C')
break;
display_board(board, ROW, COL);//打印棋盘
}
if (ret == '*')
{
printf("玩家赢\n");
display_board(board, ROW, COL);//打印棋盘
}
else if (ret == '#')
{
printf("电脑赢\n");
display_board(board, ROW, COL);//打印棋盘
}
else
{
printf("平局\n");
display_board(board, ROW, COL);//打印棋盘s
}
}
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("游戏结束\n");
break;
default:
printf("输入错误,请重新选择:>");
break;
}
} while (input);
return 0;
}
三子棋代码的总览思路
主函数的代码块是基本的套路,知道怎么做即可。
如下:
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("游戏结束\n");
break;
default:
printf("输入错误,请重新选择:>");
break;
}
} while (input);
return 0;
}
三子棋总览如下,即game()函数的实现:
创建二维数组
初始化全为空格
打印棋盘
While(1)
{
玩家下棋
判断输赢
展示棋盘电脑下棋
判断输赢
展示棋盘
}
加油呀!
边栏推荐
- The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]
- Static routing + PAT + static NAT (explanation + experiment)
- 跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
- The application of AI in the whole process of medical imaging equipment
- 7. List of private messages
- Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
- Draw Your Cards
- cudaMemcpy study notes
- The effective square of the test (one question of the day 7/29)
- Real-time image acquisition based on FPGA
猜你喜欢

10、Redis实现点赞(Set)和获取总点赞数

CentOS7下mysql5.7.37的安装【完美方案】

How to do a startup CTO?

基于opencv实现人脸检测

4. Sensitive word filtering (prefix tree)

局域网电脑硬件信息收集工具

221. Largest Square
![The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]](/img/8a/28427aa773e46740eda9e95f6669f2.png)
The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]

coldfusion8 background scheduled tasks take shell

Huawei od dice js
随机推荐
Static routing + PAT + static NAT (explanation + experiment)
[1154] How to convert string to datetime
Unity3D Button mouse hover enter and mouse hover exit button events
coldfusion8 background scheduled tasks take shell
The final exam first year course
【shell基础】判断目录是否为空
JetPack component Databinding
自动化办公案例:如何自动生成期数据?
知识蒸馏7:知识蒸馏代码详解
Coldfusion file read holes (CVE - 2010-2861)
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
AI在医疗影像设备全流程应用
CorelDRAW2022精简亚太新增功能详细介绍
开题报告之论文框架
The use of font compression artifact font-spider
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络
Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
11、Redis实现关注、取消关注以及关注和粉丝列表
编译Hudi