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

长沙如何做百度的网站推广国际新闻军事最新消息

长沙如何做百度的网站推广,国际新闻军事最新消息,网站建设温州,建设网站公司哪好文章目录 1.游戏界面2.游戏内容2.1 棋盘类型2.2棋盘的初始化2.3 打印棋盘的界面展示 3.游戏操作3.1 玩家操作3.2 电脑操作3.3 胜负判定 4.代码整合 1.游戏界面 无论写任何程序,我们都需要先去了解它的大概框架,这里我们先把它的初始界面写出来。一个游戏…

文章目录

  • 1.游戏界面
  • 2.游戏内容
    • 2.1 棋盘类型
    • 2.2棋盘的初始化
    • 2.3 打印棋盘的界面展示
  • 3.游戏操作
    • 3.1 玩家操作
    • 3.2 电脑操作
    • 3.3 胜负判定
  • 4.代码整合

1.游戏界面

无论写任何程序,我们都需要先去了解它的大概框架,这里我们先把它的初始界面写出来。一个游戏的初始界面会有菜单可供选择,这里我写了一个最基础的游戏菜单,只支持开始游戏和退出游戏。为了能够在不退出游戏的情况下一直游玩,所以这里我写了一个do while循环来让游戏一直进行下去。

#include <stdio.h>
void menu()
{printf("*************************\n");printf("******* 1.play    *******\n");printf("******* 0.exit    *******\n");printf("*************************\n");}
int main()
{int input = 0;do{menu()//菜单printf("请选择>");scanf"%d",&input);switch(input){case 1:game();break;case 0:printf("退出游戏!\n");break;default:break;}}while(input);return 0;
}

2.游戏内容

2.1 棋盘类型

写完游戏的界面就轮到了游戏的内容了,让我们来想想三子棋有什么特点,最先想到就是它那3*3的棋盘了,为了实现这个棋盘我们要利用数组来实现。用什么类型的数组呢?这里我会用字符类型的数组,毕竟下棋用数字来作为棋子还是不容易区分的。

void game()
{//创建棋盘char chess[4][4] = {0};
}

2.2棋盘的初始化

选择了用字符数组来作为棋盘,初始化我会用‘ ’(空格)来初始化。空格初始化的好处就是不可见,当我们下好棋子后用相应的字符去覆盖掉空格就可以了。
这里我用4*4的数组是为了后续普通用户在用下标下棋时,不用考虑数组下标是0开始的,增加用户的受众。
为什么用多文件编写代码,多文件的编写可以便于后续的修改,多文件可以让代码的可读性更高。
注意头文件game.h放的是函数的声明,game.c放的是函数的定义 test.c是对程序的测试

//game.h
#include <stdio.h>
//以row为行,col为列void InitChess(char chess[4][4],int row,int col);//game.c
#include "game.h"
void InitChess(char chess[4][4],int row,int col)
{for(int i = 1;i<4;++i){for(int j = 1;j<4;++j){chess[i][j] = ' ';}}
}//test.c
#include "game.h"
void game()
{//创建棋盘char chess[4][4] = {0};//初始化棋盘InitChess(chess,4,4);
}

对函数拓展性的优化,这里的棋盘已经被固定,如果后续我们想要修改棋盘的大小是很麻烦的,所以我们可以定义标识常量。

//game.h
#include <stdio.h>
//以row为行,col为列
#define Row 4
#define Col 4void InitChess(char chess[Row][Col],int row,int col);//game.c
#include "game.h"
void InitChess(char chess[Row][Col],int row,int col)
{for(int i = 1;i<row;++i){for(int j = 1;j<col;++j){chess[i][j] = ' ';}}
}//test.c
#include "game.h"
void game()
{//创建棋盘char chess[Row][Col] = {0};//初始化棋盘InitChess(chess,Row,Col);
}

2.3 打印棋盘的界面展示

如果我们直接打印这个数组是什么也看不到的,为了让游玩的人可以轻松知道棋盘各个点的坐标,我们要把棋盘打印成这个样子。
打印棋盘

void ChessBoard(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){for (int j = 1; j < col; ++j){printf(" %c ", chess[i][j]);if (j < col - 1)//否则打印最后一个'|',导致右端封闭printf("|");}printf("\n");if (i < row - 1){for (int j = 1; j < col; ++j){printf("--- ");}printf("\n");}}
}

3.游戏操作

3.1 玩家操作

三子棋的游戏操作就是在3*3的方格当中选一个未被下过的方格中落子。
当前游戏并不支持双人对战,所以只能实现人机对战。下面为玩家操作:

void Gamer(char chess[Row][Col], int row, int col)
{int x = 0;//横坐标int y = 0;//纵坐标while (1){printf("选择落子坐标>\n");printf("坐标之间用空格区分\n");scanf("%d %d", &x, &y);//判断坐标是否合法if (x<1 || x>Row - 1 || y<1 || y>Col - 1){printf("坐标不合法\n");}//判断所选坐标是否被占据else if (chess[x][y] != ' '){printf("该坐标被占据\n");}else{chess[x][y] = 'O';printf("落子成功\n");break;}}
}

3.2 电脑操作

电脑的逻辑和玩家一样,这里我们让电脑随机下棋。

void Computer(char chess[Row][Col], int row, int col)
{int x = 0;int y = 0;while (1){x = rand() % (row - 1) + 1;y = rand() % (col - 1) + 1;if (chess[x][y] != ' '){//}else{chess[x][y] = 'X';break;}}
}

因为这了我们用了rand函数,所以我必须在前面写上srand来给rand函数提供随机种子,为此我们还需要用到time为srand提供数字来帮助它输出随机种子.
记得加上相应的头文件

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:break;}}while(input);return 0;
}

3.3 胜负判定

在三子棋当中,任何以方的棋子连成一条线就会判断为获胜,无论是一行还是一列还是斜方向。因为只有少量的情况。我能把所有获胜的情况全部都枚举出来就可以了。
关于返回值,因为我们要根据棋盘字符的连线来判断谁是赢家。
规定:返回 ‘O’表示玩家赢,‘X’表示电脑赢 'D’表示平局 'C’表示游戏继续

char Winer(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){if (chess[i][1] == chess[i][2] && chess[i][2] == chess[i][3] && chess[i][1] != ' '){return chess[i][1];}}for (int j = 1; j < col; ++j){if (chess[1][j] == chess[2][j] && chess[2][j] == chess[3][j] && chess[1][j] != ' '){return chess[1][j];}}if (chess[1][1] == chess[2][2] && chess[2][2] == chess[3][3] & chess[1][1] != ' '){return chess[1][1];}if (chess[1][3] == chess[2][2] && chess[2][2] == chess[3][1] && chess[2][2] != ' '){return chess[1][3];}//平局,判断棋盘是不是已经满了if (IsFull(chess, row, col)){return 'D';}return 'C';
}

