dimanche 19 mars 2017

C++ Yahtzee program - Scoring functions

I'm pretty new to programming in general as this is an introductory class, so please forgive my inexperience.

I have made a program that rolls 5 dice and allows the user to select which ones to hold, then rerolls, up to 2 times. After these rerolls, the program shows 5 dice that need to be scored.

For example, if the final 5 dice were [2, 2, 3, 5, 2], how would you score these dice according to certain functions. One of these functions would be 'scoreTwos', which would total all of the twos and input it into a variable that can be totalled with the rest of the scores later.

I have looked at many examples of similar programs, but none of them use the same sort of methods that my instructor wants us to use, particularly the use of vectors to store data.

I'm sorry for any confusion, as I said, I am a beginner, so I appreciate the help.

Here is what I have so far:

#include <iostream>
#include <vector>
#include "mssm.h"

using namespace std;


vector<int> rollDice(int numDice) {
    vector <int> rolledDice;
    for (int i = 0; i < numDice; i++) {
        rolledDice.push_back(randomInt(1,6));
    }
    cout<<"First roll:"<<endl;
    return rolledDice;
}
vector <int> selectReroll(){
    string values;
    int input;
    vector <int> diceToReroll;
    cout<<endl<<"Enter dice to reroll:"<<endl;
    getline(cin, values);
    istringstream stream(values);
    while(stream >> input){
        diceToReroll.push_back(input);
    }
    return diceToReroll;
}
vector<int> reRollDice(vector<int> dice, vector<int> diceToReroll) {
    for(int x=0; x<diceToReroll.size(); x++){
        dice[diceToReroll[x]-1]=randomInt(1,6);
    }
    return dice;
}

void printDice(vector<int> dice)
{
    printVector(dice); //Actual dice creation below//
}

void dicePics(int numDots){
    switch (numDots){
    case 1:
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        cout<<"|"<<"         "<<"|   \n";
        cout<<"|"<<"    *    "<<"|   \n";
        cout<<"|"<<"         "<<"|   \n";
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        break;
    case 2:
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        cout<<"|"<<"       * "<<"|   \n";
        cout<<"|"<<"         "<<"|   \n";
        cout<<"|"<<" *       "<<"|   \n";
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        break;
    case 3:
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        cout<<"|"<<"       * "<<"|   \n";
        cout<<"|"<<"    *    "<<"|   \n";
        cout<<"|"<<" *       "<<"|   \n";
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        break;
    case 4:
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        cout<<"|"<<" *     * "<<"|   \n";
        cout<<"|"<<"         "<<"|   \n";
        cout<<"|"<<" *     * "<<"|   \n";
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        break;
    case 5:
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        cout<<"|"<<" *     * "<<"|   \n";
        cout<<"|"<<"    *    "<<"|   \n";
        cout<<"|"<<" *     * "<<"|   \n";
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        break;
    case 6:
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        cout<<"|"<<" *     * "<<"|   \n";
        cout<<"|"<<" *     * "<<"|   \n";
        cout<<"|"<<" *     * "<<"|   \n";
        for(int x=0; x<11; x++){
            cout<<"-";
        }
        cout<<endl;
        break;
    }
}
void drawDice(vector <int> dice){
    for(int x=0; x<dice.size();x++){
        int num=dice[x];
        switch (num){
        case 1:
            dicePics(1);
            break;

        case 2:
            dicePics(2);
            break;

        case 3:
            dicePics(3);
            break;

        case 4:
            dicePics(4);
            break;

        case 5:
            dicePics(5);
            break;

        case 6:
            dicePics(6);
            break;
        }
    }
}

int main() {

    cout<<"WELCOME TO YAHTZEE" << endl;
    cout<< endl;
    cout<<"Game Rules:" << endl;
    cout<< endl;
    cout<<"Game starts with the initial roll" << endl;
    cout<<"You are allowed up to two rerolls to attempt to get the best possible outcome" << endl;
    cout<<"To select dice to reroll, enter the corresponding number of the dice, with each number seperated by a space (i.e. '1 2 3 4 5')" << endl;
    cout<<"To keep the current dice shown without rerolling, hit enter without inputting any numbers until both rerolls are used." << endl;
    cout<<"At the end of each turn, you may choose how you would like to score the dice (each method may only be used once, and if you cannot use a method, you must choose one to score as 0."<< endl;
    cout<<"Scoring Methods are as follows:" << endl;
    cout<< endl;
    cout<<"UPPER SCORES: Total only the specified die face."<< endl;
    cout<<"Ones: Total and score all ones"<< endl;
    cout<<"Twos: Total and score all twos"<< endl;
    cout<<"Threes: Total and score all threes"<< endl;
    cout<<"Fours: Total and score all fours"<< endl;
    cout<<"Fives: Total and score all fives"<< endl;
    cout<<"Sixes: Total and score all sixes"<< endl;
    cout<<"Upper Bonus: If total score of upper section >= 63, score 35"<< endl;
    cout<< endl;
    cout<<"LOWER SCORES: Scored only when certain conditions are met"<< endl;
    cout<<"3oak: Total all dice"<< endl;
    cout<<"4oak a kind: Total all dice"<< endl;
    cout<<"FullHouse: Score 25"<< endl;
    cout<<"SmStraight: Score 30"<< endl;
    cout<<"LgStraight: Score 40"<< endl;
    cout<<"Yahtzee: Score 50"<< endl;
    cout<<"Chance: Total and score all dice"<< endl;
    cout<<"YahtzeeBonus: (Score 100 for each additional Yahtzee after the first"<< endl;
    cout<< endl;
    cout<<"To score your dice after each turn input the name of the method you would like to score, for example: 'Ones' or '4oak'" << endl;
    cout<<"Once all scores have been filled, the game will end and you will be presented with your final score."
    cout<< endl;
    cout<< endl;
    cout<< endl;


    vector<int> dice = rollDice(5);
    printVector(dice);
    drawDice(dice);

    vector <int> throwaway=selectReroll();
    dice=reRollDice(dice, throwaway);
    printVector(dice);
    drawDice(dice);

    throwaway=selectReroll();
    dice=reRollDice(dice, throwaway);
    printVector(dice);
    drawDice(dice);

    return 0;
}

Also here is another source file that some of that the above relies on:

#include <iostream>

#include "mssm.h"

using namespace std;



randomGenerator::randomGenerator(int seed) : mersenneTwister(randDevice())
{
   if (seed)
   {
       mersenneTwister.seed(seed); // fixed seed to get repeatable pseudorandom values for testing
   }
   else
   {
       mersenneTwister.seed((std::chrono::system_clock::now().time_since_epoch()).count());
   }
}

int randomGenerator::getInt(int minVal, int maxVal)
{
   std::uniform_int_distribution<int> dist(minVal, maxVal);
   return dist(mersenneTwister);
}

double randomGenerator::getDouble(double minVal, double maxVal)
{
   std::uniform_real_distribution<double> dist(minVal, maxVal);
   return dist(mersenneTwister);
}

randomGenerator rnd;

int randomInt(int minVal, int maxVal)
{
    return rnd.getInt(minVal, maxVal);
}

double randomDouble(double minVal, double maxVal)
{
    return rnd.getDouble(minVal, maxVal);
}

Aucun commentaire:

Enregistrer un commentaire