morpion_ia_opti.py

Created by florian-allard

Created on August 08, 2020

4.45 KB

Version optimisée (script plus court mais moins explicite) du jeu de Morpion disponible ici.


from ion import *
from kandinsky import *
from time import sleep
from random import randint,choice

kd = keydown
KE = KEY_EXE
KO = KEY_OK

def joueur_joue(j=1):
  k = 9
  while k == 9: #Le tour n'est pas encore joué
    for touche in [30,31,32,36,37,38,42,43,44]:
      case = touche%5+touche//3-10
      if kd(touche) and Jeu[case] == "":
        k = case
  Jeu[k] = j
  draw_string(str(j),72+30*(k%3),80+30*(k//3),'red')

def IA_joue(i = 9):
  if i == 9:
    i = choice([k for k in range(len(Jeu)) if Jeu[k] == ""])
  Jeu[i] = 0
  sleep(t)
  draw_string(str(j),72+30*(i%3),80+30*(i//3),'red')

def tr1(k): #Symétrie d'axe vertical
  return 2+6*(k//3)-k

def tr2(k): #Symétrie d'axe horizontal
  return 6+2*(k%3)-k

def tr3(k): #Symétrie d'axe la 1ère diagonale
  return 4*(k//3+k%3)-k

def tr4(k): #Symétrie d'axe la 2ème diagonale
  return 4+2*(k//3+2-k%3)-k

def trans(k):
  for rang in range(4):
    if [k1,k2,k3,k4][rang]:
      k = [tr1,tr2,tr3,tr4][rang](k)
  return k

def triplet(a,b,c):
  global gagnant,couleur
  if Jeu[a] in [0,1] and Jeu[a] == Jeu[b] == Jeu[c]:
    gagnant = Jeu[a]
    couleur = ['blue','green'][gagnant]
    for d in [a,b,c]:
      draw_string(str(Jeu[d]),72+30*(d%3),80+30*(d//3),couleur)
  
def jeu_fini(Jeu):
  global gagnant
  gagnant = 2 #Pas de gagnant
  for k in range(4):
    triplet(k,4,8-k)
    triplet(max(0,4*k-6),1+2*k,min(8,4*k+2))
  if gagnant < 2:
    draw_string("Joueur"+str(gagnant)+" a gagné",84,25,couleur)
    score[gagnant] += 1
    draw_string(str(score[gagnant]),270,100+20*gagnant,couleur)
    return True
  elif Jeu.count("") == 0:
    draw_string("Personne n'a gagné",68,25)
    return True
  return False

def JcJ():
  global j
  while not jeu_fini(Jeu):
    joueur_joue(j)
    j = 1-j

def JcIA_facile():
  global j
  if j == 0:
    IA_joue()
    j = 1-j
  while not jeu_fini(Jeu):
    [IA_joue,joueur_joue][j]()
    j = 1-j

def enchainement(a,b,c,d,e):
  IA_joue(trans(a))
  joueur_joue()
  if Jeu[trans(b)] == "":
    IA_joue(trans(b))
  else:
    petit_ench(c,d,e)

def petit_ench(a,b,c):
  IA_joue(trans(a))
  joueur_joue()
  if Jeu[trans(b)] == "":
    IA_joue(trans(b))
  else:
    IA_joue(trans(c))

def JcIA():
  j = 0
  IA_joue(trans(0))
  joueur_joue()

  if Jeu[trans(4)]:
    petit_ench(2,1,7)
    if not jeu_fini(Jeu):
      joueur_joue()
      for k in [3,5,6,8]:
        if Jeu[trans(k)]:
          petit_ench(14-k-6*(k%2),11-k,k-3+6*(k%2))
          break
  else:
    for k in range(1,9):
      if k%2 and Jeu[trans(k)]:
        enchainement(4,8,2*abs(k-4),abs(k-4),8-2*abs(k-4))
        break
      elif k%2 == 0 and k != 4 and Jeu[trans(k)]: 
        enchainement(1+abs(7-k),(1+abs(7-k))//2,min(8,14-k),4,min(7,11-k))
        break
  jeu_fini(Jeu)  

def partie():
  global j,score,t,mode,diff,k1,k2,k3,k4
  draw_string("Appuyez sur EXE",84,46,(160,0,255))
  while not (kd(KE) or kd(KO)):True
  draw_string(" "*15,84,46)
  draw_string(" "*20,60,25)
  for d in range(9):
    draw_string(" ",72+30*(d%3),80+30*(d//3))

  [k1,k2,k3,k4] = [randint(0,1) for i in range(4)]
  [[JcJ],[JcIA_facile,JcIA]][mode][diff]()

def menu():
  d = 0
  m = 0
  draw_string("Choix du mode :",85,30,'orange')
  draw_string("Joueur contre joueur",60,60,'blue')
  draw_string("Joueur contre IA",80,82,'blue')
  while kd(KE) or kd(KO):True
  while not (kd(KE) or kd(KO)):
    draw_string("[",60+20*m-2*10,60+22*m,'blue')
    draw_string("]",60+21*10-20*m,60+22*m,'blue')
    if kd(KEY_DOWN) or kd(KEY_UP):
      draw_string(" ",60+20*m-2*10,60+22*m)
      draw_string(" ",60+21*10-20*m,60+22*m)
      m = 1-m
      while kd(KEY_DOWN) or kd(KEY_UP):True

  if m:
    while kd(KE) or kd(KO):
      draw_string("Choix de la difficulté :",40,132,'orange')
      draw_string("Facile",130,162,'blue')
      draw_string("Difficile",115,184,'blue')
    while kd(KE) or kd(KO):True
    while not (kd(KE) or kd(KO)):
      draw_string("[",130-15*d-2*10,162+22*d,'blue')
      draw_string("]",130+7*10+15*d,162+22*d,'blue')
      if kd(KEY_DOWN) or kd(KEY_UP):
        draw_string(" ",130-15*d-2*10,162+22*d)
        draw_string(" ",130+7*10+15*d,162+22*d)
        d = 1-d
        while kd(KEY_DOWN) or kd(KEY_UP):True
  fill_rect(2,5,315,210,'white')
  return m,d

global Jeu
score = [0,0]
j = 0 #IA ou joueur0 commence
t = 0.5
mode,diff = menu()

for a in range(65,160,30):
    fill_rect(65-3,a+8,91,1,'orange')
    fill_rect(a-3,73,1,90,'orange')
for n in [0,1]:
  draw_string("Joueur"+str(n)+" : "+str(0),170,100+20*n,['blue','green'][n])

while 1:
  Jeu=[""]*9
  partie()

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.