Skip to content

Instantly share code, notes, and snippets.

@gus3000
Created October 25, 2019 15:33
Show Gist options
  • Save gus3000/52718e4ea5f6b30179848940149360b1 to your computer and use it in GitHub Desktop.
Save gus3000/52718e4ea5f6b30179848940149360b1 to your computer and use it in GitHub Desktop.
Un morpion rapide fait en démo
import math
import random
from pprint import pprint
import arcade
from arcade import color
class FenetreMorpion(arcade.Window):
def __init__(self, width: float = 800, height: float = 600):
super().__init__(width, height, "Morpion")
arcade.set_background_color(color.WHITE)
self.grille = [
[0,0,0],
[0,0,0],
[0,0,0],
]
self.taille_symbole = 50
self.joueur = 1
def on_draw(self):
arcade.start_render()
for i in range(3):
for j in range(3):
if self.grille[i][j] == 1:
self.dessiner_croix(i, j)
elif self.grille[i][j] == 2:
self.dessiner_rond(i, j)
def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
if button != 1:
return
colonne = math.floor(x / self.width * 4 - 0.5)
ligne = math.floor(y / self.height * 4 - 0.5)
if colonne < 0 or colonne > 2:
return
if ligne < 0 or ligne > 2:
return
joue(self.grille, self.joueur, ligne, colonne)
if self.joueur == 1:
self.joueur = 2
else:
self.joueur = 1
def dessiner_croix(self, ligne, colonne):
x = self.width * (colonne + 1) / 4
y = self.height * (ligne + 1) / 4
arcade.draw_line(x - self.taille_symbole, y - self.taille_symbole,
x + self.taille_symbole, y + self.taille_symbole,
color.BLUE, 3)
arcade.draw_line(x - self.taille_symbole, y + self.taille_symbole,
x + self.taille_symbole, y - self.taille_symbole,
color.BLUE, 3)
def dessiner_rond(self, ligne, colonne):
x = self.width * (colonne + 1) / 4
y = self.height * (ligne + 1) / 4
arcade.draw_circle_outline(x, y, self.taille_symbole, color.RED, 3)
def joue(grille, joueur, ligne, colonne):
"""
Retourne True si le coup était valide, faux sinon
"""
if grille[ligne][colonne] == 0:
grille[ligne][colonne] = joueur
return True
else:
return False
def vainqueur(grille):
"""
Renvoie le joueur qui a gagné, 0 si aucun, -1 si la partie n'est pas finie
"""
triplets = [
# lignes
grille[0],
grille[1],
grille[2],
# colonnes
[grille[x][0] for x in range(3)],
[grille[x][1] for x in range(3)],
[grille[x][2] for x in range(3)],
# diagonales
[grille[x][x] for x in range(3)],
[grille[x][-1 - x] for x in range(3)]
]
for triplet in triplets:
if triplet[0] == triplet[1] and triplet[0] == triplet[2] and triplet[0] != 0:
# joueur 1 ou 2 a gagné
return triplet[0]
for i in range(3):
for j in range(3):
if grille[i][j] == 0:
# pas fini
return -1
# égalité
return 0
def afficher_grille(grille):
for ligne in grille:
for indice_joueur in ligne:
if indice_joueur == 0:
symbole = " "
elif indice_joueur == 1:
symbole = "X"
else:
symbole = "O"
print(symbole, end='')
print()
grille = [
[1, 0, 2],
[1, 2, 2],
[0, 1, 0],
]
gagnant = vainqueur(grille)
joueur = 1
while gagnant == -1:
ligne = random.randint(0, 2)
colonne = random.randint(0, 2)
while not joue(grille, joueur, ligne, colonne):
ligne = random.randint(0, 2)
colonne = random.randint(0, 2)
if joueur == 1:
joueur = 2
else:
joueur = 1
gagnant = vainqueur(grille)
afficher_grille(grille)
print(gagnant)
fenetre_morpion = FenetreMorpion(512, 512)
arcade.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment