纸牌丰收之c++版本
来源:互联网 发布:剪音乐的软件 编辑:程序博客网 时间:2024/05/17 08:34
直接上代码吧,哎,还是不熟悉STL中的deque的用法,凑合吧。
代码中会涉及到几个基本知识点:
1. c++中的输出重定向
2. deque的一些简单用法
上!
//// main.cpp// fengshou//// Created by Apple on 15/11/1.// Copyright (c) 2015年 星月传奇. All rights reserved.///* 丰收 *//* * 规则简介如下: 一副牌不含大小王,4个人玩 * 每人13张牌,每人出一张到桌上,按次序排放 * 出牌之后,检查桌上牌,从后往前检查,如果有和自己那张出的牌面值一样(如都是7), * 那么两张7之间的牌收进来,放到自己的牌的最后,同时再出一张,再次检查,直到没有相同的为止。 */#include<fstream>#include <iostream>#include <deque>#include <algorithm>#include <cstdlib>#include <iomanip>#include <time.h>#include <string>using namespace std;#define CARDSNUM52#define KINDS13/* 4种花色,其中这里用不到 */typedef enum _EKIND{E_S = 1,E_H = 2,E_C = 3,E_D = 4}EKIND;/* 牌的结构体 */typedef struct _POKER{int number;EKIND kind;}POKER;#define CP(dst, src)\do{\dst.number = src.number;\dst.kind = src.kind;\}while(0);#define myprintf1(play, name)\do{\int i = 0;\cout << left;\cout << "---" << name << "---" << endl;\for(; i < play.size(); i++){\cout << setw(2) << i << " " << setw(2) << play[i].number << " ";\switch(play[i].kind){\case E_S:\cout << "Spade" << endl;\break;\case E_H:\cout << "Heart" << endl;\break;\case E_C:\cout << "Club" << endl;\break;\case E_D:\cout << "Diamond" << endl;\break;\default:\cout << endl;\break;\}\}\cout << "end of " << name << endl;\}while(0);/* 其实这4个play可以使用非双向列表的 *//* play1 */deque<POKER> play1;/* play2 */deque<POKER> play2;/* play3 */deque<POKER> play3;/* play4 */deque<POKER> play4;/* buf 用于处理牌桌中央的牌 */deque<POKER> buf;/* total cards */deque<POKER> cards(CARDSNUM);/* when iLosemen equals 3, winner is created! */int iLosemen = 0;/* 表示已经输掉的人,最开始没人输,所以为0 */int iLost[4] = {0, 0, 0, 0}; /* 分别代表 play 1,2,3,4是否已经输掉,0表示没有输,1表示输了 */void initCards(){int i = 0;int nm = 0;srand((unsigned)time(NULL));//初始化牌到cardsfor(; i<CARDSNUM; i++){cards[i].number = i%KINDS+1;cards[i].kind = (EKIND)(i/KINDS+1);}/* 随机发牌 */for(i=CARDSNUM; i>0;){/* 给play1 一张牌 */nm = rand()%i;play1.push_front(cards[nm]);if(nm != i - 1){CP(cards[nm], cards[i-1]);}i--;/* 给play2 一张牌 */nm = rand()%i;play2.push_front(cards[nm]);if(nm != i - 1){CP(cards[nm], cards[i-1]);}i--;/* 给play3 一张牌 */nm = rand()%i;play3.push_front(cards[nm]);if(nm != i - 1){CP(cards[nm], cards[i-1]);}i--;/* 给play4 一张牌 */nm = rand()%i;play4.push_front(cards[nm]);if(nm != i - 1){CP(cards[nm], cards[i-1]);}i--;}cout << "Deal finished" << endl;myprintf1(play1, "play1");myprintf1(play2, "play2");myprintf1(play3, "play3");myprintf1(play4, "play4");cout << "All players printf finished" << endl;cout << "Let's begin!!" << endl;}void playCards(deque<POKER> &play, deque<POKER> &locbuf, string name){ if (play.empty()) {/* 检测选手是否已经输掉 */ if (0 == name.compare("play1")) { if (0 == iLost[0]) { iLost[0] = 1; } } if (0 == name.compare("play2")) { if (0 == iLost[1]) { iLost[1] = 1; } } if (0 == name.compare("play3")) { if (0 == iLost[2]) { iLost[2] = 1; } } if (0 == name.compare("play4")) { if (0 == iLost[3]) { iLost[3] = 1; } } }else{ if (3 == iLosemen) {/* 已经有3人输了,而剩下的人就是赢家了 */ cout << name << " is winner and game over" << endl; iLosemen++; return; }//cout << "#############################################" <<endl;//myprintf1(play, name);//myprintf1(locbuf, "buf"); POKER tPoker;/* 用于暂时保存选手要打出去的牌 */ tPoker = play.front();int idx = locbuf.size()-1;for(; idx >= 0; idx--){if(locbuf[idx].number == tPoker.number) {/* 检测有相同的牌,开始收牌 *///cout << "Equal number is in [" << idx << "]" << endl;play.push_back(play.front());play.pop_front();for(int j = locbuf.size() - 1; j >= idx; j--){play.push_back(locbuf.back());locbuf.pop_back();}/* 收完牌之后,选手重新出牌 */tPoker = play.front();}} /* 把选手的牌按序放到桌面上 */ locbuf.push_back(play.front()); play.pop_front();//cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" <<endl;//myprintf1(play, name);//myprintf1(buf, "buf"); /* 检测当前选手手中是否还有牌,如果没有则表示已经输了 */ if (play.empty()) { iLosemen++; cout << name << " has lost" << endl; } }}int main(int argc, const char * argv[]) {//ofstream log("debug.txt");//cout.rdbuf(log.rdbuf()); cout << "******Now we are playing******" << endl;/* 初始化,即发牌 */initCards();/* 游戏过程中,退出循环即为游戏结束 */while (4 != iLosemen) {playCards(play1, buf, "play1");playCards(play2, buf, "play2");playCards(play3, buf, "play3");playCards(play4, buf, "play4");cout << "******printf every players info******" << endl;cout << "play1.size()=" << play1.size() << endl;cout << "play2.size()=" << play2.size() << endl;cout << "play3.size()=" << play3.size() << endl;cout << "play4.size()=" << play4.size() << endl;cout << "buf.size()=" << buf.size() << endl;cout << "*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*" << endl;}cout << "play cards is over" << endl;cout << "-----------------------------------------" << endl;return 0;}
0 0
- 纸牌丰收之c++版本
- C语言纸牌程序代码
- 贪心之均分纸牌
- 纸牌游戏之三
- 蓝桥杯之纸牌三角形
- 纸牌
- 题解。2014、C/C++本科B组【史丰收速算】
- 山东省第五届蓝桥杯 ///标题:史丰收速算//c/c++组
- js练习之纸牌互换
- 贪心算法之均分纸牌
- 农民伯伯喜气洋洋在阳光下唱丰收之歌
- C语言-纸牌计算24点小游戏
- HDU 2209 C - 翻纸牌游戏
- 24点纸牌游戏 c语言
- C语言(14)纸牌游戏1
- 丰收WF
- wikioi天梯之1098 均分纸牌 贪心
- 猜纸牌游戏之二 实体类
- IE盒子模型和标准W3C盒子模型
- 17个新手常见Python运行时错误
- linux配置oracle客户端,sqlplus、sqlldr、exp、imp
- 浅析深究什么是中间件
- Codeforces 598E Chocolate Bar (dp)
- 纸牌丰收之c++版本
- Linux平台Oracle多个实例启动
- OpenCV2:Mat属性type,depth,step
- 一些学习链接
- 安卓 利用videoView 播放资源文件 并且全屏播放 适用于闪屏页
- NP-完全性介绍
- Android 实现控件浮动效果
- Android实现字母索引查找地名、联系人(二)
- Oracle将字符串转换为数字,并替换特殊字符