import pyxel pyxel.init(250,250) pyxel.mouse(True) morpion = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] numero = 1 #joueur 1 qui commence def centre_case(x, y): if 50 <= x <= 200: xc= (x // 50) * 50 + 25 if 50 <= y <= 200: yc= (y // 50) * 50 + 25 return xc, yc def dessiner_quadrillage(): for k in range(50, 250, 50): pyxel.line(50, k, 200, k, 5) pyxel.line(k, 50, k, 200, 5) def dessiner_symbole(x, y, num): xc, yc = centre_case(x, y) if num == 1: pyxel.circb(xc, yc, 20, 5) else: pyxel.line(xc-20, yc-20, xc+20, yc+20, 5) pyxel.line(xc-20, yc+20, xc+20, yc-20, 5) def dessiner_grille(): pyxel.cls(0) dessiner_quadrillage() for i in range(3): for j in range(3): if morpion[i][j] != 0: dessiner_symbole(75 + 50*j, 75 + 50 *i, morpion[i][j]) def clic_valide(): if 50 <= pyxel.mouse_x <= 200 and 50 <= pyxel.mouse_y <= 200: j = pyxel.mouse_x // 50 - 1 i = pyxel.mouse_y // 50 - 1 if morpion[i][j] == 0: return True return False def mise_a_jour_morpion(n): if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT): if clic_valide(): j = pyxel.mouse_x // 50 - 1 i = pyxel.mouse_y // 50 - 1 morpion[i][j] = numero return (n * 2) % 3 return n def gagnant(): #renvoie 0 si pas de gagnant, sinon 1 ou 2 (le numéro du gagnant) #test sur les lignes for i in range(3): p = 1 for j in range(3): p = p * morpion[i][j] if p == 1: return 1 elif p == 8: return 2 #test sur les colonnes for j in range(3): p = 1 for i in range(3): p = p * morpion[i][j] if p == 1: return 1 elif p == 8: return 2 #test sur une diagonale p = 1 for i in range(3): p = p * morpion[i][i] if p == 1: return 1 elif p == 8: return 2 #test sur une diagonale p = 1 for i in range(3): p = p * morpion[i][2-i] if p == 1: return 1 elif p == 8: return 2 return 0 def fin_de_partie(): rempli = True #la grille est remplie for i in range(3): if 0 in morpion[i]: rempli = False if not rempli : if gagnant() != 0: #il y a un gagnant return True return rempli def update(): global numero if not fin_de_partie(): numero = mise_a_jour_morpion(numero) def draw(): dessiner_grille() if fin_de_partie(): if gagnant() == 0: pyxel.text(10, 10, "Pas de gagnant !", 7) elif gagnant() == 1: pyxel.text(10, 10, "Les ronds gagnent", 7) else: pyxel.text(10, 10, "Les croix gagnent", 7) pyxel.run(update, draw)