0% found this document useful (0 votes)
9 views5 pages

Gully Cricket Game Simulation Code

Uploaded by

Asjad Bahoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Gully Cricket Game Simulation Code

Uploaded by

Asjad Bahoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <unistd.h> // For usleep()

using namespace std;

// Team class to store team details

class Team {

public:

string name;

string players[3]; // 3 players in each team

int totalRuns; // Total runs scored by the team

Team(string teamName, string p1, string p2, string p3) {

name = teamName;

players[0] = p1;

players[1] = p2;

players[2] = p3;

totalRuns = 0;

};

// Global variables to store current batsman and bowler

string currentBatsman;

string currentBowler;

// Function to greet the user

void greetUser() {
cout << "\nWelcome to the Gully Cricket App!" << endl;

cout << "Let's enjoy the game of cricket!" << endl;

usleep(1000000); // Wait for 1 second

// Function to display team details

void displayTeamDetails(Team &team) {

cout << "\nTeam: " << [Link] << endl;

cout << "Players: " << [Link][0] << ", " << [Link][1] << ", " << [Link][2] << endl;

// Function to randomly select a batsman and bowler for an inning

void selectBatsmanAndBowler(Team &battingTeam, Team &bowlingTeam) {

int batsmanIndex = rand() % 3;

int bowlerIndex = rand() % 3;

currentBatsman = [Link][batsmanIndex];

currentBowler = [Link][bowlerIndex];

cout << "\nBatsman: " << currentBatsman << " | Bowler: " << currentBowler << endl;

// Function to simulate a delivery (random score between 0 and 6)

int bowlDelivery() {

return rand() % 7; // Random score between 0 and 6

// Function to play an inning

int playInning(Team &battingTeam, Team &bowlingTeam) {

int runs = 0;
int balls = 6; // Each inning consists of 6 balls (one over)

for (int i = 0; i < balls; i++) {

int score = bowlDelivery(); // Random score for the ball

cout << "Ball " << (i + 1) << ": " << score << " runs" << endl;

runs += score;

usleep(500000); // Wait for 0.5 second between balls for better user experience

return runs;

// Function to display the runs scored by the batting team at the end of the inning

void displayInningRuns(int runs, string teamName) {

cout << teamName << " scored " << runs << " runs in this inning." << endl;

// Function to decide the winner

void decideWinner(Team &teamA, Team &teamB) {

cout << "\nMatch Over!" << endl;

cout << [Link] << " scored " << [Link] << " runs." << endl;

cout << [Link] << " scored " << [Link] << " runs." << endl;

if ([Link] > [Link]) {

cout << [Link] << " wins!" << endl;

} else if ([Link] > [Link]) {

cout << [Link] << " wins!" << endl;

} else {

cout << "It's a tie!" << endl;


}

int main() {

srand(time(0)); // Seed for random number generation

// Initialize two teams

Team TeamA("TeamA", "PlayerA1", "PlayerA2", "PlayerA3");

Team TeamB("TeamB", "PlayerB1", "PlayerB2", "PlayerB3");

// Greet the user

greetUser();

// Display team details

displayTeamDetails(TeamA);

displayTeamDetails(TeamB);

// First inning: TeamA bats and TeamB bowls

cout << "\nInning 1: " << [Link] << " bats" << endl;

selectBatsmanAndBowler(TeamA, TeamB);

[Link] = playInning(TeamA, TeamB);

displayInningRuns([Link], [Link]);

// Second inning: TeamB bats and TeamA bowls

cout << "\nInning 2: " << [Link] << " bats" << endl;

selectBatsmanAndBowler(TeamB, TeamA);

[Link] = playInning(TeamB, TeamA);

displayInningRuns([Link], [Link]);
// Decide the winner

decideWinner(TeamA, TeamB);

return 0;

You might also like