游戏规则如下:写一个程序,允许一定数量的玩家做一定数量的掷骰子。让程序计算每个玩家投掷的总和并打印出胜利者(总和最大的一个)。如果两个或两个以上的玩家拥有相同的金额,那么赢家就是那个赢得6个奖金的人。
这是我到目前为止所得到的:
#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;
}
我坚持到了这一点,我需要打印出一个投掷的数字,总结每个玩家的投掷并扫描投掷更多6个投注者。
答案 0 :(得分:2)
以下是使用std::vector
以及库count
和accumulate
算法的实现。
基本上这里发生的是,我制作一个名为playerThrows的向量矢量,其中包含每个玩家的投掷向量。这样你就可以跟踪每个玩家的每一次投掷。然后,当你遍历numberOfPlayers
时,我在playerThrows
中设置一个新的向量,并且对于每次投掷,我将一个值推回到该投掷向量。
然后,在完成所有投掷之后。我通过调用accumulte
算法找到所有抛出的总和。并使用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;
}
您可能还需要考虑查看<random>
标头以获得更好的随机数生成,并<chrono>
进行计时。
根据评论的要求。这是一个只使用C ++标题的版本:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <random>
int main() {
// Using <random> header
std::random_device seeder {};
std::mt19937 generator(seeder());
std::uniform_int_distribution<int> diceGenerator(1, 6);
int numberOfPlayers;
int numberOfThrows;
std::vector<std::vector<int>> playerThrows;
std::cout << "Enter the amount of players: ";
std::cin >> numberOfPlayers;
std::cout << "How many throws does each one have: ";
std::cin >> numberOfThrows;
std::cout << std::endl;
// Do throws for each player
for (int i = 0; i != numberOfPlayers; ++i) {
playerThrows.emplace_back(std::vector<int>{}); // Make vector for player
for (int j = 0; j < numberOfThrows; ++j)
playerThrows[i].push_back(diceGenerator(generator));
// Find sum
auto sum = std::accumulate(playerThrows[i].begin(), playerThrows[i].end(), 0);
// Find 6's
auto sixes = std::count(playerThrows[i].begin(), playerThrows[i].end(), 6);
std::cout << "Sum of player " << i + 1 << " is: " << sum << '\n';
std::cout << "They had " << sixes << " 6's" << "\n\n";
}
return 0;
}
答案 1 :(得分:0)
满足您需求的一种方法是为玩家创建一个结构,可以存储他的投掷总数以及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人。