C语言实现小猫钓鱼游戏
更新时间:2020年4月25日 17:26 点击:1554
本文实例为大家分享了C语言实现小猫钓鱼游戏的具体代码,供大家参考,具体内容如下
#include<stdio.h> #include<time.h> #include<string.h> #include<stdlib.h> #include<windows.h> typedef struct { int data[3600]; int col[3600]; int top; } stack; typedef struct { int data[3600]; int col[3600]; int head; int tail; } queue; queue p1, p2; stack table; void printmenu(); void deal(); void printp1(); void printtable(); void printzzz(); void judge(int a, int player); void game(); void ask(); int main() { while(1) { char ch; printmenu(); scanf("%c",&ch); switch(ch) { case '1': game(); break; case '2': exit(0); default: system("cls"); break; } } return 0; } void printmenu() { printf("*****************************\n"); printf("* *\n"); printf("* *\n"); printf("* 金钩钓鱼 *\n"); printf("* *\n"); printf("* *\n"); printf("* *\n"); printf("*****************************\n"); printf("\n\n\n"); printf("游戏规则:\n"); printf("每人各拿一半的牌,然后你放1张牌,\n"); printf("他放一张牌,如跟上面的牌一样,\n"); printf("即可把当中的牌全部吃掉,\n"); printf("直至对手手上无牌为止。\n\n\n"); printf("输入1开始游戏\n"); printf("输入2退出程序\n"); } void deal() { p1.head = 1; p1.tail = 1; p2.head = 1; p2.tail = 1; table.top = 0; int i, card, book[60]; srand((unsigned)time(0)); memset(book, 0, sizeof(book)); for(i = 1; i <= 27;) { card = rand()%55; if(card == 0) continue; else if(card >= 1 && card <= 13 && book[card] == 0) { p1.data[p1.tail] = card; p1.col[p1.tail] = 1; p1.tail++; book[card]=1; i++; } else if(card >= 14 && card <= 26 && book[card] == 0) { p1.data[p1.tail] = card - 13; p1.col[p1.tail] = 2; p1.tail++; book[card]=1; i++; } else if(card >= 27 && card <= 39 && book[card] == 0) { p1.data[p1.tail] = card - 26; p1.col[p1.tail] = 3; p1.tail++; book[card]=1; i++; } else if(card >= 40 && card <= 52 && book[card] == 0) { p1.data[p1.tail] = card - 39; p1.col[p1.tail] = 4; p1.tail++; book[card]=1; i++; } else if(card == 53) { p1.data[p1.tail] = card; p1.col[p1.tail] = 5; p1.tail++; book[card]=1; i++; } else if(card == 54) { p1.data[p1.tail] = card; p1.col[p1.tail] = 5; p1.tail++; book[card]=1; i++; } } for(i = 1; i <= 27;) { card = rand()%55; if(card == 0) continue; else if(card >= 1 && card <= 13 && book[card] == 0) { p2.data[p2.tail] = card; p2.col[p2.tail] = 1; p2.tail++; book[card]=1; i++; } else if(card >= 14 && card <= 26 && book[card] == 0) { p2.data[p2.tail] = card - 13; p2.col[p2.tail] = 2; p2.tail++; book[card]=1; i++; } else if(card >= 27 && card <= 39 && book[card] == 0) { p2.data[p2.tail] = card - 26; p2.col[p2.tail] = 3; p2.tail++; book[card]=1; i++; } else if(card >= 40 && card <= 52 && book[card] == 0) { p2.data[p2.tail] = card - 39; p2.col[p2.tail] = 4; p2.tail++; book[card]=1; i++; } else if(card == 53 && book[card] == 0) { p2.data[p2.tail] = card; p2.col[p2.tail] = 5; p2.tail++; book[card]=1; i++; } else if(card == 54 && book[card] == 0) { p2.data[p2.tail] = card; p2.col[p2.tail] = 5; p2.tail++; book[card]=1; i++; } } return; } void printp1() { int i; printf("您的牌有: \n"); for(i = p1.tail-1; i >= p1.head; i--) { if(p1.col[i] == 1) { printf("方片"); } else if(p1.col[i] == 2) { printf("红桃"); } else if(p1.col[i] == 3) { printf("黑桃"); } else if(p1.col[i] == 4) { printf("梅花"); } if(p1.data[i] == 11) { printf("J "); } else if(p1.data[i] == 12) { printf("Q "); } else if(p1.data[i] == 13) { printf("K "); } else if(p1.data[i] == 53) { printf("小王 "); } else if(p1.data[i] == 54) { printf("大王 "); } else printf("%d ", p1.data[i]); } printf("\n\n"); return; } void printtable() { int i; printf("桌上的牌有:\n"); if(table.top == 0) { printf("(无)\n"); } for(i = 1; i <= table.top; i++) { if(table.col[i] == 1) { printf("方片"); } else if(table.col[i] == 2) { printf("红桃"); } else if(table.col[i] == 3) { printf("黑桃"); } else if(table.col[i] == 4) { printf("梅花"); } if(table.data[i] == 11) { printf("J "); } else if(table.data[i] == 12) { printf("Q "); } else if(table.data[i] == 13) { printf("K "); } else if(table.data[i] == 53) { printf("小王 "); } else if(table.data[i] == 54) { printf("大王 "); } else printf("%d ", table.data[i]); } printf("\n\n\n"); return; } void printzzz() { system("cls"); Sleep(400); printf("正 "); Sleep(400); printf("在 "); Sleep(400); printf("出 "); Sleep(400); printf("牌 "); Sleep(400); printf(". "); Sleep(400); printf(". "); Sleep(400); printf(". "); Sleep(800); judge(table.data[table.top], 1); system("cls"); printf("玩家2 开始回合!!\n\n\n\n"); printtable(); table.top++; table.data[table.top] = p2.data[p2.head]; table.col[table.top] = p2.col[p2.head]; p2.head++; Sleep(600); printf("玩 "); Sleep(400); printf("家 "); Sleep(400); printf("2 "); Sleep(400); printf("正 "); Sleep(400); printf("在 "); Sleep(400); printf("出 "); Sleep(400); printf("牌 "); Sleep(400); printf(". "); Sleep(400); printf(". "); Sleep(400); printf(". "); Sleep(1000); judge(table.data[table.top], 2); return; } void judge(int a, int player) { int i, j, sum = 2; if(player == 1) { for(i = table.top-1; i >= 1; i--, sum++) { if(a == table.data[i] || ((a == 53 || a == 54) && (table.data[i] == 53 || table.data[i] == 54))) { for(j = table.top; j >= i; j--) { p1.data[p1.tail] = table.data[table.top]; p1.col[p1.tail] = table.col[table.top]; p1.tail++; table.top--; } break; } } if(i != 0) { system("cls"); Sleep(400); printf("玩 "); Sleep(400); printf("家 "); Sleep(400); printf("1 "); Sleep(400); printf("赢 "); Sleep(400); printf("了 "); Sleep(400); printf("%d ", sum); Sleep(400); printf("张"); Sleep(400); printf("牌 "); Sleep(400); printf("! "); Sleep(400); printf("! "); Sleep(400); printf("! "); Sleep(2000); system("cls"); } } else if(player == 2) { for(i = table.top-1; i >= 1; i--, sum++) { if(a == table.data[i] || ((a == 53 || a == 54) && (table.data[i] == 53 || table.data[i] == 54))) { for(j = table.top; j >= i; j--) { p2.data[p2.tail] = table.data[table.top]; p2.col[p2.tail] = table.col[table.top]; p2.tail++; table.top--; } break; } } if(i != 0) { system("cls"); Sleep(400); printf("玩 "); Sleep(400); printf("家 "); Sleep(400); printf("2 "); Sleep(400); printf("赢 "); Sleep(400); printf("了 "); Sleep(400); printf("%d ", sum); Sleep(400); printf("张"); Sleep(400); printf("牌 "); Sleep(400); printf("! "); Sleep(400); printf("! "); Sleep(400); printf("! "); Sleep(2000); system("cls"); } } return; } void game() { deal(); getchar(); while(p1.head != p1.tail && p2.head != p2.tail) { system("cls"); int i; printf("玩家1 开始回合!!\n\n\n\n"); printp1(); printtable(); printf("按回车来出牌\n"); getchar(); table.top++; table.data[table.top] = p1.data[p1.head]; table.col[table.top] = p1.col[p1.head]; p1.head++; printzzz(); } if(p1.head == p1.tail) { printf("恭喜您取得了胜利!!!\n"); printf("感谢使用本程序\n"); system("pause"); } else { printf("你没有牌了!"); printf("不好意思,您输了。。。\n"); printf("不要灰心,可以再来一局!!\n"); system("pause"); } return; } void ask() { char ch; system("cls"); printf("你确定要退出?\n"); printf("(输入y确认对出)\n"); printf("Y/N\n"); scanf("%c", &ch); if(ch == 'y' || ch == 'Y') { exit(0); } else { return; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: C++小知识:大于0并不意味着等于1
下一篇: OpenCV实现彩色照片转换成素描卡通片
相关文章
- 这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- 这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
详解C语言中的rename()函数和remove()函数的使用方法
这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25- 这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
- 本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
- 这篇文章主要给大家分享的是pygame游戏进行声音添加的方法,这文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!...2021-10-23
- 这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了JavaScript 实现生命游戏的示例步骤,帮助大家更好的理解和学习使用JavaScript,感兴趣的朋友可以了解下...2021-04-24
- 今天小编就为大家分享一篇C语言实现从文件读入一个3*3数组,并计算每行的平均值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了C语言中memcpy 函数的用法详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要为大家详细介绍了C#拼图游戏的编写代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 今天小编跟大家分享13个有趣又好玩的Python小游戏示例代码,教你如何通过边打游戏边学编程!感兴趣的小伙伴快跟随小编一起学习起来...2022-02-23
- 这篇文章主要介绍了C语言中查找字符在字符串中出现的位置的方法,分别是strchr()函数和strrchr()函数的使用,需要的朋友可以参考下...2020-04-25
- 很多同学在学习c语言的时候是不是会碰到a++和++a都有甚么作用啊。今天我们就来探讨下...2020-04-25
基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
这篇文章主要介绍了基于Python-Pycharm实现的猴子摘桃小游戏,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-20- 这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,需要的朋友可以参考下...2020-04-25