八,五子棋游戏
程序应用C语言编写程序,可以在计算机上实现二人对弈五子棋功能。
功能要求:
(1)欢迎界面生成、游戏界面生成;
(2)光标移动和落子显示;
(3)判断胜负、悔棋功能,提供音效;
(4)综合应用结构体、数组、按键处理和图形编程等编程方法。

#include <stdio.h>
#include<windows.h>
#include <stdlib.h>
#include<conio.h>
int s;
int winner;
int player;
int Q[200][200] = { 0 };
char button;
struct Point
{int x;int y;
} point, game;
struct Pieces
{struct Point coord;struct Pieces *fore;
};
struct Pieces *p, *ptr, *ptr1, *head;
void goto_xy(int x, int);
void init();
void welcome();
void showwho();
void draw();
void clean();
void menu(char press);
void go_back(int x1, int y1);
void record();
void putdown();
void play(char ch);
int judge();
int main();
void goto_xy(int x, int y)          //光标移动函数
{COORD c;c.X = 2 * x;c.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//使光标 到(x,y)位置的函数//调用 COORD 需要#include<windows.h>.
}
void init()//初始化函数,将记录棋子的数组初始化
{for (int i = 0; i < 200; i++)for (int j = 0; j < 200; j++)Q[i][j] = 0;s = 0;player = 1;p = (struct Pieces *)malloc(sizeof(struct Pieces)); //  动态声明了一个pieces结构体的内存空间, 并将这块空间的指针保存给p变量                                                                    head = p;
}
void welcome()
{system("color 2F");goto_xy(17, 1);printf("\n\t\t*****************欢迎来到五子棋小游戏******************\n");goto_xy(13, 3);printf("\n\t\t\t********主菜单***********\n");goto_xy(13, 4);printf("\n\t\t\t\t***人人对战***\n");printf("\n\t\t\t\t\t退出");point.x = 12;point.y = 3;goto_xy(0, 0);
}
void showwho()
{goto_xy(17, 22);if (player == 0)printf("轮到甲方落子");elseprintf("轮到乙方落子");goto_xy(point.x, point.y);
}
void draw()
{game.x = 10;game.y = 3;system("cls");system("color 3F");goto_xy(15, 1);printf("欢迎进入五子棋小游戏!!\n");goto_xy(29, 22);printf("重新开始请输入r\n");goto_xy(1, 22);printf("悔棋请输入b\n");goto_xy(1, 23);printf("退出请按ESC\n");const int i = 8;    //const 定义的数据不可以被改变 而且修改数据比较方便     const int j = 19;const int k = 3;goto_xy(game.x - i, game.y + k);  //甲光标的初始化位置printf("甲方:●\n");goto_xy(game.x - i, game.y + k + 2);printf("向上移动:↑\n");goto_xy(game.x - i, game.y + k + 4);printf("向下移动:↓\n");goto_xy(game.x - i, game.y + k + 6);printf("向左移动:←\n");goto_xy(game.x - i, game.y + k + 8);printf("向右移动:→\n");goto_xy(game.x - i, game.y + k + 10);printf("落子: Enter\n");goto_xy(game.x + j, game.y + k);printf("乙方:○\n");goto_xy(game.x + j, game.y + k + 2);printf("向上移动:↑\n");goto_xy(game.x + j, game.y + k + 4);printf("向下移动:↓\n");goto_xy(game.x + j, game.y + k + 6);printf("向左移动:←\n");goto_xy(game.x + j, game.y + k + 8);printf("向右移动:→\n");goto_xy(game.x + j, game.y + k + 10);printf("落子: Enter\n");for (int k1 = 0; k1 < 200; k1++)     //初始化棋子记录,在第二局时有明确的作用         for (int k2 = 0; k2 < 200; k2++)Q[k1][k2] = 0;for (int i = 0; i < 18; i++){if (i == 0){goto_xy(10, i + 3);printf("┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐");}if (i != 0 && i != 17){goto_xy(10, i + 3);printf("├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤");}if (i == 17){goto_xy(10, i + 3);printf("└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘");}}point.x = 19;point.y = 12;goto_xy(19, 12);
}
void clean()
{goto_xy(10, 3);printf(" ");goto_xy(24, 3);printf(" ");goto_xy(10, 4);printf(" ");goto_xy(24, 4);printf(" ");
}
void menu(char press)
{if (press == 72)    //↑的ASCLL码     {if (point.y == 3)point.y = 4;elsepoint.y = 3;clean();goto_xy(10, point.y);printf("—→");}if (press == 80)   //↓的ASCLL码     {if (point.y == 4)point.y = 3;elsepoint.y = 4;clean();goto_xy(10, point.y);printf("—→");}if (press == 13)  // 13:回车键的ASCLL码     {s = point.y - 2;    //s 为1或2    }
}
void go_back(int x1, int y1)     //悔棋函数
{goto_xy(x1, y1);if (x1 == 10){if (y1 == 3){printf("┌");}elseif (y1 == 20){printf("└");}else{printf("├");}}else if (x1 == 27){if (y1 == 3){printf("┐");}elseif (y1 == 20){printf("┘");}else{printf("┤");}}else{if (y1 == 3){printf("┬");}elseif (y1 == 20){printf("┴");}else{printf("┼");}}Q[point.x][point.y] = 0;goto_xy(x1, y1);
}
void record()    //记录棋子的情况
{p->coord.x = point.x;p->coord.y = point.y;ptr = p;p = (struct Pieces *)malloc(sizeof(struct Pieces));p->fore = ptr;showwho();Q[point.x][point.y] = player + 1;if (player){player = 0;return;}player = 1;goto_xy(point.x, point.y);
}
void putdown()
{if (Q[point.x][point.y] == 0)    //先判断该位置是否有棋子    {if (player){printf("●");printf("\a");     //'\a'表示蜂鸣声             record();}else{printf("○");printf("\a");record();}goto_xy(point.x, point.y);}
}
void play(char ch)
{if (ch == 72){if (point.y <= 3)point.y = 20;elsepoint.y--;goto_xy(point.x, point.y);}if (ch == 75)      //←的ASCLL码    光标左移     {if (point.x <= 10)point.x = 27;elsepoint.x--;goto_xy(point.x, point.y);}if (ch == 77)      //→的ASCLL码  光标右移     {if (point.x >= 27)point.x = 10;elsepoint.x++;goto_xy(point.x, point.y);}if (ch == 80)      //↓的ASCLL码   光标下移    {if (point.y >= 20)point.y = 3;elsepoint.y++;goto_xy(point.x, point.y);}if (ch == 13)    //回车键的ASCLL码  下棋     {putdown();}if (button == 'b' || button == 'B')       //悔棋的操作    {ptr1 = p;if (p != head){p = p->fore;free(ptr1);point.x = p->coord.x;point.y = p->coord.y;go_back(point.x, point.y);}}
}
int judge()
{int count = 0;int pp = player == 0 ? 2 : 1;int c = 0;int r = 0;int rr = r;int cc = c;for (c = 0; c < 200; c++){for (r = 0; r < 200; r++){if (Q[r][c] != pp)continue;while (--cc >= 3 && Q[rr][cc] == pp)count++;cc = c;while (++cc < 23 && Q[rr][cc] == pp)count++;cc = c;if (count >= 4)return pp;count = 0;while (--rr >= 10 && Q[rr][cc] == pp)count++;rr = r;while (++rr < 30 && Q[rr][cc] == pp)count++;rr = r;if (count >= 4)return pp;count = 0;cc--;rr--;while ((cc >= 3 || rr >= 10) && Q[rr][cc] == pp){count++;cc--;rr--;}rr = r;cc = c;cc++;rr++;while ((cc < 23 || rr < 30) && Q[rr][cc] == pp){count++;cc++;rr++;}rr = r;cc = c;if (count + 1 >= 5)return pp;count = 0;cc++;rr--;while ((cc < 23 || rr >= 10) && Q[rr][cc] == pp){count++;cc++;rr--;}rr = r;cc = c;cc--;rr++;while ((cc >= 3 || rr < 30) && Q[rr][cc] == pp){count++;cc--;rr++;}rr = r;cc = c;if (count + 1 >= 5)return pp;count = 0;}}     return 0;
}
int main(void)
{system("color 4E");printf("\n欢迎来到五子棋小游戏\n");welcome();Sleep(3000);system("cls");while (1){init();int winner = 0;welcome();while (1)     //读取菜单选项         {char choice = getch();menu(choice);if (s != 0)break;}if (s == 1)    //选择人人对战        {draw();goto_xy(17, 22);printf("轮到甲方落子");goto_xy(point.x, point.y);while (1){button = getch();play(button);if (button == 27)   //ESC的ASCLL码                {if (MessageBox(NULL, TEXT("确定退出?"), TEXT(""), MB_ICONQUESTION | MB_OKCANCEL) == IDOK){system("cls");printf("\n    谢谢使用!\n");return 0;}}if (button == 13)    //回车键ASCLL码  开始判断输赢                    winner = judge();if (winner != 0){goto_xy(15, 24);if (winner == 2){printf("恭喜!甲方赢!\n");}if (winner == 1){printf("恭喜!乙方赢!\n");}}if (winner != 1 && winner != 2){continue;}goto_xy(13, 22);printf("   继续游戏?(Y/N):    ");while (1){button = getch();if (button == 'n' || button == 'N' || button == 'y' || button == 'Y')break;}if (button == 'n' || button == 'N'){if (MessageBox(NULL, TEXT("  确定退出"), TEXT(""), MB_ICONQUESTION | MB_OKCANCEL) == IDOK){system("cls");printf("\n    谢谢使用!\n");return 0;}}if (button == 'y' || button == 'Y'){winner = 0;system("cls");break;}}}if (s == 2){if (MessageBox(NULL, TEXT("  确定"), TEXT(""), MB_ICONQUESTION | MB_OKCANCEL) == IDOK){printf("\n\n    谢谢使用!\n");return 0;}}}return 0;
}

不足之处,请多指教!!

2019C语言课程设计相关推荐

  1. c语言课程设计的摘要,投票程序设计-C语言课程设计摘要.doc

    投票程序设计-C语言课程设计摘要.doc C 语 言 课 程 设 计 题 目: <7>投票程序 设 计 者: 专 业: 班级学号: 所属院系:机电学院 指导教师: 2010年7月17日 1 ...

  2. c语言课程设计商品销售系统,c语言课程设计商品销售管理系统.pdf

    C语言课程设计商 品销售管理系统 1 2020 年 4 月 19 日 文档仅供参考 商品销售管理系统 目录 一. 需求分析 2 二.概要设计 2 三.详细设计 4 四.调试分析 14 五.用户手册 1 ...

  3. 简单的c语言课程设计管理类,C语言课程设计-学生成绩简单管理程序.doc

    C语言课程设计报告 --学生成绩简单管理程序 一.程序的主要功能 用单向链表结构实现简单的学生成绩管理功能,要求具有链表建立.链表输出.结点有序插入.节电删除.数据查询等功能. 各项菜单功能: (1) ...

  4. C语言课程设计—图书管理系统

    这是本人大一第二学期初C语言课程设计的作品,嘿嘿,本来以为已经找不到原稿了,今天无意中居然在QQ网络硬盘中找到了当初的teta版,公布于此,以作纪念. C源码例如以下: #include<std ...

  5. c语言排班系统设计报告,C语言课程设计关于排班系统的一些问题

    已结贴√ 问题点数:10 回复次数:5 C语言课程设计关于排班系统的一些问题 小女大一,课程设计是百度排班系统(虽然不知老师干嘛现在布置类似小系统的作业),在论坛中找到个代码,其实只要交了代码就好,但 ...

  6. 小学生数学测试软件c语言流程图,小学生数学测试软件-C语言课程设计

    小学生数学测试软件-C语言课程设计 C语言课程设计 设计期限 2013 年 6 月 17 开始 至 2013 年 6 月 20 结束 系 别 信息管理与信息系统 专 业 班级 学生姓名 学号 指导教师 ...

  7. c语言结构体老师信息管理系统,C语言课程设计职工信息管理系统结构体数组实现程序源代码.doc...

    word格式 整理版 学习参考 //C语言课程设计 职工信息管理系统-结构体数组实现 #include #include #include struct employee { char num[10] ...

  8. C语言课程设计选哪个,C语言课程设计选题及要求.docx

    C语言课程设计题目 课程设计是对学生的一种全面综合训练, 是与课堂听讲. 自学和练习相辅相 成的.必不可少的一个教学环节. 通常,课程设计中的问题比平时的习题复杂的 多,也更接近实际. 课程设计着眼于 ...

  9. c++图书管理系统_轻松学做C语言课程设计:图书管理系统-数组实现

    C语言课程设计,对于初学者来说,主要是综合运用C语言基础知识,以实际项目的形式锻炼编程能力.从今天开始,一起轻松学做C语言课程设计常见项目,建议先运行代码,再一步步理解其实现. 题目要求 图书管理系统 ...

最新文章

  1. Fiddler抓包使用教程-过滤
  2. 设计进步,记一笔,控制层的代码,他不光控制还要校验数据!以前理解错啦
  3. width用计算机英语,计算机的英语词汇
  4. 容器编排技术 -- Kubernetes 给容器和Pod分配内存资源
  5. 在线小说网站的设计与实现(附源码)
  6. 用excel制作双层饼图_Excel做的双层饼图,太漂亮了
  7. 职场保护自己利益的技巧,你知道多少?
  8. Enterprise Architect综合建模平台
  9. HDOJ 4622 Reincarnation (hash)
  10. QNX 7.1 交叉编译 cron
  11. 计算机一级插入页码,同一篇文档中插入相同页码的小技巧
  12. 群晖nas免费内网穿透,实现外网异地远程访问
  13. Qgis教程3:数据美化
  14. 四种做动态数据可视化的方法
  15. i春秋新春战疫公益赛复现
  16. 医疗保健数据接口_为什么需要用于医疗保健业务的应用程序
  17. 这个四川女生火了!692分想当程序员:女生学编程的3大优势
  18. Spring Security应用详解(集成SpringBoot)
  19. APPInventor用mySQL_Android app界面设计工具AppInventor初体验
  20. macOS 版微信小助手,支持微信多开、防撤回、远程控制mac、自动回复等等

热门文章

  1. Single image dehazing (Fattal)
  2. dubbo推荐用什么协议?
  3. 网易邮箱恢复服务器上删除邮件吗,网易企业邮箱普通邮箱删除邮件找回
  4. 如何通俗的理解函数的极限_函数的极限问题怎么解释更通俗易懂?初高中数学辅导...
  5. 谷歌学术里面的代码查找
  6. ArrayList的实现原理以及实现线程安全
  7. TreeMap用法 示例
  8. 计算机网络基础选择题
  9. C语言学习笔记第十天
  10. Qt QComboBox下拉菜单背景透明