tictactoe.py

Created by schraf

Created on November 12, 2021

1.14 KB


from turtle import *
from random import random,randint,choice

def trait(x, y, a, d):
  penup()
  goto(x, y)
  pendown()
  setheading(a)
  forward(d)

def grille(nb):
  case = min(H,L) // nb
  H_m, L_m = -nb * case / 2, -nb * case /2
  for i in range(1,nb):
    trait(L_m, H_m + case * i, 0, nb * case)
    trait(L_m + case * i, H_m, 90, nb * case)
  return case, H_m, L_m

def cercle(x,y,r,c):
  penup()
  color(c)
  goto(x,y - r)
  setheading(0)
  pendown()
  circle(int(r))

def croix(x,y,r,c):
 for i in [-1,1]:
  penup()
  color(c)
  goto(x - r, y + i * r)
  pendown()
  goto(x + r, y - i * r)

def pion(x,y,r,t1,t2):
  if t1 != t2:
    if t1 == 1: cercle(x,y,r,"white")
    elif t1 == 2: croix(x,y,r,"white")
    if t2 == 1: cercle(x,y,r,"black")
    elif t2 == 2: croix(x,y,r,"black")

speed(0)
hideturtle()
L,H = 320, 222
taille = 3
jeu = [0 for _ in range(taille * taille)]
case, H_m, L_m = grille(taille)
r = case / 2.3

while True:
 for c in range(taille):
  for l in range(taille):
   x, y, n = case * (c - (taille - 1) / 2),  case * (l - (taille - 1) / 2), taille * c + l
   v = choice([0,1,2])
   pion(x,y,r,jeu[n],v)
   jeu[n] = v

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.