Question

In: Computer Science

How would you code players taking turns, while having a score system. Please add examples in...

How would you code players taking turns, while having a score system. Please add examples in C++.

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int dieRoll();
int humanTurn(int);
int computerTurn(int);

int main()
{
        int humanTotalScore = 0, computerTotalScore = 0;
                
        //loop to keep playing until someone scores 100+
        do
        {
                humanTotalScore = humanTotalScore + humanTurn(humanTotalScore); //add the score from a new turn to the running total
                cout << "Your total score so far is " << humanTotalScore << "." << endl;
                if(humanTotalScore >= 100)
                {
                        cout << "You win!";
                        break;
                }
                computerTotalScore = computerTotalScore + computerTurn(computerTotalScore); //add the score from a new turn to the running total
                cout << "CPU total score so far is " << computerTotalScore << "." << endl;
                if(computerTotalScore >= 100)
                {
                        cout << "Computer wins!";
                        break;
                }
        }
        while(humanTotalScore < 100 && computerTotalScore < 100);
                
        return 0;
}

//simulate rolling of die
int dieRoll()
{
                return (rand() % 6) + 1; //call to rand() returns 0-5, + 1 to give range 1-6, best way to avoid impossible die roll of 0
}

int humanTurn(int humanTotalScore)
{
        int thisTurnScore = 0, score = 0;
        char rollOrHold;
        
        //loop to keep going as long the player chooses Roll Again or a 1 is thrown
        do
        {
                        score = dieRoll(); //roll the die
                
                if(score == 1)
                {
                        cout << "You rolled a 1.  End of turn." << endl;
                        break;
                }
                
                thisTurnScore = thisTurnScore + score; //running total for this turn only
                                
                cout << "You rolled a " << score << ".  Score so far this turn is " << thisTurnScore << "." << endl;
                cout << "Roll again (r) or Hold (h)? ";
                cin >> rollOrHold;
        }
        while(rollOrHold == 'r' || rollOrHold == 'R');
        
        if(rollOrHold == 'h' || rollOrHold == 'H') return thisTurnScore; //finsh turn and return total score if player chooses to Hold
                
        return 0; //will only get this far if player rolled a 1
}

int computerTurn(int computerTotalScore)
{
        int thisTurnScore = 0, score = 0;
        
        //loop to keep going as long the CPU score for this turn is less than 20
        do
        {
                score = dieRoll(); //roll the dice
                
                if(score == 1)
                {
                        cout << "CPU rolled a 1.  End of turn." << endl;
                        break;
                }
                
                thisTurnScore = thisTurnScore + score; //running total for this turn only
                                
                cout << "CPU rolled a " << score << ".  Score so far this turn is " << thisTurnScore << "." << endl;
        }
        while(thisTurnScore < 20);

        //finsh turn and return total score if the CPU scored 20+
        if(thisTurnScore >= 20)
        {
                cout << "CPU holds." << endl;
                return thisTurnScore;
        }
                
        return 0; //will only get this far if CPU rolled a 1
}

Related Solutions

How would I add an automatic please fill out this info to my complete code. Something...
How would I add an automatic please fill out this info to my complete code. Something similar to this code. <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Untitled Document</title> <script>    function phonenumber(inputtxt){           var phoneno = (\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4};       if(inputtxt.value.match(phoneno)){        document.getElementById("yourEntry").innerHTML = document.getElementById("myphone").value;    }    //if(!inputtxt.value.match(phoneno)){        // alert("Does not Work!")        //}    } </script> </head> <body> <form name="form1" action="#"> <input type="text" name="myphone" id="myphone" value="" pattern= "(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}" placeholder="(555) 555-1212" required /> <input...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest.  The highest average should then be on the last row.. When you sort...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
How would I add a quickSort function to the below C++ code to sort the randomly...
How would I add a quickSort function to the below C++ code to sort the randomly generated numbers? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int i; int array[10]; int odd; int Max; int counter = 0; int main() { cout << "The 10 random elements are: "; cout << endl; srand ( time(0) ); for (int j = 0; j < 99; j++) { i = rand() % 100; if (i != i - 1) array[j] =...
(Python Code please) Guess the number! You will add to the program you created last week....
(Python Code please) Guess the number! You will add to the program you created last week. This week you will add quite a bit of code to your project. You will add an option for the computer to guess as well as the user to guess a number. In addition, you will add a menu system. Be sure to import random at the beginning of your code and use a comment block explaining what your program does #Guess the number...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please (in your...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please (in your own words)
HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE # DEFINE 'all_keywords',...
HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE # DEFINE 'all_keywords', A LIST COMPRISED OF ALL OUR NEGATIVE SEARCH KEYWORDS AS REQUIRED BY THE PROJECT # FOR EACH ELEMENT IN 'full_list' (THE LIST OF LISTS YOU WOULD HAVE CREATD ABOVE THE PREVIOUS LINE) # INITIALIZE THE SET 'detected_keywords' TO EMPTY # DEFINE VARIABLE 'current_reviewer' FROM CURRENT ELEMENT OF 'full_list' BY EXTRACTING ITS FIRST SUB-ELEMENT,... # ...CONVERTING IT TO A STRING, AND THEN STRIPPING THE SUBSTRING...
(Python) How would I add this input into my code? "Enter Y for Yes or N...
(Python) How would I add this input into my code? "Enter Y for Yes or N for No:" as a loop. def getMat(): mat = np.zeros((3, 3)) print("Enter your first 3 by 3 matrix:") for row in range(3): li = [int(x) for x in input().split()] mat[row,0] = li[0] mat[row,1] = li[1] mat[row,2] = li[2] print("Your first 3 by 3 matrix is :") for i in range(3): for k in range(3): print(str(int(mat[i][k]))+" ",end="") print() return mat def checkEntry(inputValue): try: float(inputValue) except...
While taking into account different dimensions of strategy diamond, would you prefer to make any change...
While taking into account different dimensions of strategy diamond, would you prefer to make any change to the model? Would you add or delete any dimension? Why or why not? Provide the rationale while supporting your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT