Skip to content

Instantly share code, notes, and snippets.

@jwatki
Last active May 2, 2017 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwatki/bfe80d125d54d4111b765edc5f96dc49 to your computer and use it in GitHub Desktop.
Save jwatki/bfe80d125d54d4111b765edc5f96dc49 to your computer and use it in GitHub Desktop.
a simple game of dice
/*
Program: dice game
Author: josh watkins
Lab section: 09
Date: 11/23/13
Purpose: the program is suppose to play a dice game.
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
double playerOneScore();
double playerTwoScore();
int main()
{
double sum1=0;
double sum2=0;
string player1Name;
string player2Name;
char player ='!';
//seeds random number generator
srand(time(0));
//allows the players to enter their names
cout << "player 1 enter name" << endl;
cin>>player1Name;
cout<<"player 2 enter name"<<endl;
cin>>player2Name;
//a while loop to go through until player 1 or player 2 reaches the right score
while (sum1<10000||sum2<10000)
{
cout<<player1Name<<" roll"<<endl;
//for the saving of score each roll to be added on
double test1=playerOneScore();
sum1+=test1;
//GETS PLAYER ONES INPUT TO PASS TO PLAYER 2
cout<< "press ! to pass to player 2"<<endl;
cin>>player;
//THE INPUT NEEDED TO SWITCH PLAYERS
while (player !='!'){
cout<<"try again"<<endl;
cin>>player;
}
if (player=='!')
{
//PLAYER2 ROLLS FOR FIRST TIME and saves the score results as sum2, adding to it each time
cout<<player2Name<<" roll"<<endl;
double test2= playerTwoScore();
sum2+=test2;
}
}
//2 if statments to determine who wins
{if (sum2>=10000){
cout<<player2Name<< "wins";}
else if (sum1>=10000 ){
cout<<player1Name<<" wins";}}
return 0;
}
// the function to hold and score player 1 for their rolls
//set to hold what each amount of dice stands for
//and sends it back to the main function to be added each time
//
double playerOneScore()
{
//GENERATES AMOUNT OF DICE ARRAY AND SETS SCORE TO 0
int dice[6];
int score=0;
int amountRolled;
int total1=0;
int total2=0;
int total3=0;
int total4=0;
int total5=0;
int total6=0;
for (int i=0;i<6;i++)
//SCOREING FOR THE DICE GIVE ALL THE DIFFERENT SCORES AND WAYS TO SCORE
{
dice[i]=rand()%6+1;
if(dice[i]==1) score+=100;
if(dice[i]==1) total1++;
if(dice[i]==5) score+=50;
if(dice[i]==5) total5++;
if(dice[i]==2) total2++;
if(dice[i]==3) total3++;
if(dice[i]==4) total4++;
if(dice[i]==6) total6++;
}
//FOR IF THE PLAYER RECEIVED A SET OF 3 OF THE SAME NUMBER (FOR 1 AND 5 I REDUCED
//LISTED SCORE SO THAT IT COULD COUNT THE 1 AND 5 BY THEIR SELVES THEN ADD TO WHAT THEY GET FOR 3 OF A KIND TO EQUAL RIGHT AMOUNT
if (total1>=3) score+=700;
if (total5>=3 ) score+=350;
if (total2>=3) score+=200;
if (total3>=3) score+=300;
if (total4>=3) score+=400;
if (total6>=3) score+=600;
cout<<dice[0]<<" "<<dice[1]<<" "<<dice[2]<<" "<<dice[3]<<" "<<dice[4]<<" "<<dice[5]<<endl;
//if (dice[0]=='1'||dice[1]=='1'||dice[2]=='1'||dice[3]=='1'){
// score+=100;}
cout<<score<<endl;
// I couldn't quite work out the setting aside of dice, I know I need to subtract from the for loop each time, just couldn't get it to work right
//cout<<"roll again set aside how many dice"<<endl;
//cin>>setAside;
//i-setAside;
}
//else
//cout<<score;
//while (choice=='y')
return score;
}}
return score;
}
//PLAYER 2 IS SAME AS PLAYER 1
double playerTwoScore()
{
int dice[6];
int score=0;
int amountRolled;
int total1=0;
int total2=0;
int total3=0;
int total4=0;
int total5=0;
int total6=0;
for (int i=0;i<6;i++)
{
dice[i]=rand()%6+1;
if(dice[i]==1) score+=100;
if(dice[i]==1) total1++;
if(dice[i]==5) score+=50;
if(dice[i]==5) total5++;
if(dice[i]==2) total2++;
if(dice[i]==3) total3++;
if(dice[i]==4) total4++;
if(dice[i]==6) total6++;
}
if (total1>=3) score+=700;
if (total5>=3 ) score+=350;
if (total2>=3) score+=200;
if (total3>=3) score+=300;
if (total4>=3) score+=400;
if (total6>=3) score+=600;
cout<<dice[0]<<" "<<dice[1]<<" "<<dice[2]<<" "<<dice[3]<<" "<<dice[4]<<" "<<dice[5]<<endl;
//if (dice[0]=='1'||dice[1]=='1'||dice[2]=='1'||dice[3]=='1'){
// score+=100;}
cout<<score<<endl;
return score;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment