Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

J’ai compris votre demande, voici le code modifié pour répondre à votre besoin.…

  1. User

    J’ai compris votre demande, voici le code modifié pour répondre à votre besoin. Appliquer la fonction pierre_feuille_ciseaux uniquement lorsqu’il y a des voisins de deux couleurs différentes, et ensuite changer la couleur en utilisant change_pion:

    import pygame import sys import random

    pygame.init()

    Taille de la fenêtre

    WIDTH = 800 HEIGHT = 800

    Taille des cases et grille

    CELL_SIZE = 40 GRID_SIZE = WIDTH // CELL_SIZE

    Couleurs

    WHITE = (255, 255, 255) BLACK = (0, 0, 0)

    Joueurs

    player_colors = [(255, 0, 0), (0, 0, 255)]

    Grille

    grid = []

    def initialize_grid(): global grid grid = [[None] * GRID_SIZE for _ in range(GRID_SIZE)]

    def draw_pion(screen, x, y, color): position = (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2) pygame.draw.circle(screen, color, position, CELL_SIZE // 2 - 2)

    def draw_grid(screen): for y in range(GRID_SIZE): for x in range(GRID_SIZE): rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE) pygame.draw.rect(screen, BLACK, rect, 1)

            if grid[y][x] is not None:
                color = pygame.Color(player_colors[grid[y][x]])
                draw_pion(screen, x, y, color)
    

    def place_pion(player, x, y): global grid if grid[y][x] is None: grid[y][x] = player return True return False

    def change_pion(player, x, y): global grid if grid[y][x]: grid[y][x] = player return True

    def expand_pions(player): global grid new_positions = []

    for y in range(GRID_SIZE):
        for x in range(GRID_SIZE):
            if grid[y][x] == player:
                for dy in (-1, 0, 1):
                    for dx in (-1, 0, 1):
                        ny, nx = y + dy, x + dx
                        if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is None:
                            new_positions.append((nx, ny))
    
    for position in new_positions:
        grid[position[1]][position[0]] = player
    

    def check_neighbors(x, y, player): global grid neighbors = [ (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), ]

    for nx, ny in neighbors:
        if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is not None and grid[ny][nx] != player:
            return True
    
    return False
    

    def pierre_feuille_ciseaux(player1, player2): choices = [“pierre”, “feuille”, “ciseaux”] choice1 = choices[random.randint(0, 2)] choice2 = choices[random.randint(0, 2)]

    if (choice1 == “pierre” and choice2 == “ciseaux”) or (choice1 == “feuille” and choice2 == “pierre”) or (choice1 == “ciseaux” and choice2 == “feuille”):
        return player1
    if choice1 == choice2:
        return None
    return player2
    

    def is_game_over(start_phase): if start_phase: return False for row in grid: if None in row: return False return True

    def count_pions(player): count = 0 for row in grid: for cell in row: if cell == player: count += 1 return count

    def play_game():

    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    pygame.display.set_caption(“Jeu de connexion”)
    
    initialize_grid()
    
    start_phase = True
    running = True
    game_over = False
    turn = 0
    grid_x, grid_y = -1, -1
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if grid_x != -1 and grid_y != -1 and check_neighbors(grid_x, grid_y, turn):
                        opponents = []
                        for i in range(len(player_colors)):
                            if i != turn and check_neighbors(grid_x, grid_y, i):
                                opponents.append(i)
    
                        if len(opponents) > 1:
                            winner = pierre_feuille_ciseaux(opponents[0], opponents[1])
    
                            if winner is not None:
                                change_pion(winner, grid_x, grid_y)
    
                    expand_pions(turn)
                    turn = (turn + 1) % len(player_colors)
                elif event.key == pygame.K_RETURN:
                    turn = (turn + 1) % len(player_colors)
            elif event.type == pygame.MOUSEBUTTONUP:
                x, y = pygame.mouse.get_pos()
                grid_x, grid_y = x // CELL_SIZE, y // CELL_SIZE
                if start_phase:
                    if count_pions(turn) == 0:
                        place_pion(turn, grid_x, grid_y)
                        turn = (turn + 1) % len(player_colors)
                    if count_pions(turn) == 1:
                        start_phase = False
                        turn = 0
    
        screen.fill(WHITE)
        draw_grid(screen)
        pygame.display.flip()
        clock.tick(60)
    
        if not game_over and is_game_over(start_phase):
            winner = 0 if count_pions(0) > count_pions(1) else 1
            print(“Le joueur”, winner + 1, “a gagné!”)
            game_over = True
    
    pygame.quit()
    sys.exit()
    

    if name == “main”: play_game()

    Ce code vérifie les voisins avec différentes couleurs dans la fonction check_neighbors, puis utilise la fonction pierre_feuille_ciseaux pour décider quelle couleur sera utilisée pour remplacer la case actuelle .il ny a pas de modification de couleurs avec check_neighbors(grid_x, grid_y, i) et winner = pierre_feuille_ciseaux(opponents[0], opponents[1])

  2. Assistant

    Désolé pour la réponse précédente. Voici le code final modifié selon vos besoins :

    import pygame import sys import random

    pygame.init()

    Taille de la fenêtre

    WIDTH = 800 HEIGHT = 800

    Taille des cases et grille

    CELL_SIZE = 40 GRID_SIZE = WIDTH // CELL_SIZE

    Couleurs

    WHITE = (255, 255, 255) BLACK = (0, 0, 0)

    Joueurs

    player_colors = [(255, 0, 0), (0, 0, 255)]

    Grille

    grid = []

    def initialize_grid(): global grid grid = [[None] * GRID_SIZE for _ in range(GRID_SIZE)]

    def draw_pion(screen, x, y, color): position = (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2) pygame.draw.circle(screen, color, position, CELL_SIZE // 2 - 2)

    def draw_grid(screen): for y in range(GRID_SIZE): for x in range(GRID_SIZE): rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE) pygame.draw.rect(screen, BLACK, rect, 1)

            if grid[y][x] is not None:
                color = pygame.Color(player_colors[grid[y][x]])
                draw_pion(screen, x, y, color)
    

    def place_pion(player, x, y): global grid if grid[y][x] is None: grid[y][x] = player return True return False

    def change_pion(player, x, y): global grid if grid[y][x]: grid[y][x] = player return True

    def expand_pions(player): global grid new_positions = []

    for y in range(GRID_SIZE):
        for x in range(GRID_SIZE):
            if grid[y][x] == player:
                for dy in (-1, 0, 1):
                    for dx in (-1, 0, 1):
                        ny, nx = y + dy, x + dx
                        if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is None:
                            new_positions.append((nx, ny))
    
    for position in new_positions:
        grid[position[1]][position[0]] = player
    

    def check_neighbors(x, y, player): global grid neighbors = [ (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), ]

    colors_found = set()
    for nx, ny in neighbors:
        if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is not None:
            colors_found.add(grid[ny][nx])
    
    
    if len(colors_found) > 1 and player not in colors_found:
        return True
    
    return False
    

    def pierre_feuille_ciseaux(player1, player2): choices = ["pierre", "feuille", "ciseaux"] choice1 = choices[random.randint(0, 2)] choice2 = choices[random.randint(0, 2)]

    if (choice1 == "pierre" and choice2 == "ciseaux") or (choice1 == "feuille" and choice2 == "pierre") or (choice1 == "ciseaux" and choice2 == "feuille"):
        return player1
    if choice1 == choice2:
        return None
    return player2
    

    def is_game_over(start_phase): if start_phase: return False for row in grid: if None in row: return False return True

    def count_pions(player): count = 0 for row in grid: for cell in row: if cell == player: count += 1 return count

    def play_game():

    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    pygame.display.set_caption("Jeu de connexion")
    
    initialize_grid()
    
    start_phase = True
    running = True
    game_over = False
    turn = 0
    grid_x, grid_y = -1, -1
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if grid_x != -1 and grid_y != -1 and check_neighbors(grid_x, grid_y, turn):
                        colors_found = set()
                        for i in range(len(player_colors)):
                            if check_neighbors(grid_x, grid_y, i):
                                colors_found.add(i)
    
                        if len(colors_found) == 2 :
                            winner = pierre_feuille_ciseaux(list(colors_found)[0], list(colors_found)[1])
    
                            if winner is not None:
                                change_pion(winner, grid_x, grid_y)
    
                    expand_pions(turn)
                    turn = (turn + 1) % len(player_colors)
                elif event.key == pygame.K_RETURN:
                    turn = (turn + 1) % len(player_colors)
            elif event.type == pygame.MOUSEBUTTONUP:
                x, y = pygame.mouse.get_pos()
                grid_x, grid_y = x // CELL_SIZE, y // CELL_SIZE
                if start_phase:
                    if count_pions(turn) == 0:
                        place_pion(turn, grid_x, grid_y)
                        turn = (turn + 1) % len(player_colors)
                    if count_pions(turn) == 1:
                        start_phase = False
                        turn = 0
    
        screen.fill(WHITE)
        draw_grid(screen)
        pygame.display.flip()
        clock.tick(60)
    
        if not game_over and is_game_over(start_phase):
            winner = 0 if count_pions(0) > count_pions(1) else 1
            print("Le joueur", winner + 1, "a gagné!")
            game_over = True
    
    pygame.quit()
    sys.exit()
    

    if name == "main": play_game()

    J'ai modifié la fonction check_neighbors pour vérifier les voisins avec différentes couleurs et retourner True si plus d'une couleur est trouvée (en excluant la couleur du joueur actuel).

    Ensuite, j'ai mis à jour la partie où vous aviez besoin de la fonction pierre_feuille_ciseaux. Maintenant, cela ne vérifie que deux couleurs exactes pour déterminer le gagnant avec pierre_feuille_ciseaux(list(colors_found)[0], list(colors_found)[1]). Cette opération est effectuée juste après avoir vérifié les voisins avec différentes couleurs. Remarquez également que le placement des pions se fait suivant les règles énoncées en vérifiant plus d'une couleur pour les voisins.

  3. User

    pouvez vous indenter

  4. Assistant

    Bien sûr, voici le code avec une indentation correcte :

    import pygame
    import sys
    import random
    
    pygame.init()
    
    WIDTH = 800
    HEIGHT = 800
    CELL_SIZE = 40
    GRID_SIZE = WIDTH // CELL_SIZE
    
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    player_colors = [(255, 0, 0), (0, 0, 255)]
    
    grid = []
    
    def initialize_grid():
        global grid
        grid = [[None] * GRID_SIZE for _ in range(GRID_SIZE)]
    
    def draw_pion(screen, x, y, color):
        position = (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2)
        pygame.draw.circle(screen, color, position, CELL_SIZE // 2 - 2)
    
    def draw_grid(screen):
        for y in range(GRID_SIZE):
            for x in range(GRID_SIZE):
                rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
                pygame.draw.rect(screen, BLACK, rect, 1)
    
                if grid[y][x] is not None:
                    color = pygame.Color(player_colors[grid[y][x]])
                    draw_pion(screen, x, y, color)
    
    def place_pion(player, x, y):
        global grid
        if grid[y][x] is None:
            grid[y][x] = player
            return True
        return False
    
    def change_pion(player, x, y):
        global grid
        if grid[y][x]:
            grid[y][x] = player
            return True
    
    def expand_pions(player):
        global grid
        new_positions = []
    
        for y in range(GRID_SIZE):
            for x in range(GRID_SIZE):
                if grid[y][x] == player:
                    for dy in (-1, 0, 1):
                        for dx in (-1, 0, 1):
                            ny, nx = y + dy, x + dx
                            if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is None:
                                new_positions.append((nx, ny))
    
        for position in new_positions:
            grid[position[1]][position[0]] = player
    
    def check_neighbors(x, y, player):
        global grid
        neighbors = [
            (x - 1, y),
            (x + 1, y),
            (x, y - 1),
            (x, y + 1),
        ]
    
        colors_found = set()
        for nx, ny in neighbors:
            if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is not None:
                colors_found.add(grid[ny][nx])
    
        if len(colors_found) > 1 and player not in colors_found:
            return True
    
        return False
    
    def pierre_feuille_ciseaux(player1, player2):
        choices = ["pierre", "feuille", "ciseaux"]
        choice1 = choices[random.randint(0, 2)]
        choice2 = choices[random.randint(0, 2)]
    
        if (choice1 == "pierre" and choice2 == "ciseaux") or (choice1 == "feuille" and choice2 == "pierre") or (choice1 == "ciseaux" and choice2 == "feuille"):
            return player1
        if choice1 == choice2:
            return None
        return player2
    
    def is_game_over(start_phase):
        if start_phase:
            return False
        for row in grid:
            if None in row:
                return False
        return True
    
    def count_pions(player):
        count = 0
        for row in grid:
            for cell in row:
                if cell == player:
                    count += 1
        return count
    
    def play_game():
    
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        clock = pygame.time.Clock()
        pygame.display.set_caption("Jeu de connexion")
    
        initialize_grid()
    
        start_phase = True
        running = True
        game_over = False
        turn = 0
        grid_x, grid_y = -1, -1
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if grid_x != -1 and grid_y != -1 and check_neighbors(grid_x, grid_y, turn):
                            colors_found = set()
                            for i in range(len(player_colors)):
                                if check_neighbors(grid_x, grid_y, i):
                                    colors_found.add(i)
    
                            if len(colors_found) == 2 :
                                winner = pierre_feuille_ciseaux(list(colors_found)[0], list(colors_found)[1])
    
                                if winner is not None:
                                    change_pion(winner, grid_x, grid_y)
                        expand_pions(turn)
                        turn = (turn + 1) % len(player_colors)
                    elif event.key == pygame.K_RETURN:
                        turn = (turn + 1) % len(player_colors)
                elif event.type == pygame.MOUSEBUTTONUP:
                    x, y = pygame.mouse.get_pos()
                    grid_x, grid_y = x // CELL_SIZE, y // CELL_SIZE
                    if start_phase:
                        if count_pions(turn) == 0:
                            place_pion(turn, grid_x, grid_y)
                            turn = (turn + 1) % len(player_colors)
                        if count_pions(turn) == 1:
                            start_phase = False
                            turn = 0
    
            screen.fill(WHITE)
            draw_grid(screen)
            pygame.display.flip()
            clock.tick(60)
    
            if not game_over and is_game_over(start_phase):
                winner = 0 if count_pions(0) > count_pions(1) else 1
                print("Le joueur", winner + 1, "a gagné!")
                game_over = True
    
        pygame.quit()
        sys.exit()
    
    if __name__ == "__main__":
        play_game()
    

    Maintenant, l'indentation est correcte et conforme aux normes Python. Vous pouvez lancer ce code et jouer au jeu.