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

天长做网站高端网站建设公司排行

天长做网站,高端网站建设公司排行,专业做律师网站的公司,温州论坛703🪐🪐🪐欢迎来到程序员餐厅💫💫💫 今天的主菜是,C语言实现的三子棋小游戏, 所属专栏: C语言知识点 主厨的主页:Chef‘s blog 前言&…

🪐🪐🪐欢迎来到程序员餐厅💫💫💫

今天的主菜是,C语言实现的三子棋小游戏,

              所属专栏:     C语言知识点   

              主厨的主页:Chef‘s blog


前言:

已经学会数组的朋友们注意啦,现在的你已经有能力写出两个小游戏了,一个是扫雷,一个是三子棋,今天咱们就来手搓三子棋代码。

涉及知识点:

  • 随机数的生成:C语言实现随机数
  • 数组的使用

1.游戏要求

1.此游戏为人机对战

2.一方的棋子连成一行或一列或对角线时胜利

3.默认是3*3的棋盘,但可修改

4.玩家可以通过菜单选择开始游戏或退出游戏

2.游戏分析

  • 1.电脑下的棋通过随机数生成
  • 2.我们应该用数组放置双方所下的棋子
  • 3.每时每刻其具有三种情况,即未分出胜负,一方获胜,平局。每次一方下完棋就该判断此时棋局的情况。
  • 4.设置菜单使玩家可以选择退出游戏或开始游戏

3.多文件操作

 为了方便代码的管理和保证游戏实现逻辑的清晰性,我们将采用多文件管理的模式。

        (1)创建头文件game.h,包含所有头文件(其他源文件只需引用它即可),以及所有游戏功能的函数接口。

        (2)创建源文件game.c,负责所有游戏功能对应函数的具体代码实现。

        (3)创建源文件main.c,负责调用函数实现来游戏。

4.  简易菜单的实现

4.1功能介绍

 1.玩家可以通过选择1进入游戏,0退出游戏。

2.选错的话提醒玩家,重新选择。

3.告诉玩家游戏规则

4.2功能实现

#define _CRT_SECURE_NO_WARNINGS 1
#include"源.h"
void menu()
{printf("*********************\n");printf("****开始   :1*******\n");printf("****结束   :0********\n");//打印菜单printf("*********************\n");
}
void rules()
{printf("游戏规则如下:\n");printf("1.此游戏为人机对战\n2.一方的棋子连成一行或一列或对角线时胜利\n3.横坐标是1—3,纵坐标是1—3\n4.输入1是开始游戏输入0是退出游戏\n");
}
int main()
{srand((unsigned int)time(NULL));//设置随机数种子int input = 0;do {rules();menu(); printf("请选择:->");//玩家输入0表示退出游戏,输入1表示开始游戏scanf("%d", &input);switch (input){case 1:printf("开始游玩三子棋,祝您好运!\n");game();//游戏内容的具体实现break;case 0:printf("退出游戏\n");break;default:printf("请输入0 或 1 !\n");//防止有人捣乱,故意输错break;}} while (input);return 0;
}

4.3效果展示

5. 游戏功能实现

  5.1 要实现的函数接口

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define _CRT_SECURE_NO_WARNINGS 1
#define L 3
#define W 3
void initboard(char board[L][W], int l, int w);
void printboard(char board[L][W], int l, int w);
void playermove(char board[L][W], int l, int w);
void computermove(char board[L][W], int l, int w);
char judge(char board[L][W], int l, int w);
void game(void);

5.2初始化棋盘

(1)要求

把棋盘都初始化为空格

(2)代码实现
void initboard(char board[L][W], int l, int  w)
{int x = 0, y = 0;for (x = 0; x < l; x++)for (y = 0; y < w; y++)board[x][y] = ' ';
}

5.3 打印棋盘

    (1)要求

1. 打印出棋盘中的元素。

2. 利用---,|模拟出棋盘框。

