Thursday, October 12, 2023

EM-Tirupati Codeathon Series #08

[Question]

ROBOTIC CRICKET MATCH

you should write a program that simulate an automatic cricket match between India and Sri Lanka. The focus is the usage of Java Thread Constructs. Each Innings happens within its thread. You decide the 11 members of each team and provide two lists to the program. The first team is represented as team 1 and second team as team 2. You have to generate a random number for toss and if the number is between 0 and 1, team 1 bats first- else, if the number is between 1 and 2, then team 2 bats first. The batting happens by starting a thread. Each ball bowled is via a random number generator (that generates number rounded off, between 0 and 7). If the number is 0, batsman is out. If the number is 1, run is 1, number is 2, run is 2, number is 3, run is 3, number is 4, run is 4, number is 5, run is 5, number is 6, run is 6. If the number is exactly 7, then it is an extra 1 run and if it is 0, batsman is out). You have to do this until 10 batsmen get out in 1 team. The final score sheet will show as follows. Then you have to start another thread and do the same as above. The match ends either when the second team batsmen are out, with scores lesser than team 1 (team 1 wins), scores are tied (match is tied) and as soon score is greater than team 1 (team 2 wins). Each over is for 6 balls, if extra happens (number 7), 1 run is awarded and 1 extra ball is bowled. Extra run and Extra Ball is not part of batsman’s account. Total Overs are 10. (T101 Match)

(No Need to Take Care of Changing Batsman Strike If Score is 1, 3 or 5. No Need to Record Bowling Figures of the Bowler in the Solution. No Need to Take Care Separately of Wide, No Balls, Byes, Leg Byes. There are No Free-Hits!).

[Sample Input]

(No Input is Required)

[Sample Output]

Toss Won By SL (Team 0)

Team 0 (SL) is Batting First

India-Batting Scoresheet

Rohit Sharma 1,4,3,6,1,0=15 (6)

Shubman Gill 0=0 (1)

Virat Kohli 4,6,1,0=11 (4)

KL Rahul 3,1,4,0=8 (4)

Ishan Kishan 6,6,6,4,0 = 22 (5)

Hardik Pandya 0 = 0 (1)

Ravindra Jadeja 6,0 = 6 (2)

Washington Sundar 1,3,0 = 4 (3)

Kuldeep Yadav 1,2,3,4,0=10 (5)

Mohammed Siraj 0 = 0 (1)

Jasprit Bumrah Did Not Bat

Extras 1, 1, 1, 1, 1, 5

Total Score 81 in 5.2 overs

Sri Lanka — Batting Scoresheet

Pathum Nissanka 0=0(1)

Kusal Perera 0=0 (1)

Kusal Mendis 1,0=1(2)

Sadeera Samarawickrama 1,1,0=2 (3)

Charith Asalanka 2,0=2(2)

Dhananjaya de Silva 4,4,0 = 8 (3)

Dasun Shanaka © 1,4,6,0=11 (4)

Dunith Wellalage 6,6,0=12 (3)

Dushan Hemantha 0=0 (1)

Pramod Madushan 1,0=1(2)

Matheesha Pathirana Did Not Bat

Extras 1, 1=2

Total Score 39 in 3.4 overs

Match Result: Team 0 (India) Won By 42 Runs

Today’s Date: 17/09/2023

Areas : Core Java, Logic, Longer Problem Solving, Understanding Requirements, Java Multi- Threading, Java Async Execution, Java Collections.

Code Explaination:

Main Class and Initialization:

  • The Question08_Venkata class contains the main method, serving as the program's entry point.
  • Two arrays, team1Players and team2Players, are created to represent the players in each team.

Team Class:

  • Represents a cricket team, encapsulating data and actions.
  • Contains instance variables for team name, players, and total score.
  • The constructor initializes the team's name and player roster.
  • Provides methods to get the team's name and total score.
  • playInnings simulates the batting innings for each player in the team.
  • Calculates and displays total score, overs, and extras.

Player Class:

  • Represents an individual player in the team.
  • Stores player's name, total runs, extras, balls bowled, and runs per ball.
  • The constructor initializes the player with a name.
  • Offers methods to get player-specific statistics.
  • playInnings simulates the player's innings, generating runs, extras, and calculating balls bowled.
  • displayScore shows the player's detailed performance.

Main Method:

  • Creates two teams, team1 and team2, with their player arrays.
  • Simulates the toss using a random number and lets the winning team choose to bat first.
  • Calls the playMatch method twice, simulating both teams' innings.
  • Determines the match result based on total scores.
  • Prints the match result and date to the console.

