gpt4 book ai didi

C++ - vector 分割错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:57 27 4
gpt4 key购买 nike

我目前正在为我的大学类(class)开发一个名为 Blackjack 的 CMSC 项目。我试图将一个名为 Card 的类添加到另一个名为 Hand 的类中的 vector 对象。 Hand 类作为对象存储在名为 Player 的类中的另一个 vector 中。

我的问题是,我尝试在名为 Blackjack 的类中调用方法 OutputPlayerHand,但出现段错误。

这是我的 Blackjack.cpp 类代码。

#include "Blackjack.h"
#include <iostream>

Blackjack::Blackjack()
{
// Initialize the dealer
Player dealer((char *) "Dealer", 100);
m_dealer = dealer;

// Initialize a player 'Jane' with 100 funds
Player player((char *) "Jane", 100);
m_players.push_back(player);
}

Blackjack::Blackjack(char *names[], int numPlayers)
{
// Initialize the dealer
Player dealer((char *) "Dealer", 100);
m_dealer = dealer;

// Loop through all passed player names
for(int i = 0; i < numPlayers; i++)
{
// Initialize a player 'names[i]' with 100 funds
Player player(names[i], 100);
m_players.push_back(player);
}
}

int Blackjack::GetNumPlayers()
{
// Return the size of the players vector
return m_players.size();
}

char *Blackjack::GetPlayerName(int player)
{
// Return the requested player's name
return m_players[player].GetName();
}

int Blackjack::GetPlayerFunds(int player)
{
// Return the requested player's funds
return m_players[player].GetFunds();
}

void Blackjack::SetPlayerFunds(int player, int amt)
{
// Set the requested player's funds
m_players[player].SetFunds(amt);
}

bool Blackjack::SetPlayerBet(int player, int amt)
{
// If the player has insufficient funds
if(m_players[player].GetFunds() < amt)
{
// Return false
return false;
}

// Subtract the amount from the player funds
m_players[player].SetFunds(m_players[player].GetFunds() - amt);
// Add the amount to the player bet
m_players[player].SetBet(amt);
// Return true
return true;
}

void Blackjack::NewDeal()
{
// Create a new unsorted 52 card deck
Deck deck;
// Initialize m_deck to the new deck
m_deck = deck;
// Shuffle m_deck
m_deck.Shuffle();

// 2 cards for dealer, 2 cards for each player
int cardsToDeal = 2 + (2 * m_players.size());

// While we still have cards to deal
while(cardsToDeal > 0)
{
// Deal to each player
for(unsigned int i = 0; i < m_players.size(); i++)
{
std::cout << "Deal Player Card" << std::endl;
// Deal one card to the player
m_players[i].GetHand().AddCard(m_deck.DealCard());
// Decrement the number of cards to deal
cardsToDeal--;
}

std::cout << "Deal Dealer Card" << std::endl;
// Deal the dealer one card
m_dealer.GetHand().AddCard(m_deck.DealCard());
// Decrement the number of cards to deal
cardsToDeal--;
}
}

void Blackjack::OutputPlayerHand(int player)
{
std::cout << "Player Output Card." << std::endl;
m_players[player].GetHand().GetCard(0).OutputCard();
}

void Blackjack::OutputDealerHand()
{
// TODO: Code Method
}

bool Blackjack::HitPlayer(int player)
{
// TODO: Code Method
return false;
}

void Blackjack::DealerPlay()
{
// TODO: Code Method
}

int Blackjack::SettlePlayerBet(int player)
{
// TODO: Code Method
return -1;
}

这是我的 Player.cpp 类代码。

#include "Player.h"

Player::Player()
{
m_name = (char *) "Jane";
m_funds = 100;
m_bet = 0;
}

Player::Player(char *name, int funds)
{
m_name = name;
m_funds = funds;
m_bet = 0;
}

char *Player::GetName()
{
return m_name;
}

void Player::SetName(char *name)
{
m_name = name;
}

int Player::GetFunds()
{
return m_funds;
}

void Player::SetFunds(int funds)
{
m_funds = funds;
}

int Player::GetBet()
{
return m_bet;
}

void Player::SetBet(int bet)
{
m_bet = bet;
}

Hand Player::GetHand()
{
return m_hand;
}

这是我的 Hand.cpp 类代码。

#include "Hand.h"

void Hand::AddCard(Card card)
{
m_cards.push_back(card);
}

Card Hand::GetCard(int card)
{
return m_cards[card];
}

int Hand::Size()
{
return m_cards.size();
}

void Hand::Clear()
{
m_cards.clear();
}

这是主类 Proj2.cpp 的代码。

/*
* CHANGES TO Blackjack.h SPEC:
* added new member funcs:
* char *GetPlayerName(int)
* int GetNumPlayers()
* void OutputDealerHand()
*
* HitPlayer() should print out the card that was dealt.
*/
#include <cstdlib>
#include <iostream>
#include "Blackjack.h"

using namespace std;

Blackjack *CreateGame(int argc, char *argv[]);
int ProcessArgs(int argCnt, char *args[], char **&names, int *&funds);
void DoNewDeal(Blackjack &game);
void ProcAllBets(Blackjack &game);
void DoAllPlays(Blackjack &game);
void PlayOnePlayer(Blackjack &game, int player);
void SettleAllPlayers(Blackjack &game);
void ShowAllPlayerFunds(Blackjack &game);
bool QueryAnotherRound();


int main(int argc, char *argv[]) {
Blackjack *game;
int round;

cout << "Welcome to CMSC 202 Blackjack!\n";
game = CreateGame(argc, argv);

round = 0;
do {
cout << "\nRound " << ++round << ":\n";
ProcAllBets(*game);
DoNewDeal(*game);
DoAllPlays(*game);
SettleAllPlayers(*game);
ShowAllPlayerFunds(*game);
} while (QueryAnotherRound());

cout << "\nGoodbye!\n";
return 0;
}


Blackjack *CreateGame(int argc, char *argv[]) {
char **names;
int *funds;
int numPlayers;
Blackjack *game;

numPlayers = ProcessArgs(argc - 1, &argv[1], names, funds);
game = new Blackjack(names, numPlayers);
for (int p = 0; p < numPlayers; p++) {
game->SetPlayerFunds(p, funds[p]);
}
return game;
}


int ProcessArgs(int argCnt, char *args[], char **&names, int *&funds) {
int i, p;
int numRecs = argCnt / 2;

names = static_cast<char **>(calloc(numRecs, sizeof(char *)));
funds = static_cast<int *>(calloc(numRecs, sizeof(int)));

for (p = 0, i = 0; p < numRecs; p++) {
names[p] = args[i++];
funds[p] = atoi(args[i++]);
}
return p;
}


void ProcAllBets(Blackjack &game) {
int numPlayers = game.GetNumPlayers();
int bet;

for (int p = 0; p < numPlayers; p++) {
cout << "How much does " << game.GetPlayerName(p) << " bet? ";
cin >> bet;
cout << endl; // For neat scripting
if (!game.SetPlayerBet(p, bet)) {
cout << "Illegal bet--changing to $0\n";
game.SetPlayerBet(p, 0);
}
}
}


void DoNewDeal(Blackjack &game) {
int numPlayers = game.GetNumPlayers();

game.NewDeal();
cout << "The players' hands:\n";
for (int p = 0; p < numPlayers; p++) {
cout << game.GetPlayerName(p) << ": ";
game.OutputPlayerHand(p);
cout << endl;
}
cout << "Dealer: ";
game.OutputDealerHand(); // This hides dealer's hole card
cout << "\n\n";
}


void DoAllPlays(Blackjack &game) {
int numPlayers = game.GetNumPlayers();
int p;

for (p = 0; p < numPlayers; p++) {
PlayOnePlayer(game, p);
}
game.DealerPlay();
}

void PlayOnePlayer(Blackjack &game, int player) {
char *name = game.GetPlayerName(player);
string answer;
bool hit, busted;

cout << ">>" << name << "'s turn:\n";
busted = false;
do {
cout << "Hand: ";
game.OutputPlayerHand(player);
cout << endl;

cout << name << "'s play: ";
cin >> answer;
cout << endl; // For neat scripting
answer[0] == 'y' || answer[0] == 'Y';
hit = (answer[0] == 'h' || answer[0] == 'H');
if (hit) {
busted = game.HitPlayer(player);
}
} while (hit && !busted);
if (busted) {
cout << "Busted!\n";
}
cout << endl;
}

void SettleAllPlayers(Blackjack &game) {
int numPlayers = game.GetNumPlayers();
int p;

for (p = 0; p < numPlayers; p++) {
game.SettlePlayerBet(p);
// Above should print out:
// Joe has busted--Dealer wins", or "Sally has 15--Dealer loses"
}
cout << endl;
}

void ShowAllPlayerFunds(Blackjack &game) {
int numPlayers = game.GetNumPlayers();
int p;

for (p = 0; p < numPlayers; p++) {
cout << game.GetPlayerName(p) << " now has $"
<< game.GetPlayerFunds(p) << endl;
}
cout << endl;
}

bool QueryAnotherRound() {
string answer;

cout << "Another round? ";
cin >> answer;
cout << endl; // For neat scripting
return answer[0] == 'y' || answer[0] == 'Y';
}

谁能告诉我我做错了什么?我不允许以任何方式编辑 Proj2.cpp 类。如果您需要更多信息,请随时询问。如果您需要查看整个项目 here是到目前为止整个项目的 Github 存储库的链接。如果您需要查看我的项目规则和说明,here是我的类(class)项目描述网站的链接。

非常感谢您在此问题上提供的任何帮助,提前感谢您的宝贵时间。

最佳答案

是空的。

此行:m_players[i].GetHand().AddCard(m_deck.DealCard()) 仅向临时拷贝添加一张牌让 GetHand() 返回一个引用,您的玩家将真正获得卡片。

此外,您不应在存储库中包含 Prog2.out 等可执行文件。

关于C++ - vector 分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22522036/

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