morpion.py

Created by gengisclan

Created on March 02, 2023

2.91 KB

Un petit morpion sympa


from math import *
from kandinsky import *
from time import *
from ion import *

def bouger_curseur(curseur,place_matrice): # OK
  fill_rect(curseur[0],curseur[1],50,50,curseur[2])
  fill_rect(curseur[0]+1,curseur[1]+1,48,48,(255,255,255))
  if keydown(KEY_LEFT)and curseur[0]>10:curseur[0]-=50;place_matrice[1]-=1
  if keydown(KEY_UP)and curseur[1]>10:curseur[1]-=50;place_matrice[0]-=1
  if keydown(KEY_DOWN)and curseur[1]<100:curseur[1]+=50;place_matrice[0]+=1
  if keydown(KEY_RIGHT)and curseur[0]<100:curseur[0]+=50;place_matrice[1]+=1

def generer_terrain(): # OK
  fill_rect(10,10,150,150,(0,0,0))
  fill_rect(11,11,148,148,(255,255,255))
  fill_rect(60,10,1,150,(0,0,0))
  fill_rect(110,10,1,150,(0,0,0))
  fill_rect(10,60,150,1,(0,0,0))
  fill_rect(10,110,150,1,(0,0,0))
  

def dessiner_signe(curseur,tour,place_matrice): # OK
  if keydown(KEY_OK):
    if terrain[place_matrice[0]][place_matrice[1]][0]==0:
      if tour%2==1:
        draw_string("x",curseur[0]+20,curseur[1]+20)
        terrain[place_matrice[0]][place_matrice[1]][0]=1 
      if tour%2==0:
        draw_string("o",curseur[0]+20,curseur[1]+20)
        terrain[place_matrice[0]][place_matrice[1]][0]=2 
      return True

def garder_signe(terrain): # OK
  for i in terrain:
    for j in i:
      if j[0]==1:draw_string("x",j[1],j[2])
      if j[0]==2:draw_string("o",j[1],j[2])

def verifier_victoire(terrain,egalitee):
  # Verticale
  if(terrain[0][0][0]==terrain[1][0][0]) and (terrain[0][0][0] == terrain[2][0][0]) and terrain[0][0][0]!=0: return True
  if(terrain[0][1][0]==terrain[1][1][0]) and (terrain[0][1][0] == terrain[2][1][0]) and terrain[0][1][0]!=0: return True
  if(terrain[0][2][0]==terrain[1][2][0]) and (terrain[0][2][0] == terrain[2][2][0]) and terrain[0][2][0]!=0: return True
  # Horizontale
  if(terrain[0][0][0]==terrain[0][1][0]) and (terrain[0][0][0] == terrain[0][2][0]) and terrain[0][0][0]!=0: return True
  if(terrain[1][0][0]==terrain[1][1][0]) and (terrain[1][0][0] == terrain[1][2][0]) and terrain[1][0][0]!=0: return True
  if(terrain[2][0][0]==terrain[2][1][0]) and (terrain[2][0][0] == terrain[2][2][0]) and terrain[2][0][0]!=0: return True
  # Diagonale
  if(terrain[0][0][0]==terrain[1][1][0]) and (terrain[0][0][0] == terrain[2][2][0]) and terrain[0][0][0]!=0: return True
  if(terrain[2][0][0]==terrain[1][1][0]) and (terrain[2][0][0] == terrain[0][2][0]) and terrain[2][0][0]!=0: return True
  # Egalitee
  for i in terrain:
    for j in i:
      if j[0]!=0: egalitee+=1
  if egalitee==9:return True

terrain=[
[[0,30,30],[0,80,30],[0,130,30]],
[[0,30,80],[0,80,80],[0,130,80]], 
[[0,30,130],[0,80,130],[0,130,130]]]
place_matrice=[0,0]
tour=1
curseur=[10,10,(255,255,0)]
egalitee=0

while 1:
  sleep(.1)
  generer_terrain()
  draw_string("Tour "+str(tour),170,50)
  bouger_curseur(curseur,place_matrice)
  garder_signe(terrain)
  if dessiner_signe(curseur,tour,place_matrice)==True:
    tour+=1
  if verifier_victoire(terrain,egalitee)==True:
    break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.