MakeProfit
Membre
- Inscription
- 14 Juin 2013
- Messages
- 111
- Réactions
- 22
- Points
- 8 956
Bonsoir a tous,
J'ai le code d'un Puissance 4 en Python avec Processing, cela fait un moment que j'essaye de le comprendre mais je ne capte pas tout.
Par exemple, pour la fonction def verify():
Je n'arrive pas a bien la comprendre, je sais quelle vérifie le vainqueur, les alignements (Honrizontal, vertical, et diagonales) mais je ne comprend pas comment.
Je cherche une personne de calé en Python qui pourrais m'éclairer un peu ce code.
J'ai le code d'un Puissance 4 en Python avec Processing, cela fait un moment que j'essaye de le comprendre mais je ne capte pas tout.
Par exemple, pour la fonction def verify():
Je n'arrive pas a bien la comprendre, je sais quelle vérifie le vainqueur, les alignements (Honrizontal, vertical, et diagonales) mais je ne comprend pas comment.
Je cherche une personne de calé en Python qui pourrais m'éclairer un peu ce code.
Python:
grid = [[[] for j in range(6)] for i in range(7)] #Grille de 6 par 7
dir = [(-1, -1), (-1, 1), (0, -1), (1, 0)] #On défini un repere
coef = 100
player = "Red"
def setup(): # 7*6
size(coef*7, coef*6)
def draw():
background(50)
for i in range(7):
for j in range(6):
fill(200)
rect(i*(width/7), j*(height/6), width/7, height/6)
if grid[i][j] != []:
fill(grid[i][j][0], grid[i][j][1], grid[i][j][2])
ellipse(i*(width/7) + width/14, j*(height/6) + height/12, width/8, height/7)
def recur(i, j, apply, search, depth):
if depth == 4:
return True
ib = i + apply[0]
jb = j + apply[1]
if 0 <= ib < 7 and 0 <= jb < 6:
if grid[ib][jb] == search:
return recur(ib, jb, apply, search, depth+1)
return False
def verify(): #Fonction qui verifie les alignement et ainsi le vaiqueur
winner = 0
for i in range(7):
for j in range(6):
search = grid[i][j]
if search != []:
for apply in dir:
test = recur(i, j, apply, search, 1)
if test == True:
winner = search
if winner != 0:
noLoop()
#background(0)
if winner == [200, 50, 50]:
winner = "Rouge"
else:
winner = "Jaune"
fill(0)
textSize(50)
text("{} a gagne".format(winner), 100, height/2)
def mousePressed(): #Fonction qui permet de savoir quand est-ce que l'utilisateur a cliqué
global player, grid
if player == "red":
pos = mouseX//(width/7)
for j in range(len(grid[pos])-1, -1, -1):
if grid[pos][j] == []:
grid[pos][j] = [200, 50, 50]
verify()
player = "yellow"
return 0
else:
pos = mouseX//(width/7)
for j in range(len(grid[pos])-1, -1, -1):
if grid[pos][j] == []:
grid[pos][j] = [200, 200, 50]
verify()
player = "red"
return 0
Merci pour votre aide !