gpt4 book ai didi

c++ - 带类(class)的二十一点模拟器

转载 作者:搜寻专家 更新时间:2023-10-31 02:17:21 25 4
gpt4 key购买 nike

我是 C++ 的新手,正在尝试制作 Blackjack 模拟器。 Player 是一个类,用于存储从一副牌中发出的牌; Card 类包含花色和面值。我不断收到 Card Player::getCard( int index ) const 的错误消息“Control may reach end of non-void function”,我做错了什么?也有人可以检查我的代码中是否存在任何逻辑缺陷,因为我无法运行它并由于错误消息而无法检查?

#include "Player.h"
#include "Game.h"
#include "Card.h"
using namespace std;

Player::Player( )
{
// The Player has no Cards in his hand
myNumberOfCards = 0;
}

std::ostream& operator <<( std::ostream& outs, const Player & p )
{
// print out all the actual cards in the array myCards
for (int i = 0; i < p.cardCount(); i++)
{
outs << p.myCards[i] << endl;
}
return( outs );
}

void Player::acceptCard(Card c)
{
// as long as there is space in the array myCards, place Card c into myCards
// if there is not enough space for another card, throw an exception
try
{
for (; myNumberOfCards < MAXCARDS; myNumberOfCards++)
myCards[ myNumberOfCards ] = c;
if (myNumberOfCards > MAXCARDS)
throw myNumberOfCards;
}
catch (int e)
{
std::logic_error( "more than maximum of cards possible" ); // Since the player must be busted if he has more than 11 cards, how should I set the outcome to playerbusted if I have a bool in the game class?
}
}

Card Player::getCard( int index ) const
{
// return the requested card
// if the index is bad, throw an exception
try
{
while ( index > 0 && index < myNumberOfCards )
return ( myCards[ index ] );
if (index < 0 || index > myNumberOfCards)
throw index;
}
catch (int e)
{
std::logic_error( "bad index" ); // why there's an error?
}
}

int Player:: cardCount() const
{
// return the number of cards stored in my array
return myNumberOfCards;
}

int Player::handcount( ) const
{
// total up the points in this player's hand
// Ace's might be worth 1 or 11
Player p;
int total = 0;
bool hasAce = false;
for (int i = 0; i < myNumberOfCards; i++)
{
total += myCards[i].count();
if (myCards[i].getFace() == ACE)
hasAce = true;
}
if (total < 11 && hasAce == true)
total += 10;
return( total );
}


bool Player::hasBlackJack( ) const
{
bool result = false;
if (myNumberOfCards == 2 && handcount() == 21)
{
result = true;
}
return( result );
}

最佳答案

// vvvv must return a Card
Card Player::getCard( int index ) const
{
try
{
// ...
throw index;
}
catch (int e)
{
std::logic_error( "bad index" ); // this doesn't throw, so will continue past
}
// ends up here, doesn't return
}

你不会抛出一个std::logic_error,只是在适当的地方构造一个然后什么也不做。

如果发生命中throw index 的错误,您最终会到达函数底部而不返回任何内容,即Undefined Behaviour。 (非常糟糕,任何事情都有可能发生)。

可能您的意思是throw std::logic_error("bad index");?这会导致您异常退出函数,而不是正常返回,因此不会导致问题。

去掉getCard中的try/catch,替换

会更简单
throw index;

throw std::logic_error("bad index");

因为调用者的最终结果是相同的。并放弃 while,因为它实际上不是一个循环,只是一个检查。这是简化版本:

Card Player::getCard( int index ) const
{
// return the requested card
// if the index is bad, throw an exception
if ( index > 0 && index < myNumberOfCards )
return ( myCards[ index ] );
else
throw std::logic_error("bad index");
}

您在 acceptCard 中犯了同样的错误,但由于它返回 void,您看不到错误消息:“falling off the bottom” of a void 函数是允许的,它就像最后一行只是 return; 一样。


此外,请重新考虑您使用的不良做法 using namespace std;endl .

关于c++ - 带类(class)的二十一点模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com