Problème de copie dans le code de mon projet
Jotaro1210
-
Utilisateur anonyme -
Utilisateur anonyme -
Bonjour, Voici un long projet dans lequel je suis. Le but du projet est très simple: Faire un petit jeu d'echec composé que de pion en terminal.
Vers la fin de mon code, il y a une boucle et chaque tour de boucle représente un tour de jeu.
Je vous décris ligne par ligne mon problème:
# L.149: 'plateau' est passé dans init_board(n) et est donc une matrice avec 2,1 et 0
# L.150: 'plateau_liste' est passé dans init_board(n) et est donc une matrice avec 2,1 et 0
# L.154: 'plateau' est passé dans print_board(board) et ça print donc le plateau
# L.169: play_move modifie 'plateau_liste' suite à un tour joué
# L.170: plateau est modifié pour redevenir une matrice que avec des 2,1 et 0
# Après avoir passé une deuxième fois dans la boucle, le plateau est donc imprimé avec le pion deplacé mais 'plateau_liste'
# est complètement modifié je ne sais pas pourquoi !
if __name__ == '__main__':
def main(n):
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v'
, 'w', 'x', 'y', 'z']
chiffres=['0', '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']
def init_board(n): #Affichage du plateau sous forme de matrice avec W=1, B=2 et .=0
board = []
for i in range(n, n - 2, -1):
liste_b = []
for a in range(n):
liste_b.append(2)
board.append(liste_b)
for i in range(n - 2, 2, -1):
liste_point = []
for a in range(n):
liste_point.append(0)
board.append(liste_point)
for i in range(2, 0, -1):
liste_w = []
for a in range(n):
liste_w.append(1)
board.append(liste_w)
res = board
return res
def print_board(board):
for i in board: # Remplace les 1 par les W, les 2 par les B et les 0 par les .
for a in range(len(i)):
if i[a] == 0:
i[a] = '.'
elif i[a] == 1:
i[a] = 'W'
elif i[a] == 2:
i[a] = 'B'
' '.join(i) # Retirement des crochets
longueur=len(board)
for i in board: # Insertion des chiffres designant les numéros de ligne et insertion des '|'
k = str(longueur)
if longueur < 10:
i.insert(0, ' ')
i.insert(0, k)
i.insert(0, ' ')
else:
i.insert(0, ' ')
i.insert(0, k[1])
i.insert(0, k[0])
i.insert(3, '|')
i.insert(len(board)+4, '|')
longueur -= 1
liste_0 = [' ', ' ', ' ', ' '] # Insertion des tirets '-' au dessus du plateau
for i in range(len(board)):
liste_0.append('-')
board.insert(0, liste_0)
liste_0 = [' ', ' ', ' ', ' '] # Insertion des tirets '-' en dessous du plateau
for i in range(len(board) - 1):
liste_0.append('-')
board.append(liste_0)
liste_0 = [' ', ' ', ' ', ' '] # Insertion des lettres de l'alphabet designant les les colonnnes
for i in range(len(board)-2):
liste_0.append(alphabet[i])
board.append(liste_0)
for i in board:
print(' '.join(i))
return None
def input_move(board, player):# -> input : str
play='ERROR'
if player == 1:
play = str(input('Joueur Blanc ,entrez votre coup: '))
elif player == 2:
play = str(input('Joueur Noir ,entrez votre coup: '))
if play[0]not in alphabet: # Cas initial: si la première variable n'est pas une lettre
input_move(board, player)
if '>' not in play: # Si > n'est pas dans le coup
input_move(board, player)
if len(play) == 5: # cas: a1>a2 ca marche !
if play[0] and play[3] not in alphabet:
input_move(board, player)
if play[1] and play[4] not in chiffres:
input_move(board, player)
elif len(play) == 6: # a23>a1 ca marche !
if play[2] in chiffres:
if play[0] and play[4] not in alphabet:
input_move(board, player)
if play[1:3] and play[-1] not in chiffres:
input_move(board, player)
else: # a2>a23
if play[0] and play[3] not in alphabet:
input_move(board, player)
if play[1] and play[4:6] not in chiffres:
input_move(board, player)
elif len(play) == 7:
if play[0] and play[4] not in alphabet:
input_move(board, player)
if play[1:3] and play[5:7] not in chiffres:
input_move(board, player)
res=play
return res
def extract_pos(n, str_pos): # -> pos : Tuple[int, int] ou None
if str_pos[0] not in alphabet:
return None
a = 0
for i in range(len(alphabet)):
if alphabet[i] == str_pos[0]:
a = i
if i > n:
return None
le_chiffre = str_pos[1:]
le_chiffre = int(le_chiffre)
le_chiffre = n-le_chiffre
if le_chiffre > n:
return None
return le_chiffre, a
def play_move(board, move, player ):
x = move[0][0]
y = move[0][1]
x_prim = move[1][0]
y_prim = move[1][1]
board[x_prim][y_prim] = board[x][y]
board[x][y] = 0
vainqueur= None
joueur=1
mouve='ERROR'
partie_1 = mouve
partie_2 = mouve
#149 plateau = init_board(n)
#150 plateau_liste = init_board(n)
changement_joueur = 1
a=1
while vainqueur == None:
#154 print(print_board(plateau))
# C'est ici qui modifie mon plateau_liste mais jsp pourquoi ??
mouve=input_move(plateau_liste,joueur)
# On partage le 'a2>a3' en deux groupes :'a2' et 'a3'
for i in range(len(mouve)):
if mouve[i]=='>':
partie_1=mouve[0:i]
partie_2=mouve[i+1:]
position_initiale = extract_pos(n,partie_1)
position_finale = extract_pos(n,partie_2)
move=(position_initiale,position_finale)
#169 play_move(plateau_liste,move,joueur)
#170 plateau=plateau_liste
changement_joueur+=1
a=-a
return None
if __name__ =='__main__':
main(7)
Vers la fin de mon code, il y a une boucle et chaque tour de boucle représente un tour de jeu.
Je vous décris ligne par ligne mon problème:
# L.149: 'plateau' est passé dans init_board(n) et est donc une matrice avec 2,1 et 0
# L.150: 'plateau_liste' est passé dans init_board(n) et est donc une matrice avec 2,1 et 0
# L.154: 'plateau' est passé dans print_board(board) et ça print donc le plateau
# L.169: play_move modifie 'plateau_liste' suite à un tour joué
# L.170: plateau est modifié pour redevenir une matrice que avec des 2,1 et 0
# Après avoir passé une deuxième fois dans la boucle, le plateau est donc imprimé avec le pion deplacé mais 'plateau_liste'
# est complètement modifié je ne sais pas pourquoi !
if __name__ == '__main__':
def main(n):
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v'
, 'w', 'x', 'y', 'z']
chiffres=['0', '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']
def init_board(n): #Affichage du plateau sous forme de matrice avec W=1, B=2 et .=0
board = []
for i in range(n, n - 2, -1):
liste_b = []
for a in range(n):
liste_b.append(2)
board.append(liste_b)
for i in range(n - 2, 2, -1):
liste_point = []
for a in range(n):
liste_point.append(0)
board.append(liste_point)
for i in range(2, 0, -1):
liste_w = []
for a in range(n):
liste_w.append(1)
board.append(liste_w)
res = board
return res
def print_board(board):
for i in board: # Remplace les 1 par les W, les 2 par les B et les 0 par les .
for a in range(len(i)):
if i[a] == 0:
i[a] = '.'
elif i[a] == 1:
i[a] = 'W'
elif i[a] == 2:
i[a] = 'B'
' '.join(i) # Retirement des crochets
longueur=len(board)
for i in board: # Insertion des chiffres designant les numéros de ligne et insertion des '|'
k = str(longueur)
if longueur < 10:
i.insert(0, ' ')
i.insert(0, k)
i.insert(0, ' ')
else:
i.insert(0, ' ')
i.insert(0, k[1])
i.insert(0, k[0])
i.insert(3, '|')
i.insert(len(board)+4, '|')
longueur -= 1
liste_0 = [' ', ' ', ' ', ' '] # Insertion des tirets '-' au dessus du plateau
for i in range(len(board)):
liste_0.append('-')
board.insert(0, liste_0)
liste_0 = [' ', ' ', ' ', ' '] # Insertion des tirets '-' en dessous du plateau
for i in range(len(board) - 1):
liste_0.append('-')
board.append(liste_0)
liste_0 = [' ', ' ', ' ', ' '] # Insertion des lettres de l'alphabet designant les les colonnnes
for i in range(len(board)-2):
liste_0.append(alphabet[i])
board.append(liste_0)
for i in board:
print(' '.join(i))
return None
def input_move(board, player):# -> input : str
play='ERROR'
if player == 1:
play = str(input('Joueur Blanc ,entrez votre coup: '))
elif player == 2:
play = str(input('Joueur Noir ,entrez votre coup: '))
if play[0]not in alphabet: # Cas initial: si la première variable n'est pas une lettre
input_move(board, player)
if '>' not in play: # Si > n'est pas dans le coup
input_move(board, player)
if len(play) == 5: # cas: a1>a2 ca marche !
if play[0] and play[3] not in alphabet:
input_move(board, player)
if play[1] and play[4] not in chiffres:
input_move(board, player)
elif len(play) == 6: # a23>a1 ca marche !
if play[2] in chiffres:
if play[0] and play[4] not in alphabet:
input_move(board, player)
if play[1:3] and play[-1] not in chiffres:
input_move(board, player)
else: # a2>a23
if play[0] and play[3] not in alphabet:
input_move(board, player)
if play[1] and play[4:6] not in chiffres:
input_move(board, player)
elif len(play) == 7:
if play[0] and play[4] not in alphabet:
input_move(board, player)
if play[1:3] and play[5:7] not in chiffres:
input_move(board, player)
res=play
return res
def extract_pos(n, str_pos): # -> pos : Tuple[int, int] ou None
if str_pos[0] not in alphabet:
return None
a = 0
for i in range(len(alphabet)):
if alphabet[i] == str_pos[0]:
a = i
if i > n:
return None
le_chiffre = str_pos[1:]
le_chiffre = int(le_chiffre)
le_chiffre = n-le_chiffre
if le_chiffre > n:
return None
return le_chiffre, a
def play_move(board, move, player ):
x = move[0][0]
y = move[0][1]
x_prim = move[1][0]
y_prim = move[1][1]
board[x_prim][y_prim] = board[x][y]
board[x][y] = 0
vainqueur= None
joueur=1
mouve='ERROR'
partie_1 = mouve
partie_2 = mouve
#149 plateau = init_board(n)
#150 plateau_liste = init_board(n)
changement_joueur = 1
a=1
while vainqueur == None:
#154 print(print_board(plateau))
# C'est ici qui modifie mon plateau_liste mais jsp pourquoi ??
mouve=input_move(plateau_liste,joueur)
# On partage le 'a2>a3' en deux groupes :'a2' et 'a3'
for i in range(len(mouve)):
if mouve[i]=='>':
partie_1=mouve[0:i]
partie_2=mouve[i+1:]
position_initiale = extract_pos(n,partie_1)
position_finale = extract_pos(n,partie_2)
move=(position_initiale,position_finale)
#169 play_move(plateau_liste,move,joueur)
#170 plateau=plateau_liste
changement_joueur+=1
a=-a
return None
if __name__ =='__main__':
main(7)
1 réponse
-
Bonjour
En Python, l’indentation est primordiale, or par défaut le site ne la conserve pas.
Pour ce faire, il faut utiliser les balises de code, voir ici https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
On pourra essayer de d’aider après que tu aies reposté correctement ton code.