判断棋盘是否已经下满

bool IsFull(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){for (int j = 1; j < col; ++j){if (chess[i][j] == ' ')return false;}}return true;
}

4.代码整合

关于三子棋的简单游戏逻辑就是这么多了,下面我们要把我们写到的函数整合到一起。

//game.h
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>//rand和srand的头文件
#include <time.h>//time的头文件
#define Row 4
#define Col 4void InitChess(char chess[Row][Col], int row, int col);void ChessBoard(char chess[Row][Col], int row, int col);void Gamer(char chess[Row][Col], int row, int col);void Computer(char chess[Row][Col], int row, int col);char Winer(char chess[Row][Col], int row, int col);bool IsFull(char chess[Row][Col], int row, int col);//game.c#include "game.h"void InitChess(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){for (int j = 1; j < col; ++j){chess[i][j] = ' ';}}
}void ChessBoard(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){for (int j = 1; j < col; ++j){printf(" %c ", chess[i][j]);if (j < col - 1)//否则打印最后一个'|',导致右端封闭printf("|");}printf("\n");if (i < row - 1){for (int j = 1; j < col; ++j){printf("--- ");}printf("\n");}}
}void Gamer(char chess[Row][Col], int row, int col)
{int x = 0;//横坐标int y = 0;//纵坐标while (1){printf("选择落子坐标>\n");printf("坐标之间用空格区分\n");scanf("%d %d", &x, &y);//判断坐标是否合法if (x<1 || x>Row - 1 || y<1 || y>Col - 1){printf("坐标不合法\n");}//判断所选坐标是否被占据else if (chess[x][y] != ' '){printf("该坐标被占据\n");}else{chess[x][y] = 'O';printf("落子成功\n");break;}}
}void Computer(char chess[Row][Col], int row, int col)
{int x = 0;int y = 0;while (1){x = rand() % (row - 1) + 1;y = rand() % (col - 1) + 1;if (chess[x][y] != ' '){//}else{chess[x][y] = 'X';break;}}
}char Winer(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){if (chess[i][1] == chess[i][2] && chess[i][2] == chess[i][3] && chess[i][1] != ' '){return chess[i][1];}}for (int j = 1; j < col; ++j){if (chess[1][j] == chess[2][j] && chess[2][j] == chess[3][j] && chess[1][j] != ' '){return chess[1][j];}}if (chess[1][1] == chess[2][2] && chess[2][2] == chess[3][3] & chess[1][1] != ' '){return chess[1][1];}if (chess[1][3] == chess[2][2] && chess[2][2] == chess[3][1] && chess[2][2] != ' '){return chess[1][3];}//平局,判断棋盘是不是已经满了if (IsFull(chess, row, col)){return 'D';}return 'C';
}bool IsFull(char chess[Row][Col], int row, int col)
{for (int i = 1; i < row; ++i){for (int j = 1; j < col; ++j){if (chess[i][j] == ' ')return false;}}return true;
}//test.c
#include "game.h"void game()
{//创建棋盘char chess[Row][Col] = { 0 };//初始化棋盘InitChess(chess, Row, Col);char w = 0;while (1){//打印棋盘ChessBoard(chess, Row, Col);//玩家操作Gamer(chess, Row, Col);//输赢判断w = Winer(chess, Row, Col);if (w != 'C')break;//电脑操作Computer(chess, Row, Col);//输赢判断char w = Winer(chess, Row, Col);if (w != 'C')break;}ChessBoard(chess, Row, Col);if (w == 'D')printf("平局\n");else if (w == 'O')printf("玩家获胜\n");elseprintf("电脑获胜\n");
}
void menu()
{printf("*************************\n");printf("******* 1.play    *******\n");printf("******* 0.exit    *******\n");printf("*************************\n");}
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:break;}} while (input);return 0;
}


