SlideShare a Scribd company logo
1 of 14
Download to read offline
this is what i have. i really need help on updating the scores only. like if a person gets pig on
their second turn their score would be what the have before.
You are to finish the program below that implements the dice game PIG played by 2 human
players.
Pig is a dice game where each player during their turn rolls a 6-sided die. If the player rolls a 2
through 6, they decide whether they want to keep their turn score or roll again. If they roll a 1,
the player loses all the points for that turn, thus receiving a turn score of 0 and that turn is over.
The first player to reach 100 points or more wins.
this is what i have. i really need help on updating the scores only. like if a person gets pig on
their second turn their score would be what the have before.
You are to finish the program below that implements the dice game PIG played by 2 human
players.
#include
#include
#include
#include
using namespace std;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;
// after long a while realized i needed this
int turn = PLAYER1;
//FIXME (1): Implement the printIntro function
void printIntro ()
{
cout << "Welcome to the dice game Pig!" << endl;
cout << "The objective is to be first to score 100 points." << endl;
return;
}
//FIXME (4, 5, 6): Implement the humanTurn function
int humanTurn (string playername, int playerscore)
{
// have to print the player name (first playerfirst)
cout << playername << endl;
// have to roll the dice to see either if they rolled a pig or not
int dice = rand() % 6 + 1;
// now have to write an if statment whether or not they get 1 or anything else
if ( dice == 1)
{
cout << "You rolled a 1 (PIG!)" << endl;
cout << "Your turn is over" << endl;
}
else
{
playerscore += dice;
cout << "You rolled a " << dice << endl;
}
cout << "Your score: " << playerscore << endl;
if( dice == 1){
// if players it the first then the next player should be player2
if(turn == PLAYER1)
turn = PLAYER2;
// vise versa
else
turn = PLAYER1;
}
else{
cout << "Do you want to roll again? (y/n): ";
char answer;
cin >> answer;
cout << endl;
if( answer != 'y')
{
if( turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
// mind tried must keepgoing
return playerscore;
}
int main() {
srand(4444);
// setup and initialize variables
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
printIntro();
cout << endl;
// FIXME (2): get names of players
cout << "Player 1 - Enter your name: ";
cin >> player1name;
cout << endl;
cout << "Player 2 - Enter your name: ";
cin >> player2name;
cout << endl;
//play game
while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) {
//player 1's turn or player 2's turn
if (turn == PLAYER1) {
player1score = humanTurn(player1name, player1score);
}
else {
player2score = humanTurn(player2name, player2score);
}
//FIXME (3): update turn value
// based on whose turn it is, update the turn variable to other player
}
// FIXME (7): Output who won the game.
if(player1score >= WINNING_SCORE)
{
cout << player1name << endl;
cout << "You rolled a 6"<< endl;
cout << "Your score: "<< player1score << endl;
cout << endl;
cout << player1name << " wins!" << endl;
}
else
{
cout << player2name << endl;
cout << "You rolled a 6" << endl;
cout << "Your score: " << player2score << endl;
cout << endl;
cout << player2name << " wins!" << endl;
}
return 0;
}
Solution
#include
#include
#include
using namespace std;
const int PIG=-1;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int WINNING_SCORE = 100;
//const PIG would indicate that a player just got 1
// after long a while realized i needed this
int turn = PLAYER1;
//FIXME (1): Implement the printIntro function
void printIntro ()
{
cout << "Welcome to the dice game Pig!" << endl;
cout << "The objective is to be first to score 100 points." << endl;
return;
}
//FIXME (4, 5, 6): Implement the humanTurn function
//I have modified the function definition as per the requirement
int humanTurn (string playername)
{
int turnscore;
// have to print the player name (first playerfirst)
cout << playername << endl;
// have to roll the dice to see either if they rolled a pig or not
int dice = rand() % 6 + 1;
// now have to write an if statment whether or not they get 1 or anything else
if ( dice == 1)
{
cout << "You rolled a 1 (PIG!)" << endl;
cout << "Your turn is over" << endl;
// if players it the first then the next player should be player2
if(turn == PLAYER1)
turn = PLAYER2;
// vise versa
else
turn = PLAYER1;
return PIG;
}
else
{
cout << "You rolled a " << dice << endl;
cout << "Do you want to roll again? (y/n): ";
char answer;
cin >> answer;
cout << endl;
if( answer != 'y')
{
turnscore=dice;
if( turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else{
int diceReturned=humanTurn (playername);
//A recursive call to humanTurn function is made if a player chooses to roll again
if(diceReturned==PIG){
return PIG;
}
else{
turnscore=diceReturned+dice;
}
//if player gets 1 on the second turn, then PIG is returned
//else, the dice is added to turn score
}
}
// mind tried must keepgoing
return turnscore;
}
int main() {
// setup and initialize variables
int scoreInitial=0;
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
printIntro();
cout << endl;
// FIXME (2): get names of players
cout << "Player 1 - Enter your name: ";
cin >> player1name;
cout << endl;
cout << "Player 2 - Enter your name: ";
cin >> player2name;
cout << endl;
//play game
while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) {
//player 1's turn or player 2's turn
if (turn == PLAYER1) {
scoreInitial = humanTurn(player1name);
if(scoreInitial==PIG){
cout << "Your score: " << player1score << endl;
cout<<" ";
}
else{
player1score+=scoreInitial;
cout << "Your score: " << player1score << endl;
cout<<" ";
}
}
else {
scoreInitial = humanTurn(player2name);
if(scoreInitial==PIG){
cout << "Your score: " << player2score << endl;
cout<<" ";
}
else{
player2score+=scoreInitial;
cout << "Your score: " << player2score << endl;
cout<<" ";
}
}
//FIXME (3): update turn value
// based on whose turn it is, update the turn variable to other player
}
// FIXME (7): Output who won the game.
if(player1score >= WINNING_SCORE)
{
cout << player1name << endl;
cout << "You rolled a 6"<< endl;
cout << "Your score: "<< player1score << endl;
cout << endl;
cout << player1name << " wins!" << endl;
}
else
{
cout << player2name << endl;
cout << "You rolled a 6" << endl;
cout << "Your score: " << player2score << endl;
cout << endl;
cout << player2name << " wins!" << endl;
}
return 0;
}
SAMPLE IMPLEMENTATION THAT I TESTED:
Welcome to the dice game Pig!
The objective is to be first to score 100 points.
Player 1 - Enter your name: K1
Player 2 - Enter your name: K2
K1
You rolled a 2
Do you want to roll again? (y/n): y
K1
You rolled a 5
Do you want to roll again? (y/n): y
K1
You rolled a 4
Do you want to roll again? (y/n): n
Your score: 11
K2
You rolled a 2
Do you want to roll again? (y/n): y
K2
You rolled a 6
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): y
K2
You rolled a 5
Do you want to roll again? (y/n): n
Your score: 15
K1
You rolled a 1 (PIG!)
Your turn is over
Your score: 11
K2
You rolled a 4
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): n
Your score: 31
K1
You rolled a 6
Do you want to roll again? (y/n): y
K1
You rolled a 5
Do you want to roll again? (y/n): y
K1
You rolled a 1 (PIG!)
Your turn is over
Your score: 11
K2
You rolled a 1 (PIG!)
Your turn is over
Your score: 31
K1
You rolled a 5
Do you want to roll again? (y/n): y
K1
You rolled a 5
Do you want to roll again? (y/n): y
K1
You rolled a 6
Do you want to roll again? (y/n): y
K1
You rolled a 3
Do you want to roll again? (y/n): n
Your score: 30
K2
You rolled a 4
Do you want to roll again? (y/n): y
K2
You rolled a 4
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): n
Your score: 50
K1
You rolled a 2
Do you want to roll again? (y/n): y
K1
You rolled a 2
Do you want to roll again? (y/n): y
K1
You rolled a 6
Do you want to roll again? (y/n): y
K1
You rolled a 1 (PIG!)
Your turn is over
Your score: 30
K2
You rolled a 1 (PIG!)
Your turn is over
Your score: 50
K1
You rolled a 1 (PIG!)
Your turn is over
Your score: 30
K2
You rolled a 6
Do you want to roll again? (y/n): y
K2
You rolled a 4
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): y
K2
You rolled a 5
Do you want to roll again? (y/n): n
Your score: 67
K1
You rolled a 2
Do you want to roll again? (y/n): y
K1
You rolled a 5
Do you want to roll again? (y/n): y
K1
You rolled a 4
Do you want to roll again? (y/n): y
K1
You rolled a 4
Do you want to roll again? (y/n): y
K1
You rolled a 4
Do you want to roll again? (y/n): n
Your score: 49
K2
You rolled a 6
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 2
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): n
Your score: 84
K1
You rolled a 6
Do you want to roll again? (y/n): y
K1
You rolled a 1 (PIG!)
Your turn is over
Your score: 49
K2
You rolled a 6
Do you want to roll again? (y/n): y
K2
You rolled a 5
Do you want to roll again? (y/n): n
Your score: 95
K1
You rolled a 2
Do you want to roll again? (y/n): n
Your score: 51
K2
You rolled a 3
Do you want to roll again? (y/n): y
K2
You rolled a 3
Do you want to roll again? (y/n): n
Your score: 101
K2
You rolled a 6
Your score: 101
K2 wins!
Please let me know in case of any doubts :)

