tic_tac_toe.py

Created by alexandre-merle

Created on March 09, 2024

5.53 KB


from random import randint
from kandinsky import fill_circle,fill_rect,draw_line,draw_string
from ion import keydown,KEY_OK,KEY_LEFT,KEY_RIGHT,KEY_UP,KEY_DOWN
from time import sleep
Grille=[[0,0,0],[0,0,0],[0,0,0]]
def DrawGrille(x,y,pos,t=60):
  global Grille
  fill_rect(x-1,y-1,(t*3)+2,(t*3)+2,(255,)*3)
  for Y in range(0,4):
    fill_rect(x-1,(y-1)+(Y*t),(t*3)+2,2,(0,)*3)
    for X in range(0,4):
      fill_rect((x-1)+(X*t),y-1,2,t*3+2,(0,)*3)
      if X==pos[0]and Y==pos[1]:fill_rect(x+(X*t),y+(Y*t)+1,t,t-2,(220,)*3);col_circle=(220,)*3
      else:col_circle=(255,)*3
      if X!=3 and Y!=3 and Grille[Y][X]==1:fill_circle(x+(X*t)+int(t/2),y+(Y*t)+int(t/2),int(t/2)-4,(0,0,255));fill_circle(x+(X*t)+int(t/2),y+(Y*t)+int(t/2),int(t/2)-6,col_circle)
      elif X!=3 and Y!=3 and Grille[Y][X]==2:draw_line(x+(X*t)+4,y+(Y*t)+4,x+(X*t)+(t-4),y+(Y*t)+(t-4),(255,0,0));draw_line(x+(X*t)+5,y+(Y*t)+4,x+(X*t)+(t-4),y+(Y*t)+(t-5),(255,0,0));draw_line(x+(X*t)+4,y+(Y*t)+5,x+(X*t)+(t-5),y+(Y*t)+(t-4),(255,0,0));draw_line(x+(X*t)+(t-4),y+(Y*t)+4,x+(X*t)+4,y+(Y*t)+(t-4),(255,0,0));draw_line(x+(X*t)+(t-5),y+(Y*t)+4,x+(X*t)+4,y+(Y*t)+(t-5),(255,0,0));draw_line(x+(X*t)+(t-4),y+(Y*t)+5,x+(X*t)+5,y+(Y*t)+(t-4),(255,0,0))
def game(T=60):
  global Grille
  Run,XC,YC=True,0,0
  DrawGrille(int((320-((T*3)+2))/2),int((222-((T*3)+2))/2),(XC,YC),T)
  while Run:
    Redraw=True
    if keydown(KEY_UP)     and YC>0:YC-=1
    elif keydown(KEY_DOWN) and YC<2:YC+=1
    elif keydown(KEY_LEFT) and XC>0:XC-=1
    elif keydown(KEY_RIGHT)and XC<2:XC+=1
    else:Redraw=False
    if Redraw:DrawGrille(int((320-((T*3)+2))/2),int((222-((T*3)+2))/2),(XC,YC),T)
    if keydown(KEY_OK)and Grille[YC][XC]==0:
      Grille[YC][XC]=1
      if Grille[0][0]+Grille[1][0]+Grille[2][0]+Grille[0][1]+Grille[1][1]+Grille[2][1]+Grille[0][2]+Grille[1][2]+Grille[2][2]!=13:
        OK=0
        for i in range(0,3):
          if Grille[i][0]==1 and Grille[i][1]==1 and Grille[i][2]==0:Grille[i][2]=2
          elif Grille[i][1]==1 and Grille[i][2]==1 and Grille[i][0]==0:Grille[i][0]=2
          elif Grille[i][0]==1 and Grille[i][2]==1 and Grille[i][1]==0:Grille[i][1]=2
          else:OK+=1
        if OK==3:
          for i in range(0,3):
            if Grille[0][i]==1 and Grille[1][i]==1 and Grille[2][i]==0:Grille[2][i]=2
            elif Grille[1][i]==1 and Grille[2][i]==1 and Grille[0][i]==0:Grille[0][i]=2
            elif Grille[0][i]==1 and Grille[2][i]==1 and Grille[1][i]==0:Grille[1][i]=2
            else:OK+=1
        else:OK+=3
        if OK==6:
          if Grille[0][0]==1 and Grille[1][1]==1 and Grille[2][2]==0:Grille[2][2]=2
          elif Grille[0][0]==1 and Grille[1][1]==0 and Grille[2][2]==1:Grille[1][1]=2
          elif Grille[0][0]==0 and Grille[1][1]==1 and Grille[2][2]==1:Grille[0][0]=2
          else:OK+=1
        if OK==7:
          if Grille[2][0]==1 and Grille[1][1]==1 and Grille[0][2]==0:Grille[0][2]=2
          elif Grille[2][0]==1 and Grille[1][1]==0 and Grille[0][2]==1:Grille[1][1]=2
          elif Grille[2][0]==0 and Grille[1][1]==1 and Grille[0][2]==1:Grille[2][0]=2
          else:OK+=1
        while OK==8:
          Xc,Yc=randint(0,2),randint(0,2)
          if Grille[Yc][Xc]==0:Grille[Yc][Xc]=2;OK=0
      else:draw_string("Match Nul",115,1);Run=False
      Run=False
      if Grille[0][0]==1 and Grille[1][1]==1 and Grille[2][2]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[0][2]==1 and Grille[1][1]==1 and Grille[2][0]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[0][0]==1 and Grille[0][1]==1 and Grille[0][2]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[1][0]==1 and Grille[1][1]==1 and Grille[1][2]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[2][0]==1 and Grille[2][1]==1 and Grille[2][2]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[0][0]==1 and Grille[1][0]==1 and Grille[2][0]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[0][1]==1 and Grille[1][1]==1 and Grille[2][1]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[0][2]==1 and Grille[1][2]==1 and Grille[2][2]==1:draw_string(" You Won ",115,1,(0,0,255))
      elif Grille[0][0]==2 and Grille[1][1]==2 and Grille[2][2]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[0][2]==2 and Grille[1][1]==2 and Grille[2][0]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[0][0]==2 and Grille[0][1]==2 and Grille[0][2]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[1][0]==2 and Grille[1][1]==2 and Grille[1][2]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[2][0]==2 and Grille[2][1]==2 and Grille[2][2]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[0][0]==2 and Grille[1][0]==2 and Grille[2][0]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[0][1]==2 and Grille[1][1]==2 and Grille[2][1]==2:draw_string("Computer Won",100,1,(255,0,0))
      elif Grille[0][2]==2 and Grille[1][2]==2 and Grille[2][2]==2:draw_string("Computer Won",100,1,(255,0,0))
      else:Run=True
      if Grille[0][0]+Grille[1][0]+Grille[2][0]+Grille[0][1]+Grille[1][1]+Grille[2][1]+Grille[0][2]+Grille[1][2]+Grille[2][2]==13:Run=False
      DrawGrille(int((320-((T*3)+2))/2),int((222-((T*3)+2))/2),(XC,YC),T)
    sleep(0.1)
  DrawGrille(int((320-((T*3)+2))/2),int((222-((T*3)+2))/2),(XC,YC),T)
def launch():
  while True:
    global Grille
    game(60)
    draw_string("Rejouer:[OK]",100,203)
    while keydown(KEY_OK):pass
    while not keydown(KEY_OK):pass
    while keydown(KEY_OK):pass
    fill_rect(0,0,320,222,(255,)*3)
    Grille=[[0,0,0],[0,0,0],[0,0,0]]
launch()
#By Merle

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.