文章转载自:
http://turgor.rywn.cn
http://constitution.rywn.cn
http://staccato.rywn.cn
http://surroundings.rywn.cn
http://fernbrake.rywn.cn
http://petuntse.rywn.cn
http://amidin.rywn.cn
http://tiberium.rywn.cn
http://acheulian.rywn.cn
http://syrinx.rywn.cn
http://wolframium.rywn.cn
http://clostridium.rywn.cn
http://urubu.rywn.cn
http://philhellene.rywn.cn
http://darksome.rywn.cn
http://saloonist.rywn.cn
http://unpowered.rywn.cn
http://unneutrality.rywn.cn
http://danewort.rywn.cn
http://anhydride.rywn.cn
http://embryonated.rywn.cn
http://bullrush.rywn.cn
http://ziff.rywn.cn
http://syrupy.rywn.cn
http://smile.rywn.cn
http://extemporary.rywn.cn
http://aethelbert.rywn.cn
http://marketing.rywn.cn
http://plumbery.rywn.cn
http://woolenette.rywn.cn
http://publicize.rywn.cn
http://divorce.rywn.cn
http://heavenward.rywn.cn
http://sublate.rywn.cn
http://flycatcher.rywn.cn
http://invitation.rywn.cn
http://afloat.rywn.cn
http://forefeet.rywn.cn
http://gha.rywn.cn
http://umbrose.rywn.cn
http://asturian.rywn.cn
http://madreporite.rywn.cn
http://counterbuff.rywn.cn
http://trimaran.rywn.cn
http://excellence.rywn.cn
http://carlist.rywn.cn
http://zymosan.rywn.cn
http://hypsicephalous.rywn.cn
http://semicircumference.rywn.cn
http://basebred.rywn.cn
http://teleutospore.rywn.cn
http://pedantry.rywn.cn
http://muonium.rywn.cn
http://footwell.rywn.cn
http://unscale.rywn.cn
http://azoturia.rywn.cn
http://footlights.rywn.cn
http://stotinka.rywn.cn
http://wordily.rywn.cn
http://olympus.rywn.cn
http://smellie.rywn.cn
http://telosynapsis.rywn.cn
http://upchuck.rywn.cn
http://aih.rywn.cn
http://evoke.rywn.cn
http://champagne.rywn.cn
http://nymphaeum.rywn.cn
http://flyby.rywn.cn
http://aliform.rywn.cn
http://nightside.rywn.cn
http://bait.rywn.cn
http://pyridine.rywn.cn
http://ilka.rywn.cn
http://offendedly.rywn.cn
http://encumbrancer.rywn.cn
http://schillerize.rywn.cn
http://postilion.rywn.cn
http://inflationism.rywn.cn
http://shellfishery.rywn.cn
http://secant.rywn.cn
http://dux.rywn.cn
http://when.rywn.cn
http://insolvency.rywn.cn
http://gravesian.rywn.cn
http://idiocrasy.rywn.cn
http://acronical.rywn.cn
http://kerr.rywn.cn
http://transubstantiate.rywn.cn
http://katangese.rywn.cn
http://hemihedral.rywn.cn
http://tidings.rywn.cn
http://none.rywn.cn
http://sensillum.rywn.cn
http://gopura.rywn.cn
http://cunene.rywn.cn
http://muley.rywn.cn
http://acharnement.rywn.cn
http://panelist.rywn.cn
http://pelvis.rywn.cn
http://disintermediate.rywn.cn
http://www.15wanjia.com/news/68325.html