More Related Content

Similar to this is what i have. i really need help on updating the scores only..pdf

This is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdfThis is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdf
rajkumarm401
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
Rahman Ash
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
Carol Bruno
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
Joe McCall
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# Adapted
Mike Clement
 

Similar to this is what i have. i really need help on updating the scores only..pdf (6)

Seminar_3.pptx
Seminar_3.pptxSeminar_3.pptx
Seminar_3.pptx
 
This is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdfThis is for an homework assignment using Java code. Here is the home.pdf
This is for an homework assignment using Java code. Here is the home.pdf
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
 
Bowling game kata
Bowling game kataBowling game kata
Bowling game kata
 
Bowling Game Kata in C# Adapted
Bowling Game Kata in C# AdaptedBowling Game Kata in C# Adapted
Bowling Game Kata in C# Adapted
 

More from arjunstores123

Money and Our Monetary SystemThe monetary system in any economy fa.pdf
Money and Our Monetary SystemThe monetary system in any economy fa.pdfMoney and Our Monetary SystemThe monetary system in any economy fa.pdf
Money and Our Monetary SystemThe monetary system in any economy fa.pdf
arjunstores123
 
JAVAFor the code in which I implemented most of the quick select a.pdf
JAVAFor the code in which I implemented most of the quick select a.pdfJAVAFor the code in which I implemented most of the quick select a.pdf
JAVAFor the code in which I implemented most of the quick select a.pdf
arjunstores123
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
hi i need to produce a 300 word report on the construction of struct.pdf
hi i need to produce a 300 word report on the construction of struct.pdfhi i need to produce a 300 word report on the construction of struct.pdf
hi i need to produce a 300 word report on the construction of struct.pdf
arjunstores123
 