The code models a cricket match using Java classes, providing detailed player scores and following SOLID principles for clean and structured code.

GitHub Repo link: http://surl.li/lyiqv  

Source Code(Java):

import java.util.Date;

public class Question08_Venkata {
   public static void main(String[] args) {
      String[] team1Players = {
            "Rohit Sharma", "Shubman Gill", "Virat Kohli", "KL Rahul", "Ishan Kishan",
            "Hardik Pandya", "Ravindra Jadeja", "Washington Sundar", "Kuldeep Yadav",
            "Mohammed Siraj", "Jasprit Bumrah"
      };

      String[] team2Players = {
            "Pathum Nissanka", "Kusal Perera", "Kusal Mendis", "Sadeera Samarawickrama",
            "Charith Asalanka", "Dhananjaya de Silva", "Dasun Shanaka", "Dunith Wellalage",
            "Dushan Hemantha", "Pramod Madushan", "Matheesha Pathirana"
      };

      Team team1 = new Team("India", team1Players);
      Team team2 = new Team("Sri Lanka", team2Players);

      // Simulate the toss
      double toss = Math.random() * 10;
      if (toss < 1) {
         System.out.println("Toss Won By SL (Team 0)");
         playMatch(team2, team1);
      } else {
         System.out.println("Toss Won By India (Team 1)");
         playMatch(team1, team2);
      }
   }

   private static void playMatch(Team battingTeam, Team bowlingTeam) {
      System.out.println(battingTeam.getName() + " - Batting Scoresheet");
      battingTeam.playInnings();

      System.out.println("\n" + bowlingTeam.getName() + " — Batting Scoresheet");
      bowlingTeam.playInnings();

      String matchResult = battingTeam.getTotalScore() > bowlingTeam.getTotalScore()
            ? battingTeam.getName()
            : (battingTeam.getTotalScore() < bowlingTeam.getTotalScore() ? bowlingTeam.getName() : "Match Tied");
      int runDifference = Math.abs(battingTeam.getTotalScore() - bowlingTeam.getTotalScore());
      System.out.println("\nMatch Result: " + matchResult + " Won By " + runDifference + " Runs");
      System.out.println("Today's Date: " + new Date());
   }
}

class Team {
   private String name;
   private String[] players;
   private int totalScore;

   public Team(String name, String[] players) {
      this.name = name;
      this.players = players;
      this.totalScore = 0;
   }

   public String getName() {
      return name;
   }

   public int getTotalScore() {
      return totalScore;
   }

   public void playInnings() {
      int totalBalls = 0;
      int totalExtras = 0;
      for (String player : players) {
         Player p = new Player(player);
         p.playInnings();
         totalScore += p.getTotalRuns();
         totalBalls += p.getBallsBowled();
         totalExtras += p.getExtras();
         p.displayScore();
      }

      double totalOvers = totalBalls / 6;
      System.out.println("Extras " + totalExtras);
      System.out.println("Total Score " + totalScore + " in " + totalOvers + " overs");
   }
}

class Player {
   private String playerName;
   private int totalRuns;
   private int extras;
   private int ballsBowled;
   private int[] runsPerBall;

   public Player(String playerName) {
      this.playerName = playerName;
      this.totalRuns = 0;
      this.extras = 0;
      this.ballsBowled = 0;
      this.runsPerBall = new int[60];
   }

   public int getTotalRuns() {
      return totalRuns;
   }

   public int getExtras() {
      return extras;
   }

   public int getBallsBowled() {
      return ballsBowled;
   }

   public void playInnings() {
      int ballsBowled = 0;
      int extras = 0;
      int totalRuns = 0;

      for (int i = 0; i < 60; i++) {
         int run = (int) (Math.random() * 10) % 8;
         ballsBowled++;
         totalRuns += run;

         if (run == 7) {
            extras++;
            run = 1;  // Convert a 7 into a 1
         } else if (run == 0) {
            break;
         }

         runsPerBall[i] = run;
      }

      this.ballsBowled = ballsBowled;
      this.extras = extras;
      this.totalRuns = totalRuns;
   }

   public void displayScore() {
      System.out.print(playerName + " " + ballsBowled + " (");
      for (int i = 0; i < runsPerBall.length; i++) {
         System.out.print(runsPerBall[i]);
         if (runsPerBall[i] == 0) {
            break;
         }
         System.out.print(", ");
      }
      System.out.println(") = " + totalRuns + " (" + extras + " extra)");
   }
}


Thank you

Venkata kishore T(Intern)

Shield Warriors,

Data Shield Team,

Enterprise Minds.

No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...