(2)实现
void printboard(char board[L][W], int l, int w)
{int i = 0;for (i = 0; i < l-1; i++){for (int j = 0; j < w-1; j++)printf(" %c |", board[i][j]);printf(" %c ", board[i][w-1]);printf("\n");                      //打印前两行for(int j=0;j<w-1;j++)printf("---|");printf("---\n");}for (int j = 0; j < w-1; j++)printf(" %c |", board[i][j]);//打印最后一行printf(" %c ", board[i][w - 1]);printf("\n");
}

(3)效果展示

5.4玩家下棋

(1)要求

1.若输入坐标不在棋盘范围中,或该位置已经有棋了,提醒玩家重新输入

2.坐标有效则更改二维数组中存放的元素为玩家对应符号‘#’

(2)代码实现

void playermove(char board[L][W], int l, int w)
{int x, y;
again:	printf("玩家下棋\n请输入坐标:>");scanf("%d %d", &x, &y);if (x <= l && x > 0 && y <= w && y > 0){if (board[x-1][y-1] == ' ')board[x-1][y-1] = '#';//坐标有效,修改数组else{printf("这个地方有棋子了,换个地方吧!\n");goto again;}}else{printf("下错了,重新下吧!\n");goto again;}
}

      (3)效果展示

5.4电脑下棋

(1)要求

1.若输入坐标不在棋盘范围中,或该位置已经有棋了,则电脑重新输入

2.坐标有效则更改二维数组中存放的元素为电脑对应符号‘*’

(2)代码实现   

void computermove(char board[L][W], int l, int w)
{printf("电脑下棋\n");int x, y;again:  x = rand() % 3 + 1, y = rand() % 3 + 1;//随机数产生坐标if (x <= l && x > 0 && y <= w && y > 0&&board[x - 1][y - 1] == ' ')board[x - 1][y - 1] = '*';elsegoto again;
}

(3)效果展示

5.6判断棋局情况

(1)要求

棋局有三种情况,平局,胜负,或还未结束,根据不同情况返回不同字符

(2)代码实现

char judge(char board[L][W], int l, int w)
{for (int i = 0; i < l; i++){int j = 0;for ( j = 0; j < w-1; j++){if (board[i][j] != board[i][j + 1])break;}if (j == w-1 && board[i][j - 1] != ' ')//检查是否有连成一行return board[i][j - 1];}for (int i = 0; i < l; i++){int j = 0;for (j = 0; j < w-1; j++){if (board[j][i] != board[j+1][i])//检查是否有连成一列break;}if (j == w-1 && board[j-1][i] != ' ')return board[j-1][i];}int x;for ( x = 0; x < l-1; x++)if (board[x][x] != board[x + 1][x + 1])//检查是否连成对角线break;if (x == l-1 && board[x - 1][x - 1] != ' ')return board[x - 1][x - 1];int y;for (y=l-1;  y>0; y--)if (board[y][y] != board[y-1][y-1])//检查是否连成另一条对角线break;if (y == 0 && board[y][y] != ' ')return board[y][y];for (int i = 0; i < l; i++){int j = 0;for (j = 0; j < w; j++){if (board[j][i] == ' ')return 'j';//检查棋盘满了没}}return 'p';//最后一种情况是平局
}

(3)效果展示

5.7调用各个函数实现游戏

void game()
{char b;char board[L][W] = { 0 };initboard(board, L, W);printboard(board, L, W);while (1){playermove(board, L, W);printboard(board, L, W);b = judge(board, L, W);if (b != 'j')break;computermove(board, L, W);printboard(board, L, W);b = judge(board, L, W);if (b != 'j')break;}if (b == '#')printf("你赢了\n");else if (b == '*')printf("你输了\n");elseprintf("平局\n");
}

6. 源码 

  (1)main.c

#include"test.h"
void menu()
{printf("*********************\n");printf("****开始   :1*******\n");printf("****结束   :0********\n");printf("*********************\n");
}
int main()
{srand((unsigned int)time(NULL));int input = 0;do {game();menu(); printf("请选择:->");scanf("%d", &input);switch (input){case 1:printf("开始游玩三子棋,祝您好运!\n");game();break;case 0:printf("退出游戏\n");break;default:printf("请输入0 或 1 !\n");break;}} while (input);return 0;
}

(2)test.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define _CRT_SECURE_NO_WARNINGS 1
#define L 3
#define W 3
void initboard(char board[L][W], int l, int w);
void printboard(char board[L][W], int l, int w);
void playermove(char board[L][W], int l, int w);
void computermove(char board[L][W], int l, int w);
char judge(char board[L][W], int l, int w);
void game(void);

(3)test.c

#include"test.h"
void initboard(char board[L][W], int l, int  w)
{int x = 0, y = 0;for (x = 0; x < l; x++)for (y = 0; y < w; y++)board[x][y] = ' ';
}
void printboard(char board[L][W], int l, int w)
{int i = 0;for (i = 0; i < l-1; i++){for (int j = 0; j < w-1; j++)printf(" %c |", board[i][j]);printf(" %c ", board[i][w-1]);printf("\n");for(int j=0;j<w-1;j++)printf("---|");printf("---\n");}for (int j = 0; j < w-1; j++)printf(" %c |", board[i][j]);printf(" %c ", board[i][w - 1]);printf("\n");
}
void playermove(char board[L][W], int l, int w)
{int x, y;
again:	printf("玩家下棋\n请输入坐标:>");scanf("%d %d", &x, &y);if (x <= l && x > 0 && y <= w && y > 0){if (board[x-1][y-1] == ' ')board[x-1][y-1] = '#';else{printf("这个地方有棋子了,换个地方吧!\n");goto again;}}else{printf("下错了,重新下吧!\n");goto again;}
}
void computermove(char board[L][W], int l, int w)
{printf("电脑下棋\n");int x, y;again:  x = rand() % 3 + 1, y = rand() % 3 + 1;if (x <= l && x > 0 && y <= w && y > 0&&board[x - 1][y - 1] == ' ')board[x - 1][y - 1] = '*';elsegoto again;
}
char judge(char board[L][W], int l, int w)
{for (int i = 0; i < l; i++){int j = 0;for ( j = 0; j < w-1; j++){if (board[i][j] != board[i][j + 1])break;}if (j == w-1 && board[i][j - 1] != ' ')return board[i][j - 1];}for (int i = 0; i < l; i++){int j = 0;for (j = 0; j < w-1; j++){if (board[j][i] != board[j+1][i])break;}if (j == w-1 && board[j-1][i] != ' ')return board[j-1][i];}int x;for ( x = 0; x < l-1; x++)if (board[x][x] != board[x + 1][x + 1])break;if (x == l-1 && board[x - 1][x - 1] != ' ')return board[x - 1][x - 1];int y;for (y=l-1;  y>0; y--)if (board[y][y] != board[y-1][y-1])break;if (y == 0 && board[y][y] != ' ')return board[y][y];for (int i = 0; i < l; i++){int j = 0;for (j = 0; j < w; j++){if (board[j][i] == ' ')return 'j';}}return 'p';
}
void game()
{char b;char board[L][W] = { 0 };initboard(board, L, W);printboard(board, L, W);while (1){playermove(board, L, W);printboard(board, L, W);b = judge(board, L, W);if (b != 'j')break;computermove(board, L, W);printboard(board, L, W);b = judge(board, L, W);if (b != 'j')break;}if (b == '#')printf("你赢了\n");else if (b == '*')printf("你输了\n");elseprintf("平局\n");
}

好啦好啦,终于写完了,完结撒花,感谢观看,下次再见


文章转载自:
http://interscapular.sqxr.cn
http://turbosphere.sqxr.cn
http://pericardiac.sqxr.cn
http://broiler.sqxr.cn
http://damnatory.sqxr.cn
http://npv.sqxr.cn
http://vauntingly.sqxr.cn
http://grundyism.sqxr.cn
http://ephebos.sqxr.cn
http://bergen.sqxr.cn
http://outermost.sqxr.cn
http://emancipation.sqxr.cn
http://plasterwork.sqxr.cn
http://dud.sqxr.cn
http://delegitimation.sqxr.cn
http://intersex.sqxr.cn
http://bibliopoly.sqxr.cn
http://movement.sqxr.cn
http://paratyphoid.sqxr.cn
http://plowshare.sqxr.cn
http://poohed.sqxr.cn
http://tetraethyl.sqxr.cn
http://otherworldly.sqxr.cn
http://parted.sqxr.cn
http://celeb.sqxr.cn
http://forbidden.sqxr.cn
http://catcall.sqxr.cn
http://eustatically.sqxr.cn
http://sultry.sqxr.cn
http://ploughshoe.sqxr.cn
http://brule.sqxr.cn
http://appetizing.sqxr.cn
http://brushwork.sqxr.cn
http://translatology.sqxr.cn
http://formulating.sqxr.cn
http://glitzy.sqxr.cn
http://asclepiad.sqxr.cn
http://inkpad.sqxr.cn
http://lexicology.sqxr.cn
http://binoculars.sqxr.cn
http://appointee.sqxr.cn
http://thermoplastic.sqxr.cn
http://climatotherapy.sqxr.cn
http://olein.sqxr.cn
http://livraison.sqxr.cn
http://emblematise.sqxr.cn
http://cycloramic.sqxr.cn
http://ionise.sqxr.cn
http://anatomist.sqxr.cn
http://picloram.sqxr.cn
http://forkful.sqxr.cn
http://pebbleware.sqxr.cn
http://cauterization.sqxr.cn
http://ghastly.sqxr.cn
http://negotiatory.sqxr.cn
http://miasmatic.sqxr.cn
http://boite.sqxr.cn
http://trinket.sqxr.cn
http://cordilleras.sqxr.cn
http://megadont.sqxr.cn
http://destructivity.sqxr.cn
http://oncogenous.sqxr.cn
http://vext.sqxr.cn
http://contextless.sqxr.cn
http://apomorphine.sqxr.cn
http://lemonade.sqxr.cn
http://speedread.sqxr.cn
http://beamish.sqxr.cn
http://outsail.sqxr.cn
http://mufti.sqxr.cn
http://piscicultural.sqxr.cn
http://introspectiveness.sqxr.cn
http://venoclysis.sqxr.cn
http://footboard.sqxr.cn
http://shipbuilder.sqxr.cn
http://hjs.sqxr.cn
http://overabundance.sqxr.cn
http://maraschino.sqxr.cn
http://thistledown.sqxr.cn
http://unkink.sqxr.cn
http://revenooer.sqxr.cn
http://murderess.sqxr.cn
http://distinguish.sqxr.cn
http://mac.sqxr.cn
http://cloven.sqxr.cn
http://customize.sqxr.cn
http://rhumba.sqxr.cn
http://hindooize.sqxr.cn
http://hatchling.sqxr.cn
http://tallowy.sqxr.cn
http://naumachy.sqxr.cn
http://marty.sqxr.cn
http://inaccessible.sqxr.cn
http://psychoneurosis.sqxr.cn
http://scrimshank.sqxr.cn
http://pedantize.sqxr.cn
http://arbiter.sqxr.cn
http://july.sqxr.cn
http://hippocrene.sqxr.cn
http://laudably.sqxr.cn
http://www.15wanjia.com/news/74426.html

相关文章:

  • 婚庆公司租车网页优化怎么做
  • 成寿寺网站建设公司深圳有实力的seo公司
  • html做动态网站需要哪些软件企业网站推广方法实验报告
  • 电子商务网站设计代码关键词优化公司靠谱推荐
  • 海南省城乡建设厅网站首页友情链接怎么连
  • 庆网站建设资源搜索器
  • 为什么百度不收录我的网站厦门人才网官网
  • ipv6 网站开发品牌推广营销
  • 制作公司网站多少钱今天株洲最新消息
  • 怎么给自己的品牌做网站郑州seo顾问外包
  • 毕业设计做网站用什么2021年10月新闻摘抄
  • 天津网站建设招聘网络营销的特点分别是
  • 建设公司网站需要多少钱深圳将进一步优化防控措施
  • 厦门网红南京企业网站排名优化
  • 做任务领佣金的网站源码网络游戏推广平台
  • wordpress 简单企业主题seo手机排名软件
  • 南宁设计网站企业邮箱查询
  • wordpress写文章怎么更换编辑器seo经验
  • 网站图片速度站长之家网站排行榜
  • 网络优化网站 site陕西今日头条新闻
  • 网站配色方案 对比色产品推广哪个平台好
  • 余杭网站建设如何出售自己的域名
  • 上海专业网站建设哪家好七牛云
  • 做app网站的软件叫什么名字百度指数数据分析平台
  • wordpress怎么建立二级域名网站seo报价
  • 山东中佛龙建设有限公司网站怎么推广自己的公司
  • 扁平化企业网站模板兰州网络推广电话
  • 南昌网站建设kaiu长春网站优化
  • 怎样做网站jsp域名注册阿里云
  • wordpress盗版seo推广有哪些公司