Skip to content

Instantly share code, notes, and snippets.

@HarmoGlace
Last active September 11, 2022 19:20
Show Gist options
  • Select an option

  • Save HarmoGlace/b5f9f82af6895f108b581400d0fd0b81 to your computer and use it in GitHub Desktop.

Select an option

Save HarmoGlace/b5f9f82af6895f108b581400d0fd0b81 to your computer and use it in GitHub Desktop.
connect4.py
from kandinsky import *
from ion import *
from random import randint
from time import sleep
kd = keydown
KE = KEY_EXE
KO = KEY_OK
def init():
global Jeu
Jeu = [[9 for m in range(7)] for n in range(6)]
# nettoyage du haut de l'écran
fill_rect(0,0,320,39,'white')
# nettoyage du plateau de jeu
for i in range(7):
for j in range(6):
jeton(i,j,'white')
# Affichage du curseur
def curseur(x,couleur):
draw_string(["v"," "][couleur=='white'],30+30*x,22,couleur)
# Déplacement du curseur
def deplacement():
global x
dx = kd(KEY_RIGHT)-kd(KEY_LEFT)
if dx != 0:
curseur(x,'white')
x = (x+dx)%7
curseur(x,couleur)
while kd(KEY_LEFT) or kd(KEY_RIGHT):True
def descente(x):
global Jeu
l = 0
while l < 6 and Jeu[l][x] == 9: # tant que la case est libre
sleep(0.1)
if l > 0:
jeton(x,l-1,'white')
Jeu[l-1][x] = 9
jeton(x,l,couleur)
Jeu[l][x] = joueur
l += 1
while kd(KE) or kd(KO):True
return l-1
def jeton(x,y,couleur):
draw_string(["o"," "][couleur=='white'],30+30*x,46+30*y,couleur)
def remplir(x,y,couleur):
fill_rect(33+30*x,54+30*y,4,4,couleur)
def verif(lig,coeff1,col,coeff2,N):
global voisins
for var in range(1,N+1):
if Jeu[lig+coeff1*var][col+coeff2*var] == k:
voisins+=1
liste_gagnant.append([lig+coeff1*var,col+coeff2*var])
else:break
def comparaison(a,b,c,d,e,f,g,h,i,j):
global voisins,maxi_voisins,liste_gagnant
voisins = 1
liste_gagnant = [[y,x]]
verif(a,b,c,d,e)
verif(f,g,h,i,j)
if voisins >= 4:
for elt in liste_gagnant:
remplir(elt[1],elt[0],couleur)
maxi_voisins = max(voisins,maxi_voisins)
def fin():
global k,maxi_voisins
k = joueur
maxi_voisins = 0
comparaison(y,-1,x,0,y,y,1,x,0,5-y) #colonne
comparaison(y,0,x,-1,x,y,0,x,1,6-x) #ligne
comparaison(y,-1,x,-1,min(x,y),y,1,x,1,min(6-x,5-y)) #diag1
comparaison(y,1,x,-1,min(x,5-y),y,-1,x,1,min(6-x,y)) #diag2
if maxi_voisins >= 4:
draw_string("Joueur"+str(k+1)+" a gagné : Puissance"+str(maxi_voisins)+" "+"!"*min((maxi_voisins-3),3),10-5*min((maxi_voisins-4),2),3,couleur)
curseur(x,'white')
draw_string("Appuyer sur EXE",85,21,(160,0,255))
draw_string("Score : ",240,110,'purple')
score[k] += 1
draw_string("J"+str(k+1)+" : "+str(score[k]),240,128+18*k,couleur)
if score[1-k] == 0:
draw_string("J"+str(2-k)+" : 0",240,110+18*(2-k),['red','blue'][k])
return True
elif str(Jeu).count('9') == 0:
draw_string("Personne n'a gagné",70,3)
curseur(x,'white')
draw_string("Appuyer sur EXE",85,21,(160,0,255))
return True
return False
joueur = 0 # vaudra 0 ou 1
x = 3
for i in range(8): # tracé du plateau
fill_rect(20+30*i,40,1,181,'orange')
fill_rect(20,40+min(30*i,30*6),211,1,'orange')
score = [0,0]
init()
while True:
couleur = ['blue','red'][joueur]
curseur(x,couleur)
while not (kd(KE) or kd(KO)):
deplacement()
if Jeu[0][x] == 9:
y = descente(x)
if fin():
while not (kd(KE) or kd(KO)):True
init()
while kd(KE) or kd(KO):True
joueur = 1-joueur
from math import *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment