morpion.py

Created by loupiot

Created on May 06, 2018

1.55 KB

Le morpion avec une interface visuelle. Rentrez morpion() et rentrez des nombres correspondant aux numéros de la grille que vous voulez jouer. Attention : ne rentrez surtout pas de texte, cela produit des erreurs !


from math import *
from kandinsky import *
def tiret(x,y):
  color(0,0,0)
  for a in range(x,x+217):
    set_pixel(a,y,color(0,0,0))
    
def ligne(x,y):
  for a in range(y,y+217):
    set_pixel(x,a,color(0,0,0))

def touche(pion):
  a=int(input())
  if 0<a<10 and type(pion[a-1])==int:
    return a
  else:
    return touche(pion)
    
def blanc(x,y):
  for a in range(x-6,x+7):
    for b in range(y-6,y+7):
      set_pixel(a,b,color(255,255,255))
      
def croix(x,y):
  blanc(x,y)
  rgb=color(255,0,0)
  for a in range(-33,34):
    set_pixel(x-a,y-a,rgb)
    set_pixel(x-a,y+a,rgb)
  
def cercle(x,y):
  blanc(x,y)
  for i in range(x-34,x+35):
    for j in range(y-34,y+35):
      if 34**2-35<(i-x)**2+(j-y)**2<34**2+35:
        set_pixel(i,j,color(0,0,255))
  
def morpion():
  pion=[1,2,3,4,5,6,7,8,9]
  coup=0
  jeu=False
  j=1
  for a in range(0,3):
    for b in range(0,3):
      ligne(52+72*a,3)
      tiret(52,3+72*b)
      draw_string(str(a+1+3*(b)),83+72*a,31+72*b)
  ligne(268,3)
  tiret(52,219)
  while coup<9 and jeu==False:
    a=touche(pion)
    if j==1:
      pion[a-1]="X"
      j=2
      croix(88+72*((a-1)%3),39+72*int((a-1)/3))
    else:
      pion[a-1]="O"
      j=1
      cercle(88+72*((a-1)%3),39+72*int((a-1)/3))
    coup+=1
    for a in range(0,3):
      if pion[3*a]==pion[3*a+1]==pion[3*a+2] or pion[a]==pion[a+3]==pion[a+6] or pion[0]==pion[4]==pion[8] or pion[2]==pion[4]==pion[6]:
        jeu=True
  if jeu==True:
    draw_string(str("le joueur {} a gagne en {} coups").format(j,coup),10,108)
  else:
    draw_string("Egalite !",118,108)

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.