cric champ game

 import random

from enum import Enum

from dataclasses import dataclass

from typing import List, Dict, Optional


class PlayerRole(Enum):

    BATTER = "Batter"

    BOWLER = "Bowler"

    ALL_ROUNDER = "All-Rounder"

    WICKET_KEEPER = "Wicket Keeper"


class ShotType(Enum):

    DEFENSIVE = "Defensive"

    DRIVE = "Drive"

    CUT = "Cut"

    PULL = "Pull"

    HOOK = "Hook"

    SWEEP = "Sweep"

    REVERSE_SWEEP = "Reverse Sweep"

    LOFTED = "Lofted"


class DeliveryType(Enum):

    YORKER = "Yorker"

    BOUNCER = "Bouncer"

    FULL = "Full"

    GOOD_LENGTH = "Good Length"

    SHORT = "Short"

    SPIN = "Spin"

    DOOSRA = "Doosra"

    GOOGLY = "Googly"


class MatchType(Enum):

    TEST = "Test Match"

    ODI = "One Day International"

    T20 = "Twenty20"

    FANTASY = "Mythic League"


@dataclass

class Player:

    name: str

    role: PlayerRole

    batting_skill: int  # 1-100

    bowling_skill: int  # 1-100

    fielding: int  # 1-100

    stamina: int  # 1-100

    confidence: int  # 1-100

    temperament: int  # 1-100 (higher = more composed)

    special_ability: Optional[str] = None  # For fantasy mode

    

    def __str__(self):

        return f"{self.name} ({self.role.value})"


@dataclass

class Team:

    name: str

    players: List[Player]

    captain: Player

    morale: int = 50  # 1-100

    

    def get_batter(self) -> Player:

        return next(p for p in self.players if p.role in [PlayerRole.BATTER, PlayerRole.ALL_ROUNDER, PlayerRole.WICKET_KEEPER])

    

    def get_bowler(self) -> Player:

        return next(p for p in self.players if p.role in [PlayerRole.BOWLER, PlayerRole.ALL_ROUNDER])


class PitchCondition(Enum):

    GREEN = "Green - Helps fast bowlers"

    DRY = "Dry - Helps spinners"

    CRACKED = "Cracked - Unpredictable bounce"

    FLAT = "Flat - Good for batting"

    DAMP = "Damp - Helps swing bowling"


class WeatherCondition(Enum):

    SUNNY = "Sunny - Good for batting"

    CLOUDY = "Cloudy - Helps swing bowling"

    OVERCAST = "Overcast - Great for swing"

    RAINING = "Raining - Reduced visibility"

    HUMID = "Humid - Helps reverse swing"


