当前位置:网站首页>[Three sons] C language implements simple three sons
[Three sons] C language implements simple three sons
2022-08-02 00:06:00 【i.e. ounces】
Two days ago, I learned about arrays and their simple applications——三子棋,这里,Write it in the form of a blog,While reviewing and consolidating, I hope it can help you.
文章目录
一、项目要求
Use two-dimensional arrays to complete three pieces of chess,realize its various functions.
二、项目功能的实现
1.棋盘的初始化及打印
要开始游戏,First we need to print out the chessboard:
Since just starting the game,Neither the player nor the computer played chess,因此,We need to move the data rows of the chessboard初始化为空格.
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
打印棋盘时,We can decompose the entire chessboard into data rows and separator rows,And take one data row and one separator row as a group,Loop prints based on the desired effect constraints.while in a row of data,We have to combine a data with a delimiter ‘|’ 作为一组,The same restrictions are used for loop printing.
void display_board(char board[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 ", 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");
}
}
}
2.玩家下棋
开始游戏后,The players play chess first.
The player enters the coordinates to drop(x,y),先判断坐标的合法性(Whether it is within the bounds of the chessboard),合法后,Then judge whether the coordinate has been occupied,若没有,则落子 ‘*’.
值得注意的是,Because of the player's thinking,棋盘的行、列都是从1开始,因此,when we judge legality,x范围应在1~ROW(最大行),y范围应在1 ~ COL(最大列),When judging whether the coordinates are occupied,应判断x-1,y-1.
void player_game(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("请玩家下棋!\n");
printf("请输入要下的坐标:");
//Check whether the input coordinates are correct
while (1)
{
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("该坐标已被占用,请重新输入:");
}
}
else
{
printf("The coordinates do not conform to the rules,请重新输入:");
}
}
}
3.电脑下棋
玩家下棋后,A random number is then generated by the computerx,yPlay chess.
利用rand()函数产生随机数,To be guaranteed to be producedx,y合法,Just take the generated random numbers separatelyrow,col的余数即可,这样,得到的x,yThe scope is always there0~row-1,0 ~ col-1.得到x,y的值后,Then judge whether the coordinate is occupied,若没有,则落子 ‘#’.
在这里,由于是电脑下棋,因此,If the generated coordinates are already occupied,It doesn't have to be displayed on the screen,Just regenerate the random number directly.
void computer_game(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋!\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
4.判断游戏状态
在这个程序中,Probably the most complicated part is judging winners and losers.We can divide the game state into 4种:
’ * '——玩家赢;
’ # '——电脑赢
’ Q '——平局
’ C '——游戏继续
If there is a winner,There are three ways to win:行、列、对角线.By judging these three cases and returning any data that matches one of them,We can get a winner.
若平局,That is, the board is full,But still no winner,Then after each chess game, we can first judge whether there is a winner,若没有,Then determine whether the chessboard is full,若棋盘已满,则为平局,返回’ Q '.
If there is no winner,The board is also not full,则返回’ C ',Start the next round of chess.
//判断棋盘是否已满
int full_board(char board[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 (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char is_win(char board[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 (board[i][j] == board[i][(j + 1)%3] && board[i][(j + 1) % 3] == board[i][(j + 2) % 3] && board[i][j] != ' ')
return board[i][0];
}
}
//列
for (j = 0; j < col; j++)
{
for (i = 0; i < row; i++)
{
if (board[i][j] == board[(i + 1)%3][j] && board[(i + 1) % 3][j] == board[(i + 2) % 3][j] && board[i][j] != ' ')
return board[i][j];
}
}
//主对角线
for (i = 0; i < row; i++)
{
if (board[i][i] == board[(i + 1) % 3][(i + 1) % 3] && board[(i + 1) % 3][(i + 1) % 3] == board[(i + 2) % 3][(i + 2) % 3] && board[i][i] != ' ')
return board[i][i];
}
//副对角线
for (i = 0; i < row - 2; i++)
{
for (j = col - 1; j >= 0; j--)
{
if (board[i][j] == board[(i + 1)%3][j - 1] && board[(i + 1)%3][j - 1] == board[(i + 2)%3][j - 2] && board[i][j] != ' ')
return board[i][j];
}
}
if (1 == full_board(board, row, col))
{
return 'Q';
}
return 'C';
}
三、项目演示
1.棋盘的打印
2.玩家下棋
3.电脑下棋
4.输/win decision
四、代码展示
1.game.h
#pragma once
#include <stdio.h>
#define ROW 3
#define COL 3
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_game(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_game(char board[ROW][COL], int row, int col);
//判断游戏状态
char is_win(char board[ROW][COL], int row, int col);
2.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
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 = 0;
int j = 0;
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_game(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("请玩家下棋!\n");
printf("请输入要下的坐标:");
//Check whether the input coordinates are correct
while (1)
{
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("该坐标已被占用,请重新输入:");
}
}
else
{
printf("The coordinates do not conform to the rules,请重新输入:");
}
}
}
void computer_game(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋!\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int full_board(char board[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 (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char is_win(char board[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 (board[i][j] == board[i][(j + 1)%3] && board[i][(j + 1) % 3] == board[i][(j + 2) % 3] && board[i][j] != ' ')
return board[i][0];
}
}
//列
for (j = 0; j < col; j++)
{
for (i = 0; i < row; i++)
{
if (board[i][j] == board[(i + 1)%3][j] && board[(i + 1) % 3][j] == board[(i + 2) % 3][j] && board[i][j] != ' ')
return board[i][j];
}
}
//主对角线
for (i = 0; i < row; i++)
{
if (board[i][i] == board[(i + 1) % 3][(i + 1) % 3] && board[(i + 1) % 3][(i + 1) % 3] == board[(i + 2) % 3][(i + 2) % 3] && board[i][i] != ' ')
return board[i][i];
}
//副对角线
for (i = 0; i < row - 2; i++)
{
for (j = col - 1; j >= 0; j--)
{
if (board[i][j] == board[(i + 1)%3][j - 1] && board[(i + 1)%3][j - 1] == board[(i + 2)%3][j - 2] && board[i][j] != ' ')
return board[i][j];
}
}
if (1 == full_board(board, row, col))
{
return 'Q';
}
return 'C';
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("********************************\n");
printf("******* 1.PLAY *********\n");
printf("******* 0.EXIT *********\n");
printf("********************************\n");
}
//玩家赢——‘*’
//电脑赢——‘#’
//平局 ——‘Q’
//游戏继续——‘C’
void game()
{
char ret = 0;
char board[ROW][COL] = {
0 };
init_board(board, ROW, COL);
display_board(board, ROW, COL);
while (1)
{
player_game(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
computer_game(board, ROW, COL);
display_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家胜利!\n");
else if (ret == '#')
printf("电脑胜利!\n");
else if (ret == 'Q')
printf("平局!\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请输入你的选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏!\n");
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}
五、总结
The code for this project is not difficult,What matters is the logical order,Especially the part about winning and losing.Just figure out the logic,The code is generally written relatively smoothly.
If you see anything you don't understand,You can comment or ask me privately.If something is wrong,I also hope that you will be able to enlighten me!
边栏推荐
- 辛普森悖论
- 使用Jenkins做持续集成,这个知识点必须要掌握
- UI自动化测试框架搭建-标记性能较差用例
- 在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
- 【三子棋】C语言实现简易三子棋
- easy-excel 解决百万数据导入导出,性能很强
- 云原生DevOps环境搭建
- YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
- Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
猜你喜欢
Spark Sql之join on and和where
Building a cloud-native DevOps environment
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
background-image使用
使用 Zadig 交付云原生微服务应用
检查 Oracle 版本的 7 种方法
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
Enterprise firewall management, what firewall management tools are there?
【MySQL篇】初识数据库
Axure教程-新手入门基础(小白强烈推荐!!!)
随机推荐
Classical Literature Reading--DLO
如何用Redis实现分布式锁?
Bean的生命周期
TexturePacker使用文档
Spark Sql之join on and和where
递归:方法调用自身
Work for 5 years, test case design is bad?To look at the big case design summary
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
在CentOS下安装MySQL
cmd command
一个有些意思的项目--文件夹对比工具(一)
ansible模块--copy模块
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
Building a cloud-native DevOps environment
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
云原生DevOps环境搭建
2022第六届强网杯部分wp
FAST-LIO2代码解析(二)
numpy.around