Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxchehab/132c8747592606ceb325fb9fb546d5bd to your computer and use it in GitHub Desktop.
Save maxchehab/132c8747592606ceb325fb9fb546d5bd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
void displayGameRules() {
cout << "Welcome to Pass the Pigs! Here are the rules. " << endl;
cout << "The player tosses two pigs, each can land in six positions: " << endl;
cout << "1. On side with dot down. " << endl;
cout << "2. On side with dot up. " << endl;
cout << "3. Razorback (on its back). " << endl;
cout << "4. Trotter (on all four feet). " << endl;
cout << "5. Snouter (balanced on snout and front two feet). " << endl;
cout << "6. Leaning jowler (balanced on one foot, snout and ear). " << endl;
cout << "After rolling each pig, player points change based off way pigs land. " << endl;
cout << "Player turns end when they roll a pig out or deciede to pass. " << endl;
}
bool isValidWinningScore(int score) {
bool status;
if ((score >= 1) && (score <= 100)) {
status = true;
}
else{
status = false;
}
return status;
}
int getValidWinningScore() { // should be a while loop in function
int score;
cout << "Enter an integer score between 1 and 100 to play to: " << endl;
cin >> score;
if (isValidWinningScore(score) ) {
cout << "The Score is valid. " << endl;
}
else{
cout << "The score value is out of range. " << endl;
}
return score;
}
string rollpig() {
string pigPosition;
int rollNum;
rollNum = rand()%100 + 1;
cout << "roll " << rollNum << endl;
if (rollNum == 1) {
pigPosition = "LEAN"; //Leaning jowler
}
else if ((rollNum >= 2) && (rollNum <= 4)) {
pigPosition = "SNOUT"; //snouter
}
else if ((rollNum >= 5) && (rollNum <= 13)) {
pigPosition = "TROT"; //Trotter
}
else if ((rollNum >= 14) && (rollNum <= 35)) {
pigPosition = "RZR"; //Razerback
}
else if ((rollNum >= 36) && (rollNum <= 65)) {
pigPosition = "SIDE-D"; //Side with dot
}
else if ((rollNum >= 66) && (rollNum <= 100)) {
pigPosition = "SIDE"; //Side without dot
}
return pigPosition;
}
string determineRollResult(string pig1, string pig2) {
string rollResult;
if (((pig1 == "SIDE") && (pig2 == "SIDE") ) || ((pig1 == "SIDE-D") && (pig2 == "SIDE-D"))) {
rollResult = "SIDER" ; //Sider
}
else if ((pig1 == "RZR") && (pig2 == "RZR")) {
rollResult = "DBL-RAZR" ; //Double Razerback
}
else if ((pig1 == "TROT") && (pig2 == "TROT")) {
rollResult = "DBL-TROT" ; //Double Trotter
}
else if ((pig1 == "SNOUT") && (pig2 == "SNOUT")) {
rollResult = "DBL-SNOUT" ; //Double Snouter
}
else if ((pig1 == "LEAN") && (pig2 == "LEAN")) {
rollResult = "DBL-LEAN" ; //Double Leaning jowler
}
else if (((pig1 == "SIDE-D") && (pig2 == "SIDE") ) || ((pig1 == "SIDE") && (pig2 == "SIDE-D"))) {
rollResult = "PIGOUT" ; //Both pigs on side, one dot up, one dot down
}
else {
rollResult = "MIXED" ;
}
return rollResult;
}
int calculateTotalRollPoints(string pig1, string pig2, string rollResult) {
int points = 0;
if (rollResult == "SIDER") {
points = 1;
}
else if (rollResult == "DBL-RAZR") {
points = 20;
}
else if (rollResult == "DBL-TROT") {
points = 20;
}
else if (rollResult == "DBL-SNOUT") {
points = 40;
}
else if (rollResult == "DBL-LEAN") {
points = 60;
}
else if (rollResult == "PIGOUT") {
points = 0;
}
else if (rollResult == "MIXED") {
if ((pig1 == "RZR") || (pig2 == "RZR")) {
points += 5;
}
if ((pig1 == "TROT") || (pig2 == "TROT")) {
points += 5;
}
if ((pig1 == "SNOUT") || (pig2 == "SNOUT")) {
points += 10;
}
if ((pig1 == "LEAN") || (pig2 == "LEAN")) {
points += 15;
}
}
return points;
}
int main () {
srand(time(0));
displayGameRules() ;
getValidWinningScore() ;
char userAnswer;
int points = 0;
int player1Points = 0;
int player2points = 0;
string rollResult;
string pig1;
string pig2;
do {
pig1 = rollpig() ;
pig2 = rollpig() ;
cout << "Your pig landed on " << pig1 << " and " << pig2 << endl;
rollResult = determineRollResult(pig1, pig2);
cout << " Your Roll result is " << rollResult << endl;
points = calculateTotalRollPoints(pig1, pig2, rollResult);
totalPoints += points ;
cout << "point total " << totalPoints << endl;
cout << "Would you like to keep rolling, y for yes, n for no? " << endl;
cin >> userAnswer;
}while ( (userAnswer == 'y') || (rollResult != "PIGOUT"));
do {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment