找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1711|回复: 0
收起左侧

有关c语言的三子棋+学生管理系统的小项目游戏源程序

[复制链接]
ID:947876 发表于 2021-7-1 17:28 | 显示全部楼层 |阅读模式
学单片机就必须会c语言,那些做个小游戏来检验一下你的c语言水平吧

C语言源程序如下:
  1. #include <stdio.h>

  2. void printf_cds(int (*array)[3]);             //打印UI
  3. int input(int cds, int (*pcds)[3], int game); //获取输入

  4. int scan_arry(int (*pcds)[3], int gamer);        //扫描数组
  5. int scan_arry_row(int (*pcds)[3], int gamer);    //扫描行
  6. int scan_arry_column(int (*pcds)[3], int gamer); //扫描列
  7. int scan_arry_x(int (*pcds)[3], int gamer);      //扫描对角线

  8. int scan_arry_zero(int (*pcds)[3]);//扫描剩余位置

  9. int main(int argc, char **argv)
  10. {
  11.     int coords[3][3] = {{0, 0, 0},
  12.                         {0, 0, 0},
  13.                         {0, 0, 0}}; //第一个玩家用 1表示,第二个玩家用 2表示
  14.     int gamer01 = 10, who = 1;
  15.     char button;

  16.     //开始界面
  17.     printf("游戏开始\n");
  18.     printf("操作输入数字键盘对应的位置\n");
  19.     printf("    7   |   8   |   9   \n");
  20.     printf("———————— ——————— ———————\n");
  21.     printf("    4   |   5   |    6  \n");
  22.     printf("———————— ——————— ———————\n");
  23.     printf("    1   |   2   |   3   \n\n");

  24.     printf_cds(coords);

  25.     while (1)
  26.     {
  27.         if (scan_arry_zero(coords) == 0)
  28.             return 0;
  29.         printf("请第%d位玩家输入\n", who);
  30.         scanf("%d", &gamer01);

  31.         if (gamer01 != 0)
  32.         {

  33.             if (who == 1)
  34.             {
  35.                 if (input(gamer01, coords, 1))
  36.                     who = 2;
  37.                 printf_cds(coords);
  38.                 if (scan_arry(coords, 1) == 1)
  39.                 {
  40.                     printf("玩家%d胜利!", 1);
  41.                     return 1;
  42.                 }
  43.             }
  44.             else if (who == 2)
  45.             {
  46.                 if (input(gamer01, coords, 2))
  47.                     who = 1;
  48.                 printf_cds(coords);
  49.                 if (scan_arry(coords, 2) == 2)
  50.                 {
  51.                     printf("玩家%d胜利!", 2);
  52.                     return 1;
  53.                 }
  54.             }
  55.         }
  56.         else //结束游戏
  57.         {
  58.             printf("是否退出游戏?(y:是,n:否)\n");
  59.             getchar();
  60.             scanf("%c", &button);
  61.             getchar();
  62.             if (button == 'y')
  63.                 return 0;
  64.             else if (button == 'n')
  65.                 printf_cds(coords);
  66.         }
  67.     }
  68. }

  69. //扫描零值
  70. int scan_arry_zero(int (*pcds)[3])
  71. {
  72.     int i, j, count = 0;

  73.     for (i = 0; i < 3; i++)
  74.     {
  75.         for (j = 0; j < 3; j++)
  76.         {
  77.             if (pcds[i][j] == 0)
  78.                 count++;
  79.         }
  80.     }

  81.     if (count == 0)
  82.     {
  83.         printf("游戏结束,平局!");
  84.         return 0;
  85.     }
  86.     else
  87.         return 1;

  88. }

  89.     //扫描数组
  90. int scan_arry(int (*pcds)[3], int gamer)
  91. {
  92.     if (scan_arry_row(pcds, gamer) == gamer)
  93.         return gamer;

  94.     else if (scan_arry_column(pcds, gamer) == gamer)
  95.         return gamer;

  96.     else if (scan_arry_x(pcds, gamer) == gamer)
  97.         return gamer;
  98.     else
  99.         return 0;
  100. }

  101. //扫描行
  102. int scan_arry_row(int (*pcds)[3], int gamer)
  103. {
  104.     int i, j, count_r = 0;
  105.     for (i = 0; i < 3; i++)
  106.     {
  107.         for (j = 0; j < 3; j++)
  108.         {

  109.             if (pcds[i][j] == gamer)
  110.             {
  111.                 count_r += pcds[i][j]; //获取行数据
  112.             }
  113.         }

  114.         if (count_r / 3 == gamer)
  115.             return gamer;
  116.         else
  117.             count_r = 0;
  118.     }
  119.     return 0;
  120. }
  121. //扫描列
  122. int scan_arry_column(int (*pcds)[3], int gamer)
  123. {
  124.     int i, j, count_c = 0;
  125.     for (i = 0; i < 3; i++)
  126.     {
  127.         for (j = 0; j < 3; j++)
  128.         {

  129.             if (pcds[j][i] == gamer)
  130.                 count_c += pcds[j][i]; //获取列数据
  131.         }

  132.         if (count_c / 3 == gamer)
  133.             return gamer;
  134.         else
  135.             count_c = 0;
  136.     }
  137.     return 0;
  138. }

  139. //扫描对角线
  140. int scan_arry_x(int (*pcds)[3], int gamer)
  141. {
  142.     int i, j, count_1 = 0, count_2 = 0;

  143.     for (i = 0; i < 3; i++)
  144.     {
  145.         for (j = 0; j < 3; j++)
  146.         {

  147.             if (pcds[i][j] == gamer)
  148.             {
  149.                 if (i == j) //获取斜边
  150.                     count_1 += pcds[i][j];
  151.                 if (i + j == 2) //获取斜边2
  152.                     count_2 += pcds[i][j];
  153.             }
  154.         }
  155.     }

  156.     if (count_1 / 3 == gamer || count_2 / 3 == gamer)
  157.         return gamer;
  158.     else
  159.         return 0;
  160. }

  161. //获取玩家输入
  162. int input(int cds, int (*pcds)[3], int gamer)
  163. {
  164.     int arry[3][3] = {{7, 8, 9},
  165.                       {4, 5, 6},
  166.                       {1, 2, 3}};
  167.     int i, j;
  168.     for (i = 0; i < 3; i++)
  169.     {
  170.         for (j = 0; j < 3; j++)
  171.         {
  172.             if (cds == arry[i][j])
  173.             {
  174.                 if (pcds[i][j] == 0)
  175.                 {
  176.                     pcds[i][j] = gamer;
  177.                     return 1;
  178.                 }
  179.                 else
  180.                 {
  181.                     printf("此位置已有数据,请重新输入\n");
  182.                     return 0;
  183.                 }
  184.             }
  185.         }
  186.     }
  187. }

  188. //打印游戏界面
  189. void printf_cds(int (*array)[3])
  190. {
  191.     int i, j;
  192.     for (i = 0; i < 3; i++)
  193.     {
  194.         for (j = 0; j < 3; j++)
  195.         {
  196.             printf("%d\t", array[i][j]);
  197.         }
  198.         printf("\n\n");
  199.     }
  200. }
