C++ 掷骰子问题
游戏规则是这样的:编写一个程序,允许一定数量的玩家掷一定数量的骰子。让程序计算每个玩家的掷骰总数,并打印出获胜者(金额最大的那个)。如果两个或两个以上的玩家有相同的总数,则赢家是投了6个中更多的那个 以下是我到目前为止得到的信息:C++ 掷骰子问题,c++,C++,游戏规则是这样的:编写一个程序,允许一定数量的玩家掷一定数量的骰子。让程序计算每个玩家的掷骰总数,并打印出获胜者(金额最大的那个)。如果两个或两个以上的玩家有相同的总数,则赢家是投了6个中更多的那个 以下是我到目前为止得到的信息: #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; int main(){ srand(time(NULL)); int
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main(){
srand(time(NULL));
int numberOfPlayers;
int numberOfThrows;
int sum = 0;
int throws = 0;
cout << "Enter the amount of players: "; cin >> numberOfPlayers;
cout << "How many throws does each one have?? "; cin >> numberOfThrows;
cout << endl;
for(int i = 1; i <= numberOfPlayers; i++){
for(int j = 0; j < numberOfThrows; j++){
throws = rand()%6+1;
sum = sum + throws;
}
cout << "Sum of the player " << i << " is: " << sum;
cout << endl;
sum = 0;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
srand(时间(空));
国际球员人数;
整数;
整数和=0;
int=0;
cout>玩家数量;
cout>投掷次数;
cout这里是一个使用std::vector
和库count
和acculate
算法的实现
基本上,这里发生的事情是,我创建一个向量,称为playerThrows,它包含每个玩家的投掷向量。这样,你可以跟踪每个玩家的每次投掷。然后,当你循环通过numberOfPlayers
,我在playerThrows
中放置一个新向量,对于每次投掷,我都将一个值向后推到这个向量是抛出的
然后,在完成所有抛出之后。我通过调用Accumute
算法来找到所有抛出的总和。然后使用count
算法来计算6的数量。之后您想做什么取决于您。对于本例,我只是简单地打印了值
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main() {
// You should look into the <random> header rather than srand
srand(time(NULL));
int numberOfPlayers;
int numberOfThrows;
vector<vector<int>> playerThrows;
cout << "Enter the amount of players: "; cin >> numberOfPlayers;
cout << "How many throws does each one have?? "; cin >> numberOfThrows;
cout << endl;
// Do throws for each player
for (int i = 0; i != numberOfPlayers; ++i) {
playerThrows.emplace_back(vector<int>{}); // Make vector for player
for (int j = 0; j < numberOfThrows; ++j)
playerThrows[i].push_back(rand() % 6 + 1);
// Find sum
auto sum = accumulate(playerThrows[i].begin(), playerThrows[i].end(), 0);
// Find 6's
auto sixes = count(playerThrows[i].begin(), playerThrows[i].end(), 6);
cout << "Sum of player " << i + 1 << " is: " << sum << '\n';
cout << "They had " << sixes << " 6's" << "\n\n";
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
//您应该查看标题,而不是srand
srand(时间(空));
国际球员人数;
整数;
矢量播放箭头;
cout>玩家数量;
cout>投掷次数;
cout满足您需求的一种方法是为一名球员创建一个结构,该结构可以存储他的掷骰总数以及6的数量
struct Player {
int sum;
int numSix;
Player():sum(0),numSix(0){}
};
使用此结构,您可以创建大小为numberOfPlayers
的数组
auto players = new Player[numberOfPlayers];
在投掷循环中,向玩家添加投掷:
throws = rand()%6+1;
players[i].sum += throws;
players[i].numSix += throws/6; //either 0 or 1 in case of 6
最后,你可以在玩家数组中循环,检查谁赢了游戏,以及每个玩家有多少个6,以防万一。查看std::vector
以存储所有掷骰。然后你可以计算该向量中的6,并计算向量中所有元素的总和。你可以打印掷骰的值ode>行后throws=rand()%6+1;
显示玩家抛出的数字。您还可以使用计数器跟踪每个玩家的6s。这里不需要手动动态分配,并且当您可以使用库容器时,使用C数组是不安全和不必要的。在本例中,您还必须记住删除[]
玩家之后。在您的结构中,您可能应该在sum
和numSix
上使用类内初始值设定项,将它们默认构造为0。很好,添加了默认构造函数。当然,使用std::vector更容易。但这取决于默认容量和使用std::vecto的玩家和抛出次数r会导致内存的多次重新分配,您无法获得尽可能高的性能。我知道,但它们在原始示例中使用过,因此我将它们保留在那里,以避免完全重写他的代码,而只是添加他缺少的内容。正如我所说的,我会使用
和
而不是C头。更新为一个不使用命名空间+ C++头的版本。谢谢。如果初学者看到尽可能多的IdoC+C++,那是更好的,因为很多课程用C来教C。
throws = rand()%6+1;
players[i].sum += throws;
players[i].numSix += throws/6; //either 0 or 1 in case of 6