当前位置:网站首页>【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)
{
玩家下棋
判断输赢
展示棋盘电脑下棋
判断输赢
展示棋盘
}
加油呀!
边栏推荐
猜你喜欢

Difference between CMOS and TTL?

汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络

工程(五)——小目标检测tph-yolov5

【Bank Series Phase 1】People's Bank of China

10. Redis implements likes (Set) and obtains the total number of likes

StringJoiner详解

221. Largest Square

跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!

什么是分布式锁?实现分布式锁的三种方式

Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
随机推荐
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
如何搭建私有yum源
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
10. Redis implements likes (Set) and obtains the total number of likes
10、Redis实现点赞(Set)和获取总点赞数
The Sad History of Image Processing Technology
10 权限介绍
Drools Rule Properties, Advanced Syntax
开题报告之论文框架
7、私信列表
Calculate S=a+aa+…+aa…a
数学解决——环形链表问题
11. Redis implements follow, unfollow, and follow and follower lists
【Bank Series Phase 1】People's Bank of China
LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路
CorelDRAW2022精简亚太新增功能详细介绍
The whole process scheduling, MySQL and Sqoop
7. List of private messages
【HCIP】ISIS