gpt4 book ai didi

c++ - 使用 push_back 在类中填充结构的值

转载 作者:行者123 更新时间:2023-11-30 03:43:40 25 4
gpt4 key购买 nike

您好,我正在做一个编程 II 项目。对于我在这里犯的任何错误,我提前道歉(这是我的第一篇文章)。我正在开发一个程序来模仿游戏“Farkle”。老师给我们提供了一个框架,我一直卡在实现roll功能上。我的目标是使用 rand 函数随机生成一个介于 1 - 6 之间的数字,并将该值放在 vector RolledDice 中(我不需要 rand 函数的帮助)我只需要帮助将 RolledDice 的值设置为等于什么rand 函数产生。

问题是Game.cpp中的Roll()函数

  • 部分问题是试图将 rolledDice 设置为 int 值。
  • 另一个问题是访问 public 类中的结构。
  • 如何使用 push_back 函数将这些值填充到 vector 中?

在我的 Game.cpp 中我有:

Game::Die::Die ()
{
// This function will: Initalize the value of the dice

pips = 0;
selected = false;
used = false;
}

ostream & operator << (ostream & outs, const Game::Die & D)
{
// This function will: output the score
return outs;
}

Game::Game (const string & T)
{
// This function will: initializer the random variables

title = T;
// srand (0);
srand (time (NULL));

}

Game::~Game ()
{


}

void Game::Init ()
{

gameState = GO;
whoseTurn = NONE;
humanOnBoard = false;
aiaOnBoard = false;
humanScore = 0;
aiaScore = 0;
lastTurn = false;
}

void Game::Instructions (ostream & outs)
{


outs << endl << "Welcome to " << title << endl;
outs << "The goal of this game is to accumulate 5000"
<< " points before your opponent." << endl;
outs << "In this case, this program is your opponent." << endl;
outs << "You must score 500 points in a single turn to"
<< " start accumulating points." << endl << endl;
sleep (1);

}

void Game::StartTurn ()
{


if (whoseTurn == NONE || whoseTurn == AIA)
{
whoseTurn = HUMAN;
Roll();
}
else
{
whoseTurn = AIA;
turnDone = false;
numToRoll = MAX_DICE;
savedDice.clear ();
subTotal = 0;
turnTotal = 0;
Roll();
}
}



bool Game::Enter (char selection, ostream & outs)
{
// This function will:

switch (selection)
{
case 'i': Instructions (outs);
break;
case 'r':
SaveSelected();
Roll ();
break;
case 's':
SaveSelected();
SaveScore();
break;
case 'q':
gameState = QUITTER;
turnDone = true;
break;
default:
{

int which = selection - 'a';
if (which < 0 || which >= rolledDice.size())
return false;
rolledDice[which].selected = !rolledDice[which].selected;
}
}
return true;
}

void Game::Roll () // Roll function
{
rolledDice.push_back(Game::Die.pips); // **This is my issue**
}

我试过:

void Game::Roll () // Roll function
{
int numRolled = rand()%6 + 1;
rolledDice.push_back(numRolled) // **This is my issue**
}

还有:

void Game::Roll () // Roll function
{
int numRolled = rand()%6 + 1;
rolledDice.pips.push_back(numRolled) // **This is my issue**
}

这不起作用,因为 rolledDice 无法转换为 int

还有很多其他的东西我不可能放在这篇文章中,我只是认为这些是最接近说明我正在尝试做的事情的东西。

在我的 Game.h 中我有:

class Game
{
public:
Game (const string & T);
~Game ();
void Init ();
void Instructions (ostream & outs);
void StartTurn ();
friend ostream & operator << (ostream & outs, const Game & G);
bool Enter (char selection, ostream & outs);
bool TurnDone ();
int AIAPlayer (ostream & outs);
bool Done ();
void Message (ostream & outs);

struct Die
{
Die ();
friend ostream & operator << (ostream & outs, const Die & D);
int pips;
bool selected;
bool used;
};

private:
void Roll ();
int CalculateScore (vector <Die> & dice);
void SaveSelected ();
void SaveScore ();

string title;
vector <Die> rolledDice; // Dice Rolled
vector <Die> remainingDice; // Will take out dice saved
vector <Die> selectedDice; // Will temporarily save the selected dice
vector <Die> savedDice; // Will save the dice
state_types gameState;
player_type whoseTurn;
bool turnDone;
int numToRoll;
int rolledScore;
int subTotal;
int turnTotal;
bool humanOnBoard;
bool aiaOnBoard;
int humanScore;
int aiaScore;
bool lastTurn;
};

如果您需要发布更多信息就可以了。非常感谢任何帮助。

最佳答案

通过创建构造函数

Die::Die(int pips)

您正在创建一个将用作隐式类型转换的构造函数。这极大地简化了添加到 vector 的语法。要没有这个,你需要关键字 explicit。它有时会在您的程序中产生不直观的语义。

一个简单的概念证明:

#include <iostream>
#include <vector>
struct Die{
Die(int pips):pips(pips){}

int pips;
};

std::vector<Die> tosses;

int main(void)
{
tosses.push_back(2); // Works because of the converting constructor.
tosses.push_back(2);
tosses.push_back(1);
tosses.push_back(2);

for(auto toss:tosses){
std::cout << toss.pips;
}

}

同时考虑 http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution用于优雅和正确的掷骰子。我见过的大多数反对 std::rand 的论点都包含掷骰子。

关于c++ - 使用 push_back 在类中填充结构的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983285/

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