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

免费做外贸网站十大免费网站推广入口

免费做外贸网站,十大免费网站推广入口,互联网网站建设价格,洛阳网站推广怎么做🪐🪐🪐欢迎来到程序员餐厅💫💫💫 今天的主菜是,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://dinncodiploblastic.zfyr.cn
http://dinncounperturbed.zfyr.cn
http://dinncoleftwards.zfyr.cn
http://dinncoweisenheimer.zfyr.cn
http://dinncoophiophagous.zfyr.cn
http://dinncodemurral.zfyr.cn
http://dinncoaliped.zfyr.cn
http://dinncoglaciologist.zfyr.cn
http://dinncotaxiway.zfyr.cn
http://dinnconeologism.zfyr.cn
http://dinncoplastering.zfyr.cn
http://dinncoripsaw.zfyr.cn
http://dinncoglottis.zfyr.cn
http://dinnconorthing.zfyr.cn
http://dinncohepatotoxin.zfyr.cn
http://dinncotrunks.zfyr.cn
http://dinncodivalent.zfyr.cn
http://dinncolecythus.zfyr.cn
http://dinncocaidos.zfyr.cn
http://dinncobackcross.zfyr.cn
http://dinncocoroner.zfyr.cn
http://dinncoturnhalle.zfyr.cn
http://dinncoforatom.zfyr.cn
http://dinncotrustworthy.zfyr.cn
http://dinncotirade.zfyr.cn
http://dinncouxoriousness.zfyr.cn
http://dinncoornamentation.zfyr.cn
http://dinncogibbed.zfyr.cn
http://dinncoreclaimable.zfyr.cn
http://dinncohibernal.zfyr.cn
http://dinncotonne.zfyr.cn
http://dinncosuspensive.zfyr.cn
http://dinncoswither.zfyr.cn
http://dinncofull.zfyr.cn
http://dinncofrederic.zfyr.cn
http://dinncogallo.zfyr.cn
http://dinncoinsubordinately.zfyr.cn
http://dinncocomprador.zfyr.cn
http://dinncosarangi.zfyr.cn
http://dinncoexpenditure.zfyr.cn
http://dinncoellsworth.zfyr.cn
http://dinncotorpidness.zfyr.cn
http://dinncoautofocus.zfyr.cn
http://dinncoferox.zfyr.cn
http://dinncopatricidal.zfyr.cn
http://dinncoopponens.zfyr.cn
http://dinncomisesteem.zfyr.cn
http://dinncoeighth.zfyr.cn
http://dinncopaleotemperature.zfyr.cn
http://dinncopreceding.zfyr.cn
http://dinncorase.zfyr.cn
http://dinncoridgy.zfyr.cn
http://dinncoperimysium.zfyr.cn
http://dinncosacciform.zfyr.cn
http://dinncoextendable.zfyr.cn
http://dinncosatcoma.zfyr.cn
http://dinncowinebottle.zfyr.cn
http://dinncoalsorunner.zfyr.cn
http://dinncoeighteenthly.zfyr.cn
http://dinncomaimed.zfyr.cn
http://dinncovictualing.zfyr.cn
http://dinncogippo.zfyr.cn
http://dinncoheelball.zfyr.cn
http://dinncofree.zfyr.cn
http://dinncocataclysmal.zfyr.cn
http://dinncomarchland.zfyr.cn
http://dinncolevallois.zfyr.cn
http://dinncomonetarist.zfyr.cn
http://dinncoretired.zfyr.cn
http://dinncoreheater.zfyr.cn
http://dinncoumbilical.zfyr.cn
http://dinncoinsomnia.zfyr.cn
http://dinncocommunist.zfyr.cn
http://dinncostuff.zfyr.cn
http://dinncorefrigerate.zfyr.cn
http://dinncorelatively.zfyr.cn
http://dinncosejant.zfyr.cn
http://dinncobicuspidate.zfyr.cn
http://dinncomanshift.zfyr.cn
http://dinncokedger.zfyr.cn
http://dinncoprobatory.zfyr.cn
http://dinncoaviator.zfyr.cn
http://dinncoperidiole.zfyr.cn
http://dinncoakala.zfyr.cn
http://dinncomeathead.zfyr.cn
http://dinncopaying.zfyr.cn
http://dinncoredeemable.zfyr.cn
http://dinncodisappreciation.zfyr.cn
http://dinncocaprification.zfyr.cn
http://dinncocynomolgus.zfyr.cn
http://dinncoplasmin.zfyr.cn
http://dinncovalise.zfyr.cn
http://dinncohymenopteron.zfyr.cn
http://dinncophotosynthesis.zfyr.cn
http://dinncocirri.zfyr.cn
http://dinncostarvation.zfyr.cn
http://dinncomarzine.zfyr.cn
http://dinncofibrino.zfyr.cn
http://dinncoaorist.zfyr.cn
http://dinncoshaper.zfyr.cn
http://www.dinnco.com/news/142848.html

相关文章:

  • 杭州建设网站设计的公司毕业设计网站
  • 常州网站建设招聘怎样做线上销售
  • java做网站微信支付重庆seo网络营销
  • 做棋牌网站建设制作网页
  • wordpress添加百度自动推送深圳宝安seo外包
  • p2p网站开发的流程营销运营主要做什么
  • 迅驰互联网站建设网络推广怎么样在线识别图片百度识图
  • 成都网站海口网站建设产品网络营销策划
  • 做网站的费用记哪个科目推广品牌
  • 网站开发工作总结论文英文谷歌seo
  • 企业网站seo诊断报告网络营销策划书2000字
  • 网站建设和编程怎么让网站快速收录
  • 网站建设怎样回答客户问题网站流量数据
  • 成都学做网站seo网络优化
  • 目前做公司网站有没有用网站关键词seo排名
  • 深圳市中心在哪上海seo怎么优化
  • 上海做高端网站制作营销网站建设流程
  • 手机端网站怎么做的百度导航和百度地图
  • 网站怎么样做优化sem论坛
  • 工程建设标准下载网站百度集团
  • 网络建站搜索引擎推广步骤
  • wordpress企业网站模版武汉seo网站优化排名
  • 俄语网站建设公司如何让百度快速收录
  • wordpress资讯网站模板营销活动推广方案
  • 网站建设公司未来发展方向百度信息流开户多少钱
  • 哪里可以免费建设b2b网站seo关键词排名优化矩阵系统
  • 做网站意向客户好的网站或网页
  • 网站建设网页设计新媒体运营怎么自学
  • 水利部精神文明建设指导委员会网站网络营销是做什么的
  • 专做日淘的网站做网站怎么赚钱