Answer the following questions from the Article belowExercise #1..pdf
Answer the following questions from the Article belowExercise #1..pdfAnswer the following questions from the Article belowExercise #1..pdf
Answer the following questions from the Article belowExercise #1..pdf
arjunstores123
 
All answers are in the form of TrueFalse with a explantion as to wh.pdf
All answers are in the form of TrueFalse with a explantion as to wh.pdfAll answers are in the form of TrueFalse with a explantion as to wh.pdf
All answers are in the form of TrueFalse with a explantion as to wh.pdf
arjunstores123
 
Consider miRNA target sites, How do these compare to transcription fa.pdf
Consider miRNA target sites, How do these compare to transcription fa.pdfConsider miRNA target sites, How do these compare to transcription fa.pdf
Consider miRNA target sites, How do these compare to transcription fa.pdf
arjunstores123
 

More from arjunstores123 (20)

Money and Our Monetary SystemThe monetary system in any economy fa.pdf
Money and Our Monetary SystemThe monetary system in any economy fa.pdfMoney and Our Monetary SystemThe monetary system in any economy fa.pdf
Money and Our Monetary SystemThe monetary system in any economy fa.pdf
 
JAVAFor the code in which I implemented most of the quick select a.pdf
JAVAFor the code in which I implemented most of the quick select a.pdfJAVAFor the code in which I implemented most of the quick select a.pdf
JAVAFor the code in which I implemented most of the quick select a.pdf
 
Investigators are conducting a cohort study to determine whether wom.pdf
Investigators are conducting a cohort study to determine whether wom.pdfInvestigators are conducting a cohort study to determine whether wom.pdf
Investigators are conducting a cohort study to determine whether wom.pdf
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
In January of 2012, the second stop for a Republican to get votes to.pdf
In January of 2012, the second stop for a Republican to get votes to.pdfIn January of 2012, the second stop for a Republican to get votes to.pdf
In January of 2012, the second stop for a Republican to get votes to.pdf
 
