Member 14005371 Ответов: 1

Код с вероятностью на языке C++


I am fairly new to coding with C++ and was wondering if anyone could assist in pointing me in the right direction to finishing a project. I am trying to create a program that distributes "gear" to the user by using probability. My program will have a pool of 12 possibilities to choose 4 categories of items. For example, I will have 2 daggers, 3 helmets, 3 shields and 4 armor. At this point I am completely confused and I have tried researching for information that I may be able to use, but I am not sure which concepts of C++ will be required for this project. Any help will be appreciated! I do not wish for the project to be completed in anyway by anyone and I am just looking for help to guide me in the right direction to find topics I need to learn in order to complete this project. Thanks a lot everyone!

Исходная задача:

Вы будете реализовывать программу, которая раздает снаряжение игрокам определенной игры. Вы можете тему вашей игры, как вы хотите.Ваша программа будет использовать вероятности, чтобы решить, какое снаряжение раздавать игроку. Снаряжение включает в себя кинжал, шлем, щит и доспехи.

ваша программа:

All of the gear that will be handed out is represented by a token in a bag. To begin, there are 2 daggers tokens, 3 helmet tokens, 3 shield tokens, and 4 armor tokens in the Token Bag.The program will choose a token from the bag and give that gear to the player. To simulate which token was chosen, you will compute the probability of choosing each piece of gear and hand out the gear with the highest probability of being chosen. If there is a tie, the user gets both (or all)of the gear with the same probability.You should display the probabilities of each kind of gear in a table before handing out the gear.See the sample output for how to format the probabilities.Remember, you cannot directly compare doubles. You have to compare using abs(x-y) < EPSILONwhere EPSILON is some very small number, like 1e-14.

Это пример снимка выходного экрана:
http://prntscr.com/l2qg5y[^]

Что я уже пробовал:

Я знаю, что мой код сейчас очень хромает, но любая помощь будет очень признательна!

// CSa262_Lab2.cpp : This file contains the 'main' function. Program 
execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
cout << "------------------------------------------------------------------- 
-------------" << endl;
cout << "Earning Gear Program" << endl;
cout << "------------------------------------------------------------------- 
-------------" << endl;
cout << "This program will simulate probability of earning different pieces 
of gear." << endl;
cout << "------------------------------------------------------------------- 
-------------" << endl;

int numArmTokens = 3;
int numHelmTokens = 3;
int numShieldTokens = 3;
int numDaggTokens = 2;
int totalTokens = (numArmTokens + numHelmTokens + numShieldTokens + 
numDaggTokens);
char ans;
int x = 1;

cout << "Your possabilites for gear are: " << endl;
do
{
    cout << "P(Armor) = " << numArmTokens << "/" << totalTokens << endl;
    cout << "P(Dagger) = " << numDaggTokens << "/" << totalTokens << endl;
    cout << "P(Helmet) = " << numHelmTokens << "/" << totalTokens << endl;
    cout << "P(Shield) = " << numShieldTokens << "/" << totalTokens << endl;


    if (numArmTokens >= numDaggTokens || numArmTokens >= numHelmTokens ||
        numArmTokens >= numShieldTokens)
    {

        cout << "You have a choice of: " << endl << ++x << ". Armor" << 
endl;
        cin >> x;
        numArmTokens--;
        totalTokens--;


    }


    if (numDaggTokens >= numArmTokens || numDaggTokens >= numHelmTokens ||
        numDaggTokens >= numShieldTokens)
    {

        cout << "You have a choice of: " << endl << "Dagger" << endl;
        cin >> x;

        numDaggTokens--;
        totalTokens--;
    }

    if (x > 4)
    {
        cout << "Please enter a valid option." << endl;
    }

    cout << "Would you like to select more gear? (Y/N)" << endl;
    cin >> ans;
} while (ans == 'y' || ans =='Y');



system("pause");
return 0;
}

OriginalGriff

И что же?
В чем проблема?
Где ты застрял?
Какая помощь вам нужна?

1 Ответов

Рейтинг:
0

KarstenK

Вам нужно обеспечить некоторую бизнес-логику для распределения передач. Вы можете использовать рант функция для генерации некоторых случайных данных, которые распределяют вероятность. Ссылка находится с образцом кода, который должен соответствовать вашим потребностям. Следите за целочисленным делением, которое сокращает дроби.

Проблема часто заключается в крайнем случае, поэтому следите за полной логикой. Как и все доспехи должны быть распределены.