Here's an advanced Java program for a game of cricket:
import java.util.Random;
import java.util.Scanner;
class Player {
private String name;
private int runs;
private int balls;
private int fours;
private int sixes;
private boolean isOut;
public Player(String name) {
this.name = name;
this.runs = 0;
this.balls = 0;
this.fours = 0;
this.sixes = 0;
this.isOut = false;
}
public void scoreRuns(int runs) {
this.runs += runs;
this.balls++;
if (runs == 4) {
this.fours++;
} else if (runs == 6) {
this.sixes++;
}
}
public void getOut() {
this.isOut = true;
}
public String getName() {
return name;
}
public int getRuns() {
return runs;
}
public int getBalls() {
return balls;
}
public int getFours() {
return fours;
}
public int getSixes() {
return sixes;
}
public boolean isOut() {
return isOut;
}
}
class Team {
private String name;
private int runs;
private int wickets;
private int overs;
private Player[] players;
public Team(String name, int overs) {
this.name = name;
this.runs = 0;
this.wickets = 0;
this.overs = overs;
this.players = new Player[11];
}
public void addPlayer(Player player, int index) {
this.players[index] = player;
}
public void scoreRuns(int runs) {
this.runs += runs;
}
public void loseWicket() {
this.wickets++;
}
public String getName() {
return name;
}
public int getRuns() {
return runs;
}
public int getWickets() {
return wickets;
}
public int getOvers() {
return overs;
}
public Player[] getPlayers() {
return players;
}
}
public class CricketGame {
private Team team1;
private Team team2;
private Random random;
private Scanner scanner;
public CricketGame(Team team1, Team team2) {
this.team1 = team1;
this.team2 = team2;
this.random = new Random();
this.scanner = new Scanner(System.in);
}
public void playGame() {
System.out.println("Team 1: " + team1.getName());
System.out.println("Team 2: " + team2.getName());
// Team 1 batting
System.out.println("\nTeam 1 batting:");
int overs = team1.getOvers();
for (int i = 0; i < overs; i++) {
System.out.println("\nOver " + (i + 1));
for (int j = 0; j < 6; j++) {
System.out.print("Ball " + (j + 1) + ": ");
String input = scanner.nextLine();
int runs = random.nextInt(7); // 0-6 runs
if (input.equalsIgnoreCase("wicket")) {
team1.loseWicket();
System.out.println("Wicket fallen!");
} else {
team1.scoreRuns(runs);
System.out.println(runs + " runs scored.");
}
}
}
System.out.println("\nTeam 1 scored " + team1.getRuns() + " runs with " + team1.getWickets() + " wickets.");
// Team 2 batting
System.out.println("\nTeam 2 batting:");
overs = team2.getOvers();
for (int i = 0; i < overs; i++) {
System.out.println("\nOver " + (i + 1));
for (int j = 0; j < 6; j++) {
System.out.print("Ball " + (j + 1) + ": ");
String input = scanner.nextLine();
int runs = random.nextInt(7); // 0-6 runs
if (input.equalsIgnoreCase("wicket")) {
team2.loseWicket();
System.out.println("Wicket fallen!");
} else {
team2.scoreRuns(runs);
System.out.println(runs + " runs scored.");
}
}
}
System.out.println("\nTeam 2 scored " + team2.getRuns() + " runs with " + team2.getWickets() + " wickets.");
// Determine winner
if (team1.getRuns() > team2.getRuns()) {
System.out.println("\nTeam 1 wins!");
} else if (team2.getRuns() > team1.getRuns()) {
System.out.println("\nTeam 2 wins!");
} else {
System.out.println("\nIt's a tie!");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Team 1 name: ");
String team1Name = scanner.nextLine();
System.out.print("Enter Team 2 name: ");
String team2Name = scanner.nextLine();
System.out.print("Enter number of overs: ");
int overs = scanner.nextInt();
Team team1 = new Team(team1Name, overs);
Team team2 = new Team(team2Name, overs);
// Add players to teams
for (int i = 0; i < 11; i++) {
System.out.print("Enter Player " + (i + 1) + " name for Team 1: ");
String playerName = scanner.next();
Player player = new Player(playerName);
team1.addPlayer(player, i);
}
for (int i = 0; i < 11; i++) {
System.out.print("Enter Player " + (i + 1) + " name for Team 2: ");
String playerName = scanner.next();
Player player = new Player(playerName);
team2.addPlayer(player, i);
}
CricketGame game = new CricketGame(team1, team2);
game.playGame();
}
}
This code creates a cricket game simulator where two teams play against each other. Each team has 11 players, and the game is played for a specified number of overs. The simulator allows the user to input the names of the teams and players, and then simulates the game, displaying the score and winner at the end.
No comments:
Post a Comment