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...
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!
(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)
a. Would you expect companies that adopt ABC to add value to shareholders? How would you...
a. Would you expect companies that adopt ABC to add value to shareholders? How would you measure such added value? b. Accountants need to identify four steps in activity-based costing. What are the four steps?
This is done by using Angular in Visual code: Can you please give examples of the...
This is done by using Angular in Visual code: Can you please give examples of the following? 1. Use Math.random function to generate a number between 20 and 100 and then use the ngSwitch directive to display a letter grade based on this class grading policy. a. add implements OnInit to the AppComponent class b. add variable x in the AppComponent class c. add ngOnInit(){ this.x = Math.floor(Math.random()*10);} d. add {{x}} in the app.components.html file e. You should see numbers...
When would you use standard precautions over sterile precautions while taking care of your patient and...
When would you use standard precautions over sterile precautions while taking care of your patient and why?
(**I would like Australian examples please. Please provide examples from Australia .) You need to demonstrate...
(**I would like Australian examples please. Please provide examples from Australia .) You need to demonstrate that you can use statistical data to interpret market trends and developments. (**I would like Australian examples please. Please provide examples from Australia .) For a specific organisation you must: (Provide example from Australia) Develop a research plan that will enable you to gather both quantitative and qualitative data about internal and external conditions that affect the market and your organisation’s product/ service positioning...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT