{ "cells": [ { "cell_type": "markdown", "id": "5acbe764", "metadata": {}, "source": [ "Describe the following code. Create a statment for a possible exercise." ] }, { "cell_type": "code", "execution_count": null, "id": "6be201a9", "metadata": {}, "outputs": [], "source": [ "import random\n", "from typing import List\n", "\n", "# Define Strategy class to encapsulate different playing strategies\n", "class Strategy:\n", " def __init__(self, name: str, offensive: int, defensive: int):\n", " self.name = name\n", " self.offensive = offensive # Offensive strength factor\n", " self.defensive = defensive # Defensive strength factor\n", "\n", " def __str__(self):\n", " return self.name\n", "\n", "# Define Team class with a strategy and methods to simulate playing and scoring\n", "class Team:\n", " def __init__(self, name: str, strategy: Strategy):\n", " self.name = name\n", " self.strategy = strategy\n", " self.goals_scored = 0\n", "\n", " def attempt_attack(self, opponent_defense: int):\n", " # Lower probability of scoring to keep total goals typically below 5\n", " attack_success = random.randint(0, 100) < self.strategy.offensive * 0.3 # 30% of original offensive strength\n", " defense_success = random.randint(0, 100) < opponent_defense * 0.3 # 30% of opponent's defensive strength\n", " return attack_success and not defense_success\n", "\n", " def __str__(self):\n", " return f\"{self.name} ({self.strategy})\"\n", "\n", "# Define Game class to manage the teams, simulate game time, and determine the winner\n", "class Game:\n", " def __init__(self, team1: Team, team2: Team, duration: int = 90):\n", " self.team1 = team1\n", " self.team2 = team2\n", " self.duration = duration # Total game time in minutes\n", "\n", " def play_game(self):\n", " print(f\"Starting game between {self.team1} and {self.team2}!\\n\")\n", "\n", " for minute in range(1, self.duration + 1):\n", " # Half-time break at 45 minutes\n", " if minute == 45:\n", " print(\"Half-time break!\\n\")\n", " continue\n", "\n", " # Alternate attacks between the two teams\n", " attacking_team, defending_team = (self.team1, self.team2) if minute % 2 == 1 else (self.team2, self.team1)\n", "\n", " # Attacking team attempts to score\n", " if attacking_team.attempt_attack(defending_team.strategy.defensive):\n", " attacking_team.goals_scored += 1\n", " print(f\"{attacking_team.name} scores a goal in minute {minute}!\")\n", "\n", " # Stop the game early if total goals reach 5\n", " if self.team1.goals_scored + self.team2.goals_scored >= 5:\n", " print(\"Reached maximum goal limit for a realistic game.\\n\")\n", " break\n", "\n", " # Print current score every 15 minutes for update\n", " if minute % 2 == 0:\n", " print(f\"Score at minute {minute}: {self.team1.name} {self.team1.goals_scored} - {self.team2.goals_scored} {self.team2.name}\")\n", "\n", " # Determine and print the final result\n", " print(f\"\\nFull-time score: {self.team1.name} {self.team1.goals_scored} - {self.team2.goals_scored} {self.team2.name}\")\n", " if self.team1.goals_scored > self.team2.goals_scored:\n", " print(f\"{self.team1.name} wins the game!\")\n", " elif self.team2.goals_scored > self.team1.goals_scored:\n", " print(f\"{self.team2.name} wins the game!\")\n", " else:\n", " print(\"The game ends in a draw!\")\n", "\n", "# Define some strategies with different offensive and defensive strengths\n", "defensive_strategy = Strategy(\"Defensive\", offensive=30, defensive=70)\n", "offensive_strategy = Strategy(\"Offensive\", offensive=70, defensive=30)\n", "balanced_strategy = Strategy(\"Balanced\", offensive=50, defensive=50)\n", "\n", "# Create teams with specific strategies\n", "team1 = Team(\"Team A\", defensive_strategy)\n", "team2 = Team(\"Team B\", defensive_strategy)\n", "\n", "# Play the game\n", "soccer_game = Game(team1, team2)\n", "soccer_game.play_game()\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }