puissance4.py

Created by ph-moutou

Created on June 12, 2018

2.69 KB

Ce programme simule le jeu “puissance 4” commercialisé en 1974 par la société MB et détenu depuis 1984 par la société Hasbro (info Wikipedia). Le joueur 1 (jaune) commence. Les 2 joueurs entrent alternativement un numéro de colonne; à la fin de la partie (quand il y a un alignement de 4 jetons d’un joueur), pour rejouer, il faut entrer 1 (entrer 0 arrête le jeu) et c’est à l’autre joueur de commencer.


from kandinsky import *
from math import *

def rectangle(x,y,c,lon,lar):
  for i in range(lon):
    for j in range(lar):
      set_pixel(x+i,y+j,c) 

def cercle(x0,y0,r,c,e):
  for i in range(2*e):
    xd=x0-int((r-i*0.5)/sqrt(2))
    xf=x0+int((r-i*0.5)/sqrt(2))
    for x in range(xd,xf+1):
      x1=x
      y1=y0+int(sqrt((r-i*0.5)**2-(x-x0)**2))
      set_pixel(x,y1,c)
      for j in range(3):
        x2=x0+y1-y0
        y2=y0+x0-x1
        set_pixel(x2,y2,c)
        x1,y1=x2,y2
        
def controler(l):
  for lig in range(6):
    H=[]#control horizontal
    for col in range(7):
      H.append(l[col][lig])
    for pos in range(4):
      if H[pos]!=0 and H[pos]==H[pos+1] and H[pos]==H[pos+2] and H[pos]==H[pos+3]:
        return H[pos]
  for col in range(7):#control vertical
    for lig in range(3):
      if l[col][lig]!=0 and l[col][lig]==l[col][lig+1] and l[col][lig]==l[col][lig+2] and l[col][lig]==l[col][lig+3]:
        return l[col][lig]
  for col in range(4):#control oblique1
    for lig in range(3):
      if l[col][lig]!=0 and l[col][lig]==l[col+1][lig+1] and l[col][lig]==l[col+2][lig+2] and l[col][lig]==l[col+3][lig+3]:
        return l[col][lig]
  for col in range(4):#control oblique2
    for lig in range(3):
      if l[6-col][lig]!=0 and l[6-col][lig]==l[5-col][lig+1] and l[6-col][lig]==l[4-col][lig+2] and l[6-col][lig]==l[3-col][lig+3]:
        return l[6-col][lig]
  return 0
  
def plateau():
  rectangle(55,20,color(0,42,224),210,180)
  for i in range(42):
    cercle(70+30*(i//6),35+30*(i%6),12,color(255,255,255),12)
  for i in range(7):
    draw_string(str(i+1),65+30*i,203)
    
def placer(col,lig,j):
  coul=[color(255,174,0),color(255,0,0)]
  cercle(70+30*(col),35+30*(lig),12,coul[j-1],12)
  
def afficher(c):
  draw_string("coup {}".format(c),125,0)
  return c  

def modifier(s):
  draw_string(str(s[0]),25,30)
  draw_string(str(s[1]),285,30)
  return s

def p():
  plateau()
  draw_string("joueur 1",5,0)
  draw_string("joueur 2",235,0)
  cercle(29,37,20,color(255,174,0),5)
  cercle(289,37,20,color(255,0,0),5)
  score=[0,0]
  modifier(score)
  L=[6*[0],6*[0],6*[0],6*[0],6*[0],6*[0],6*[0]]
  ligne=False
  joueur=1
  cp=0
  afficher(cp)
  while ligne==False:
    place,n=0,0
    while n==0 or n>7 or place>5:
      n=int(input())
      while L[n-1][place]!=0:
        place+=1
    L[n-1][place]=joueur
    placer(n-1,5-place,joueur)
    cp+=1
    afficher(cp)
    fin=controler(L)
    if fin==0:
      joueur=(joueur)%2+1
    else:
      score[fin-1]+=1
      modifier(score)
      ligne=True
      e=int(input("nouveau? (1:oui,0:non)"))
      if e>=1:
        cp=0
        joueur=joueur%2+1
        plateau()
        L=[6*[0],6*[0],6*[0],6*[0],6*[0],6*[0],6*[0]]
        ligne=False