Hello. I'm stuck with my Yahtzee project.
The first five lines are filled with a brief explanation of the case. Please read them
/* I'm writing Yahtzee in C++. So far so good, but I'm stuck now on the recognition of staights and full houses.
It's about the lines 131 to 144. There's supposed to be a sorting mechanism there. I just can't get it done.
Please don't copy this for your own purpose.
*/
#include <iostream>
#include <stdlib.h>
#include <string>
#define SHOWSCORES_PURPOSE_SHOWSCORES 1
#define SHOWSCORES_PURPOSE_ENTEROPTIONS 2
#define FIELDNAME_1S 1
#define FIELDNAME_2S 2
#define FIELDNAME_3S 3
#define FIELDNAME_4S 4
#define FIELDNAME_5S 5
#define FIELDNAME_6S 6
#define FIELDNAME_BONUS 7
#define FIELDNAME_THREEOFAKIND 8
#define FIELDNAME_FOUROFAKIND 9
#define FIELDNAME_FULLHOUSE 10
#define FIELDNAME_SMALL_STRAIGHT 11
#define FIELDNAME_LARGE_STRAIGHT 12
#define FIELDNAME_YAHTZEE 13
#define FIELDNAME_CHANCE 14
using namespace std;
int FieldScore(int Field, int nPlayer);
void ShowScoresUpperSection(int nPlayer, int Purpose);
void ShowScoresLowerSection(int nPlayer, int Purpose);
void ShowDice();
void RollDice(int DiceToRoll);
void EnterScore();
int Die[5];
int ScoreField[14][10]; // For max 10 player
const string FieldName[20][14] = {
"1's ",
"2's ",
"3's ",
"4's ",
"5's ",
"6's ",
"Bonus ",
"Three-of-a-kind ",
"Four-of-a-kind ",
"Full House ",
"Small straight ",
"Large straight ",
"Yahtzee ",
"Chance "
};
int main()
{
int nNoPlayers;
cout << "Enter number of players" << endl;
cin >> nNoPlayers;
bool bGameOver = false;
while( !bGameOver ) {
int TimesDiceRolled = 0;
for( int n = 0; n < nNoPlayers; n++ ) {
cout << "Player " << n << "'s turn" << endl;
cout << "Pick an option: " << endl
<< "1) Show upper scorefields" << endl
<< "2) Show lower scorefields" << endl
<< "3) Show dice" << endl
<< "4) (Re)roll dice" << endl
<< "5) Enter score" << endl;
char cOption;
cin >> cOption;
if( cOption == '1' ) {
ShowScoresUpperSection(n, SHOWSCORES_PURPOSE_SHOWSCORES);
}
if( cOption == '2' ) {
ShowScoresLowerSection(n, SHOWSCORES_PURPOSE_SHOWSCORES);
}
if( cOption == '3' ) {
ShowDice();
}
if( cOption == '4' ) {
int DiceToRoll = 0;
cout << "Enter dice to roll in this way: 124 rolling dice 1, 2 and 4" << endl;
cin >> DiceToRoll;
RollDice(DiceToRoll);
TimesDiceRolled++;
}
if( cOption == '5' ) {
ShowScoresUpperSection(n, SHOWSCORES_PURPOSE_ENTEROPTIONS);
ShowScoresLowerSection(n, SHOWSCORES_PURPOSE_ENTEROPTIONS);
}
if( TimesDiceRolled >= 3 ) {
break;
}
}
cout << "Play another game (Y/N)?" << endl;
char cAnotherGameYN;
cin >> cAnotherGameYN;
if( cAnotherGameYN == 'N' || cAnotherGameYN == 'n' ) {
bGameOver = true;
}
}
cout << "Game over";
}
int FieldScore(int nField, int nPlayer) {
int Score = 0;
struct dieset {
int value;
int location;
}
dieset sortedDie[5];
for( int val = 1; val <= 6; val++ ) {
for( d = 0; d < 5; d++ ) {
if( Die[d] > val ) {
sortedDie[d].value = val;
}
}
}
switch(nField) {
case FIELDNAME_1S:
Score = 0;
for(int n; n < 5; n++) {
if( Die[n] == 1 ) {
Score += 1;
}
}
break;
case FIELDNAME_2S:
Score = 0;
for(int n; n < 5; n++) {
if( Die[n] == 2 ) {
Score += 2;
}
}
break;
case FIELDNAME_3S:
Score = 0;
for(int n; n < 5; n++) {
if( Die[n] == 3 ) {
Score += 3;
}
}
break;
case FIELDNAME_4S:
Score = 0;
for(int n; n < 5; n++) {
if( Die[n] == 4 ) {
Score += 4;
}
}
break;
case FIELDNAME_5S:
Score = 0;
for(int n; n < 5; n++) {
if( Die[n] == 5 ) {
Score += 5;
}
}
break;
case FIELDNAME_6S:
Score = 0;
for(int n; n < 5; n++) {
if( Die[n] == 6 ) {
Score += 6;
}
}
break;
case FIELDNAME_BONUS:
if( ScoreField[0][nPlayer] > 0 &&
ScoreField[1][nPlayer] > 0 &&
ScoreField[2][nPlayer] > 0 &&
ScoreField[3][nPlayer] > 0 &&
ScoreField[4][nPlayer] > 0 &&
ScoreField[5][nPlayer] > 0 &&
ScoreField[0][nPlayer] +
ScoreField[1][nPlayer] +
ScoreField[2][nPlayer] +
ScoreField[3][nPlayer] +
ScoreField[4][nPlayer] +
ScoreField[5][nPlayer]
>= 63 ) {
Score = 35;
}
break;
case FIELDNAME_THREEOFAKIND:
if( Die[0] == Die[1] && Die[0] == Die[2] ||
Die[0] == Die[1] && Die[0] == Die[3] ||
Die[0] == Die[1] && Die[0] == Die[4] ||
Die[1] == Die[2] && Die[1] == Die[3] ||
Die[1] == Die[2] && Die[1] == Die[4] ||
Die[2] == Die[3] && Die[2] == Die[4] ) {}
Score = Die[0] + Die[1] + Die[2] + Die[3] + Die[4];
}
break;
}
return Score;
}
// const char FieldName[15][14] = {
void ShowScoresUpperSection(int nPlayer, int Purpose) {
if(Purpose == SHOWSCORES_PURPOSE_SHOWSCORES) {
for( int n = 0; n < 7; n++ ) {
cout << FieldName[0][n] << ScoreField[n][nPlayer] << endl;
}
cout << endl;
}
else if(Purpose == SHOWSCORES_PURPOSE_ENTEROPTIONS) {
for( int n = 0; n < 7; n++ ) {
cout << FieldName[0][n] << ScoreField[n][nPlayer] << endl;
}
cout << endl;
}
return;
}
void ShowScoresLowerSection(int nPlayer, int Purpose) {
if( Purpose == SHOWSCORES_PURPOSE_SHOWSCORES ) {
for( int n = 7; n < 13; n++ ) {
cout << FieldName[0][n] << ScoreField[n][nPlayer] << endl;
}
cout << endl;
}
else if(Purpose == SHOWSCORES_PURPOSE_ENTEROPTIONS) {
for( int n = 7; n < 13; n++ ) {
cout << FieldName[0][n] << ScoreField[n][nPlayer] << endl;
}
cout << endl;
}
return;
}
void ShowDice() {
cout << Die[0] << " " << Die[1]<< " " << Die[2] << " " << Die[3] << " " << Die[5] << endl;
return;
}
void RollDice(int DiceToRoll) {
cout << rand() << endl;
}
void EnterScore() {
}