Yahtzee遊戲 By C++11規範
代碼:
C++11:
#include<iostream>
//standard namespace
using namespace std;
//Struct of Menu
struct MenuOptionNumberAndContent
{
int Item_Number;
char Item_Content[100];
};
//Menu
struct MenuOptionNumberAndContent Option[13] = {
{ 1, ". Aces" },
{ 2, ". Twos" },
{ 3, ". Threes" },
{ 4, ". Fours" },
{ 5, ". Fives" },
{ 6, ". Sixes" },
{ 7, ". 3 of a Kind" },
{ 8, ". 4 of a Kind" },
{ 9, ". Full Houses" },
{ 10,". Small Straight" },
{ 11,". Large Straight" },
{ 12,". Yahtzee" },
{ 13,". Chance" }
};
//declaration of function named SwapSortNumber
void SwapSortNumber (int a, int b);
int CalculateDicesPlusOne (int arrayIndex[6]);
int CalculateDicesPlusTwo (int arrayIndex[6]);
int CalculateDicesPlusThree(int arrayIndex[6]);
int CalculateDicesPlusFour (int arrayIndex[6]);
int CalculateDicesPlusFive (int arrayIndex[6]);
int CalculateDicesPlusSix (int arrayIndex[6]);
int BubbleSortInGame (int arrayIndex[6]);
int CalculateScoreByDices (int arrayIndex[6]);
int SameNumerOfThree (int a, int b, int c, int d, int e, int f);
int SameNumerOfFive (int a, int b, int c, int d, int e, int f);
//the variable
int array_dices[6]; //array of dices
int random_seed = 0; //random seed
int total_Score = 0; /*the mark calculation*/
//calculator of number
int numofOne_dices = 0;
int numofTwo_dices = 0;
int numofThree_dices = 0;
int numofFour_dices = 0;
int numofFive_dices = 0;
int numofSix_dices = 0;
//Difference between two Number
int difference = 0;
int option_id;
char holdOrRoll[6]; //h & r
int main()
{
cout << "Please Enter the Random_seed_Number :";
cin >> random_seed;
for (int option_cycle_index = 0; option_cycle_index < 13; option_cycle_index++){
cout << "Round " << (option_cycle_index + 1) << " is going..." << endl;
cout << "Rolling the dices..." << endl;
cout << "The dices were:";
//initial first Six dices
for (int array_dices_cycle_index = 0; array_dices_cycle_index < 5; array_dices_cycle_index++){
//seed calculation
array_dices[array_dices_cycle_index] = rand() % random_seed;
cout << array_dices[array_dices_cycle_index];
}
cout << endl << "What re-rolls? ";
//input the array include
for (int hold_or_roll_cycle_index = 0; hold_or_roll_cycle_index < 5; hold_or_roll_cycle_index++){
cin >> holdOrRoll[hold_or_roll_cycle_index];
if (holdOrRoll[hold_or_roll_cycle_index] == 'r')
array_dices[hold_or_roll_cycle_index] = rand() % random_seed;
else ;
}
cout << "You got:";
//you got the new or keep the old num
for (int new_array_dices_cycle_index = 0; new_array_dices_cycle_index < 5; new_array_dices_cycle_index++){
//Calculate the account of the same numner among the six dices
if (array_dices[new_array_dices_cycle_index] == 1)
numofOne_dices += 1;
else if (array_dices[new_array_dices_cycle_index] == 2)
numofTwo_dices += 1;
else if (array_dices[new_array_dices_cycle_index] == 3)
numofThree_dices += 1;
else if (array_dices[new_array_dices_cycle_index] == 4)
numofFour_dices += 1;
else if (array_dices[new_array_dices_cycle_index] == 5)
numofFive_dices += 1;
else if (array_dices[new_array_dices_cycle_index] == 6)
numofSix_dices += 1;
//output the dices
cout << array_dices[new_array_dices_cycle_index] << " ";
}
cout << endl;
//Use an ReferenceArray which initial as 1 to 13 to imitate the step switch of the Menu Option
int the_referenceArray[13] = { 1,2,3,4,5,6,7,8,9,10,11,12,13 };
//Output the MenuOptionContent and MenuOptionID
for (int option_cycle_index = 0; option_cycle_index < 13; option_cycle_index++){
if (Option[option_cycle_index].Item_Number != 0)
cout << Option[option_cycle_index].Item_Number << Option[option_cycle_index].Item_Content << endl;
}
//c in the optionID and if this id can not be accepted, you have to input it again until it can be accepted
while (cin){
cout << "Please choose an option:";
cin >> option_id;
//Because the range of the MenuOptioID id 1 to 13, but wo only can switch it by begin with 0,
//so we can set an switchid which equal to the (optionid - 1)
int switchid = option_id - 1;
//if Option[switchid].Item_Number is valid
if (Option[switchid].Item_Number){
switch (switchid){
//if we choose the option 1
case 0: {
total_Score += CalculateDicesPlusOne(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[0] = NULL;
Option[0].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 2
case 1: {
total_Score += CalculateDicesPlusTwo(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[1] = NULL;
Option[1].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 3
case 2: {
total_Score += CalculateDicesPlusThree(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[2] = NULL;
Option[2].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 4
case 3: {
total_Score += CalculateDicesPlusFour(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[3] = NULL;
Option[3].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 5
case 4: {
total_Score += CalculateDicesPlusFive(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[4] = NULL;
Option[4].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 6
case 5: {
total_Score += CalculateDicesPlusSix(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[5] = NULL;
Option[5].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 7
case 6: {
total_Score += CalculateScoreByDices(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[6] = NULL;
Option[6].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 8
case 7: {
total_Score += CalculateScoreByDices(array_dices);
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[7] = NULL;
Option[7].Item_Number = NULL;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
break;
}
//if we choose the option 9
case 8: {
//if we have 3 same number and the remain number are the same as eachother
total_Score += SameNumerOfThree(numofOne_dices, numofTwo_dices, numofThree_dices,numofFour_dices, numofFive_dices, numofSix_dices);
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[8] = NULL;
Option[8].Item_Number = NULL;
break;
}
//if we choose the option 10
case 9: {
//begin the bubble sort to make the numbers which are sorted by From small to large
difference = BubbleSortInGame(array_dices);
//then calculate the account of the index with difference between two number
if (difference == 4)
total_Score += 30;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[9] = NULL;
Option[9].Item_Number = NULL;
break;
}
//if we choose the option 11
case 10: {
//begin the bubble sort to make the numbers which are sorted by From small to large
difference = BubbleSortInGame(array_dices);
//then calculate the account of the index with difference between two number
if (difference == 5)
total_Score += 40;
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
//set the number and id to 0,that it can be out put in the next cycle;
the_referenceArray[10] = NULL;
Option[10].Item_Number = NULL;
break;
}
//if we choose the option 12
case 11: {
//if we have 5 same number
total_Score += SameNumerOfFive(numofOne_dices, numofTwo_dices, numofThree_dices,numofFour_dices, numofFive_dices, numofSix_dices);
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
//set the number and id to 0,that it can be out put in the next cycle
the_referenceArray[11] = NULL;
Option[11].Item_Number = NULL;
break;
}
//if we choose the option 13
case 12: {
total_Score += CalculateDicesPlusThree(array_dices);
//out put the totalscore
cout << "The score of first six categories is " << total_Score << endl;
cout << "The score of all categories is " << total_Score << endl;
//set the number and id to 0,that it can be out put in the next cycle
the_referenceArray[12] = NULL;
Option[12].Item_Number = NULL;
break;
}
};
}
else{
//input again
continue;
}
//break the current Cycle
break;
}
}
}
void SwapSortNumber(int a, int b){
int c;
c = a;
a = b;
b = c;
}
int BubbleSortInGame(int arrayIndex[6]){
int temp = 0;
void SwapSortNumber(int a, int b);
for (int fisrstCycle_index = 0; fisrstCycle_index < 5; fisrstCycle_index++){
for (int SecondCycle_index = fisrstCycle_index + 1; SecondCycle_index < 5; SecondCycle_index++){
if (arrayIndex[fisrstCycle_index] > arrayIndex[SecondCycle_index])
SwapSortNumber(arrayIndex[fisrstCycle_index], arrayIndex[SecondCycle_index]);
}
}
//then calculate the difference between two number
for (int fisrstCycle_index = 0; fisrstCycle_index < 5; fisrstCycle_index++){
for (int SecondCycle_index = fisrstCycle_index + 1; SecondCycle_index < 5; SecondCycle_index++){
if (arrayIndex[SecondCycle_index] - arrayIndex[fisrstCycle_index] == 1)
temp += 1;
}
}
return temp;
}
int CalculateScoreByDices(int arrayIndex[6])
{
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
temp += arrayIndex[array_dices_index];
}
return temp;
}
int CalculateDicesPlusOne(int arrayIndex[6])
{
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
if (arrayIndex[array_dices_index] == 1)
temp += 1;
}
return temp;
}
int CalculateDicesPlusTwo(int arrayIndex[6]){
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
if (arrayIndex[array_dices_index] == 2)
temp += 2;
}
return temp;
}
int CalculateDicesPlusThree (int arrayIndex[6]){
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
if (arrayIndex[array_dices_index] == 3)
temp += 3;
}
return temp;
}
int CalculateDicesPlusFour(int arrayIndex[6]) {
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
if (arrayIndex[array_dices_index] == 4)
temp += 4;
}
return temp;
}
int CalculateDicesPlusFive(int arrayIndex[6]) {
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
if (arrayIndex[array_dices_index] == 5)
temp += 5;
}
return temp;
}
int CalculateDicesPlusSix(int arrayIndex[6]) {
int temp = 0;
for (int array_dices_index = 0; array_dices_index < 5; array_dices_index++){
if (arrayIndex[array_dices_index] == 6)
temp += 6;
}
return temp;
}
int SameNumerOfThree(int a, int b, int c, int d, int e, int f)
{
int temp = 0;
if ((a == 3 && (b == 2 || c == 2 || d == 2 || e == 2 || f == 2)) ||
(b == 3 && (a == 2 || c == 2 || d == 2 || e == 2 || f == 2)) ||
(c == 3 && (b == 2 || a == 2 || d == 2 || e == 2 || f == 2)) ||
(d == 3 && (b == 2 || c == 2 || a == 2 || e == 2 || f == 2)) ||
(e == 3 && (b == 2 || c == 2 || d == 2 || a == 2 || f == 2)) ||
(f == 3 && (b == 2 || c == 2 || d == 2 || e == 2 || a == 2)))
temp += 25;
else
temp += 0;
return temp;
}
int SameNumerOfFive (int a, int b, int c, int d, int e, int f) {
int temp = 0;
if (a == 5 || b == 5 || c == 5 ||
d == 5 || e == 5 || f == 5)
temp += 50;
return temp;
}