当前位置:网站首页>[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)
{
玩家下棋
判断输赢
展示棋盘电脑下棋
判断输赢
展示棋盘
}
加油呀!
边栏推荐
- 品牌广告投放平台的中台化应用与实践
- [Compilation principle] Lexical analysis program design principle and implementation
- Ambiguous method call.both
- IDEA 注释报红解决
- Use of QML
- Analysis summary - self-use
- Installation of mysql5.7.37 under CentOS7 [perfect solution]
- Detailed explanation of TCP (1)
- CloudCompare&PCL 计算两个点云之间的重叠度
- TCP和UDP详解
猜你喜欢
随机推荐
endian mode
Multilingual settings of php website (IP address distinguishes domestic and foreign)
[C language] Preprocessing operation
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
刚出道“一战成名”,安全、舒适一个不落
解析小结—自用
LeetCode每日一练 —— 138. 复制带随机指针的链表
C primer plus学习笔记 —— 8、结构体
6. Display comments and replies
自己的一些思考
浅识Flutter 基本组件之CheckboxListTile组件
WebSocket Session is null
2022牛客多校联赛第四场 题解
The Map Entry understanding and application
数据库实现分布式锁
WebSocket Session为null
Chapter 9 SVM实践
Installation of mysql5.7.37 under CentOS7 [perfect solution]
【C语言】三子棋(经典解法+一览图)
C# remote debugging