当前位置:网站首页>【n-piece chess】
【n-piece chess】
2022-08-07 06:36:00 【lazy panda】
文章目录
点击跳转:井字棋
前言
大家好,Today we come to the second part of the three-piece game,在本章,What we want to do is an advanced introduction to three-bang(In fact, it is just some small conjectures of the blogger Panda himself),This time compared to the previous chapter,There are three changes:
1.define定义的row和col可以更改(最小为3);
2.Added tooltips to the initial menu:nLianzi wins;
3.The winning and losing part is no longer listed one by one in the mechanical version,Instead, conditions are given for automatic judgment.
示例如下 :
一、内容封装
game.h:头文件的引入,全局变量、自定义函数的声明
game.c:自定义函数的实现
test.c:主函数所在
二、基本流程
(一)游戏菜单
游戏第一步当然是设计游戏菜单来告诉玩家如何开始游戏
代码如下(示例):
void Menu() {
// 以五子棋为例
puts("********************");
puts("***** 1.play *****");
puts("***** 0.quit *****");
puts("********************");
printf("**** %dLianzi wins ****\n", ROW / 2 + 2);
}

(二)数组初始化
我们在创建数组时给数组赋值为‘0’,而字符‘0’是不会在屏幕上输出的,
为了让玩家知道应该在何处下棋,我们可以将他赋值为空格
代码如下(示例):
void Init(char arr[ROW][COL], int row, int col) {
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
arr[i][j] = ' ';
}
(三)棋盘打印
该游戏名为井字棋,那么我们的棋盘当然是要设置为‘井’字型才更加彰显个性喽!
代码如下(示例):
void Chess(char arr[ROW][COL],int row,int col) {
int i = 0;
int j = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf(" %c ", arr[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");
}
}

(四)玩家落子
玩家通过棋盘的坐标落子,我们让玩家使用‘X’作为棋子,
这里我们就要来判断该坐标是否存在,存在的话该坐标是否为空
代码入下(示例):
void Piece_w(char arr[ROW][COL], int row, int col, char ch) {
int x = 0;
int y = 0;
printf("玩家落子:");
while (1) {
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
//位置存在
if (arr[x - 1][y - 1] == ' ') {
//该位置为空
arr[x - 1][y - 1] = ch;
break;
}
else
printf("该位置不为空,请重新输入\n");
}
else
printf("该位置不存在,请重新输入\n");
}
}
(五)电脑落子
电脑落子和和玩家不同的一点为:x,y为随机值,其他判断条件相同
void Piece_c(char arr[ROW][COL], int row, int col, char ch) {
printf("电脑落子:");
while (1) {
int x = rand() % row;
int y = rand() % col;
if (x >= 0 && x < row && y >= 0 && y < col) {
//位置存在
if (arr[x][y] == ' ') {
//该位置为空
printf("%d %d\n", x + 1, y + 1);
arr[x][y] = ch;
break;
}
}
}
}
(六)判断输赢
井字棋获胜的条件为:有三个连续且相同的棋子
这里分为三种情况:行连续,列连续以及对角线连续;
如果没有人获胜的话就判断是否为平局.
//This function is only used in this file,所以加上static限制
static char draw(char arr[ROW][COL], int row, int col) {
//判断平局
int i = 0;
int j = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (arr[i][j] == ' ')//If there is a space, the game continues
return 'c';
}
}
return 'p';
}
char Judge(char arr[ROW][COL], int row, int col) {
int i = 0, j = 0, n = 0;
for (i = 0; i < row; i++){
// 行
for (j = 1; j < col; j++){
if (arr[i][j - 1] != arr[i][j]) {
//Here it is judged whether it is equal,Check if there is a space below
if (i <= row / 2 - 2)//Since one line is requiredrow/2+2Only a couple of sons can win,
//So if the frontrow-(row/2+2)More than one piece is not the same
continue;
else
//It's impossible to get together in this linerow/2+2个相同的棋子,所以直接break跳出,后面的colAnd the same goes for the diagonal
break;
}
else if (arr[i][j - 1] != ' ')
n++;//same logarithm row/2-1 对
if (n == col / 2 + 1)
return arr[i][j - 1];
}
// 列
n = 0;
for (j = 1; j < row; j++){
if (arr[j - 1][i] != arr[j][i]) {
if (i <= row / 2 - 2)
continue;
else
break;
}
else if (arr[j - 1][i] != ' ')
n++;
if (n == row / 2 + 1)
return arr[j - 1][i];
}
}
// '\'
n = 0;
for (i = 1; i < row; i++){
if (arr[i - 1][i - 1] != arr[i][i]) {
if (i <= row / 2 - 1)
continue;
else
break;
}
else if (arr[i - 1][i - 1] != ' ')
n++;
if ((n == row / 2 + 1))
return arr[i - 1][i - 1];
}
// '/'
n = 0;
for (i = 1; i < row; i++){
if (arr[i - 1][row - i] != arr[i][row - i - 1]) {
if (i <= row / 2 - 1)
continue;
else
break;
}
else if (arr[i - 1][row - i] != ' ')
n++;
if ((n == row / 2 + 1))
return arr[i - 1][row - i];
}
// 判断平局
return draw(arr,row,col);
}
三、运行实例


