暂时有点bug,我回去研究一下
上课不能玩的Bug正在修复
V 2.0 解决了闪退的Bug
V 3.0 解决了人机太傻的Bug
#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=AI
int 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; // 换AI
print("换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);
}
}
}