纸牌丰收之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
原创粉丝点击