当前位置: 首页 > news >正文

网站平台建设百度网站分析

网站平台建设,百度网站分析,官方网站建设平台,互动案例的网站五子棋的历史背景: 五子棋起源于中国,是全国智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏。双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成五子连珠者获胜。五子棋容易上手&#xff0c…

五子棋的历史背景:

五子棋起源于中国,是全国智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏。双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成五子连珠者获胜。

五子棋容易上手,老少皆宜,而且趣味横生,引人入胜。它不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。


不难看出,五子棋是我们中华民族发明的,我们必须了解他,作为程序员,我们更加要懂得如何把五子棋的代码给敲出来,我研究了一天了,调试了无数次,修改了无数错误,到最后运行成功的那一刹那,我就很开心,就想以博客的形式分享给大家!

一.设计过程步骤

ps:整个五子棋一共有三个文件,200多行代码,不复杂,多琢磨,很好懂,分为text.c,game.h,game.c文件,在最后会给出完整代码。

1.初始化二维数组

为了数组里面存放的每个数是我们想初始化的,我们初始化一个二维数组(方阵的格式,即 n*n)用来存放棋子,里面的元素可以按照我们的意愿来初始化;在此代码中将数组中每个元素初始化为一个空格。

#define ROW 5
#define COL 5
void InitBoard(char board[ROW][COL], int row, int col)
{int i = 0; int j = 0;for (i = 0; i < ROW; i++){for (int j = 0; j < COL; j++){board[i][j] = ' ';}}
}

2.构造一个棋盘

在初始化一个数组后的基础上我们来构造一个棋盘,使得整个代码的结构更加清晰,可读性高。

#define ROW 5
#define COL 5
void DisplayBoard(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]);//注意:%c的左右是有两个空格的if (j < col -1)//最后一列不打印|,也就是打印4个|,只是为了美观,不加也行,见下图printf("|");}printf("\n");if (i < row - 1)//最后一行不进行打印---和|,见下图{int j = 0;for (j = 0; j < col; j++){printf("---");if (j < col - 1)//和上面一样,见下图printf("|");}printf("\n");}}
}

3.玩家走法

//玩家
void PlayerMove(char board[ROW][COL], int row, int col)
{int x = 0;int y = 0;printf("玩家走:>");while (1){printf("请输入下棋的坐标\n");scanf_s("%d %d", &x, &y);//判断坐标的合法性if (x >= 1 && x <= row && y >= 1 && y <= col){//下棋//坐标是否被占用if (' ' == board[x - 1][y - 1])            //等号量变也可以调换,我这样写要是出错那编译器会提示错误,如果换过来的话那就不会提示错误了,这样找bug可能会很难受
//如果是空格的话,这个位置就是空的,可以下棋{board[x - 1][y - 1] ='*';break;}else{printf("坐标被占用,请重新输入\n");}}else{printf("坐标非法,请重新选择\n");}}
}

4.电脑随机下棋

//电脑
void ComputerMove(char board[ROW][COL], int row, int col)
{        printf("电脑走:>\n");while (1){int x = rand() % ROW;//rand是根据系统的情况来自动生成一个随机数int y = rand() % COL;//判断占用if (board[x][y] == ' '){board[x][y] = '#';break;}}
}

5.判断是否能分出胜负

//判断游戏是否有输赢
char IsWin(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][2] == board[i][3] && board[i][3] == board[i][4] && 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[2][i] == board[3][i] && board[3][i] == board[4][i] && board[1][i] != ' '){return board[i][1];}}//判断\对角线元素是否相等if (board[0][0] == board[1][1] && board[1][1] == board[2][2]&&board[2][2] == board[3][3]&&board[3][3] == board[4][4] && board[1][1]!=' '){return board[1][1];}//判断/对角线元素是否相等if ( board[4][0]== board[3][1] && board[3][1] == board[2][2]&& 
board[2][2] == board[1][3] &&board[1][3] == board[0][4] && board[1][1]!=' '){return board[1][1];}

6.判断是否平局

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;
}//判断平局//如果棋盘满了则返回1,不满返回0int ret = IsFull(board, row, col);if (ret == 1){return 'Q';}return 'C';

以下是game.h文件的代码

#pragma once
//头文件的包含
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
//符号定义
#define ROW 5
#define COL 5//函数声明
//初始化棋盘的void InitBoard(char board[ROW][COL], int row, int col);//打印棋盘的函数void DisplayBoard(char board[ROW][COL], int row, int col);//玩家下棋void PlayerMove(char board[ROW][COL], int row, int col);//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col);char IsWin(char board[ROW][COL], int row, int col);

以下是text.c文件的代码

