From d9b6e803d5f8f71844a16d787cb737cc29dfaf73 Mon Sep 17 00:00:00 2001 From: Anushka Saxena Date: Fri, 5 Jul 2024 00:01:39 +0530 Subject: [PATCH] AI Opponent --- .../AI Opponent -checkpoint.ipynb | 216 ++++++++++++++++++ AI Opponent .ipynb | 216 ++++++++++++++++++ 2 files changed, 432 insertions(+) create mode 100644 .ipynb_checkpoints/AI Opponent -checkpoint.ipynb create mode 100644 AI Opponent .ipynb diff --git a/.ipynb_checkpoints/AI Opponent -checkpoint.ipynb b/.ipynb_checkpoints/AI Opponent -checkpoint.ipynb new file mode 100644 index 0000000..73bb296 --- /dev/null +++ b/.ipynb_checkpoints/AI Opponent -checkpoint.ipynb @@ -0,0 +1,216 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "101d4f7a", + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'pygame'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpygame\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msys\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Initialize pygame\u001b[39;00m\n", + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pygame'" + ] + } + ], + "source": [ + "import pygame\n", + "import sys\n", + "\n", + "# Initialize pygame\n", + "pygame.init()\n", + "\n", + "# Screen dimensions\n", + "WIDTH, HEIGHT = 600, 600\n", + "ROWS, COLS = 4, 4\n", + "BOX_SIZE = WIDTH // (COLS + 1)\n", + "\n", + "# Colors\n", + "WHITE = (255, 255, 255)\n", + "BLACK = (0, 0, 0)\n", + "BLUE = (0, 0, 255)\n", + "RED = (255, 0, 0)\n", + "\n", + "# Set up the display\n", + "screen = pygame.display.set_mode((WIDTH, HEIGHT))\n", + "pygame.display.set_caption(\"Dot and Boxes\")\n", + "\n", + "# Game state\n", + "lines = []\n", + "boxes = []\n", + "player_turn = 1\n", + "scores = {1: 0, 2: 0}\n", + "\n", + "# AI Difficulty Levels\n", + "EASY, MEDIUM, HARD = 1, 2, 3\n", + "ai_difficulty = MEDIUM # Change this to EASY or HARD for different difficulties\n", + "\n", + "def draw_grid():\n", + " for row in range(ROWS + 1):\n", + " for col in range(COLS + 1):\n", + " pygame.draw.circle(screen, BLACK, (col * BOX_SIZE, row * BOX_SIZE), 5)\n", + " if row < ROWS and col < COLS:\n", + " pygame.draw.rect(screen, WHITE, (col * BOX_SIZE + 5, row * BOX_SIZE + 5, BOX_SIZE - 10, BOX_SIZE - 10))\n", + "\n", + "def draw_lines():\n", + " for line in lines:\n", + " pygame.draw.line(screen, BLUE if line[2] == 1 else RED, line[0], line[1], 5)\n", + "\n", + "def check_boxes():\n", + " global scores\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " if top in lines and right in lines and bottom in lines and left in lines and (row, col) not in boxes:\n", + " boxes.append((row, col))\n", + " scores[player_turn] += 1\n", + "\n", + "def draw_boxes():\n", + " for box in boxes:\n", + " row, col = box\n", + " pygame.draw.rect(screen, BLUE if player_turn == 1 else RED, (col * BOX_SIZE + 5, row * BOX_SIZE + 5, BOX_SIZE - 10, BOX_SIZE - 10))\n", + "\n", + "def ai_move():\n", + " global player_turn\n", + " if ai_difficulty == EASY:\n", + " easy_ai_move()\n", + " elif ai_difficulty == MEDIUM:\n", + " medium_ai_move()\n", + " else:\n", + " hard_ai_move()\n", + " player_turn = 1 if player_turn == 2 else 2\n", + "\n", + "def easy_ai_move():\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " possible_moves = [\n", + " ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE)),\n", + " (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE)),\n", + " ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE)),\n", + " ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " ]\n", + " for move in possible_moves:\n", + " if move not in lines and (move[1], move[0]) not in lines:\n", + " lines.append((*move, 2))\n", + " return\n", + "\n", + "def medium_ai_move():\n", + " # AI tries to complete a box\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " sides = [top, right, bottom, left]\n", + " filled_sides = [side for side in sides if side in lines or (side[1], side[0]) in lines]\n", + " if len(filled_sides) == 3:\n", + " for side in sides:\n", + " if side not in lines and (side[1], side[0]) not in lines:\n", + " lines.append((*side, 2))\n", + " return\n", + " easy_ai_move()\n", + "\n", + "def hard_ai_move():\n", + " # AI avoids creating a box for the opponent\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " sides = [top, right, bottom, left]\n", + " filled_sides = [side for side in sides if side in lines or (side[1], side[0]) in lines]\n", + " if len(filled_sides) == 3:\n", + " for side in sides:\n", + " if side not in lines and (side[1], side[0]) not in lines:\n", + " lines.append((*side, 2))\n", + " return\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " sides = [top, right, bottom, left]\n", + " filled_sides = [side for side in sides if side in lines or (side[1], side[0]) in lines]\n", + " if len(filled_sides) == 2:\n", + " for side in sides:\n", + " if side not in lines and (side[1], side[0]) not in lines:\n", + " lines.append((*side, 2))\n", + " return\n", + " easy_ai_move()\n", + "\n", + "# Main game loop\n", + "running = True\n", + "while running:\n", + " for event in pygame.event.get():\n", + " if event.type == pygame.QUIT:\n", + " running = False\n", + " if event.type == pygame.KEYDOWN:\n", + " if event.key == pygame.K_r:\n", + " # Reset the game\n", + " lines.clear()\n", + " boxes.clear()\n", + " scores = {1: 0, 2: 0}\n", + " player_turn = 1\n", + " if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:\n", + " running = False\n", + "\n", + " if player_turn == 2:\n", + " ai_move()\n", + " else:\n", + " # Handle player input (e.g., mouse clicks or key presses)\n", + " pass # Placeholder for player input handling\n", + "\n", + " check_boxes()\n", + "\n", + " screen.fill(WHITE)\n", + " draw_grid()\n", + " draw_lines()\n", + " draw_boxes()\n", + " pygame.display.flip()\n", + "\n", + "pygame.quit()\n", + "sys.exit()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b23e459", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/AI Opponent .ipynb b/AI Opponent .ipynb new file mode 100644 index 0000000..3416c36 --- /dev/null +++ b/AI Opponent .ipynb @@ -0,0 +1,216 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "101d4f7a", + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'pygame'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpygame\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msys\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Initialize pygame\u001b[39;00m\n", + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pygame'" + ] + } + ], + "source": [ + "import pygame\n", + "import sys\n", + "\n", + "# Initialize pygame\n", + "pygame.init()\n", + "\n", + "# Screen dimensions\n", + "WIDTH, HEIGHT = 600, 600\n", + "ROWS, COLS = 4, 4\n", + "BOX_SIZE = WIDTH // (COLS + 1)\n", + "\n", + "# Colors\n", + "WHITE = (255, 255, 255)\n", + "BLACK = (0, 0, 0)\n", + "BLUE = (0, 0, 255)\n", + "RED = (255, 0, 0)\n", + "\n", + "# Set up the display\n", + "screen = pygame.display.set_mode((WIDTH, HEIGHT))\n", + "pygame.display.set_caption(\"Dot and Boxes\")\n", + "\n", + "# Game state\n", + "lines = []\n", + "boxes = []\n", + "player_turn = 1\n", + "scores = {1: 0, 2: 0}\n", + "\n", + "# AI Difficulty Levels\n", + "EASY, MEDIUM, HARD = 1, 2, 3\n", + "ai_difficulty = MEDIUM # Change this to EASY or HARD for different difficulties\n", + "\n", + "def draw_grid():\n", + " for row in range(ROWS + 1):\n", + " for col in range(COLS + 1):\n", + " pygame.draw.circle(screen, BLACK, (col * BOX_SIZE, row * BOX_SIZE), 5)\n", + " if row < ROWS and col < COLS:\n", + " pygame.draw.rect(screen, WHITE, (col * BOX_SIZE + 5, row * BOX_SIZE + 5, BOX_SIZE - 10, BOX_SIZE - 10))\n", + "\n", + "def draw_lines():\n", + " for line in lines:\n", + " pygame.draw.line(screen, BLUE if line[2] == 1 else RED, line[0], line[1], 5)\n", + "\n", + "def check_boxes():\n", + " global scores\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " if top in lines and right in lines and bottom in lines and left in lines and (row, col) not in boxes:\n", + " boxes.append((row, col))\n", + " scores[player_turn] += 1\n", + "\n", + "def draw_boxes():\n", + " for box in boxes:\n", + " row, col = box\n", + " pygame.draw.rect(screen, BLUE if player_turn == 1 else RED, (col * BOX_SIZE + 5, row * BOX_SIZE + 5, BOX_SIZE - 10, BOX_SIZE - 10))\n", + "\n", + "def ai_move():\n", + " global player_turn\n", + " if ai_difficulty == EASY:\n", + " easy_ai_move()\n", + " elif ai_difficulty == MEDIUM:\n", + " medium_ai_move()\n", + " else:\n", + " hard_ai_move()\n", + " player_turn = 1 if player_turn == 2 else 2\n", + "\n", + "def easy_ai_move():\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " possible_moves = [\n", + " ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE)),\n", + " (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE)),\n", + " ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE)),\n", + " ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " ]\n", + " for move in possible_moves:\n", + " if move not in lines and (move[1], move[0]) not in lines:\n", + " lines.append((*move, 2))\n", + " return\n", + "\n", + "def medium_ai_move():\n", + " # AI tries to complete a box\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " sides = [top, right, bottom, left]\n", + " filled_sides = [side for side in sides if side in lines or (side[1], side[0]) in lines]\n", + " if len(filled_sides) == 3:\n", + " for side in sides:\n", + " if side not in lines and (side[1], side[0]) not in lines:\n", + " lines.append((*side, 2))\n", + " return\n", + " easy_ai_move()\n", + "\n", + "def hard_ai_move():\n", + " # AI avoids creating a box for the opponent\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " sides = [top, right, bottom, left]\n", + " filled_sides = [side for side in sides if side in lines or (side[1], side[0]) in lines]\n", + " if len(filled_sides) == 3:\n", + " for side in sides:\n", + " if side not in lines and (side[1], side[0]) not in lines:\n", + " lines.append((*side, 2))\n", + " return\n", + " for row in range(ROWS):\n", + " for col in range(COLS):\n", + " top = ((col * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, row * BOX_SIZE))\n", + " right = (((col + 1) * BOX_SIZE, row * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " bottom = ((col * BOX_SIZE, (row + 1) * BOX_SIZE), ((col + 1) * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " left = ((col * BOX_SIZE, row * BOX_SIZE), (col * BOX_SIZE, (row + 1) * BOX_SIZE))\n", + " sides = [top, right, bottom, left]\n", + " filled_sides = [side for side in sides if side in lines or (side[1], side[0]) in lines]\n", + " if len(filled_sides) == 2:\n", + " for side in sides:\n", + " if side not in lines and (side[1], side[0]) not in lines:\n", + " lines.append((*side, 2))\n", + " return\n", + " easy_ai_move()\n", + "\n", + "# Main game loop\n", + "running = True\n", + "while running:\n", + " for event in pygame.event.get():\n", + " if event.type == pygame.QUIT:\n", + " running = False\n", + " if event.type == pygame.KEYDOWN:\n", + " if event.key == pygame.K_r:\n", + " # Reset the game\n", + " lines.clear()\n", + " boxes.clear()\n", + " scores = {1: 0, 2: 0}\n", + " player_turn = 1\n", + " if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:\n", + " running = False\n", + "\n", + " if player_turn == 2:\n", + " ai_move()\n", + " else:\n", + " # Handle player input (e.g., mouse clicks or key presses)\n", + " pass # Placeholder for player input handling\n", + "\n", + " check_boxes()\n", + "\n", + " screen.fill(WHITE)\n", + " draw_grid()\n", + " draw_lines()\n", + " draw_boxes()\n", + " pygame.display.flip()\n", + "\n", + "pygame.quit()\n", + "sys.exit()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b23e459", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}