Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .Rhistory
Empty file.
48 changes: 48 additions & 0 deletions Cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdlib.h>

class Cpu
{
private:
int score;
int score_total;
int turns;
bool holding;

public:
Cpu()
{

}

~Cpu()
{

}
int score_total(void)
{
return score_total;
}

void roll(void)
{
holding=false;
outcome=rand() % 6 + 1;
if(outcome==1)
{
score=0;
turns++;
}
else
{
score+=outcome;
turns++;
}

}
void hold(void)
{
score_total+=score;
holding=true;
}

}
675 changes: 675 additions & 0 deletions LICENSE~

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ The first player to score 100 or more points wins.
From: https://en.wikipedia.org/wiki/Pig_(dice_game)

This implementation is a single player game against a computer player.

THIS file was edited in master branch
17 changes: 17 additions & 0 deletions README.md~
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# PigsGame
Pigs dice game

This is a command-line implementation of Pig. The rules of the game as explaned on Wikipedia are:s

Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player decides to "hold":

If the player rolls a 1, they score nothing and it becomes the next player's turn.
If the player rolls any other number, it is added to their turn total and the player's turn continues.
If a player chooses to "hold", their turn total is added to their score, and it becomes the next player's turn.
The first player to score 100 or more points wins.

From: https://en.wikipedia.org/wiki/Pig_(dice_game)

This implementation is a single player game against a computer player.

This was edited by egerber
Binary file added a.out
Binary file not shown.
83 changes: 83 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>
#include "player.h"
#include <stdlib.h>
using namespace std;

int roll()
{
int outcome;
outcome=rand() % 6 + 1;
return outcome;
}

int main()
{
Player *player1 = new Player();
Player *computer1 = new Player();
char response;

while(!player1->did_win || computer1->did_win)
{
cout << "\nPlayer 1's turn.\nYour score = " << player1->score;
player1->hold = false;
while(!player1->hold)
{
int cur_roll = roll();
cout << "\nYou rolled a " << cur_roll << "\n";
if(cur_roll == 1)
{
cout << "Your turn is over.\n";
player1->endTurn(1);
}
else
{
player1->increaseTurnTotal(cur_roll);
cout << "Your current turn total = " << player1->turn_total;
cout << "\nEnter 0 to hold or any thing else to continue \n";

cin >> response;
if(response == '0')
{
player1->endTurn(0);
}
}
}
if(player1->did_win)
{
break;
}
cout << "\nComputer's turn.\nCurrent score = " << computer1->score;
computer1->hold = false;
while(!computer1->hold)
{
int cur_roll = roll();
cout << "\nComputer rolled a " << cur_roll << "\n";
if(cur_roll == 1)
{
cout << "Computer's turn is over.\n";
computer1->endTurn(1);
}
else
{
computer1->increaseTurnTotal(cur_roll);
cout << "Computer current turn total = " << computer1->turn_total;
cout << "\nComputer holds\n";
computer1->endTurn(0);
}
}
if(computer1->did_win)
{
break;
}

}
cout << "Game Over!" << endl;
if(player1->score > computer1->score)
{
cout << "Player 1 wins!" << endl;
}
else
{
cout << "Computer 1 wins!" << endl;
}
}
39 changes: 39 additions & 0 deletions player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using namespace std;
#include "player.h"

Player::Player()
{
score = 0;
turn_total = 0;
hold = false;
did_win = false;
}

Player::~Player(){}

void Player::endTurn(int roll)
{
hold = true;
if(roll == 0)
{
//Hold
score = turn_total + score;
turn_total = 0;
if(score >= 100)
{
did_win = true;
}
}
else
{
//Rolled a 1
turn_total = 0;

}
return;
}
void Player::increaseTurnTotal(int roll)
{
turn_total = turn_total + roll;
return;
}
17 changes: 17 additions & 0 deletions player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef PLAYER_H
#define PLAYER_H

class Player
{
public:
Player();
~Player();
int score;
int turn_total;
bool hold;
void endTurn(int roll);
void increaseTurnTotal(int roll);
bool did_win;
};

#endif