0% found this document useful (0 votes)
5 views

Game App Algorithm

Uploaded by

Asjad Bahoor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Game App Algorithm

Uploaded by

Asjad Bahoor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#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: " << team.name << endl;

cout << "Players: " << team.players[0] << ", " << team.players[1] << ", " << team.players[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 = battingTeam.players[batsmanIndex];

currentBowler = bowlingTeam.players[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 << teamA.name << " scored " << teamA.totalRuns << " runs." << endl;

cout << teamB.name << " scored " << teamB.totalRuns << " runs." << endl;

if (teamA.totalRuns > teamB.totalRuns) {

cout << teamA.name << " wins!" << endl;

} else if (teamB.totalRuns > teamA.totalRuns) {

cout << teamB.name << " 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: " << TeamA.name << " bats" << endl;

selectBatsmanAndBowler(TeamA, TeamB);

TeamA.totalRuns = playInning(TeamA, TeamB);

displayInningRuns(TeamA.totalRuns, TeamA.name);

// Second inning: TeamB bats and TeamA bowls

cout << "\nInning 2: " << TeamB.name << " bats" << endl;

selectBatsmanAndBowler(TeamB, TeamA);

TeamB.totalRuns = playInning(TeamB, TeamA);

displayInningRuns(TeamB.totalRuns, TeamB.name);
// Decide the winner

decideWinner(TeamA, TeamB);

return 0;

You might also like