21点游戏c++编程
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#include<iostream>
#include "blackjack.h"
#include "card_and_player.h"
using namespace std;
char face[4] = { 3,4,5,6 }; //扑克牌花色ASCII码
char num[13] = { 'A','2','3','4','5','6','7','8','9','T','J','Q','K' };//扑克牌点数
blackjack::blackjack()
{
}
blackjack::~blackjack()
{
}
//生成牌的函数
void blackjack::start()
{
int j = 0, k = 0; //j控制花色,k控制点数
for (int i = 0; i < 52; i++)
{
if (i % 4 == 0)
j = 0;
if (i % 13 == 0)
k = 0;
while (j < 4 && k < 13)
{
card[i].face = face[j]; //花色
card[i].num = num[k]; //点数
j++;
k++;
break;
}
}
}
//洗牌函数
void blackjack::wash_card()
{
srand((unsigned int)time(0));
int j = 0;
for (int i = 0; i < 52; i++)
{
j = rand() % 52; //生成0~51随机数
struct card temp;
temp = card[i];
card[i] = card[j];
card[j] = temp;
}
for (int i = 0; i < 52; i++)
{
printf("%c%c ", card[i].face, card[i].num);
if ((i + 1) % 13 == 0)
{
printf("\n");
}
}
}
//发牌函数
void blackjack::send_card()
{
for (int i = 0; i < 2; i++)
{
struct player* pa = &player[i];
if (pa->flag) //代表他还要牌
{
pa->my_card[pa->num_of_card] = card[52 - left_card];
pa->num_of_card++;
left_card--;
}
else
continue;
}
}
//计算点数
void blackjack::sum()
{
for (int i = 0; i < 2; i++)
{
int sum = 0;
struct player* pb = &player[i];
struct card* temp = pb->my_card;
for (int i = 0; i < pb->num_of_card; i++)
{
if (temp->num == 'T' || temp->num == 'J' || temp->num == 'Q' || temp->num == 'K')
{
sum = sum + 10;
}
else if (temp->num == 'A')
{
if (sum + 1 == 21)
sum = sum + 1;
if (sum + 11 == 21)
sum = sum + 11;
if (sum + 11 > 21)
sum = sum + 1;
if (sum + 11 < 21)
sum = sum + 11;
}
else
{
sum = sum + (temp->num) - 48;
}
temp++;
}
cout<<" i + 1号玩家 的点数和为sum "<< endl;
printf("\n");
pb->SUM = sum;
}
printf("\n");
}
//统计并展示 单局结果
void blackjack::show()
{
if (player[0].SUM <= 21 && player[1].SUM <= 21) //都没爆
{
if ((21 - player[0].SUM) < (21 - player[1].SUM))
{
cout << "1号玩家获胜!" << endl;
player[0].score++;
}
if ((21 - player[0].SUM) > (21 - player[1].SUM))
{
cout << "2号玩家获胜!" << endl;
player[1].score++;
}
if ((21 - player[0].SUM) == (21 - player[1].SUM))
cout << "平局!" << endl;
}
if (player[0].SUM > 21 && player[1].SUM > 21) //都爆了
{
if ((player[0].SUM - 21) < (player[1].SUM - 21))
{
cout << "都爆了!1号玩家获胜!" << endl;
player[0].score++;
}
if ((player[0].SUM - 21) > (player[1].SUM - 21))
{
cout << "都爆了!2号玩家获胜!" << endl;
player[1].score++;
}
if ((player[0].SUM - 21) == (player[1].SUM - 21))
cout << "都爆了!平局!" << endl;
}
if (player[0].SUM > 21 && player[1].SUM <= 21) // 1号玩家爆了
{
cout << "2号玩家获胜!" << endl;
player[1].score++;
}
if (player[0].SUM <= 21 && player[1].SUM > 21) //2号玩家爆了
{
cout << "1号玩家获胜!" << endl;
player[0].score++;
}
if (player[0].SUM == 21 && player[1].SUM == 21) //俩人都刚好21
cout << "都是21,平局!" << endl;
cout << "1号玩家 和 2号玩家 比分为 player[0].score:player[1].score" << endl;
}
main函数是这么写的
#include<iostream>
#include"blackjack.h"
#include"card_and_player.h"
using namespace std;
int main()
{
blackjack game;
cout << "*************************" << endl;
cout << "!! 欢迎游玩21点游戏 !!" << endl;
cout << "*************************" << endl;
printf("\n");
cout << "请输入y开始新的游戏" << endl;
char text1 = '0';
cin >> text1;
getchar();
if (text1 == 'y' || text1 == 'Y')
{
game.start();
game.wash_card();
for (int i = 0;; i++)
{
printf("\n");
cout << "***************第 i+1 局游戏***************" << endl;
printf("\n");
//每小局开始把flag的初值设置成1,保证都有两张底牌
player[0].flag = 1;
player[1].flag = 1;
game.send_card();
game.send_card();
game.sum();
//发牌循环
for (;;)
{
//发完两张底牌可能就会爆,就会分胜负,所以在这里加了几条判断!!!!!!!!!!!!!!!!!!!!!!!
if (player[0].SUM > 21 && player[1].SUM <= 21) //刚发完牌 1号爆了
{
cout << "可惜~~刚发完牌 一号玩家就爆了!" << endl;
player[1].score++;
cout << "1号玩家 和 2号玩家 比分为 player[0].score:player[1].score" << endl;
break;
}
if (player[0].SUM <= 21 && player[1].SUM > 21) //刚发完牌 2号爆了
{
cout << "可惜~~刚发完牌 二号玩家就爆了!" << endl;
player[0].score++;
cout << "1号玩家 和 2号玩家 比分为 player[0].score:player[1].score" << endl;
break;
}
if (player[0].SUM > 21 && player[1].SUM > 21) //刚发完牌 都爆了
{
if ((player[0].SUM - 21) < (player[0].SUM - 21))
{
cout << "可惜~~刚发完牌 两位玩家都爆了!" << endl;
player[0].score++;
cout << "1号玩家 和 2号玩家 比分为 player[0].score:player[1].score" << endl;
break;
}
if ((player[0].SUM - 21) > (player[0].SUM - 21))
{
cout << "可惜~~刚发完牌 两位玩家都爆了!" << endl;
player[1].score++;
cout << "1号玩家 和 2号玩家 比分为 player[0].score:player[1].score" << endl;
break;
}
if ((player[0].SUM - 21) == (player[0].SUM - 21))
{
cout << "可惜~~刚发完牌 两位玩家都爆了!最终平局!" << endl;
cout << "1号玩家 和 2号玩家 比分为 player[0].score:player[1].score" << endl;
break;
}
}
if (player[0].flag)
{
cout << "->1号玩家是否继续要牌?" << endl;
char text2 = '0';
cin >> text2;
getchar();
if (text2 == 'y' || text2 == 'Y')
player[0].flag = 1;
else
player[0].flag = 0;
}
if (player[1].flag)
{
cout << "->2号玩家是否继续要牌?" << endl;
char text3 = '0';
scanf_s("%c", &text3, 1);
getchar();
if (text3 == 'y' || text3 == 'Y')
player[1].flag = 1;
else
player[1].flag = 0;
}
game.send_card();
game.sum();
//可能给发完牌,牌就没有了
if (game.left_card == 0) //没有牌了,自动退出
{
printf("\n");
printf("-------------------------------------------------\n");
printf("| 剩余牌数为0 已自动退出游戏! 最终比分如下 |\n");
printf("| 1号玩家 和 2号玩家 比分为 %d:%d |\n", player[0].score, player[1].score);
printf("------------------------------------------------\n");
printf("\n");
return 0;
}
if (player[0].num_of_card == 5 || player[1].num_of_card == 5)
{
game.show();
break;
}
if (player[0].flag == 0 && player[1].flag == 0)
{
game.show();
break;
}
if (player[0].SUM > 21 && player[1].SUM > 21)
{
game.show();
break;
}
if (player[0].SUM > 21 && player[1].SUM <= 21)
{
cout << "一号玩家爆了!" << endl;
game.show();
break;
}
if (player[0].SUM <= 21 && player[1].SUM > 21)
{
cout << "二号玩家爆了!" << endl;
game.show();
break;
}
}
printf("\n");
cout << "一号玩家的牌依次为:";
for (int i = 0; i < player[0].num_of_card; i++)
{
printf(" %c%c ", player[0].my_card[i].face, player[0].my_card[i].num);
}
printf("\n");
cout << "二号玩家的牌依次为:";
for (int i = 0; i < player[1].num_of_card; i++)
{
printf(" %c%c ", player[1].my_card[i].face, player[1].my_card[i].num);
}
printf("\n");
printf("\n");
if (game.left_card < 4) //剩余牌不够发下一回合的底牌,自动退出
{
printf("------------------------------------------------------------\n");
printf("| 剩余牌数为不足下一回合底牌 已自动退出游戏! 最终比分如下 |\n");
printf("| 1号玩家 和 2号玩家 比分为 %d:%d |\n", player[0].score, player[1].score);
printf("------------------------------------------------------------\n");
printf("\n");
return 0;
}
cout << "***是否继续进行下一局游戏?(剩余 game.left_card 张牌)" << endl;
char text4 = '0';
scanf_s("%c", &text4, 1);
getchar();
if (text4 == 'y' || text4 == 'Y')
{
for (int i = 0; i < 2; i++) //玩家数据再次初始化
{
struct player* pc = &player[i];
pc->num_of_card = 0;
pc->SUM = 0;
player[i].my_card[5].num = '0';
player[i].my_card[5].face = '0';
}
}
else
{
cout << "已退出游戏!" << endl;
break;
}
}
}
return 0;
}
运行结果及详细报错内容
头文件里创建了blackjack类 和玩家以及牌两个结构体
报错内容如下:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2005 "struct player * player" (?player@@3PAU0@A) 已经在 main.obj 中定义 21点c++ D:\ConsoleApplication1\21点c++\21点c++\函数.obj 1
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2005 "struct card * card" (?card@@3PAU0@A) 已经在 main.obj 中定义 21点c++ D:\ConsoleApplication1\21点c++\21点c++\函数.obj 1
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK1169 找到一个或多个多重定义的符号 21点c++ D:\ConsoleApplication1\21点c++\x64\Debug\21点c++.exe 1
我的解答思路和尝试过的方法
我一开始以为是结构体指针名字都叫p,就改成了pa pb pc。但还是不可以