Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:family-feud [2015/06/04 18:02] (current)
cs142ta created
Line 1: Line 1:
 +=Family Feud!=
 +==Background==
 +* You want a program to track points for two teams on (a simplified) Family Feud game!
 +* In each round a survey question is given that has been asked to 100 people and the top answers have been recorded
 +* To start the round, a representative from each team is chosen two play in a “toss-up”
 +* The survey question is read
 +* Whichever team’s rep buzzes in and correctly guesses a top answer first, that team gets to guess the rest of the top survey answers
 +* Points are allotted based on the popularity of the survey answer
 +==Problem==
 +* You want a program to track points for two teams
 +* By using a pointer, it is possible to switch to a different team without modifying the code for adjusting a team’s score.
 +* Start with two score-keeping variables:
 +** int team_A_score = 0;
 +** int team_B_score = 0;
 +* How would you write code to adjust the score of whichever team wins the toss-up?
 +* For each of five rounds (i.e., survey questions), ​
 +** Prompt for the team that won the toss-up
 +** Prompt for points to add for each guess
 +** End the round after three wrong guesses (i.e., 0 points added)
 +* Print out the winner
 +* Let’s play the Feud!
  
 +==Solution==
 +<code cpp>
 +#include <​iostream>​
 +#include <​vector>​
 +#include <​string>​
 +
 +using namespace std;
 +
 +const int MATCH_MISS_COUNT = 3;
 +const int ROUNDS = 2;
 +
 +string prompt_for_team();​
 +int prompt_for_points();​
 +
 +int main()
 +{
 + int team_A_score = 0;
 + int team_B_score = 0;
 +
 + // By using a pointer, we can write code to adjust an arbitrary team's score and switch to different teams as necessary.
 + int * winning_team_score = NULL;
 +
 + // SIMULATE MULTIPLE ROUNDS
 + for (int round = 0; round < ROUNDS; round++)
 + {
 + // SIMULATE ONE ROUND
 + // Prompt for the team that won the toss-up (winning_team_score)
 + string winning_team = prompt_for_team();​
 +
 + if (winning_team == "​A"​)
 + {
 + // A pointer must be assigned to the address of a variable ​
 + winning_team_score = &​team_A_score;​
 + }
 + else if (winning_team == "​B"​)
 + {
 + winning_team_score = &​team_B_score;​
 + }
 +
 + int wrong_guesses = 0;
 + while (wrong_guesses < 3)
 + {
 + // Prompt for points to add for each guess
 + int points_to_add = prompt_for_points();​
 +
 + if (points_to_add == 0)
 + {
 + wrong_guesses++;​
 + }
 + else
 + {
 + // We avoid having to duplicate this code for both team A and team B by using a pointer
 + (*winning_team_score) = (*winning_team_score) + points_to_add;​
 + }
 +
 + // End the guessing after a certain number of wrong guesses (i.e., 0 points added)
 + }
 + cout << "Score at end of round " << round << " is A - " << team_A_score << ", B - " << team_B_score << endl;
 + }
 + cout << "Team " << (team_A_score > team_B_score ? "​A"​ : "​B"​) << " WINS!" << endl;
 + cout << "GAME OVER" << endl;
 + system("​pause"​);​
 + return 0;
 +}
 +
 +/*
 +Prompt for team that won the toss-up, read the input (no input validation)
 +@return user input (A or B)
 +*/
 +string prompt_for_team()
 +{
 + cout << "In the toss-up, which was the winning team (A,B)? ";
 + string team_that_answered;​
 + cin >> team_that_answered;​
 +
 + return team_that_answered;​
 +}
 +
 +/*
 +Prompt for points to be added (no input validation)
 +@return user input (points)
 +*/
 +int prompt_for_points()
 +{
 + cout << "​Points to add:";
 + int points_to_add;​
 + cin >> points_to_add;​
 +
 + return points_to_add;​
 +}
 +</​code>​
cs-142/family-feud.txt · Last modified: 2015/06/04 18:02 by cs142ta
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0