hexapawn.py

Created by schraf

Created on May 17, 2022

3.05 KB

Explications en vidéo

But : Essayez d’arriver à 15 parties gagnantes avant la machine !

Dans la vidéo je dis que s’il y a un blocage, c’est qu’il reste le même nombre de pions blancs et de pions noirs, ce n’est pas exact :

X.X
X.O
O..

est une situation bloquée et pourtant les nombres de pions sont différents ;-) J’ai donc changé le test et mis tout simplement :

if len(jouables(position)) == 0: return 1

En d’autres termes, s’il n’y a pas de déplacement possible, c’est que la situation est bloquée.


from random import choice

strategie = {}
gagne = [0,0]

def surPlateau(n): return 1 <= n <= 9
def colGauche(n): return n % 3 == 1
def colDroite(n): return n % 3 == 0

def key(position):
  [hu,ia] = position
  plateau = ""
  for k in [7,8,9,4,5,6,1,2,3]:
    if k in hu: plateau += 'O'
    elif k in ia: plateau += 'X'
    else: plateau += ' '
  return plateau

def jouables(position):
  global strategie
  cle = key(position)
  if cle in strategie:
    return strategie[cle]
  [hu,ia] = position
  dep = []
  for k in ia:
    avancer = k - 3
    if surPlateau(avancer) and avancer not in hu and avancer not in ia:
      dep += [10 * k + avancer]
    droite = k - 2
    if surPlateau(droite) and not(colDroite(k)) and droite in hu:
      dep += [10 * k + droite]
    gauche = k - 4
    if surPlateau(gauche) and not(colGauche(k)) and gauche in hu:
      dep += [10 * k + gauche]
  strategie[cle] = dep
  return dep

def aff(c): print(c, end='')

def affiche(position):
  [hu,ia] = position
  plateau = ""
  for k in [7,8,9,4,5,6,1,2,3]:
    if k in hu: aff("O")
    elif k in ia: aff("X")
    else: aff(".")
    if k % 3 == 0: print()
  print("-"*10 + " (J:{} - IA:{})".format(gagne[0],gagne[1]))
  
def gagnante(position):
  [hu,ia] = position
  hu.sort()
  ia.sort()  
  if any([v in [7,8,9] for v in hu]): return 1
  if any([v in [1,2,3] for v in ia]): return 1
  if len(hu) == 0 or len(ia) == 0: return 1
  if len(jouables(position)) == 0: return 1
  return 0
  
def humain(coup, position):
 [hu,ia] = position
 [d,a] = [int(v) for v in coup]
 if not(d in hu): return False
 ti = a in ia
 tg = colGauche(d)
 td = colDroite(d)
 tv = not(ti) and not(a in hu)
 if a == d + 3 and tv: return True
 if a == d + 2 and not(tg) and ti: return True
 if a == d + 4 and not(td) and ti: return True
 return False

def jouer(coup, position, xo):
  global strategie
  [hu, ia] = position
  [d, a] = [int(v) for v in coup]
  if xo == 0:
    hu.remove(d)
    hu += [a]
    if a in ia: ia.remove(a)
  else:
    ia.remove(d)
    ia += [a]
    if a in hu: hu.remove(a)
    print("Je joue", coup)  
  affiche([hu, ia])
  return [hu, ia]    

def hexapawn():
  global gagne
  while True:
    position = [[1,2,3],[7,8,9]]
    print("-"*10)
    affiche(position)
    while gagnante(position) == 0:
      valide = False
      while not(valide):
        coup = input("Votre coup (ex: 14 ou 0) ? ")
        if coup == '0': return
        valide = humain(coup, position)
        if valide: position = jouer(coup, position, 0)
        else: print("Vous ne pouvez pas jouer ca !")
      if gagnante(position):
        print('Vous gagnez...')
        gagne[0] += 1
        strategie[historique[0]].remove(historique[1])
      else:
        iacherche = choice(jouables(position))
        historique = [key(position)]
        jouer(str(iacherche), position, 1)
        historique += [iacherche]
        if gagnante(position):
          print('Je gagne...')
          gagne[1] += 1
          s = strategie[historique[0]]
          if any(v != historique[1] for v in s):
           strategie[historique[0]] += [historique[1]]      
hexapawn()

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>.