版本比较
UNO
正文
增加 256 行删除 376 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416暂时有点bug,我回去研究一下上课不能玩的Bug正在修复```cpp#include<bits/stdc++.h>#include<windows.h>#include<conio.h>using namespace std;struct node{int con,col;};stack<node> cards;//打字动画输出void print(string s){int len=s.length();for(int i=0;i<len;i++){putchar(s[i]);Sleep(10);}}//打印卡牌信息void put(node c){switch(c.col){case 1: print("红色 "); break;case 2: print("蓝色 "); break;case 3: print("黄色 "); break;case 4: print("绿色 "); break;case 5: print("黑色 "); break;}if(c.con>=0&&c.con<=9) cout<<c.con<<"\n";else{if(c.con==10) print("转向\n");if(c.con==11) print("跳过\n");if(c.con==12) print("+2\n");if(c.con==13) print("+4\n");if(c.con==14) print("万能\n");}}//牌堆不足时重新洗牌生成void refillDeck(){print("\n【警告】牌堆已空,重新生成牌堆!\n");while(!cards.empty()) cards.pop();for(int i=1;i<=2000;i++){node card;card.col=rand()%4+1;card.con=rand()%15;if(card.con==13||card.con==14) card.col=5;cards.push(card);}}//安全摸牌,如果空则自动补牌node drawCard(){if(cards.empty()) refillDeck();node res = cards.top();cards.pop();return res;}int main(){srand((unsigned int)time(0));print("作者:whx\n");print("按下任意键继续......\n");while(!_kbhit()) Sleep(1);_getch();system("cls");print("欢迎来到 UNO!\n");print("规则如下:\n");print("================================\n");print("【基本规则】\n");print("1. 每位玩家初始手牌 7 张\n");print("2. 按顺时针方向轮流出牌\n");print("3. 出牌必须与上一张牌的颜色或数字相同\n");print("4. 若无法出牌则从牌堆摸一张牌(摸牌后本轮不能出牌)\n");print("5. 先出完所有手牌的玩家获胜\n");print("\n");print("【功能牌】\n");print("1. 转向 —— 改变出牌方向\n");print("2. 跳过 —— 下一位玩家被跳过\n");print("3. +2 —— 下一位玩家摸 2 张牌并跳过回合\n");print("4. +4 —— 下一位玩家摸 4 张牌并跳过回合,同时可指定颜色\n");print("5. 万能 —— 出牌者可指定下一轮的颜色\n");print("\n");print("【颜色】\n");print("1 —— 红色\n");print("2 —— 蓝色\n");print("3 —— 黄色\n");print("4 —— 绿色\n");print("\n");print("【胜利条件】\n");print("率先出完所有手牌的玩家获胜!\n");print("================================\n");print("祝各位游戏愉快!\n");print("按下任意键继续......\n");while(!_kbhit()) Sleep(1);_getch();system("cls");//初始化牌堆for(int i=1;i<=10000;i++){node card;card.col=rand()%4+1;card.con=rand()%15;if(card.con==13||card.con==14) card.col=5;cards.push(card);}int mode=0;print("请选择:人机对战 (a) 或 双人对战 (b)\n");print("请按下 a 或 b.....\n");while(true){if(GetAsyncKeyState('A')&0x8000){print("你选择了:人机对战\n");mode=1;break;}if(GetAsyncKeyState('B')&0x8000){print("你选择了:双人对战\n");mode=2;break;}Sleep(10);}Sleep(200);while(_kbhit()) _getch(); //清空按键缓冲区system("cls");bool win=false;// direction=1顺时针,-1逆时针(修复转向逻辑)int turn=1; //1玩家,0AI(人机模式)int dir = 1;if(mode == 1) //人机对战{vector<node> YourCards,AIsCards;//发7张牌for(int i=1;i<=7;i++){YourCards.push_back(drawCard());AIsCards.push_back(drawCard());}node Top;//起始牌不能是功能牌do{Top=drawCard();}while(Top.con>=10);while(!win){if(turn==1) //玩家回合{print("====================\n");print("你的回合开始\n");print("场上当前牌:");put(Top);print("你的手牌:\n");for(int i=0;i<YourCards.size();i++){cout<<(i+1)<<':';put(YourCards[i]);}bool hand_out=false;bool drawFlag = false; //是否已经摸牌while(!hand_out){print("请选择要出的牌号(输入 0 摸一张):");int chs;cin>>chs;if(chs==0){if(drawFlag){print("本轮已经摸过牌,不能再次摸牌!只能结束回合\n");continue;}drawFlag=true;print("你选择摸一张牌\n");node get = drawCard();YourCards.push_back(get);print("你摸到了 ");put(get);print("摸牌后本轮无法出牌,回合结束\n");break;}if(chs<1 || chs > (int)YourCards.size()){print("你没有这张牌,请重新选择\n");continue;}node ocard=YourCards[chs-1];//判断能否出牌:同色 / 同数字 / 黑色万能牌if(ocard.con!=Top.con && ocard.col!=Top.col && ocard.col!=5){print("你不能出这张牌\n");continue;}hand_out=true;print("你打出了 ");put(ocard);YourCards.erase(YourCards.begin()+chs-1);Top=ocard;//处理功能牌if(ocard.con==10) //转向{dir *= -1;print("【转向】出牌方向反转!\n");}if(ocard.con==11) //跳过{print("【跳过】AI本回合被跳过\n");}if(ocard.con==12) //+2{print("【+2】AI摸2张牌并跳过回合\n");AIsCards.push_back(drawCard());AIsCards.push_back(drawCard());}if(ocard.con==13) //+4{print("【+4】AI摸4张牌并跳过回合\n");for(int i=0;i<4;i++) AIsCards.push_back(drawCard());print("请指定下一回合出牌颜色:1红 2蓝 3黄 4绿\n");int colored;cin>>colored;while(colored<1||colored>4){print("输入错误,请重新输入1~4:");cin>>colored;}Top.col=colored;}if(ocard.con==14) //万能{print("请指定下一回合出牌颜色:1红 2蓝 3黄 4绿\n");int colored;cin>>colored;while(colored<1||colored>4){print("输入错误,请重新输入1~4:");cin>>colored;}Top.col=colored;}}//玩家胜利判定if(YourCards.empty()){system("color 0a");system("cls");print("🎉 恭喜,你赢了 🎉\n");system("color 0F");system("pause");return 0;}//切换回合逻辑if(Top.con == 11 || Top.con ==12 || Top.con ==13){//跳过/加牌,对方不能行动,继续玩家回合turn=1;}else{turn=0;}print("你的回合结束\n");Sleep(500);}else //AI回合 turn=0{print("====================\n");print("AI 的回合开始\n");print("场上当前牌:");put(Top);int selectedIdx = -1;//寻找可以打出的牌for(int i=0;i<AIsCards.size();i++){node IT=AIsCards[i];if(IT.con==Top.con||IT.col==Top.col||IT.col==5){selectedIdx = i;break;}}bool hand_out = false;if(selectedIdx != -1){node IT = AIsCards[selectedIdx];print("AI 打出了 ");put(IT);Top=IT;AIsCards.erase(AIsCards.begin()+selectedIdx);hand_out=true;if(IT.con==10) //转向{dir *= -1;print("【转向】出牌方向反转!\n");}if(IT.con==11) //跳过{print("【跳过】你本回合被跳过\n");}if(IT.con==12) //+2{print("【+2】你摸2张牌并跳过回合\n");node c1=drawCard();node c2=drawCard();print("你摸到:");put(c1);print("你摸到:");put(c2);YourCards.push_back(c1);YourCards.push_back(c2);}if(IT.con==13) //+4{print("【+4】你摸4张牌并跳过回合\n");for(int i=0;i<4;i++){node get=drawCard();print("你摸到:");put(get);YourCards.push_back(get);}int colored=rand()%4+1;Top.col=colored;switch(colored){case 1:print("AI指定颜色:红色\n");break;case 2:print("AI指定颜色:蓝色\n");break;case 3:print("AI指定颜色:黄色\n");break;case 4:print("AI指定颜色:绿色\n");break;}}if(IT.con==14) //万能{int colored=rand()%4+1;Top.col=colored;switch(colored){case 1:print("AI指定颜色:红色\n");break;case 2:print("AI指定颜色:蓝色\n");break;case 3:print("AI指定颜色:黄色\n");break;case 4:print("AI指定颜色:绿色\n");break;}}}if(!hand_out){print("AI 没有可出的牌,摸一张\n");node get=drawCard();AIsCards.push_back(get);}//AI胜利判定if(AIsCards.empty()){system("color 0c");system("cls");print("😥 AI获胜,你输了 😥\n");system("color 0F");system("pause");return 0;}//回合切换if(Top.con == 11 || Top.con ==12 || Top.con ==13){turn=0;}else{turn=1;}print("AI 的回合结束\n");Sleep(800);}}}else if(mode == 2){print("双人对战模式正在开发中!当前仅支持人机对战\n");print("按任意键退出程序\n");_getch();}return 0;}```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296暂时有点bug,我回去研究一下上课不能玩的Bug正在修复V 2.0 解决了闪退的BugV 3.0 解决了人机太傻的Bug```cpp#include<bits/stdc++.h>#include<windows.h>#include<conio.h>using namespace std;struct node { int con, col; };stack<node> cards;void print(string s) {for(char c : s) { putchar(c); Sleep(10); }}void put(node c) {switch(c.col) {case 1: print("红色 "); break;case 2: print("蓝色 "); break;case 3: print("黄色 "); break;case 4: print("绿色 "); break;case 5: print("黑色万能 "); break;}if(c.con>=0 && c.con<=9) cout<<c.con<<"\n";else {if(c.con==10) print("转向\n");if(c.con==11) print("跳过\n");if(c.con==12) print("+2\n");if(c.con==13) print("+4\n");if(c.con==14) print("万能\n");}}void refillDeck() {print("\n【警告】牌堆已空,重新生成!\n");while(!cards.empty()) cards.pop();for(int i=0; i<2000; i++) {node card;card.col = rand()%4+1;card.con = rand()%15;if(card.con==13 || card.con==14) card.col=5;cards.push(card);}}node drawCard() {if(cards.empty()) refillDeck();node res = cards.top(); cards.pop();return res;}// 判断能否出牌bool canPlay(node ocard, node top) {if(ocard.col == 5) return true;if(ocard.con == top.con) return true;if(ocard.col == top.col) return true;return false;}int main() {srand((unsigned)time(0));print("作者:whx\n按下任意键继续......\n");while(!_kbhit()) Sleep(1);_getch(); system("cls");print("欢迎来到 UNO!\n规则如下:\n""1. 初始7张手牌\n2. 顺时针出牌\n""3. 同色或同数字出牌\n4. 不能出则摸牌\n""5. 先出完获胜\n\n""功能牌:转向/跳过/+2/+4/万能\n""颜色:1红 2蓝 3黄 4绿\n""按任意键继续......\n");while(!_kbhit()) Sleep(1);_getch(); system("cls");// 初始化牌堆for(int i=0; i<10000; i++) {node card;card.col = rand()%4+1;card.con = rand()%15;if(card.con==13||card.con==14) card.col=5;cards.push(card);}print("请选择:人机对战(a) 双人对战(b)\n");int mode=0;while(true) {if(GetAsyncKeyState('A')&0x8000) { print("人机对战\n"); mode=1; break; }if(GetAsyncKeyState('B')&0x8000) { print("双人对战\n"); mode=2; break; }Sleep(10);}Sleep(200);while(_kbhit()) _getch();system("cls");if(mode != 1) {print("双人模式开发中!\n"); _getch(); return 0;}vector<node> YourCards, AICards;for(int i=0; i<7; i++) {YourCards.push_back(drawCard());AICards.push_back(drawCard());}node Top;do { Top = drawCard(); } while(Top.con >= 10);int turn = 1; // 1=玩家 0=AIint dir = 1; // 1=顺时针 -1=逆时针(当前未用到方向切换玩家)bool win = false;while(!win) {if(turn == 1) { // ===== 玩家回合 =====print("====================\n你的回合\n场上:");put(Top);print("你的手牌:\n");for(int i=0; i<(int)YourCards.size(); i++) {cout<<(i+1)<<':'; put(YourCards[i]);}bool hand_out = false;bool drawFlag = false;while(!hand_out) {print("出牌号(0摸牌):");int chs; cin>>chs;if(chs == 0) {if(drawFlag) {print("已摸过牌!回合结束\n"); continue;}drawFlag = true;node get = drawCard();YourCards.push_back(get);print("摸到:"); put(get);print("本轮无法出牌,回合结束\n");break;}if(chs<1 || chs>(int)YourCards.size()) {print("无效选择!\n"); continue;}node ocard = YourCards[chs-1];if(!canPlay(ocard, Top)) {print("不能出这张牌!\n"); continue;}hand_out = true;print("你打出:"); put(ocard);YourCards.erase(YourCards.begin()+chs-1);Top = ocard;// 处理功能牌if(ocard.con==10) { dir*=-1; print("【转向】\n"); }if(ocard.con==11) { print("【跳过】AI被跳过\n"); }if(ocard.con==12) {print("【+2】AI摸2张\n");AICards.push_back(drawCard());AICards.push_back(drawCard());}if(ocard.con==13) {print("【+4】AI摸4张\n");for(int i=0;i<4;i++) AICards.push_back(drawCard());int col;print("指定颜色(1红2蓝3黄4绿):");cin>>col;while(col<1||col>4) { print("重新输入:"); cin>>col; }Top.col = col;switch(col) {case 1:print("红色\n");break;case 2:print("蓝色\n");break;case 3:print("黄色\n");break;case 4:print("绿色\n");break;}}if(ocard.con==14) {print("指定颜色(1红2蓝3黄4绿):");int col; cin>>col;while(col<1||col>4) { print("重新输入:"); cin>>col; }Top.col = col;switch(col) {case 1:print("红色\n");break;case 2:print("蓝色\n");break;case 3:print("黄色\n");break;case 4:print("绿色\n");break;}}}// ===== 修复:回合切换逻辑 =====if(Top.con==10 || Top.con==11 || Top.con==12 || Top.con==13) {print("功能牌!你继续出牌\n");// turn 不变,继续玩家回合} else {turn = 0; // 换AIprint("换AI回合\n");}if(YourCards.empty()) {system("color 0a"); system("cls");print("🎉 你赢了!🎉\n");system("color 0F"); system("pause");return 0;}Sleep(500);} else { // ===== AI回合 =====print("====================\nAI回合\n场上:");put(Top);// AI策略:找最优牌(优先出功能牌)int bestIdx = -1;int bestScore = -1;for(int i=0; i<(int)AICards.size(); i++) {if(canPlay(AICards[i], Top)) {int score = 0;if(AICards[i].con>=10) score = 20; // 功能牌优先else if(AICards[i].con>=0) score = 10;if(score > bestScore) {bestScore = score;bestIdx = i;}}}bool hand_out = false;if(bestIdx != -1) {node IT = AICards[bestIdx];print("AI打出:"); put(IT);Top = IT;AICards.erase(AICards.begin()+bestIdx);hand_out = true;if(IT.con==10) { dir*=-1; print("【转向】\n"); }if(IT.con==11) { print("【跳过】你被跳过\n"); }if(IT.con==12) {print("【+2】你摸2张\n");YourCards.push_back(drawCard());YourCards.push_back(drawCard());}if(IT.con==13) {print("【+4】你摸4张\n");for(int i=0;i<4;i++) YourCards.push_back(drawCard());int col = rand()%4+1;Top.col = col;switch(col) {case 1:print("AI指定红色\n");break;case 2:print("AI指定蓝色\n");break;case 3:print("AI指定黄色\n");break;case 4:print("AI指定绿色\n");break;}}if(IT.con==14) {int col = rand()%4+1;Top.col = col;switch(col) {case 1:print("AI指定红色\n");break;case 2:print("AI指定蓝色\n");break;case 3:print("AI指定黄色\n");break;case 4:print("AI指定绿色\n");break;}}}if(!hand_out) {print("AI摸牌\n");AICards.push_back(drawCard());}// ===== 修复:AI回合切换 =====if(Top.con==10 || Top.con==11 || Top.con==12 || Top.con==13) {print("功能牌!AI继续\n");// turn 不变,继续AI回合} else {turn = 1; // 换玩家print("换你回合\n");}if(AICards.empty()) {system("color 0c"); system("cls");print("😥 AI赢了 😥\n");system("color 0F"); system("pause");return 0;}Sleep(800);}}}```