How to resolve the algorithm Penney's game step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Penney's game step by step in the C++ programming language

Table of Contents

Problem Statement

Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin. It is common to agree on a sequence length of three then one player will openly choose a sequence, for example: The other player on seeing the first players choice will choose his sequence. The coin is tossed and the first player to see his sequence in the sequence of coin tosses wins.

One player might choose the sequence HHT and the other THT. Successive coin tosses of HTTHT gives the win to the second player as the last three coin tosses are his sequence.

Create a program that tosses the coin, keeps score and plays Penney's game against a human opponent.

Show output of a game where the computer chooses first and a game where the user goes first here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Penney's game step by step in the C++ programming language

The code is a simple game of "Penney Matching" between a human player and a computer. Here's a step-by-step explanation of the code:

Header Includes and Namespace Declaration:

#include <time.h>
#include <iostream>
#include <string>

using namespace std;

These lines include the necessary header files and declare that the code is using the standard C++ namespace.

penney Class:

The penney class represents the game and contains all the game logic and data.

Data Members:

  • playerChoice, computerChoice, sequence: Strings to store the player's choice, the computer's choice, and the sequence of H's and T's being tossed.
  • pW, cW: Integers to keep track of the number of wins for the player and the computer.

Methods:

  • gameLoop(): The main game loop that runs until the player chooses to stop playing.
  • computer(): Generates the computer's choice of sequence based on the player's previous choice or randomly if it's the first move.
  • player(): Reads the player's choice from the console.
  • play(): Simulates the game by randomly generating a sequence of H's and T's until either the player's choice or the computer's choice is found in the sequence.
  • showWinner(): Displays the winning player (player or computer) and increments the corresponding win counter.

main() Function:

  • srand(): Initializes the random number generator.
  • penney game;: Creates an instance of the penney class.
  • game.gameLoop();: Starts the game loop.

Game Flow:

  1. The game loop starts by initializing the player and computer choices to empty strings and randomly determining which player goes first.
  2. The player enters their choice of three consecutive H's or T's.
  3. The computer generates its choice based on the player's choice or randomly if it's the first move.
  4. The game randomly generates a sequence of H's and T's until either the player's choice or the computer's choice is found in the sequence.
  5. The winner is displayed, and the corresponding win counter is incremented.
  6. The player is prompted to play again. If they choose to continue, the loop starts again from step 1; otherwise, the loop exits.

Note: The game is not optimized for performance and may not handle all possible scenarios. It's a simplified version of the game designed to demonstrate the basic implementation of a simple game using C++.

Source code in the cpp programming language

#include <time.h>
#include <iostream>
#include <string>

using namespace std;

class penney
{
public:
    penney()
    { pW = cW = 0;  }
    void gameLoop()
    {
	string a;
	while( true )
	{
	    playerChoice = computerChoice = "";
	    if( rand() % 2 ) 
	    { computer(); player(); }
	    else
	    { player(); computer(); }

	    play();

	    cout << "[Y] to play again "; cin >> a;
	    if( a[0] != 'Y' && a[0] != 'y' ) 
	    {
		cout << "Computer won " << cW << " times." << endl << "Player won " << pW << " times.";
		break;
	    }
	    cout << endl << endl;
        }
    }
private:
    void computer()
    {
        if( playerChoice.length() == 0 )
	{
	    for( int x = 0; x < 3; x++ )
		computerChoice.append( ( rand() % 2 ) ? "H" : "T", 1 );	
	}
	else
	{
	    computerChoice.append( playerChoice[1] == 'T' ? "H" : "T", 1 );
	    computerChoice += playerChoice.substr( 0, 2 );
	}
	cout << "Computer's sequence of three is: " << computerChoice << endl;
    }
    void player()
    {
	cout << "Enter your sequence of three (H/T) "; cin >> playerChoice;
    }
    void play()
    {
	sequence = "";
	while( true )
	{
	    sequence.append( ( rand() % 2 ) ? "H" : "T", 1 );
	    if( sequence.find( playerChoice ) != sequence.npos )
	    {
		showWinner( 1 );
		break;
	    }
	    else if( sequence.find( computerChoice ) != sequence.npos )
	    {
		showWinner( 0 );
		break;
	    }
	}
    }
    void showWinner( int i )
    {
	string s;
	if( i ) { s = "Player wins!"; pW++; }
	else { s = "Computer wins!"; cW++; }
	cout << "Tossed sequence: " << sequence << endl << s << endl << endl;
    }
    string playerChoice, computerChoice, sequence;
    int pW, cW;
};
int main( int argc, char* argv[] )
{
    srand( static_cast<unsigned>( time( NULL ) ) );
    penney game; game.gameLoop();
    return 0;
}


  

You may also check:How to resolve the algorithm Circular primes step by step in the RPL programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Tcl programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Rust programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the FreeBASIC programming language