当前位置:网站首页>[C language] Three-pointed chess (classic solution + list diagram)
[C language] Three-pointed chess (classic solution + list diagram)
2022-07-31 03:19:00 【rookieﻬ°】
文章目录
什么是三子棋?
Three-piece chess is in3*3的棋盘中,Both sides carry out containment and ideas,One side reaches the connection first3A pawn is a victory;
Backgammon needs to be masteredCwhere the language is?
According to the author's process of completing three pieces of chess,Backgammon only needs to be mastered二维数组即可.
How to implement three pieces?
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))
{
//Determine if there is a chess piece
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = '*';
break;
}
else
{
printf("This location is already occupied,请重新输入\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;
//Determine if there is a chess piece
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;
}
An overview idea of chess code
The code block of the main function is the basic routine,Just know how to do it.
如下:
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;
}
An overview of the three-piece game is as follows,即game()函数的实现:
创建二维数组
Initialize all spaces
打印棋盘
While(1)
{
玩家下棋
判断输赢
展示棋盘电脑下棋
判断输赢
展示棋盘
}
加油呀!
边栏推荐
- 【AUTOSAR-RTE】-4-Port和Interface以及Data Type
- Ambiguous method call.both
- 【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
- [Dynamic programming] Maximum sum of consecutive subarrays
- LeetCode每日一练 —— OR36 链表的回文结构
- Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
- Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
- 10. Redis implements likes (Set) and obtains the total number of likes
- 11. Redis implements follow, unfollow, and follow and follower lists
- [Godot][GDScript] 二维洞穴地图随机生成
猜你喜欢

浅识Flutter 基本组件之CheckBox组件

分布式系统架构需要解决的问题

Problems that need to be solved in distributed system architecture

Graphical lower_bound & upper_bound

Mysql 45讲学习笔记(二十五)MYSQL保证高可用

11. Redis implements follow, unfollow, and follow and follower lists

Ambiguous method call.both

立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业

IIR滤波器和FIR滤波器

7年经验,功能测试工程师该如何一步步提升自己的能力呢?
随机推荐
SQL injection Less54 (limited number of SQL injection + union injection)
【编译原理】递归下降语法分析设计原理与实现
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
The distance value between two arrays of LeetCode simple questions
els block to the right
The els block moves the boundary to the right, and accelerates downward.
False positives and false negatives in testing are equally worthy of repeated corrections
els 方块向右移动边界判断、向下加速
Automation strategies for legacy systems
WebSocket Session为null
SIP Protocol Standard and Implementation Mechanism
华为分布式存储FusionStorage知识点总结【面试篇】
TCP/IP four-layer model
想从手工测试转岗自动化测试,需要学习哪些技能?
Use of QML
els 方块向左移动条件判断
Database implements distributed locks
LeetCode中等题之分数加减运算
TCP和UDP详解
3.5 】 【 Cocos Creator slow operating system to stop all animations