jeudi 24 octobre 2019

C++ Cant sum up dice in Yahtzee game

my program as in the title is a Yahtzee game and i have trouble with summing up my dice sums to the table content. If you have any ideas on how i can sum up the dice sums automatically, that would be lovely.

The program is in 3 different cpp program and one header in total.

the program works without what i am asking.

DiceRoll.cpp //getting the dice rolls

#include <iostream>
#include <time.h>
#include "header.h"

using namespace std;


int getRandomDieRoll(){
    int randNumber=0;
    randNumber=rand()%6+1;
    return randNumber;
}


int Dice[5];
string keepers;


void diceRoll(){
    srand(time(NULL));
    cout<<"First roll: "<<endl;
    for(int i=0;i<5;i++){
        Dice[i]=getRandomDieRoll();
    }
    cout<<Dice[0]<<" "<<Dice[1]<<" "<<Dice[2]<<" "<<Dice[3]<<" "<<Dice[4]<<endl<<endl;
    cout<<"Do you like to reroll?\n"
          "1- Keep dice\n"
          "0- Reroll\n"
          "Exsample: 01100 is when you roll dice 1, 4, 5"<<endl<<endl;
    while(true){//Asking if you want to hold your dice or notx3
        cin>>keepers;
        bool go=false;
        for(size_t i=0; i<keepers.size();i++){
            if(keepers[i]<'0'||keepers[i]>'1'){
                go=true;
            }
        }
    break;
    }

    cout<<"Your secound roll: "<<endl;
    for(int i=0;i<5;i++){
        if (keepers[i]!='1'){
            Dice[i]=getRandomDieRoll();
        }
    }
    cout<<Dice[0]<<" "<<Dice[1]<<" "<<Dice[2]<<" "<<Dice[3]<<" "<<Dice[4]<<endl<<endl;
    cout<<"Do you like to reroll?\n"
                    "1- Keep dice\n"
                    "0- Reroll"<<endl<<endl;

    //3rd roll
    while(true){
        cin>>keepers;
        bool go=false;
        for(size_t i=0; i<keepers.size();i++){
            if(keepers[i]<'0'||keepers[i]>'1'){
                go=true;
            }
        }
    break;
    }


    cout<<"Your final rolls are: "<<endl;
    for(int i=0;i<5;i++){
        if (keepers[i]!='1'){
            Dice[i]=getRandomDieRoll();
        }
    }
    cout<<Dice[0]<<" "<<Dice[1]<<" "<<Dice[2]<<" "<<Dice[3]<<" "<<Dice[4]<<endl<<endl;
}

table.cpp

//getting the main table and showing it.
#include <iostream>
#include "header.h"
#include <stdio.h>
#include <string>


using namespace std;

int players=0;

int once=0;
int two=0;
int threes=0;
int fours=0;
int fives=0;
int sixes=0;

int threeOfaKind=0;
int fourOfaKind=0;
int fullHouse=0;
int smallStraight=0;
int bigStraight=0;
int yahtzee=0;
int chance=0;
int bonus=0;

int upperScore;
int lowerScore;
int score;

bool catUsed[13];//array for double checking if one of the tables has allready been used


void scorenote(){
    int option;
    diceRoll();
    cout<<"Choose witch one you want (0-12): ";cin>>option;
    for(int i=0;i<13;i++){//making a for loop instead of using catUsed[0]=false...
        catUsed[i]=false;
        }
    if(catUsed[option]==true){
        cout<<"Pick another category"<<endl;
        }
    else {
        switch (option) {
//starting the switch case.
//Every switch case is that you have to do the math youself, problem is that I want it to `be automatic`
        case 0:
            cout<<"Enter ones: ";cin>>once;
            catUsed[option]=true;
            break;
        case 1:
            cout<<"Enter twos: ";cin>>two;
            catUsed[option]=true;
            break;
        case 2:
            cout<<"Enter threes: ";cin>>threes;
            catUsed[option]=true;
            break;
        case 3:
            cout<<"Enter fours: ";cin>>fours;
            catUsed[option]=true;
            break;
        case 4:
            cout<<"Enter fives: ";cin>>fives;
            catUsed[option]=true;
            break;
        case 5:
            cout<<"Enter sixes: ";cin>>sixes;
            catUsed[option]=true;
            break;
        case 6:
            cout<<"Enter Three of A Kind: ";cin>>threeOfaKind;
            catUsed[option]=true;
            break;
        case 7:
            cout<<"Enter Four of A Kind: ";cin>>fourOfaKind;
            catUsed[option]=true;
            break;
        case 8:
            cout<<"Enter Full House: ";cin>>fullHouse;
            catUsed[option]=true;
            break;
        case 9:
            cout<<"Enter Small Straight: ";cin>>smallStraight;
            catUsed[option]=true;
            break;
        case 10:
            cout<<"Enter Big Straight: ";cin>>bigStraight;
            catUsed[option]=true;
            break;
        case 11:
            cout<<"Enter Yahtzee: ";cin>>yahtzee;
            catUsed[option]=true;
            break;
        case 12:
            cout<<"Enter Chance: ";cin>>chance;
            catUsed[option]=true;
            break;
        default:
            cout<<"Wrong input, please select from 0-12: ";cin>>option;
            break;
        }
    }
    system("cls");
}

void table(){
    upperScore=once+two+threes+fours+fives+sixes;
        if(upperScore>=63){
            bonus=35;
            }
        else
            bonus=0;

lowerScore=threeOfaKind+fourOfaKind+fullHouse+smallStraight+bigStraight+yahtzee+chance;
score=upperScore+lowerScore+bonus;
    cout<<"0- Once: "<<once<<endl
        <<"1- Twos: "<<two<<endl
        <<"2- Threes: "<<threes<<endl
        <<"3- Fours: "<<fours<<endl
        <<"4- Fives: "<<fives<<endl
        <<"5- Sixes: "<<sixes<<endl
        <<"Upper Score: "<<upperScore<<endl
        <<"Bonus: "<<bonus<<endl
        <<"6- Three of a Kind: "<<threeOfaKind<<endl
        <<"7- Four of a Kind: "<<fourOfaKind<<endl
        <<"8- Full House: "<<fullHouse<<endl
        <<"9- Small Straight: "<<smallStraight<<endl
        <<"10- Big Straight: "<<bigStraight<<endl
        <<"11- YAHTZEE: "<<yahtzee<<endl
        <<"12- Chance: "<<chance<<endl
        <<"Lower Score: "<<lowerScore<<endl
        <<"Total score: "<<score<<endl<<endl;
    scorenote();
}

main.cpp

#include <iostream>
#include "header.h"

using namespace std;

int maxRound=0;




int main(){
    do{
        table();
        maxRound+=1;
    }while(maxRound<13);
    return 0;
}

header.h

#ifndef HEADER_H
#define HEADER_H
void table();
void scorenote();
void diceRoll();
#endif // HEADER_H

Aucun commentaire:

Enregistrer un commentaire