got to write a yahtzee program
Feb 10, 2011 at 9:37pm UTC
ok so today in class i was given the instructions to write a program that allows you to play yahtzee. must calculate scores on its own. so far i got
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
// yahtzee.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));
const int DICE_ROLLS = 5;
int firstRoll = rand() % 6 + 1;
int secondRoll = rand() % 6 + 1;
int thirdRoll = rand() % 6 + 1;
int fourthRoll = rand() % 6 + 1;
int fifthRoll = rand() % 6 + 1;
int roll = rand() % 6 + 1;
int allRoles = firstRoll + secondRoll + thirdRoll + fourthRoll + fifthRoll;
int one;
int two;
int three;
int four;
int five;
int six;
int totalTop;
int totalBottom;
int threekind;
int fourkind;
int fullHouse;
int smStraight;
int lgStraight;
int yahtzee;
int chance;
int bonus = 35;
int subTotal;
char rollKey;
char keepAnother = 'y' ;
int keepDie;
int dieKeptCount = 0;
int rollAgain = DICE_ROLLS - dieKeptCount;
/*gotoxy (48,52);
cout << "Ones: \n";*/
cout << "Welcome to Yahtzee!!\n\n" ;
cout << "Press 'R' to roll.\n" ;
cin >> rollKey;
if (rollKey == 'r' ){
cout << "Die 1: \t" <<firstRoll<<"\n" ;
cout << "Die 2: \t" <<secondRoll<<"\n" ;
cout << "Die 3: \t" <<thirdRoll<< "\n" ;
cout << "Die 4: \t" <<fourthRoll<< "\n" ;
cout << "Die 5: \t" <<fifthRoll<< "\n\n" ;
}
do {
cout << "Please enter die number you would like to keep(1-5), or 0 for none : " ;
cin >> keepDie;
dieKeptCount++;
}
while (keepDie != 0);
switch (rollAgain) {
case 0: cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;break ;
case 1: cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;break ;
case 2: cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;break ;
case 3: cout << "You roll : " <<roll<< "\n" ;
cout << "You roll : " <<roll<< "\n" ;break ;
case 4: cout << "You roll : " <<roll<< "\n" ;break ;
}
system("pause" );
return 0;
}
and for some reason when i compile, the switch statement will not initialize :(
any help would be great
Feb 11, 2011 at 8:26am UTC
Many things to improve here, but th one you need is that line 59 happens before the lines 82-87 loop so on line 59 dieKeptCount = 0 and thus rollAgain = 5. There is no 5'th case in your switch.
Topic archived. No new replies allowed.