class Match:

    def __init__(self, team1: Team, team2: Team, match_type: MatchType, overs: int = 50):

        self.team1 = team1

        self.team2 = team2

        self.match_type = match_type

        self.overs = overs

        self.current_over = 0

        self.current_ball = 0

        self.batting_team = team1

        self.bowling_team = team2

        self.runs = 0

        self.wickets = 0

        self.pitch = random.choice(list(PitchCondition))

        self.weather = random.choice(list(WeatherCondition))

        self.batter = self.batting_team.get_batter()

        self.bowler = self.bowling_team.get_bowler()

        self.commentary = []

        

    def simulate_ball(self):

        # Base probabilities

        bowler_advantage = self._get_bowler_advantage()

        batter_advantage = self._get_batter_advantage()

        

        # Determine possible outcomes

        outcomes = {

            "dot": 30 - bowler_advantage + batter_advantage,

            "1": 20,

            "2": 10,

            "3": 5,

            "4": 15 + batter_advantage,

            "6": 5 + batter_advantage,

            "wicket": 15 + bowler_advantage - batter_advantage

        }

        

        # Normalize probabilities

        total = sum(outcomes.values())

        normalized = {k: v/total for k, v in outcomes.items()}

        

        # Determine outcome

        rand = random.random()

        cumulative = 0

        outcome = None

        

        for k, v in normalized.items():

            cumulative += v

            if rand <= cumulative:

                outcome = k

                break

                

        # Handle outcome

        if outcome == "dot":

            self._add_commentary(f"{self.bowler} delivers a dot ball!")

        elif outcome == "wicket":

            self.wickets += 1

            self._add_commentary(f"OUT! {self.bowler} takes the wicket of {self.batter}!")

            self.batter = self.batting_team.get_batter()

        else:

            runs = int(outcome)

            self.runs += runs

            self._add_commentary(f"{self.batter} scores {runs} runs off {self.bowler}'s delivery!")

        

        # Update ball count

        self.current_ball += 1

        if self.current_ball >= 6:

            self.current_over += 1

            self.current_ball = 0

            # Switch bowler

            self.bowler = self.bowling_team.get_bowler()

            self._add_commentary(f"End of over {self.current_over}. Score: {self.runs}/{self.wickets}")

            

    def _get_bowler_advantage(self):

        advantage = self.bowler.bowling_skill / 20

        

        # Pitch effects

        if self.pitch == PitchCondition.GREEN and self.bowler.role == PlayerRole.BOWLER:

            advantage += 2

        elif self.pitch == PitchCondition.DRY and self.bowler.role == PlayerRole.ALL_ROUNDER:

            advantage += 1.5

            

        # Weather effects

        if self.weather == WeatherCondition.CLOUDY:

            advantage += 1

        elif self.weather == WeatherCondition.OVERCAST:

            advantage += 2

            

        return advantage

    

    def _get_batter_advantage(self):

        advantage = self.batter.batting_skill / 20

        

        # Pitch effects

        if self.pitch == PitchCondition.FLAT:

            advantage += 2

            

        # Weather effects

        if self.weather == WeatherCondition.SUNNY:

            advantage += 1

            

        # Confidence boost

        advantage += (self.batter.confidence - 50) / 50

        

        return advantage

    

    def _add_commentary(self, text):

        self.commentary.append(f"Over {self.current_over}.{self.current_ball}: {text}")

        print(self.commentary[-1])

    

    def play_match(self):

        print(f"\n=== {self.team1.name} vs {self.team2.name} ===")

        print(f"Match Type: {self.match_type.value}")

        print(f"Pitch: {self.pitch.value}")

        print(f"Weather: {self.weather.value}\n")

        

        while self.current_over < self.overs and self.wickets < 10:

            self.simulate_ball()

            

        print(f"\nFinal Score: {self.runs}/{self.wickets} in {self.current_over}.{self.current_ball} overs")


# Example usage

if __name__ == "__main__":

    # Create players

    virat = Player("Virat Kohli", PlayerRole.BATTER, 95, 30, 85, 90, 80, 75)

    bumrah = Player("Jasprit Bumrah", PlayerRole.BOWLER, 20, 95, 70, 85, 75, 80)

    jadeja = Player("Ravindra Jadeja", PlayerRole.ALL_ROUNDER, 75, 85, 95, 80, 70, 85)

    dhoni = Player("MS Dhoni", PlayerRole.WICKET_KEEPER, 85, 10, 90, 95, 95, 95)

    

    smith = Player("Steve Smith", PlayerRole.BATTER, 90, 50, 80, 85, 85, 90)

    starc = Player("Mitchell Starc", PlayerRole.BOWLER, 25, 90, 75, 80, 70, 75)

    maxwell = Player("Glenn Maxwell", PlayerRole.ALL_ROUNDER, 80, 70, 85, 75, 65, 70)

    paine = Player("Tim Paine", PlayerRole.WICKET_KEEPER, 70, 15, 85, 80, 75, 80)

    

    # Create teams

    india = Team("India", [virat, bumrah, jadeja, dhoni], dhoni, 80)

    australia = Team("Australia", [smith, starc, maxwell, paine], paine, 75)

    

    # Play match

    match = Match(india, australia, MatchType.ODI, 5)  # Short 5-over match for demo

    match.play_match()

Comments