Monday 9 October 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 (c) 1,4,6,0=11 (4)

DunithWellalage 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.

Points to Note

200 Marks will be Awarded to this Question. Pass Marks are at 120. You have to understand if you do not use any threading or async task execution in java, you will be awarded at the maximum 120- 140 marks, provided that your logic is working correctly. The Remaining 60-80 marks are given to showcase/prove your skills in Java Multi-Threading or Async Task Execution. You have to start the first thread for the first innings while the main program runs, then once the innings is complete, you have to make sure that the main program understands that. Similar way you have to start the second thread and note there might be shared objects or shared data to use. Please make sure you also understand/demonstrate thread safety.

[Explanation of Solution]

This Java program simulates a cricket match between India and Sri Lanka using multi-threading. It begins by randomly determining which team bats first and then runs the innings in separate threads.

Each ball bowled is generated with a random number from 0 to 7, where 0 means the batsman is out, 1 to 6 represent the runs scored, and 7 signifies an extra run. The match consists of two innings, and the program keeps track of batsmen, their scores, and extras.

The match result is determined based on the scores of both teams. If the second team's score is lower than the first team's, team 1 wins. If the scores are tied, it's a tie, and if the second team scores more, team 2 wins.

The program showcases multi-threading concepts to simulate the cricket match and provides a detailed scorecard at the end. 

[Solution (Java Code)] 

package codeathon;

import java.util.Date;

public class Codeathon08_vijay {
public static void main(String arg[])  {
String team1[] = {"Rohit Sharma", "Shubman Gill", "Virat Kohli", "KL Rahul", "Ishan Kishan",
                "Hardik Pandya", "Ravindra Jadeja", "Washington Sundar", "Kuldeep Yadav",
                "Mohammed Siraj", "Jasprit Bumrah"};
        String team2[] = {"Pathum Nissanka", "Kusal Perera", "Kusal Mendis", "Sadeera Samarawickrama",
                "Charith Asalanka", "Dhananjaya de Silva", "Dasun Shanaka", "Dunith Wellalage",
                "Dushan Hemantha", "Pramod Madushan", "Matheesha Pathirana"};
        Team india = new Team("India", team1);
        Team sriLanka = new Team("Sri Lanka", team2);
        Thread team1Thread = new Thread(india);
        Thread team2Thread = new Thread(sriLanka);
        double random = Math.random() * 10;
        double toss = random % 2;
        for(int i = 0; i < 2; i++) {
            if(toss > 0 && toss < 1) {
                if(i == 0) {
                    System.out.println("India Batting first");
                } else {
                    System.out.println("\nIndia Batting Second");
                }
                team1Thread.start();
                try {
                    team1Thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                toss = 1.5;
            } else {
                if(i == 0) {
                    System.out.println("Sri Lanka Batting first");
                } else {
                    System.out.println("\nSri Lanka Batting Second");
                }
                team2Thread.start();
                try {
                    team2Thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                toss = 0.5;
            }
        }
        if(india.totalScore > sriLanka.totalScore) {
            System.out.println("India Won By " + (india.totalScore - sriLanka.totalScore));
            System.out.println("Today date: = " + new Date());
        }
        if(sriLanka.totalScore > india.totalScore) {
            System.out.println("Sri Lanka Won By " + (sriLanka.totalScore - india.totalScore));
            System.out.println("Today date: = " + new Date());
        }
    }
}

class Team implements Runnable {
    String teamName;
    String playerName[];
    int totalScore = 0;
    int runsPerBall[];
    play players[];

    Team(String team, String player[]) {
        teamName = team;
        playerName = player;
        runsPerBall = new int[60];
        players = new play[11];
    }

    @Override
    public void run() {
        int ballsBowled = 0;
        int extras = 0;
        int totalRuns = 0;

        for (int i = 0; i < 11; i++) {
            players[i] = new play(teamName, playerName[i]);
            totalRuns = 0;
            int ballLength = 0;
            extras = 0;
            for (int j = 0; ballsBowled < 60; j++) {
                double random = Math.random() * 10;
                int runs = (int) random % 8;
                ballsBowled++;
                ballLength++;
                totalRuns += runs;
                if (runs == 7) {
                    extras++;
                    runs = 6;
                }
                runsPerBall[j] = runs;
                if (runs == 0) {
                    break;
                }
            }
            if (ballLength == 0) {
                players[i].count = new int[]{0, 0, 0};
            } else {
                players[i].count = runsPerBall;
            }
            players[i].ballsBowled = ballLength;
            players[i].extras = extras;
            players[i].totalRuns = totalRuns;
            m1(players[i]);
        }
        for (int i = 0; i < 10; i++) {
            totalScore = totalScore + players[i].totalRuns;
        }
        double overs = (double) ballsBowled / 6;
        System.out.println("\nTotal Score = " + totalScore + " total Overs = " + (int) (Math.round(overs * 10)) / 10.0);
    }

    void m1(play player) {
        System.out.print(player.playerName + " " + player.ballsBowled + "(");
        for (int i = 0; i < 60; i++) {
            System.out.print(player.count[i]);
            if (player.count[i] == 0) {
                break;
            } else {
                System.out.print(",");
            }
        }
        System.out.print(") = " + player.totalRuns + "  (total extra = " + player.extras + ")");
        System.out.println();
    }
}

class play {
    String teamName;
    String playerName;
    int ballsBowled;
    int extras;
    int[] count;
    int totalRuns;

    play(String team, String player) {
        teamName = team;
        playerName = player;
        count = new int[60];
    }
}

Thank you,

k.vijay(Intern)

vijay.keradhi@eminds.ai

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 focu...