fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <unistd.h> // for getpid()
  5.  
  6. #define NUM_OVERS 5
  7. #define NUM_PLAYERS 11
  8. #define MAX_BALLS_PER_OVER 6
  9. #define RUNS_PER_WICKET 10
  10.  
  11. // Function to simulate a ball
  12. int simulateBall() {
  13. return rand() % 7; // Return a random number between 0 and 6 representing the runs scored
  14. }
  15.  
  16. // Function to simulate an innings
  17. int simulateInnings(int teamNumber) {
  18. int totalRuns = 0;
  19. int wickets = 0;
  20. int balls = 0;
  21.  
  22. printf("Team %d batting:\n", teamNumber);
  23.  
  24. // Simulate the innings
  25. while (balls < NUM_OVERS * MAX_BALLS_PER_OVER && wickets < NUM_PLAYERS) {
  26. printf("Press Enter to bowl...");
  27. getchar(); // Wait for user to press Enter
  28. int runs = simulateBall();
  29. balls++;
  30.  
  31. if (runs == 0) {
  32. printf("Out! Player %d dismissed.\n", wickets + 1);
  33. wickets++;
  34. } else {
  35. totalRuns += runs;
  36. printf("Runs scored: %d\n", runs);
  37. }
  38. }
  39.  
  40. printf("Innings complete. Total runs scored: %d for %d wickets.\n", totalRuns, wickets);
  41. return totalRuns;
  42. }
  43.  
  44. int main() {
  45. srand(time(NULL) ^ getpid()); // Seed the random number generator with time and process ID
  46.  
  47. printf("Welcome to the Cricket Game!\n");
  48.  
  49. // Simulate innings for team 1 and team 2
  50. int team1Runs = simulateInnings(1);
  51. int team2Runs = simulateInnings(2);
  52.  
  53. // Print the scores
  54. printf("Team 1 scored: %d\n", team1Runs);
  55. printf("Team 2 scored: %d\n", team2Runs);
  56.  
  57. // Determine the winner
  58. if (team1Runs > team2Runs) {
  59. printf("Team 1 wins!\n");
  60. } else if (team2Runs > team1Runs) {
  61. printf("Team 2 wins!\n");
  62. } else {
  63. printf("It's a tie!\n");
  64. }
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Welcome to the Cricket Game!
Team 1 batting:
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Out! Player 1 dismissed.
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 2
Press Enter to bowl...Runs scored: 2
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Out! Player 2 dismissed.
Press Enter to bowl...Out! Player 3 dismissed.
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 1
Press Enter to bowl...Out! Player 4 dismissed.
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 1
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Out! Player 5 dismissed.
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 1
Innings complete. Total runs scored: 94 for 5 wickets.
Team 2 batting:
Press Enter to bowl...Runs scored: 3
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 2
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Out! Player 1 dismissed.
Press Enter to bowl...Runs scored: 2
Press Enter to bowl...Out! Player 2 dismissed.
Press Enter to bowl...Runs scored: 1
Press Enter to bowl...Runs scored: 2
Press Enter to bowl...Runs scored: 1
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 1
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 1
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 4
Press Enter to bowl...Runs scored: 6
Press Enter to bowl...Runs scored: 2
Press Enter to bowl...Runs scored: 5
Press Enter to bowl...Runs scored: 6
Innings complete. Total runs scored: 112 for 2 wickets.
Team 1 scored: 94
Team 2 scored: 112
Team 2 wins!