四、完整代码
(一)game.h
代码如下(示例):
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 5
#define COL 5
void Menu();
void Init(char arr[ROW][COL], int row, int col);
void Chess(char arr[ROW][COL], int row, int col);
void Piece_w(char arr[ROW][COL], int row, int col, char ch);
void Piece_c(char arr[ROW][COL], int row, int col, char ch);
char Judge(char arr[ROW][COL], int row, int col);
(二)game.c
代码如下(示例):
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Menu() {
puts("********************");
puts("***** 1.play *****");
puts("***** 0.quit *****");
puts("********************");
printf("**** %dLianzi wins ****\n", ROW / 2 + 2);
}
void Init(char arr[ROW][COL], int row, int col) {
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
arr[i][j] = ' ';
}
void Chess(char arr[ROW][COL],int row,int col) {
int i = 0;
int j = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf(" %c ", arr[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 Piece_w(char arr[ROW][COL], int row, int col, char ch) {
int x = 0;
int y = 0;
printf("玩家落子:");
while (1) {
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
//位置存在
if (arr[x - 1][y - 1] == ' ') {
//该位置为空
arr[x - 1][y - 1] = ch;
break;
}
else
printf("该位置不为空,请重新输入\n");
}
else
printf("该位置不存在,请重新输入\n");
}
}
void Piece_c(char arr[ROW][COL], int row, int col, char ch) {
printf("电脑落子:");
while (1) {
int x = rand() % row;
int y = rand() % col;
if (x >= 0 && x < row && y >= 0 && y < col) {
//位置存在
if (arr[x][y] == ' ') {
//该位置为空
printf("%d %d\n", x + 1, y + 1);
arr[x][y] = ch;
break;
}
}
}
}
static char draw(char arr[ROW][COL], int row, int col) {
int i = 0;
int j = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (arr[i][j] == ' ')
return 'c';
}
}
return 'p';
}
char Judge(char arr[ROW][COL], int row, int col) {
int i = 0, j = 0, n = 0;
for (i = 0; i < row; i++){
// 行
for (j = 1; j < col; j++){
if (arr[i][j - 1] != arr[i][j]) {
if (i <= row / 2 - 2)
continue;
else
break;
}
else if (arr[i][j - 1] != ' ')
n++;
if (n == col / 2 + 1)
return arr[i][j - 1];
}
// 列
n = 0;
for (j = 1; j < row; j++){
if (arr[j - 1][i] != arr[j][i]) {
if (i <= row / 2 - 2)
continue;
else
break;
}
else if (arr[j - 1][i] != ' ')
n++;
if (n == row / 2 + 1)
return arr[j - 1][i];
}
}
// '\'
n = 0;
for (i = 1; i < row; i++){
if (arr[i - 1][i - 1] != arr[i][i]) {
if (i <= row / 2 - 1)
continue;
else
break;
}
else if (arr[i - 1][i - 1] != ' ')
n++;
if ((n == row / 2 + 1))
return arr[i - 1][i - 1];
}
// '/'
n = 0;
for (i = 1; i < row; i++){
if (arr[i - 1][row - i] != arr[i][row - i - 1]) {
if (i <= row / 2 - 1)
continue;
else
break;
}
else if (arr[i - 1][row - i] != ' ')
n++;
if ((n == row / 2 + 1))
return arr[i - 1][row - i];
}
// 平局
return draw(arr,row,col);
}
//char Judge(char arr[ROW][COL], int row, int col) {
// int i = 0;
// //row
// for (i = 0; i < row; i++) {
// if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][1] != ' ')
// return arr[i][0];
// }
// //col
// for (i = 0; i < col; i++) {
// if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i] && arr[1][i] != ' ')
// return arr[0][i];
// }
// //对角线
// if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[1][1] != ' ')
// return arr[1][1];
// if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[1][1] != ' ')
// return arr[1][1];
// //平局
// return draw(arr, row, col);
//}
(三)test.c
代码如下(示例):
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Game(char arr[ROW][COL]) {
//设置棋盘
Chess(arr, ROW, COL);
char ch = 'c';
while (1) {
//下棋
Piece_w(arr, ROW, COL, 'X');
Chess(arr, ROW, COL);
//判断
ch = Judge(arr, ROW, COL);
if (ch != 'c')
break;
Piece_c(arr, ROW, COL, 'O');
Chess(arr, ROW, COL);
ch = Judge(arr, ROW, COL);
if (ch != 'c')
break;
}
if ('X' == ch)
printf("玩家获胜\n");
else if ('O' == ch)
printf("电脑获胜\n");
else
printf("平局\n");
}
int main() {
char arr[ROW][COL] = {
0 };
srand((unsigned int)time(NULL));
int input = 0;
do {
//菜单
Menu();
//初始化棋子
Init(arr, ROW, COL);
printf("请选择:");
scanf("%d", &input);
switch (input) {
case 1:
Game(arr);
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("无效数字,请重新输入\n");
break;
}
system("pause");
system("cls");
} while (input);
return 0;
}
总结
That's all we've learned together today,If you want to expand the board later, you just need to change itrow和col即可.另外,In fact, there are many shortcomings in the supplement of this chapter,For example, the judgment on the diagonal can only be established on the main and auxiliary diagonals,And if the board is too big, it's too difficult to win,大家也可以发挥自己的想象力,If you have other good ideas, you can leave a message in the comment area,在此感谢大家对
的支持,这是熊猫持续更新下去的巨大推进力!
边栏推荐
- servlet 教程 1:环境搭建和新建 servlet 项目
- R语言结合并行计算的实例一文讲懂环境
- Taro官网 写法最佳实践
- VoLTE基础自学系列 | IMS网络中的IP层路由寻址过程(注册流程中的实现)
- VoLTE基础自学系列 | IMS、VOIP、VoLTE、RCS的关系?
- LeetCode 剑指 Offer 09. 用两个栈实现队列
- 路由交换综合实验
- grid grid layout
- 360全网数字安全大脑获“数字经济创新引领成果”奖项
- The spyder/conda installation package reports an error: conda info could not be constructed. KeyError: 'pkgs_dirs'
猜你喜欢
随机推荐
VoLTE基础自学系列 | IMS网络概述
微信小程序--》小程序全局配置和详解下拉刷新和上拉触底页面事件
grid grid layout
R语言包的升级与降级
LeetCode 628. 三个数的最大乘积
VoLTE Basic Self-Learning Series | What are transparent data and non-transparent data in VoLTE?
VoLTE Basic Self-Learning Series | What is Forking in SIP and IMS
Tencent cloud deployment
js 几种继承的方式
[acwing周赛复盘] 第 63 场周赛20220806
Detailed explanation of fixture test fixture of pytest framework
【n子棋】
netstat&firewall
Swordsman Offer II 091. Paint the House
Related methods of getting the absolute path of the current file in the OS module
C语言力扣第60题之排列序列。广度优先搜索、简单除法定位
QGIS最受欢迎的20个插件
spyder/conda安装包报错:conda info could not be constructed. KeyError: ‘pkgs_dirs‘
VoLTE Basic Self-Learning Series | The relationship between IMS, VOIP, VoLTE, and RCS?
leetcode 110. Balanced Binary Trees









