当前位置:网站首页>C语言—————三子棋游戏
C语言—————三子棋游戏
2020-11-09 12:58:00 【osc_rmqoxylv】
三子棋游戏
问题描述:
3*3的棋盘中,只要一条线上出现三个一样的棋子就获胜(玩家或电脑);如果棋盘已经放满还未出现三个棋子一条线则打成平手。
具体细节:
- 初始化棋盘(用空格初始化)
//初始化棋盘
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
- 打印棋盘
//打印棋盘
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
- 电脑落子(用o表示电脑落子)
//电脑落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
- 玩家落子
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("请输入您的落子地点:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您输入的落子位置有误,请重新输入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("该位置已有棋子,请重新输入:");
}
}
- 三个棋子一条线
- 在一行或一列实现三个棋子一条线
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
- 对角线实现三个棋子一条线
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
- 和棋
- 棋盘放满还未获胜,则为和棋,打成了平手。
在这里插入代码片
//和棋
if(isFull(checkbox)){
return 'a';
}
- 输赢约定:
- 返回x代表玩家获胜
if (isWinner(chessbox) == 'x'){
printf("恭喜您赢啦!\n");
break;
}
- 返回o代表电脑获胜
if (isWinner(chessbox) == 'o'){
printf("很遗憾,您输了!\n");
break;
}
- 返回a代表和棋(打成平手)
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
- 判断棋盘是否放满:
- 返回1代表棋盘已满
- 返回0代表棋盘未满
//判断棋盘是否摆满
//1表示满;0表示不满。
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,说明未满
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
源代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 3
#define COL 3
//玩家落子(用x表示)
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf("请输入您的落子地点:");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf("您输入的落子位置有误,请重新输入:");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf("该位置已有棋子,请重新输入:");
}
}
//电脑落子(用o表示)
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
//初始化棋盘
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
//打印棋盘
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
//判断棋盘是否摆满,摆满返回1;未满返回0
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
//找到空格,说明未满
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
//约定:返回x代表玩家赢了;返回o代表电脑赢了;返回a代表和棋
char isWinner(char chessbox[ROW][COL]){
//行
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
//列
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
//对角线
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
//和棋
if (isFullChess(chessbox)){
return 'a';
}
return 0;
}
//开始一局游戏
void game(){
char chessbox[ROW][COL] = {
0 };
initChess(chessbox);
printf("游戏开始啦!\n");
printChess(chessbox);
while (1){
//玩家落子
playerMove(chessbox);
//打印棋牌
printChess(chessbox);
//判断胜负
if (isWinner(chessbox) == 'x'){
printf("恭喜您赢啦!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
//电脑落子
computerMove(chessbox);
//打印棋牌
printChess(chessbox);
//判断胜负
if (isWinner(chessbox) == 'o'){
printf("很遗憾,您输了!\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf("你和电脑同一水平呦!\n");
break;
}
}
}
int menu(){
printf("===============\n");
printf("1.开始游戏\n");
printf("0.结束游戏\n");
printf("===============\n");
int choice;
printf("请输入您的选择:");
scanf("%d", &choice);
return choice;
}
int main()
{
ile (1){
int choice=menu();
if (choice == 1){
game();
continue;
}
else if (choice == 0){
break;
}else{
printf("输入有误,请您重新输入!\n");
continue;
}
}
system("pause");
return 0;
}
运行结果:




版权声明
本文为[osc_rmqoxylv]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4313784/blog/4709402
边栏推荐
- Understanding runloop in OC
- 使用TreeView树型菜单栏(递归调用数据库自动创建菜单)
- IDEA解决yml配置文件中文输出乱码问题
- 深圳C1考证历程
- Explain Python input() function: get user input string
- 技美那么贵,不如找顾问 | AALab企业顾问业务
- Show profile analysis of SQL statement performance overhead
- Setting up a proxy for the WGet command
- How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)
- Is SEO right or wrong?
猜你喜欢

JVM学习(六)-内存模型和线程

零基础IM开发入门(四):什么是IM系统的消息时序一致性?

使用TreeView树型菜单栏(递归调用数据库自动创建菜单)

“开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖

Rainbow sorting | Dutch flag problem

EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL

Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive

Handwriting Koa.js Source code

导师制Processing网课 双十一优惠进行中

如何用函数框架快速开发大型 Web 应用 | 实战
随机推荐
Reading design patterns adapter patterns
New features of Fedora 33 workstation
Using stream to read and write files to process large files
Visual Studio (MAC) installation process notes
导师制Processing网课 双十一优惠进行中
Interview summary on November 7, 2020 (interview 12K)
10款必装软件,让Windows使用效率飞起!
The third way to realize webrtc in embedded devices
外贸自建网站域名的选择— Namesilo 域名购买
03.优先链接模型
Using rem, the font size changes when the screen zooms
Android 集成支付的四部曲
Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters
Suning's practice of large scale alarm convergence and root cause location based on Knowledge Map
Android Studio Avd「真·小白食用方法」
Explain Python input() function: get user input string
For and for... In, for each and map and for of
Learning notes of nodejs
实现商品CRUD操作
The history of C1 research in Shenzhen