Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxchehab/747f19325a998aadaac89210e6c8586c to your computer and use it in GitHub Desktop.
Save maxchehab/747f19325a998aadaac89210e6c8586c 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 = -1;
do{
cout << "Enter an integer score between 1 and 100 to play to: " << endl;
cin >> score;
if (!isValidWinningScore(score)) {
cout << "The Score is valid. " << endl;
}
} while(!isValidWinningScore(score));
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;
}
bool gameIsWon(int points, int winningScore) {
return points >= winningScore;
}
int main () {
srand(time(0));
displayGameRules() ;
int winningScore = getValidWinningScore();
char userAnswer;
int points = 0;
int player1Points = 0;
int player2Points = 0;
bool finished = false;
int counter = 0;
string rollResult;
string passResult;
while(!finished) {
cout << "Game Status:\tPlayer1: " << player1Points << "\tPlayer2: " << player2Points << "\tWinning Score: " << winningScore << endl;
cout << ((counter % 2 == 0) ? "Player1" : "Player2") << " would you like to pass or continue. Type [pass] to pass or anything else to continue." << endl;
cin >> passResult;
if (passResult != "pass") {
string pig1 = rollpig();
string pig2 = rollpig();
rollResult = determineRollResult(pig1, pig2);
cout << "rollReseult: " << rollResult << endl;
int points = calculateTotalRollPoints(pig1, pig2, rollResult);
cout << "You have an additional " << points << " points." << endl;
if(counter % 2 == 0) {
if(points == 0) {
player1Points = 0; // PIGOUT was called
}
player1Points += points;
finished = gameIsWon(player1Points, winningScore);
cout << "finshed: " << finished << endl;
} else {
if(points == 0) {
player2Points = 0; // PIGOUT was called
}
player2Points += points;
finished = gameIsWon(player2Points, winningScore);
cout << "finshed: " << finished << endl;
}
} else {
cout << ((counter % 2 == 0) ? "Player1" : "Player2") << " has passed" << endl;
}
cout << endl;
counter++;
}
cout << "Game Status:\tPlayer1: " << player1Points << "\tPlayer2: " << player2Points << "\tWinning Score: " << winningScore << endl;
if (player1Points > player2Points) {
cout << "Player 1 has won!" << endl;
} else {
cout << "Player 2 has won!" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment