1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| import random
#-------------------FONCTIONS----------------------
def joueurIA3() :
"""tant que la partie n'est pas gagnée, permet de faire jouer un joueur puis l'ordi au niveau 3"""
p = 1
gagne = False
while not(gagne):
ialvl3(p)
gagne = verif (p)
p = 3 - p
def afficher_grille():
"""afficher la grille"""
for ligne in grille:
print(ligne)
grille = [[0 for j in range(7)] for i in range(6)]
def jouer(p):
"""demande au joueur de jouer dans quelle colonne il veut jouer + si coup impossible, fait rejouer le joueur"""
print ("C'est au joueur",p,"de jouer :")
j = int(input("Joueur "+str(p)+", dans quelle colonne voulez-vous jouer ? "))
if j>6 or j <0 :
print ("Impossible, veuillez choisir une colonne entre 0 et 6 !")
return jouer(p)
if grille[0][j]!= 0:
print ("Impossible, veuillez jouer dans une autre colonne !")
return jouer(p)
i=0
while i<6 and grille[i][j] == 0:
i = i+1
grille[i-1][j] = p
return afficher_grille()
def verif_hori (p):
"""Vérifie si il y a 4 pièces alignés horizontalement"""
for i in range (6):
for j in range (4):
if grille[i][j]==grille[i][j+1]==grille[i][j+2]==grille[i][j+3]==p:
return True
return False
def verif_verti(p):
"""Vérifie si il y a 4 pièces alignés verticalement"""
for i in range (3):
for j in range (7):
if grille[i][j]==p and grille[i+1][j]==p and grille[i+2][j]==p and grille[i+3][j]==p:
return True
return False
def verif_diago (p):
"""Vérifie si il y a 4 pièces alignés diagonalement"""
for i in range (3):
for j in range (4):
if grille[i][j]==grille[i+1][j+1]==grille[i+2][j+2]==grille[i+3][j+3]==p:
return True
for i in range (3):
for j in range (3,7):
if grille[i][j]==grille[i+1][j-1]==grille[i+2][j-2]==grille[i+3][j-3]==p:
return True
return False
def verif (p):
"""Si une vérif est OK alors on affiche le joueur qui a gagné"""
gagne = False
if verif_hori(p)== True :
print("Le joueur", p,"a gagné !")
gagne = True
elif verif_verti(p)== True :
print("Le joueur", p,"a gagné !")
gagne = True
elif verif_diago(p)== True :
print("Le joueur", p,"a gagné !")
gagne = True
return gagne
def choix_adv ():
"""permet de choisir contre qui le joueur veut jouer ainsi que les niveaux de difficulté (contre l'ordinateur)"""
adv=int(input("Contre qui souhaitez-vous jouer : l'ordinateur ? (tapez 1) un autre joueur ? (tapez 2) "))
if adv != 1 and adv != 2 :
print("Veuillez choisir un nombre entre 1 et 2 !")
choix_adv()
if adv==1:
ia=int(input("Quel niveau de difficulté voulez-vous ? (1/2/3): "))
if ia != 1 and ia != 2 and ia != 3 :
print ("Veuillez choisir un niveau entre 1 et 3 !")
choix_adv()
if ia==1 :
joueurIA1()
if ia==2 :
joueurIA2()
if ia ==3 :
joueurIA3()
if adv==2:
return joueur()
def ialvl3(p):
global i, j
"""demande au joueur de jouer + fait jouer l'ordinateur (niveau 3)"""
if p == 1 :
print ("C'est à vous de jouer :")
j = int(input("Dans quelle colonne voulez-jouer ?"))
if j>6 :
print ("impossible, veuillez rejouer !")
return ialvl3(p)
i=0
while i<6 and grille[i][j] == 0:
i = i+1
grille[i-1][j] = p
return afficher_grille()
else:
print("L'ordinateur joue :")
i = 0
Ajoue = False
#VERIF HORIZONTALE
for i in range (5,6):
for j in range (5):
if grille[i][j]==grille[i][j+1]==1 and not(Ajoue):
if grille[i][j+2]==2:
grille[i][j-1]=p
Ajoue = True
else :
grille[i][j+2]=p
Ajoue = True
for i in range (6):
for j in range (5):
if grille[i][j]==grille[i][j+1]==1 and not(Ajoue) :
if grille[i][j+2]==2 and i+1<6 and grille[i+1][j-1]!=0:
grille[i][j-1]=p
Ajoue = True
elif i+1<6 and grille[i+1][j+2]!=0:
grille[i][j+2]=p
Ajoue = True
#VERIF VERTICALE
for i in range (4):
for j in range(7):
if grille[i][j]==grille[i+1][j]==grille[i+2][j]==1 and not(Ajoue):
grille[i-1][j]=p
Ajoue = True
#VERIF DIAGONALE
for i in range (3):
for j in range (4):
if grille[i][j]==grille[i+1][j+1]==grille[i+2][j+2]==p and not(Ajoue) :
grille[i+3][j+3]==p
Ajoue = True
for i in range (3):
for j in range (3,7):
if grille[i][j]==grille[i+1][j-1]==grille[i+2][j-2]==p and not(Ajoue):
grille[i+3][j-3]==p
Ajoue = True
if not(Ajoue):
j = random.randint(0,6)
while i<6 and grille[i][j] == 0:
i = i+1
grille[i-1][j] = p
return afficher_grille()
#-------------------CORPS DU PROGRAMME----------------------
choix_adv() |
Partager