当前位置:网站首页>Notes on chess (C language)
Notes on chess (C language)
2022-08-06 05:14:00 【RoseLJ】
目录
实现思路:
1.代码搭建:
2.框架搭建:
3.菜单功能实现:
4.游戏功能实现:
1.game()函数:
5.主要函数:
1.创建棋盘并初始化:
2.打印棋盘:
3.玩家下棋:
4.电脑下棋:
5.判断输赢:
6.整体代码:
实现思路:
1.代码搭建:
使用的vs2019编译器进行编译
test.c文件实现整个游戏框架,game.c文件实现整个游戏的具体游戏功能,game.h文件实现整个游戏需要的声明和头文件.
2.框架搭建:
让玩家来选择开始游戏,或者退出游戏,为了让玩家玩完一把继续下一把,采用do……while循环
int main()
{
int input = 0;
do
{
menu();
printf("Please select the item you want to select\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("您输入的选择有误,请重新选择\n");
break;
}
} while (input);
}
3.菜单功能实现:
1.创建菜单menuFunctions allow players to make choices
void menu()
{
printf("************************\n");
printf("****** 1.play ******\n");
printf("****** 0.eixt ******\n");
printf("************************\n");
}
4.游戏功能实现:
在game()函数内实现:
1.game()函数:
void game()
{
//创建棋盘
char board[ROW][COL] = { 0 };
//初始化棋盘
init_board(board, ROW, COL);
//打印棋盘
print_board(board, ROW, COL);
char ret = '0';//接受游戏状态
while (1)
{
//man starts playing chess
people_play(board, ROW, COL);
print_board(board, ROW, COL);
//判断输赢
ret = judge_lw(board, ROW, COL);
if (ret != 'C')
break;
//电脑下棋
com_play(board, ROW, COL);
print_board(board, ROW, COL);
ret = judge_lw(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
}
5.主要函数:
1.创建棋盘并初始化:
本函数init_board()The main function is to initialize the chessboard,Assign all board spaces to spaces,
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] = ' ';
}
}
}
2.打印棋盘:
print_board()The main function of the function is to print the chessboard and display it on the screen; A printed checkerboard The method is to use two layers of loops to print out the space of the move first(%c),Then print the outline;
3.玩家下棋:
people_play()The function is used to make the player play chess ;首先从键盘录入两个坐标,判断坐标的合法性,如果是空格(” “)则赋值"*"(玩家下的棋子);
4.电脑下棋:
com_play()The function implements the computer to play chess;Computers play chess using random numbers 需要现在main函数先声明 :srand((unsigned int)time(NULL));
然后再用 rand()函数设置随机数
注:srand rand 的头文件是#include<stdlib.h>
time的头文件是#include<time.h>
5.判断输赢:
judge_lw()The function is used to determine winning or losing,如果返回"*"则代表玩家赢,返回"#"则代表电脑赢,返回"Q"则代表平局,返回"C"则代表继续
实现IsFullThe function is used to determine a draw
Return if the game is full1 ,不满返回0
6.整体代码:
test.c 文件
#include"game.h"
void menu()
{
printf("************************\n");
printf("****** 1.play ******\n");
printf("****** 0.eixt ******\n");
printf("************************\n");
}
void game()
{
//创建棋盘
char board[ROW][COL] = { 0 };
//初始化棋盘
init_board(board, ROW, COL);
//打印棋盘
print_board(board, ROW, COL);
char ret = '0';//接受游戏状态
while (1)
{
//man starts playing chess
people_play(board, ROW, COL);
print_board(board, ROW, COL);
//判断输赢
ret = judge_lw(board, ROW, COL);
if (ret != 'C')
break;
//电脑下棋
com_play(board, ROW, COL);
print_board(board, ROW, COL);
ret = judge_lw(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("Please select the item you want to select\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("您输入的选择有误,请重新选择\n");
break;
}
} while (input);
}geme.h文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define COL 3
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void print_board(char board[ROW][COL], int row, int col);
//人下棋
void people_play(char board[ROW][COL], int row, int col);
//电脑下棋
void com_play(char board[ROW][COL], int row, int col);
//判断输赢
char judge_lw(char board[ROW][COL], int row, int col);game.c文件
#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 print_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)
{
printf("---|---|---\n");
}
}
}
//人下棋
void people_play(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("Please select the coordinates you want to fill in\n");
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("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
//电脑下棋
void com_play(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(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 judge_lw(char board[ROW][COL], int row, int col)
{
int i = 0;
//判断三行
for (i = 0;i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
//判断三列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[0][i];
}
}
//判断斜线
if (board[0][0] == board[1][1] && board[2][2] == board[0][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//判断平局
//Return if the game is full1 ,不满返回0
int ret = IsFull(board, row, col);
if (ret == 1)
{
return 'Q';
}
//继续
return 'C';
}
边栏推荐
猜你喜欢

Docker基础(七)—安装mysql服务

dedecms collection selection content model display complete

MySQL - 事务四大特性、事务隔离级别、事务的脏读、不可重复读、幻读

Clion sets toolchains error

响应式织梦模板潜水游泳类网站

Temperature (acute/PH/electric/temperature/PH sensitive/double magnetic field sensitive hydrogel preparation

HCL(二)—配置FTP服务器

Zabbix 5.0 监控教程(三)

Practical debugging tips

NFT Insider #69:星巴克将公布基于Web3的忠诚度计划,林俊杰宣布持有蒂芙尼NFT
随机推荐
[C language array subscript out of bounds] Infinite loop caused by array subscript out of bounds
数据库基础
Basis of database
【D1 Dock Pro Development Board】LED light flashing
fegin feign.FeignException: status 404 reading StudentService#getAll()
Introduction and use of Jhipster
Why does Baidu Intelligent Cloud Digital Human lead the Chinese AI Digital Human?
The difference between defineProperty and proxy
使用 Monaco Editor 开发一个 SQL 编辑器
flinkcdc mysql DataStream API问题错在哪?
MySQL - 事务四大特性、事务隔离级别、事务的脏读、不可重复读、幻读
响应式织梦模板潜水游泳类网站
el-table筛选数据
MySQL - MySQL 常用存储引擎简介
golang 处理变量模板
论文阅读 (65):RePaint: Inpainting using Denoising Diffusion Probabilistic Models
Zabbix 5.0 监控教程(三)
当下企业源代码数据防泄密工作该如何进行
aws篇8
离散信源最大熵定理的Matlab实现