二十一点计划
我必须创建一个与 7 个玩家和一个庄家一起玩的 BlackJack 程序。我必须对套装类型使用 switch 或 if 语句,并且该语句只能使用一次。
我所知道的是 Shuffle() 函数不起作用:
当我在没有调试的情况下运行时,它会给我两次类似 (A of Spades) 的输出 但我放置了一个数组,这样它就不会复制卡片。
然而,当我调试它并进入每一行代码时,输出会给我两张非重复的卡。
我也无法计算玩家总数,要求“编写一个函数向每个玩家发两张牌。”,如何将两张牌添加到没有确定大小的数组中?
此网络相册包含我的项目的完整要求:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
#include <stdio.h>
using namespace std;
void Header();
void Shuffle(int&, char&);
void Dealer();
void Deal();
void Another_Card();
void Fin_Dealer();
void WLT();
void gotoxy(int h, int w)
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {h, w};
SetConsoleCursorPosition ( hConsole, pos );
}
return;
}
int main()
{
Header();
Dealer();
Deal();
WLT();
gotoxy(0,19);
return 0;
}
void Header()
{
cout <<"\t\t\tWelcome to BlackJack!\n\n";
gotoxy(3,3);
cout <<"Dealer Player1 Player2 Player3 Player4 Player5 Player6 Player7 "<<endl;
return;
}
void Shuffle(int& num, char& suit)
{
int cards[52], dup[52];
int card;
srand(time(NULL));
for(int i = 0; i <52; i++)
dup[i] = 0;
card = rand() % 52;
while(dup[card])
card = rand() % 52;
dup[card] = 1;
suit = char(card/13 + 3); //display suit
num = card % 13;
switch(num)
{
case 0: cout<<setw(6)<<right<<" A"<<suit;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9: cout<<setw(6)<<right<<" "<<num + 1<<suit;
break;
case 10: cout<<setw(6)<<right<<" J"<<suit;
break;
case 11:cout<<setw(6)<<right<<" Q"<<suit;
break;
case 12:cout<<setw(6)<<right<<" K"<<suit;
break;
default:cout<<setw(6)<<right<<"Error";
break;
}
return;
}
void Dealer()
{
int i = 0;
int num = 0;
char suit;
for(i = 4;i <6; i++)
{
gotoxy(0,i);
Shuffle(num, suit);
}
return;
}
void Deal()
{
int i = 0;
int num = 0;
int row = 4;
char suit;
for(i = 1; i < 8; i++)
{
gotoxy(i*8, row);
Shuffle(num,suit);
}
if(playertotal <=20)
{
Another_Card()
}
return;
}
void Another_Card()
{
return;
}
void Fin_Dealer()
{
return;
}
void WLT()
{
return;
}
I have to create a BlackJack program that plays with 7 players and a Dealer. I have to use a switch or and if statement for the suit type and this statement can only be used once.
What I know is not working is the Shuffle() function:
When I run without debugging it will give me and output like (A of Spades) twice
but I put an array so it would not duplicate a card.
However when I do debug it and get into every line of code the output is would give me two non-duplicated cards.
I am also having trouble calculating player totals a requirement"Write a function to deal two cards to each player.", How do I add the two cards to an array that has no definite size?
This web albulm contains full requirements of my project:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
#include <stdio.h>
using namespace std;
void Header();
void Shuffle(int&, char&);
void Dealer();
void Deal();
void Another_Card();
void Fin_Dealer();
void WLT();
void gotoxy(int h, int w)
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {h, w};
SetConsoleCursorPosition ( hConsole, pos );
}
return;
}
int main()
{
Header();
Dealer();
Deal();
WLT();
gotoxy(0,19);
return 0;
}
void Header()
{
cout <<"\t\t\tWelcome to BlackJack!\n\n";
gotoxy(3,3);
cout <<"Dealer Player1 Player2 Player3 Player4 Player5 Player6 Player7 "<<endl;
return;
}
void Shuffle(int& num, char& suit)
{
int cards[52], dup[52];
int card;
srand(time(NULL));
for(int i = 0; i <52; i++)
dup[i] = 0;
card = rand() % 52;
while(dup[card])
card = rand() % 52;
dup[card] = 1;
suit = char(card/13 + 3); //display suit
num = card % 13;
switch(num)
{
case 0: cout<<setw(6)<<right<<" A"<<suit;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9: cout<<setw(6)<<right<<" "<<num + 1<<suit;
break;
case 10: cout<<setw(6)<<right<<" J"<<suit;
break;
case 11:cout<<setw(6)<<right<<" Q"<<suit;
break;
case 12:cout<<setw(6)<<right<<" K"<<suit;
break;
default:cout<<setw(6)<<right<<"Error";
break;
}
return;
}
void Dealer()
{
int i = 0;
int num = 0;
char suit;
for(i = 4;i <6; i++)
{
gotoxy(0,i);
Shuffle(num, suit);
}
return;
}
void Deal()
{
int i = 0;
int num = 0;
int row = 4;
char suit;
for(i = 1; i < 8; i++)
{
gotoxy(i*8, row);
Shuffle(num,suit);
}
if(playertotal <=20)
{
Another_Card()
}
return;
}
void Another_Card()
{
return;
}
void Fin_Dealer()
{
return;
}
void WLT()
{
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的洗牌代码不会跟踪已经发出的牌。每次调用它时,它都会重新初始化 dup 数组,从而可以多次处理同一张牌。
作为建议,创建两个子例程。每手洗一副牌一次。洗完牌后的第二张牌。
Your shuffle code isn't keeping track of what cards have already been dealt. Each time you call it, it re-initializes the dup array making it possible to deal the same card multiple times.
As a suggestion, create two subroutines. One shuffles a deck of cards once for each hand. The second deals from that shuffled deck.
您的 Shuffle() 循环执行如下操作:
考虑当该循环的迭代 x 生成卡片 x+n 时会发生什么。当您进行迭代 x+n 时,您将覆盖 dup[x+n]。
第一行任意将当前索引设置为 0(假)。 Shuffle 函数的其余部分根据随机“卡片”对 dup[] 数组进行索引。您将 dup[] 数组中的随机索引设置为“1”(true),然后可能在以后的迭代中覆盖它,从而使 dup[] 数组不正确。
从技术上讲,不必将数组项初始化为 0。这应该自动发生。但是,由于各种原因,通常认为预先初始化整个数组是一种好的做法。所以,做一个快速循环:
所以,快速的答案是删除 for 循环中的 dup[] = 0 行。
Your Shuffle() loop does something like this:
Consider what happens when iteration x of this loop generates card x+n. When you get to iteration x+n, you overwrite dup[x+n].
The first line arbitrarily sets the current index to 0 (false). The rest of your Shuffle function indexes the dup[] array based on the random 'card'. You are setting a random index in your dup[] array to '1' (true), then potentially overwriting it on a later iteration, making the dup[] array incorrect.
Technically, one should not have to initialize the array items to 0. That should happen automatically. But, for various reasons, it is generally considered good practice to initialize the entire array up front anyway. So, do a quick loop:
So, the quick answer is to remove the dup[] = 0 line in the for loop.