复制代码

  1. /*做一个小项目:(简单的学生信息管理系统--》可以管理10个学生的信息)

  2.     定义一个结构体数据类型,里面包括学生信息:
  3.         姓名、性别、学号、年龄、电话号码、成绩(语数英体),
  4.             可以自行选择操作的增加、删除、更改、查询学生的信息等功能
  5. */

  6. #include <stdio.h>
  7. #include <strings.h>
  8. #include <string.h>

  9. int len = 10; //要管理的学生人数 ------------测试用,完成功能后改回项目要求

  10. //成绩 的结构体数据类型定义
  11. struct score
  12. {
  13.     int Chinese;
  14.     int Math;
  15.     int English;
  16.     int PE;
  17. };
  18. //学生 的 结构体数据类型定义
  19. struct student
  20. {
  21.     char name[20];
  22.     char sex;
  23.     int ID;
  24.     unsigned char age;
  25.     unsigned long long phone_num;
  26.     struct score student_sc;
  27. };

  28. //函数声明
  29. void printf_UI(void);
  30. void add_data(struct student *p_data);
  31. void printf_data(struct student *p_data);
  32. void delete_data(struct student *p_data);
  33. void change_data(struct student *p_data);

  34. int main(int argc, char **argv)
  35. {
  36.     int cmd = 0;
  37.     struct student yq[len];
  38.     bzero(yq, sizeof(yq));

  39.     while (1)
  40.     {
  41.         printf_UI();
  42.         scanf("%d", &cmd);

  43.         if (cmd == 1)
  44.             add_data(yq);
  45.         else if (cmd == 2)
  46.             printf_data(yq);
  47.         else if (cmd == 3)
  48.             change_data(yq);
  49.         else if (cmd == 4)
  50.             delete_data(yq);
  51.     }
  52.     return 0;
  53. }


  54. //修改学生数据
  55. void change_data(struct student *p_data)
  56. {
  57.     int i, num=0;
  58.     for (i = 0; i < len; i++)
  59.     {
  60.         printf("学生%d姓名:%s\n", i + 1, p_data[i].name);
  61.     }
  62.     printf("\n请输入需要修改的学生(1~10)\n");
  63.     scanf("%d", &num);
  64.     if(num<1||num>10)
  65.     {
  66.         printf("输入有误!\n");
  67.         return;
  68.     }
  69.     num -= 1;
  70.     printf("请输入学生的姓名:");
  71.     scanf("%s", p_data[num].name);

  72.     getchar();
  73.     printf("请输入学生的性别(M->男, W-->女):");
  74.     scanf("%c", &p_data[num].sex);

  75.     getchar();
  76.     printf("请输入学生的学号:");
  77.     scanf("%d", &p_data[num].ID);

  78.     printf("请输入学生的年龄(0~255):");
  79.     scanf("%hhu", &p_data[num].age);

  80.     printf("请输入学生的电话号码:");
  81.     scanf("%llu", &p_data[num].phone_num);

  82.     printf("请输入学生的语文成绩:");
  83.     scanf("%d", &p_data[num].student_sc.Chinese);

  84.     printf("请输入学生的数学成绩:");
  85.     scanf("%d", &p_data[num].student_sc.Math);

  86.     printf("请输入学生的英语成绩:");
  87.     scanf("%d", &p_data[num].student_sc.English);

  88.     printf("请输入学生的体育成绩:");
  89.     scanf("%d", &p_data[num].student_sc.PE);
  90.     printf("\n");

  91.     printf("修改完成!\n\n");
  92. }

  93. //删除数据
  94. void delete_data(struct student *p_data)
  95. {
  96.     int i;
  97.     for (i = 0; i < len; i++)
  98.         bzero(&p_data[i], sizeof(*p_data));

  99.     printf("数据已清除!\n\n");
  100. }

  101. //增加数据
  102. void add_data(struct student *p_data)
  103. {
  104.     int i;
  105.     for (i = 0; i < len; i++)
  106.     {
  107.         //姓名、性别、学号、年龄、电话号码、成绩(语数英体),
  108.         printf("请输入学生的姓名:");
  109.         scanf("%s", p_data[i].name);

  110.         getchar();
  111.         printf("请输入学生的性别(M->男, W-->女):");
  112.         scanf("%c", &p_data[i].sex);

  113.         getchar();
  114.         printf("请输入学生的学号:");
  115.         scanf("%d", &p_data[i].ID);

  116.         printf("请输入学生的年龄(0~255):");
  117.         scanf("%hhu", &p_data[i].age);

  118.         printf("请输入学生的电话号码:");
  119.         scanf("%llu", &p_data[i].phone_num);

  120.         printf("请输入学生的语文成绩:");
  121.         scanf("%d", &p_data[i].student_sc.Chinese);

  122.         printf("请输入学生的数学成绩:");
  123.         scanf("%d", &p_data[i].student_sc.Math);

  124.         printf("请输入学生的英语成绩:");
  125.         scanf("%d", &p_data[i].student_sc.English);

  126.         printf("请输入学生的体育成绩:");
  127.         scanf("%d", &p_data[i].student_sc.PE);
  128.         printf("\n");
  129.     }
  130.     printf("数据输入完成\n\n");
  131. }

  132. //打印数据
  133. void printf_data(struct student *p_data)
  134. {
  135.     int i;
  136.     for (i = 0; i < len; i++)
  137.     {
  138.         //姓名、性别、学号、年龄、电话号码、成绩(语数英体),
  139.         printf("学生%d姓名:%s\n", i + 1, p_data[i].name);
  140.         printf("学生%d性别:%c\n", i + 1, p_data[i].sex);
  141.         printf("学生%d学号:%d\n", i + 1, p_data[i].ID);
  142.         printf("学生%d年龄:%hhu\n", i + 1, p_data[i].age);
  143. ……………………

  144. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码


51hei.png
所有资料51hei提供下载:
三字棋游戏+学生简易管理系统.7z (9.64 KB, 下载次数: 7)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表