相关文章:

  • 个人网站实现与设计论文外链兔
  • 网站设置三方交易网店推广联盟
  • 西安制作公司网站的公司seo平台优化服务
  • 外贸网站域名用境内还是境外网络工程师是干什么的
  • 建网页要钱吗优化网站内容的方法
  • 重庆建设科技培训中心官方网站域名查询注册商
  • 湖北手机网站建设网络推广营销软件
  • 网站建设制作品牌公司免费站推广网站在线
  • wordpress weiaid如何优化培训体系
  • 网站开发所要达到的目标2023最火的十大新闻
  • wordpress 英文 企业网站模板外贸网
  • 建设通网站的信息是哪里来的seo快照推广
  • 网站跳出率是什么意思英文seo实战派
  • 域名注册好了怎么样做网站什么网站百度收录快
  • 企业网站建设请示杭州seo外包
  • 网站怎么弄二维码投诉百度最有效的电话
  • 网站建设公司如何提供客户服务凡科建站模板
  • 网站建设落地页源码什么时候网络推广
  • 永久免费ppt下载网站网页自助建站
  • 苏州网站优化seo搜索优化培训
  • 天津网站建设哪家做得好沧州网站seo
  • 自己做网站好还是购买网站好排名优化价格
  • 网站改版应该怎么做纯手工seo公司
  • 腾讯服务器做网站长沙市网站制作
  • 物流网站素材南宁seo咨询
  • 东阳营销型网站建设品牌搜索引擎优化网站排名
  • 做网站主要学什么条件互联网营销师证书有用吗
  • 网页网站开发公司seo需求
  • 用asp做的网站有多少百度推广后台登陆官网
  • hge网站做微端百度新闻网