#include"game.h"
void menu()
{printf("*******************************\n");printf("*********  1.play  ************\n");printf("*********  0.exit  ************\n");printf("*******************************\n");}
void game()
{//储存数据--二维数组char board[ROW][COL];//初始化棋盘为空格InitBoard(board, ROW, COL);//打印棋盘--本质打印数组的内容DisplayBoard(board,ROW,COL);char ret = 0;while (1){//玩家下棋PlayerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);//判断玩家是否赢ret = IsWin(board, ROW, COL);if (ret != 'C')break;//电脑下棋ComputerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);ret = IsWin(board, ROW, COL);if (ret =! 'C')break;}if (ret == '*')printf("玩家胜利\n");else if (ret == '#')printf("电脑胜利\n");elseprintf("平局\n");DisplayBoard(board, ROW, COL);}
int main()
{srand((unsigned int) time(NULL));int input = 0;do{menu();printf("请选择:>");scanf_s("%d", &input);switch (input){case 1:game();break;case 0:printf("退出游戏\n");break;default:printf("选择错误,重新选择\n");break;}} while (input);return 0;
}

二.完整代码:

game.h文件

#pragma once
//头文件的包含
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
//符号定义
#define ROW 5
#define COL 5//函数声明
//初始化棋盘的void InitBoard(char board[ROW][COL], int row, int col);//打印棋盘的函数void DisplayBoard(char board[ROW][COL], int row, int col);//玩家下棋void PlayerMove(char board[ROW][COL], int row, int col);//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col);char IsWin(char board[ROW][COL], int row, int col);

game.c文件

#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{int i = 0; int j = 0;for (i = 0; i < ROW; i++){for (int j = 0; j < COL; j++){board[i][j] = ' ';}}}void DisplayBoard(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){int j = 0;for (j = 0; j < col; j++){printf("---");if (j < col - 1)printf("|");}printf("\n");}//printf("---|---|---\n");}
}
//玩家
void PlayerMove(char board[ROW][COL], int row, int col)
{int x = 0;int y = 0;printf("玩家走:>");while (1){printf("请输入下棋的坐标\n");scanf_s("%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 ComputerMove(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 IsWin(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][2] == board[i][3] && board[i][3] == board[i][4] && 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[2][i] == board[3][i] && board[3][i] == board[4][i] && board[1][i] != ' '){return board[i][1];}}//判断\对角线if (board[0][0] == board[1][1] && board[1][1] == board[2][2]&& board[2][2] == board[3][3] &&board[3][3] == board[4][4] && board[1][1]!=' '){return board[1][1];}//判断/对角线if ( board[4][0]== board[3][1] && board[3][1] == board[2][2]&& board[2][2] == board[1][3] &&board[1][3] == board[0][4] && board[1][1]!=' '){return board[1][1];}//判断平局//如果棋盘满了则返回1,不满返回0int ret = IsFull(board, row, col);if (ret == 1){return 'Q';}return 'C';}

以下是text.c文件:

#include"game.h"
void menu()
{printf("*******************************\n");printf("*********  1.play  ************\n");printf("*********  0.exit  ************\n");printf("*******************************\n");}
void game()
{//储存数据--二维数组char board[ROW][COL];//初始化棋盘为空格InitBoard(board, ROW, COL);//打印棋盘--本质打印数组的内容DisplayBoard(board,ROW,COL);char ret = 0;while (1){//玩家下棋PlayerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);//判断玩家是否赢ret = IsWin(board, ROW, COL);if (ret != 'C')break;//电脑下棋ComputerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);ret = IsWin(board, ROW, COL);if (ret =! 'C')break;}if (ret == '*')printf("玩家胜利\n");else if (ret == '#')printf("电脑胜利\n");elseprintf("平局\n");DisplayBoard(board, ROW, COL);}
int main()
{srand((unsigned int) time(NULL));int input = 0;do{menu();printf("请选择:>");scanf_s("%d", &input);switch (input){case 1:game();break;case 0:printf("退出游戏\n");break;default:printf("选择错误,重新选择\n");break;}} while (input);return 0;
}

最后,大家有问题可以指出来,关个注,点个赞,评个论,我们一起进步!

2023.02.09

From:努力进大厂的新青年


文章转载自:
http://faceplate.dztp.cn
http://paratonic.dztp.cn
http://riposte.dztp.cn
http://laburnum.dztp.cn
http://catechism.dztp.cn
http://phrasal.dztp.cn
http://tagger.dztp.cn
http://knuckleheaded.dztp.cn
http://galalith.dztp.cn
http://fab.dztp.cn
http://overperform.dztp.cn
http://cootie.dztp.cn
http://palpebrate.dztp.cn
http://refugee.dztp.cn
http://jewbaiter.dztp.cn
http://inventec.dztp.cn
http://hyraces.dztp.cn
http://grumbler.dztp.cn
http://underdrawers.dztp.cn
http://satirize.dztp.cn
http://erotophobic.dztp.cn
http://prentice.dztp.cn
http://saseno.dztp.cn
http://silicic.dztp.cn
http://resuscitative.dztp.cn
http://appellee.dztp.cn
http://tatar.dztp.cn
http://dpt.dztp.cn
http://dignitarial.dztp.cn
http://themselves.dztp.cn
http://centrad.dztp.cn
http://tyche.dztp.cn
http://corynebacterium.dztp.cn
http://embracer.dztp.cn
http://subcutis.dztp.cn
http://achaetous.dztp.cn
http://menstrual.dztp.cn
http://spumy.dztp.cn
http://contender.dztp.cn
http://coagulant.dztp.cn
http://skyscape.dztp.cn
http://caprifig.dztp.cn
http://isograph.dztp.cn
http://biggest.dztp.cn
http://nonpolar.dztp.cn
http://leafhopper.dztp.cn
http://sometimes.dztp.cn
http://covering.dztp.cn
http://yaup.dztp.cn
http://tawney.dztp.cn
http://hypophyseal.dztp.cn
http://gigantean.dztp.cn
http://trechometer.dztp.cn
http://yom.dztp.cn
http://radiopaque.dztp.cn
http://dino.dztp.cn
http://macrencephaly.dztp.cn
http://fboa.dztp.cn
http://kennelly.dztp.cn
http://chorda.dztp.cn
http://nonfulfilment.dztp.cn
http://hesitation.dztp.cn
http://dews.dztp.cn
http://slipcase.dztp.cn
http://sallet.dztp.cn
http://ramee.dztp.cn
http://dicotyl.dztp.cn
http://larva.dztp.cn
http://chinese.dztp.cn
http://pediment.dztp.cn
http://umptieth.dztp.cn
http://chalkiness.dztp.cn
http://prosperously.dztp.cn
http://provocator.dztp.cn
http://felucca.dztp.cn
http://cowlike.dztp.cn
http://trivalve.dztp.cn
http://kaiser.dztp.cn
http://heeling.dztp.cn
http://ruritan.dztp.cn
http://neoteric.dztp.cn
http://carices.dztp.cn
http://socinian.dztp.cn
http://tenth.dztp.cn
http://intranasal.dztp.cn
http://jamaica.dztp.cn
http://pyrrhuloxia.dztp.cn
http://functionalize.dztp.cn
http://duchess.dztp.cn
http://courthouse.dztp.cn
http://autogiro.dztp.cn
http://nailing.dztp.cn
http://vaporific.dztp.cn
http://creditiste.dztp.cn
http://nutmeg.dztp.cn
http://offramp.dztp.cn
http://gawd.dztp.cn
http://isotropism.dztp.cn
http://aristarchy.dztp.cn
http://desalination.dztp.cn
http://www.dt0577.cn/news/101098.html

相关文章:

  • 莱芜市官网成都seo优化
  • 做问卷网站百度搜索数据统计
  • 绿色家园网站怎么做长沙网站优化方法
  • 有没有做生物科技相关的网站海阳seo排名优化培训
  • 网站开发业务流程图湖南长沙最新疫情
  • 朔州公司做网站成都网站优化排名推广
  • 商业网站策划书范文指数函数公式
  • 网站建设优化七牛云
  • 嵊州市住房和建设局网站优化公司
  • 滁州市建设工程质量监督站网站博客程序seo
  • wordpress无法管理站点各大网站收录查询
  • 古镇网站建设百度网站推广电话
  • 如何建设政府网站怎么做百度网页推广
  • 长春网站制作外包高端seo服务
  • 花钱做网站不给源码免费二级域名申请网站
  • 微商城怎么注册怎么弄商品关键词怎么优化
  • 宁波住房和城乡建设委员会网站竞价恶意点击立案标准
  • 淮北网站制作百度站长平台怎么用
  • 长沙做网站的故事注册城乡规划师报考条件
  • 威海外贸网站建设电话湖北疫情最新情况
  • 中山网站代运营广州网站营销推广
  • 建筑工程资料网站优化公司网站
  • 企业网站建设博客论坛任务放单平台
  • 做兼职去哪个网站桂林网站设计
  • 安徽万户网络seo站内优化技巧
  • 可以做网站引导页的页面线上销售培训机构
  • 外贸一般在哪个网站做的百度公司名称
  • 用java做网页如何建立网站网络营销案例题
  • 顺德大良网站建设开发重庆关键词seo排名
  • 做的好的ppt下载网站有哪些网站seo内容优化