In straight-11 ping-pong, the first player to reach 11 points wins; the
player need not be ahead by 2. Suppose that when Ada plays Blaise, Ada has
a .56 probability of winning any given point. We want to write a program
that simulates a game between Ada and Blaise, with the printout displaying
a running score of the game. For example a typical output might be:

  Game 1 

Ada Blaise

1 0
2 0
2 1
. .
. .
. .
11 8

          Ada won 11 to 8 

Write a program in which Ada and Blaise play three games of straight-11
ping-pong. Print the results of each game as well as the overall winner of
the three matches

i am having truble being able to set up a game like this if i could get an example

Dani AI

Generated

A simple, robust way to do this is to treat every point as an independent Bernoulli trial with P(Ada wins) = 0.56, run a loop until one player reaches 11 (straight-11 stops there—no 2-point rule), repeat that three times, and count match wins. This follows ’s loop hint and the randomness suggestions from and , but uses modern C++ random facilities (safer and clearer than C rand()).

#include <iostream>
#include <random>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::bernoulli_distribution adaWins(0.56);

    int adaMatchWins = 0, blaiseMatchWins = 0;
    for (int game = 1; game <= 3; ++game) {
        int ada = 0, blaise = 0;
        std::cout << "Game " << game << '\n';
        while (ada < 11 && blaise < 11) {
            if (adaWins(gen)) ++ada; else ++blaise;
            std::cout << ada << " " << blaise << '\n';
        }
        if (ada == 11) { ++adaMatchWins; std::cout << "Ada won " << ada << " to " << blaise << '\n'; }
        else           { ++blaiseMatchWins; std::cout << "Blaise won " << blaise << " to " << ada << '\n'; }
    }

    std::cout << "Match result: ";
    if (adaMatchWins > blaiseMatchWins) std::cout << "Ada wins " << adaMatchWins << " to " << blaiseMatchWins << '\n';
    else if (blaiseMatchWins > adaMatchWins) std::cout << "Blaise wins " << blaiseMatchWins << " to " << adaMatchWins << '\n';
    else std::cout << "Tie " << adaMatchWins << " each\n";
    return 0;
}

Notes and troubleshooting:

  • Compile with a C++11-or-later flag (for example g++ -std=c++11).
  • For repeatable debugging use a fixed seed (std::mt19937 gen(12345);) instead of std::random_device.
  • Avoid rand() % n because of bias and portability issues; std::bernoulli_distribution directly models the 0.56 probability.
  • Printing every point is fine for three games; if you simulate many trials, accumulate stats instead to avoid huge output.
  • If you run into off-by-one issues, check that your loop condition is while (ada < 11 && blaise < 11) so the loop stops as soon as someone reaches 11.

Recommended Answers

All 6 Replies

Alright firstly, this forumn the people are not going to do something for you if you aren't prepared to do something yourself... I will however try to help you in getting started...

First place... find what is expected of your program... is there any user input? or is it all automatic? (HINT: "Ada has a .56 probability of winning any given point.")

how do you know the program has completed its purpose? (HINT: "the first player to reach 11 points wins")

Now tell me what you understand under the above statements... attempt to write a bit of code... and I'll help you further

well i did not thnk what had so far was useful and i really am not looking some to d ths for here is what i have

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
include <iostream>
include <cstdlib>

using std::cout;
using std::rand;

int main()
{
int ada
int bliase
ada=0
blaise= 0
for( )// i know i need some type of function here but not sure what
if
ada= ada +1;
else
blaise = blaise+1;

return 0;
}
Emphasized Text Here
i am really not sue how to add in the .56 probablity thing

i forgot to add the end for when it equals 11

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
for (ada =10; ada>10;blaise =10; bliase>10;)
 {
for( )// i know i need some type of function here but not sure what
if
ada= ada +1;
else
blaise = blaise+1;
if (ada==11; blaise==11)
{
  cout << "game 1";
  break;

There is probably a mathematical way to account for the 56% probability, but you could also hard code it. To hard code it you could create and array of 100 ints all assigned to 0s. Then place 44 randomly placed 1s in the array. Then for each turn, get a random int called index from 0-99. If the value of the element of the game array at index is 0 Ada wins the point. Otherwise Blaise wins the point. If you want, reassign the random int array with each new game, but that probably isn't necessary since each play is random to begin with.

i am really not sue how to add in the .56 probablity thing

here's one way:
considering that you know how to use rand()
you could randomize a value from 0 to 99 and if it lands between 0-55 player ada wins else if it's 56 - 99 the other player wins

commented: agree with the function suggested 100% +0

As Zeroliken said rand() is your best bet...

I assume you are being tested on the random function because that is really all that makes sense to me...

If you were supposed to work with input that was another situation... either way...

"rand() % 10 + 1" gives you a value between 1 and 10...

the reason for the "+ 1" is that rand() starts at 0... now using what zeroliken said above... look if you can complete the function in your for (I would use a while btw)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.