Bonjour, je suis actuellement en première et je dois programmer un morpion en python. J’ai quasiment fini mais je n’arrive pas à faire terminer le jeu, il continue malgré les fonctions qui permettent d’y mettre fin. J’ai également reçu de l’aide de mon professeur qui m’a donné des pistes, mais je n’arrive toujours pas à voir la faute. ( ci-joint le message de mon professeur et le code ).
PS : Les commentaires sont en anglais car je travaille avec une anglophone.`py
#import
import random
import sys
#grid declaration
line1 =["00","01","02"]
line2 =["10","11","12"]
line3 =["20","21","22"]
grid=[line1, line2, line3]
#function to show the grid
def showGrid() :
print(line1[0], line1[1], line1[2])
print(line2[0], line2[1] ,line2[2])
print(line3[0], line3[1], line3[2])
#function with the whole game
def turn() :
showGrid()
#The player choose what case he wants to play
playerChoice=input("What case do you want to play ( you'll play the crosses) : ")
#Three loops, one for each line, to put the "X" at the chosen spot
n=0
for case in line1:
if case == playerChoice:
line1[n] = "X"
n+=1
o=0
for case in line2:
if case == playerChoice:
line2[o] = "X"
o+=1
p=0
for case in line3:
if case == playerChoice:
line3[p] = "X"
p+=1
#The "A.I." choose randomly a case among the "aiGrid" list
aiGrid=["00", "01", "02", "10", "11", "12", "20", "21", "22"]
played = False
while played == False:
aiChoice= random.choice(aiGrid)
if grid[int(aiChoice[0])][int(aiChoice[1])] != "O" and grid[int(aiChoice[0])][int(aiChoice[1])] != "X":
grid[int(aiChoice[0])][int(aiChoice[1])] = "O"
played = True
else:
played = False
#function to detect if the player wins
def playerWin(gameFinished):
#lines and columns
for i in range(3):
if all("X" == grid[i][j] for j in range(3)) or all(
"X" == grid[j][i] for j in range(3)
):
gameFinished = True
break
# diagonals
if (
not gameFinished
and all("X" == grid[i][i] for i in range(3))
or all("X" == grid[i][2 - i] for i in range(3))):
gameFinished = True
print("Bravo !")
return gameFinished
#function to detect if the player wins
def aiWin(gameFinished1):
#lines and columns
for i in range(3):
if all("O" == grid[i][j] for j in range(3)) or all(
"O" == grid[j][i] for j in range(3)
):
gameFinished = True
break
#diagonals
if (
gameFinished == False
and all("O" == grid[i][i] for i in range(3))
or all("O" == grid[i][2 - i] for i in range(3))
):
gameFinished1 = True
print("Dommage !")
return gameFinished
#gameFinished is for the player and gameFinished for the A.I.
gameFinished=False
#Main loop
while gameFinished == False or gameFinished1 == False:
turn()
gamefinished=playerWin(gameFinished)
gamefinished=aiWin(gameFinished)
sys.exit()
+0
-0