I need some help answering these three questions listed below.1. A.pdf
I need some help answering these three questions listed below.1. A.pdfI need some help answering these three questions listed below.1. A.pdf
I need some help answering these three questions listed below.1. A.pdf
 
hi i need to produce a 300 word report on the construction of struct.pdf
hi i need to produce a 300 word report on the construction of struct.pdfhi i need to produce a 300 word report on the construction of struct.pdf
hi i need to produce a 300 word report on the construction of struct.pdf
 
HTML elements are split into two types “block” elements and “inline.pdf
HTML elements are split into two types “block” elements and “inline.pdfHTML elements are split into two types “block” elements and “inline.pdf
HTML elements are split into two types “block” elements and “inline.pdf
 
How does multicore differ from a classic multiprocessing environment.pdf
How does multicore differ from a classic multiprocessing environment.pdfHow does multicore differ from a classic multiprocessing environment.pdf
How does multicore differ from a classic multiprocessing environment.pdf
 
How the non-dense index worksHow the non-dense index works.pdf
How the non-dense index worksHow the non-dense index works.pdfHow the non-dense index worksHow the non-dense index works.pdf
How the non-dense index worksHow the non-dense index works.pdf
 
Given below is the response of several types of temperature sensors. .pdf
Given below is the response of several types of temperature sensors. .pdfGiven below is the response of several types of temperature sensors. .pdf
Given below is the response of several types of temperature sensors. .pdf
 
Genetic recombination can occur in many different stages. Identify t.pdf
Genetic recombination can occur in many different stages. Identify t.pdfGenetic recombination can occur in many different stages. Identify t.pdf
Genetic recombination can occur in many different stages. Identify t.pdf
 
For every one NADH molecule oxidized at complex I, how many TOTAL hy.pdf
For every one NADH molecule oxidized at complex I, how many TOTAL hy.pdfFor every one NADH molecule oxidized at complex I, how many TOTAL hy.pdf
For every one NADH molecule oxidized at complex I, how many TOTAL hy.pdf
 
Find the domain of the function. (Enter your answer using interval no.pdf
Find the domain of the function. (Enter your answer using interval no.pdfFind the domain of the function. (Enter your answer using interval no.pdf
Find the domain of the function. (Enter your answer using interval no.pdf
 
explain as specifically as possible how each of the following helps .pdf
explain as specifically as possible how each of the following helps .pdfexplain as specifically as possible how each of the following helps .pdf
explain as specifically as possible how each of the following helps .pdf
 
Each of the following lymphatic structures in the correct category ba.pdf
Each of the following lymphatic structures in the correct category ba.pdfEach of the following lymphatic structures in the correct category ba.pdf
Each of the following lymphatic structures in the correct category ba.pdf
 
Answer the following questions from the Article belowExercise #1..pdf
Answer the following questions from the Article belowExercise #1..pdfAnswer the following questions from the Article belowExercise #1..pdf
Answer the following questions from the Article belowExercise #1..pdf
 
All answers are in the form of TrueFalse with a explantion as to wh.pdf
All answers are in the form of TrueFalse with a explantion as to wh.pdfAll answers are in the form of TrueFalse with a explantion as to wh.pdf
All answers are in the form of TrueFalse with a explantion as to wh.pdf
 
Consider miRNA target sites, How do these compare to transcription fa.pdf
Consider miRNA target sites, How do these compare to transcription fa.pdfConsider miRNA target sites, How do these compare to transcription fa.pdf
Consider miRNA target sites, How do these compare to transcription fa.pdf
 
Anaerobic Respiration & Fermentation (Microbiology)What are the bu.pdf
Anaerobic Respiration & Fermentation (Microbiology)What are the bu.pdfAnaerobic Respiration & Fermentation (Microbiology)What are the bu.pdf
Anaerobic Respiration & Fermentation (Microbiology)What are the bu.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 

this is what i have. i really need help on updating the scores only..pdf

  • 1. this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. You are to finish the program below that implements the dice game PIG played by 2 human players. Pig is a dice game where each player during their turn rolls a 6-sided die. If the player rolls a 2 through 6, they decide whether they want to keep their turn score or roll again. If they roll a 1, the player loses all the points for that turn, thus receiving a turn score of 0 and that turn is over. The first player to reach 100 points or more wins. this is what i have. i really need help on updating the scores only. like if a person gets pig on their second turn their score would be what the have before. You are to finish the program below that implements the dice game PIG played by 2 human players. #include #include #include #include using namespace std; const int PLAYER1 = 0; const int PLAYER2 = 1; const int WINNING_SCORE = 100; // after long a while realized i needed this int turn = PLAYER1; //FIXME (1): Implement the printIntro function void printIntro () { cout << "Welcome to the dice game Pig!" << endl; cout << "The objective is to be first to score 100 points." << endl; return; } //FIXME (4, 5, 6): Implement the humanTurn function int humanTurn (string playername, int playerscore) {
  • 2. // have to print the player name (first playerfirst) cout << playername << endl; // have to roll the dice to see either if they rolled a pig or not int dice = rand() % 6 + 1; // now have to write an if statment whether or not they get 1 or anything else if ( dice == 1) { cout << "You rolled a 1 (PIG!)" << endl; cout << "Your turn is over" << endl; } else { playerscore += dice; cout << "You rolled a " << dice << endl; } cout << "Your score: " << playerscore << endl; if( dice == 1){ // if players it the first then the next player should be player2 if(turn == PLAYER1) turn = PLAYER2; // vise versa else turn = PLAYER1; } else{ cout << "Do you want to roll again? (y/n): "; char answer; cin >> answer; cout << endl;
  • 3. if( answer != 'y') { if( turn == PLAYER1) turn = PLAYER2; else turn = PLAYER1; } } // mind tried must keepgoing return playerscore; } int main() { srand(4444); // setup and initialize variables int player1score = 0; int player2score = 0; string player1name; string player2name; printIntro(); cout << endl; // FIXME (2): get names of players cout << "Player 1 - Enter your name: "; cin >> player1name; cout << endl; cout << "Player 2 - Enter your name: "; cin >> player2name; cout << endl; //play game while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) { //player 1's turn or player 2's turn
  • 4. if (turn == PLAYER1) { player1score = humanTurn(player1name, player1score); } else { player2score = humanTurn(player2name, player2score); } //FIXME (3): update turn value // based on whose turn it is, update the turn variable to other player } // FIXME (7): Output who won the game. if(player1score >= WINNING_SCORE) { cout << player1name << endl; cout << "You rolled a 6"<< endl; cout << "Your score: "<< player1score << endl; cout << endl; cout << player1name << " wins!" << endl; } else { cout << player2name << endl; cout << "You rolled a 6" << endl; cout << "Your score: " << player2score << endl; cout << endl; cout << player2name << " wins!" << endl; } return 0; } Solution #include #include #include using namespace std;
  • 5. const int PIG=-1; const int PLAYER1 = 0; const int PLAYER2 = 1; const int WINNING_SCORE = 100; //const PIG would indicate that a player just got 1 // after long a while realized i needed this int turn = PLAYER1; //FIXME (1): Implement the printIntro function void printIntro () { cout << "Welcome to the dice game Pig!" << endl; cout << "The objective is to be first to score 100 points." << endl; return; } //FIXME (4, 5, 6): Implement the humanTurn function //I have modified the function definition as per the requirement int humanTurn (string playername) { int turnscore; // have to print the player name (first playerfirst) cout << playername << endl; // have to roll the dice to see either if they rolled a pig or not int dice = rand() % 6 + 1; // now have to write an if statment whether or not they get 1 or anything else if ( dice == 1) { cout << "You rolled a 1 (PIG!)" << endl; cout << "Your turn is over" << endl; // if players it the first then the next player should be player2 if(turn == PLAYER1) turn = PLAYER2; // vise versa else turn = PLAYER1; return PIG;
  • 6. } else { cout << "You rolled a " << dice << endl; cout << "Do you want to roll again? (y/n): "; char answer; cin >> answer; cout << endl; if( answer != 'y') { turnscore=dice; if( turn == PLAYER1) turn = PLAYER2; else turn = PLAYER1; } else{ int diceReturned=humanTurn (playername); //A recursive call to humanTurn function is made if a player chooses to roll again if(diceReturned==PIG){ return PIG; } else{ turnscore=diceReturned+dice; } //if player gets 1 on the second turn, then PIG is returned //else, the dice is added to turn score } } // mind tried must keepgoing return turnscore; } int main() { // setup and initialize variables int scoreInitial=0;
  • 7. int player1score = 0; int player2score = 0; string player1name; string player2name; printIntro(); cout << endl; // FIXME (2): get names of players cout << "Player 1 - Enter your name: "; cin >> player1name; cout << endl; cout << "Player 2 - Enter your name: "; cin >> player2name; cout << endl; //play game while (player1score < WINNING_SCORE && player2score < WINNING_SCORE) { //player 1's turn or player 2's turn if (turn == PLAYER1) { scoreInitial = humanTurn(player1name); if(scoreInitial==PIG){ cout << "Your score: " << player1score << endl; cout<<" "; } else{ player1score+=scoreInitial; cout << "Your score: " << player1score << endl; cout<<" "; } } else { scoreInitial = humanTurn(player2name); if(scoreInitial==PIG){ cout << "Your score: " << player2score << endl; cout<<" "; } else{
  • 8. player2score+=scoreInitial; cout << "Your score: " << player2score << endl; cout<<" "; } } //FIXME (3): update turn value // based on whose turn it is, update the turn variable to other player } // FIXME (7): Output who won the game. if(player1score >= WINNING_SCORE) { cout << player1name << endl; cout << "You rolled a 6"<< endl; cout << "Your score: "<< player1score << endl; cout << endl; cout << player1name << " wins!" << endl; } else { cout << player2name << endl; cout << "You rolled a 6" << endl; cout << "Your score: " << player2score << endl; cout << endl; cout << player2name << " wins!" << endl; } return 0; } SAMPLE IMPLEMENTATION THAT I TESTED: Welcome to the dice game Pig! The objective is to be first to score 100 points. Player 1 - Enter your name: K1 Player 2 - Enter your name: K2 K1 You rolled a 2 Do you want to roll again? (y/n): y
  • 9. K1 You rolled a 5 Do you want to roll again? (y/n): y K1 You rolled a 4 Do you want to roll again? (y/n): n Your score: 11 K2 You rolled a 2 Do you want to roll again? (y/n): y K2 You rolled a 6 Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): y K2 You rolled a 5 Do you want to roll again? (y/n): n Your score: 15 K1 You rolled a 1 (PIG!) Your turn is over Your score: 11 K2 You rolled a 4 Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): y
  • 10. K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): n Your score: 31 K1 You rolled a 6 Do you want to roll again? (y/n): y K1 You rolled a 5 Do you want to roll again? (y/n): y K1 You rolled a 1 (PIG!) Your turn is over Your score: 11 K2 You rolled a 1 (PIG!) Your turn is over Your score: 31 K1 You rolled a 5 Do you want to roll again? (y/n): y K1 You rolled a 5 Do you want to roll again? (y/n): y K1 You rolled a 6 Do you want to roll again? (y/n): y K1 You rolled a 3 Do you want to roll again? (y/n): n Your score: 30 K2 You rolled a 4
  • 11. Do you want to roll again? (y/n): y K2 You rolled a 4 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): n Your score: 50 K1 You rolled a 2 Do you want to roll again? (y/n): y K1 You rolled a 2 Do you want to roll again? (y/n): y K1 You rolled a 6 Do you want to roll again? (y/n): y K1 You rolled a 1 (PIG!) Your turn is over Your score: 30 K2 You rolled a 1 (PIG!) Your turn is over Your score: 50 K1 You rolled a 1 (PIG!)
  • 12. Your turn is over Your score: 30 K2 You rolled a 6 Do you want to roll again? (y/n): y K2 You rolled a 4 Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): y K2 You rolled a 5 Do you want to roll again? (y/n): n Your score: 67 K1 You rolled a 2 Do you want to roll again? (y/n): y K1 You rolled a 5 Do you want to roll again? (y/n): y K1 You rolled a 4 Do you want to roll again? (y/n): y K1 You rolled a 4 Do you want to roll again? (y/n): y K1 You rolled a 4 Do you want to roll again? (y/n): n Your score: 49 K2 You rolled a 6 Do you want to roll again? (y/n): y K2 You rolled a 3
  • 13. Do you want to roll again? (y/n): y K2 You rolled a 2 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): n Your score: 84 K1 You rolled a 6 Do you want to roll again? (y/n): y K1 You rolled a 1 (PIG!) Your turn is over Your score: 49 K2 You rolled a 6 Do you want to roll again? (y/n): y K2 You rolled a 5 Do you want to roll again? (y/n): n Your score: 95 K1 You rolled a 2 Do you want to roll again? (y/n): n Your score: 51 K2 You rolled a 3 Do you want to roll again? (y/n): y K2 You rolled a 3 Do you want to roll again? (y/n): n Your score: 101
  • 14. K2 You rolled a 6 Your score: 101 K2 wins! Please let me know in case of any doubts :)