class jeu:
def __init__(self):
self.plateau=[[0 for i in range(7)]for i in range(6)]
def afficher(self):
for i in range(6):
for j in range(7):
if self.plateau[i][j]==0:
print("| ",end="")
if self.plateau[i][j]==1:
print("|X",end="")
if self.plateau[i][j]==2:
print("|O",end="")
print()
print(" - - - - - - -")
print(" 1 2 3 4 5 6")
def combiendanscolonne(self,k):
s=0
for i in range (6):
if self.plateau[5-i][k-1]!=0:
s=s+1
return (s)
def empiler(self,k,c):
n=self.combiendanscolonne(k)
if n<6:
self.plateau[5-n][k-1]=c
def test_ligne(self):
L=self.plateau
for i in range (6):
for j in range (4):
if L[i][j]==L[i][j+1]==L[i][j+2]==L[i][j+3]!=0:
return L[i][j]
def test_colone(self):
L = self.plateau
for i in range(3):
for j in range(7):
if L[i][j] == L[i + 1][j] == L[i + 2][j] == L[i + 3][j] != 0:
return L[i][j]
def test_diagonale(self):
L = self.plateau
for i in range(3):
for j in range(4):
if L[i][j] == L[i + 1][j + 1] == L[i + 2][j + 2] == L[i + 3][j + 3] != 0:
return L[i][j]
for i in range(3, 6):
for j in range(4):
if L[i][j] == L[i - 1][j + 1] == L[i - 2][j + 2] == L[i - 3][j + 3] != 0:
return L[i][j]
def rejouer():
while True:
reponse = input("Voulez-vous rejouer ? (O/N) : ").strip().lower()
if reponse == 'o':
return True
elif reponse == 'n':
return False
else:
print("Réponse invalide. Veuillez répondre par O ou N.")
def game():
Monjeu = jeu()
current_player = 1
while True:
while True:
k1 = int(input("joueur 1 donner la colone :"))
if (1 <= k1 <= 7) and Monjeu.combiendanscolonne(k1) < 6:
Monjeu.empiler(k1, c=1)
break
else:
print("non")
Monjeu.afficher()
if Monjeu.test_ligne() == 1 or Monjeu.test_colone() == 1 or Monjeu.test_diagonale() == 1:
print("joueur 1 a gagné")
break
while True:
k2 = int(input("joueur 2 donner la colone :"))
if (1 <= k2 <= 7) and Monjeu.combiendanscolonne(k2) < 6:
Monjeu.empiler(k2, c=2)
break
else:
print("non")
Monjeu.afficher()
if Monjeu.test_ligne() == 2 or Monjeu.test_colone() == 2 or Monjeu.test_diagonale() == 2:
print("joueur 2 a gagné")
break
if rejouer():
game()
else:
print("Merci d'avoir joué !")
game()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter