|
| 1 | +import pygame |
| 2 | +import sys |
| 3 | + |
| 4 | +# Define the game window size |
| 5 | +WINDOW_WIDTH = 800 |
| 6 | +WINDOW_HEIGHT = 600 |
| 7 | + |
| 8 | +# Define colors |
| 9 | +WHITE = (255, 255, 255) |
| 10 | +BLACK = (0, 0, 0) |
| 11 | +RED = (255, 0, 0) |
| 12 | +GREEN = (0, 255, 0) |
| 13 | +BLUE = (0, 0, 255) |
| 14 | + |
| 15 | +# Define tile size and number of tiles |
| 16 | +TILE_SIZE = 20 |
| 17 | +NUM_TILES_X = WINDOW_WIDTH // TILE_SIZE |
| 18 | +NUM_TILES_Y = WINDOW_HEIGHT // TILE_SIZE |
| 19 | + |
| 20 | +# Define directions |
| 21 | +UP = (0, -1) |
| 22 | +DOWN = (0, 1) |
| 23 | +LEFT = (-1, 0) |
| 24 | +RIGHT = (1, 0) |
| 25 | + |
| 26 | + |
| 27 | +class TronGame: |
| 28 | + def __init__(self): |
| 29 | + pygame.init() |
| 30 | + self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) |
| 31 | + pygame.display.set_caption("AI-driven AI Tron") |
| 32 | + self.clock = pygame.time.Clock() |
| 33 | + self.running = True |
| 34 | + self.board = [[BLACK for _ in range(NUM_TILES_X)] for _ in range(NUM_TILES_Y)] |
| 35 | + self.player1_pos = (NUM_TILES_X // 4, NUM_TILES_Y // 2) |
| 36 | + self.player2_pos = (NUM_TILES_X * 3 // 4, NUM_TILES_Y // 2) |
| 37 | + self.player1_direction = RIGHT |
| 38 | + self.player2_direction = LEFT |
| 39 | + |
| 40 | + def draw_board(self): |
| 41 | + for y in range(NUM_TILES_Y): |
| 42 | + for x in range(NUM_TILES_X): |
| 43 | + pygame.draw.rect(self.screen, self.board[y][x], (x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)) |
| 44 | + |
| 45 | + def move_player(self, player_pos, player_direction, player_color): |
| 46 | + x, y = player_pos |
| 47 | + dx, dy = player_direction |
| 48 | + new_x, new_y = x + dx, y + dy |
| 49 | + |
| 50 | + if not (0 <= new_x < NUM_TILES_X) or not (0 <= new_y < NUM_TILES_Y): |
| 51 | + return False |
| 52 | + |
| 53 | + if self.board[new_y][new_x] == BLACK: |
| 54 | + self.board[new_y][new_x] = player_color |
| 55 | + return (new_x, new_y) |
| 56 | + return False |
| 57 | + |
| 58 | + def update(self): |
| 59 | + new_player1_pos = self.move_player(self.player1_pos, self.player1_direction, BLUE) |
| 60 | + new_player2_pos = self.move_player(self.player2_pos, self.player2_direction, RED) |
| 61 | + |
| 62 | + if not new_player1_pos or not new_player2_pos: |
| 63 | + self.running = False |
| 64 | + return |
| 65 | + |
| 66 | + self.player1_pos = new_player1_pos |
| 67 | + self.player2_pos = new_player2_pos |
| 68 | + |
| 69 | + def ai_move(self, player_pos, player_direction, opponent_pos): |
| 70 | + def is_valid_move(new_pos): |
| 71 | + x, y = new_pos |
| 72 | + return (0 <= x < NUM_TILES_X) and (0 <= y < NUM_TILES_Y) and self.board[y][x] == BLACK |
| 73 | + |
| 74 | + def simulate_move(pos, direction): |
| 75 | + x, y = pos |
| 76 | + dx, dy = direction |
| 77 | + return (x + dx, y + dy) |
| 78 | + |
| 79 | + def alpha_beta_search(pos, direction, opponent_pos, depth, alpha, beta, max_player): |
| 80 | + if depth == 0: |
| 81 | + return self.heuristic_evaluation(pos, opponent_pos) |
| 82 | + |
| 83 | + if max_player: |
| 84 | + value = -float('inf') |
| 85 | + for d in [UP, DOWN, LEFT, RIGHT]: |
| 86 | + new_pos = simulate_move(pos, d) |
| 87 | + if is_valid_move(new_pos): |
| 88 | + value = max(value, alpha_beta_search(new_pos, d, opponent_pos, depth - 1, alpha, beta, False)) |
| 89 | + alpha = max(alpha, value) |
| 90 | + if beta <= alpha: |
| 91 | + break |
| 92 | + return value |
| 93 | + else: |
| 94 | + value = float('inf') |
| 95 | + for d in [UP, DOWN, LEFT, RIGHT]: |
| 96 | + new_pos = simulate_move(opponent_pos, d) |
| 97 | + if is_valid_move(new_pos): |
| 98 | + value = min(value, alpha_beta_search(pos, direction, new_pos, depth - 1, alpha, beta, True)) |
| 99 | + beta = min(beta, value) |
| 100 | + if beta <= alpha: |
| 101 | + break |
| 102 | + return value |
| 103 | + |
| 104 | + best_score = -float('inf') |
| 105 | + best_move = None |
| 106 | + for d in [UP, DOWN, LEFT, RIGHT]: |
| 107 | + new_pos = simulate_move(player_pos, d) |
| 108 | + if is_valid_move(new_pos): |
| 109 | + score = alpha_beta_search(new_pos, d, opponent_pos, depth=3, alpha=-float('inf'), beta=float('inf'), max_player=False) |
| 110 | + if score > best_score: |
| 111 | + best_score = score |
| 112 | + best_move = d |
| 113 | + return best_move |
| 114 | + |
| 115 | + def heuristic_evaluation(self, player_pos, opponent_pos): |
| 116 | + # The heuristic evaluation function calculates the manhattan distance between the player's position and the opponent's position |
| 117 | + return -(abs(player_pos[0] - opponent_pos[0]) + abs(player_pos[1] - opponent_pos[1])) |
| 118 | + |
| 119 | + def run(self): |
| 120 | + while self.running: |
| 121 | + for event in pygame.event.get(): |
| 122 | + if event.type == pygame.QUIT: |
| 123 | + self.running = False |
| 124 | + elif event.type == pygame.KEYDOWN: |
| 125 | + if event.key == pygame.K_w and self.player1_direction != DOWN: |
| 126 | + self.player1_direction = UP |
| 127 | + elif event.key == pygame.K_s and self.player1_direction != UP: |
| 128 | + self.player1_direction = DOWN |
| 129 | + elif event.key == pygame.K_a and self.player1_direction != RIGHT: |
| 130 | + self.player1_direction = LEFT |
| 131 | + elif event.key == pygame.K_d and self.player1_direction != LEFT: |
| 132 | + self.player1_direction = RIGHT |
| 133 | + |
| 134 | + self.screen.fill(WHITE) |
| 135 | + self.update() |
| 136 | + self.draw_board() |
| 137 | + pygame.display.flip() |
| 138 | + self.clock.tick(10) |
| 139 | + |
| 140 | + pygame.quit() |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + game = TronGame() |
| 145 | + game.run() |
| 146 | + |
0 commit comments