- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在为我的大学类(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/
我有这个析构函数,它在运行时产生错误“vector 迭代器不可取消引用”。 gridMatrix 是一个 std::vector * > * > * > * > 我添加了 typename 和 typ
我有一个 vector 的 vector ,比方说 std::vector > my2dArray; 现在我想要一个 vector ,其中包含 my2dArray 中 vector 的大小。手动这看起
假设我有一些 vector :v1、v2、v3 假设我还有一个 vector 来保存这些 vList = {v1, v2, v3} 如果我同步了 (vList),这是否意味着 v1、v2 和 v3 也
我正在创建一个 char 的二维 vector 数组作为类变量,但我在将 vector 添加到 vector 数组中时遇到了麻烦。 我正在使用 C++ 11 标准运行 gcc。 我尝试使用 vecto
如何修改 Vec基于 Vec 中某项的信息没有对向量的不可变和可变引用? 我已尝试创建一个最小示例来演示我的特定问题。在我的真实代码中,Builder struct 已经是其他答案提出的中间结构。具体
这个问题在这里已经有了答案: What is the idiomatic Rust way to copy/clone a vector in a parameterized function? (
在我的程序中,我有一个整数 vector 的 vector 。现在我想从 vector 的 vector 中取出一个 vector 并在另一个 vector 容器中对其进行操作,但是我得到了错误...
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
我有一个函数,该函数应使用来自字符串类型的给定 vector vector 中的某些元素初始化来自字符串类型的空 vector vector 。我的语法看起来像这样 std::vector> extr
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
这里有很多类似的问题,但我没有真正找到任何可以特别回答我的问题的问题。 我有一个 vector 的 vector 作为类的属性。另一个属性是 bucket_count。我想将 vector 的 vec
如果我像这样创建一个 vector 的 vector : std::vector> myVectorOfVectors; 然后用一些东西填充它: std::vector myVector1; myVe
我正在用 C++ 编写自定义 vector 类。我对这样的代码有疑问: vector vec; vec.push_back(one); vec.push_back(two);
这是我发布的问题 c++ program for reading an unknown size csv file (filled only with floats) with constant (b
vector> a; for (int i=0;i v(i+1); iota(v.begin(),v.end(),1); a.push_back(v); } a.erase(a.beg
也许已经晚了,但我不明白为什么我会得到一个超出此代码范围的 vector 下标: int m = 3; int n = 2; std::vector> path(m, std::vector(n, 0
这个问题真的很奇怪,我似乎找不到任何导致它的原因。 所以这里有一个赋值运算符重载函数,鸟类和哺乳动物都是 vector 。 (下面是类) const Register& Register::opera
我怎么去 std::vector> 只是 std::vector> ?有真正有效的方法吗? 最佳答案 我会做这样的事情: #include #include int main() { //
我正在尝试将这些 vector 中的一些数据写入文本文件。当我运行代码时,它返回运行时错误。 Category、Product、Cart、Customer和Address都是struct 包含每个 g
显然它会因您使用的编译器而异,但我很好奇执行 vector> 时的性能问题与 vector*> ,尤其是在 C++ 中。具体来说: 假设您的外部 vector 已满,您想要开始将元素插入到第一个内部
我是一名优秀的程序员,十分优秀!