0

I am trying to do a chess game but i have problems and i couldn't find these problems. It is long and hard to read but i am a student and i don't know too much thing in python.

i have a file like that:

move p3 c4

move p4 d3

import sys
f=open(sys.argv[1],'r')
commands=[[line.split()] for line in f.readlines()]
length=len(commands)
counter=0

line8=['R1','N1','B1','QU','KI','B2','N2','R2']
line7=['P1','P2','P3','P4','P5','P6','P7','P8']
line6=[' ',' ',' ',' ',' ',' ',' ',' ']
line5=[' ',' ',' ',' ',' ',' ',' ',' ']
line4=[' ',' ',' ',' ',' ',' ',' ',' ']
line3=[' ',' ',' ',' ',' ',' ',' ',' ']
line2=['p1','p2','p3','p4','p5','p6','p7','p8']
line1=['r1','n1','b1','qu','ki','b2','n2','r2']

for i in range(0,length-1):
    if commands[i][0][0]=='move':
#PAWNS

        if commands[i][0][1] in['p1','p2','p3','p4','p5','p6','p7','p8']:
            letterlist = ['','a','b','c','d','e','f','g','h']
            pawnandnumber=list(commands[i][0][1])
            moveandnumber = list((commands[i][0][2]))
            #LINE BY LINE PAWN MOVES

            if commands[i][0][1] in line2:
                where_is_pawn=line2.index(commands[i][0][1])
                if pawnandnumber[1]==str(letterlist.index(moveandnumber[0])) and (moveandnumber[1]=='3' or '4'):
                    print('>',end=' ')
                    print(commands[i][0])
                    print('OK')
                    if moveandnumber[1]=='3':
                        if line3[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                            line3.pop(where_is_pawn)
                            line2.pop(where_is_pawn)
                            line2.insert(where_is_pawn,' ')
                            line3.insert(where_is_pawn, line2[where_is_pawn])

                    if moveandnumber[1]=='4':
                        if line4[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                            line4.pop(where_is_pawn)
                            line2.pop(where_is_pawn)
                            line2.insert(where_is_pawn, ' ')
                            line4.insert(where_is_pawn, commands[i][0][1])
            if commands[i][0][1] in line3:
                if pawnandnumber[1]==str(letterlist.index(moveandnumber[0])):
                    where_is_pawn = line3.index(commands[i][0][1])
                    if line4[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                        line4.pop(where_is_pawn)
                        line3.pop(where_is_pawn)
                        line3.insert(where_is_pawn, ' ')
                        line4.insert(where_is_pawn, commands[i][0][1])
            if commands[i][0][1] in line4:
                if pawnandnumber[1]==str(letterlist.index(moveandnumber[0])):
                    where_is_pawn = line4.index(commands[i][0][1])
                    if line5[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                        line5.pop(where_is_pawn)
                        line4.pop(where_is_pawn)
                        line4.insert(where_is_pawn,' ')
                        line5.insert(where_is_pawn, commands[i][0][1])
            if commands[i][0][1] in line5:
                if pawnandnumber[1]==str(letterlist.index(moveandnumber[0])):
                    where_is_pawn = line5.index(commands[i][0][1])
                    if line6[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                        line6.pop(where_is_pawn)
                        line5.pop(where_is_pawn)
                        line5.insert(where_is_pawn, ' ')
                        line6.insert(where_is_pawn, commands[i][0][1])

            if commands[i][0][1] in line6:
                if pawnandnumber[1]==str(letterlist.index(moveandnumber[0])):
                    where_is_pawn = line6.index(commands[i][0][1])
                    if line7[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                        line7.pop(where_is_pawn)
                        line6.pop(where_is_pawn)
                        line6.insert(where_is_pawn, ' ')
                        line7.insert(where_is_pawn, commands[i][0][1])

            if commands[i][0][1] in line7:
                if pawnandnumber[1]==str(letterlist.index(moveandnumber[0])):
                    where_is_pawn = line7.index(commands[i][0][1])
                    if line8[where_is_pawn] in ['R1','N1','B1','QU','KI','B2','N2','R2','P1','P2','P3','P4','P5','P6','P7','P8',' ']:
                        line8.pop(where_is_pawn)
                        line7.pop(where_is_pawn)
                        line7.insert(where_is_pawn, ' ')
                        line8.insert(where_is_pawn, commands[i][0][1])


print(line8)
print(line7)
print(line6)
print(line5)
print(line4)
print(line3)
print(line2)
print(line1)

i have an output like that:

> ['move', 'p3', 'c3']
OK
['R1', 'N1', 'p3', 'QU', 'KI', 'B2', 'N2', 'R2']
['P1', 'P2', ' ', 'P4', 'P5', 'P6', 'P7', 'P8']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['p1', 'p2', ' ', 'p4', 'p5', 'p6', 'p7', 'p8']
['r1', 'n1', 'b1', 'qu', 'ki', 'b2', 'n2', 'r2']

p3 should be in 2 upper line and p4 should be 1 upper line what can i do? Or if you think my method is wrong is there an easy way to do it? I am designing a chess game the rows are a,b,c... the lines are 1,2,3... i have an input that contains some commands like 'move' i need to read line by line and print them. Thanks in advance.

Alexandra
  • 27
  • 5
  • You'll have to be more specific about that. – quamrana Dec 12 '20 at 11:01
  • @Alexandra The duplicate addresses exactly the problem in your `if 'p1'or'p2'or'p3'or'p4'or'p5'or'p6'or'p7'or'p8'==commands[i][0][1]:` line. You should solve this first using the answers to the duplicate and then see if you have other problems in your code. – Thierry Lathuille Dec 12 '20 at 11:02
  • This line is incorrect: `if 'R1' or 'R2' or 'N1' or'N2'or 'B1' or 'B2' or 'QU' or 'P1' or 'P2' or 'P3' or 'P4' or 'P5' or 'P6' or 'P7' or 'P8' or ' ' ==line8[where_is_pawn]:` it's always True, as @quamrana 's link – IoaTzimas Dec 12 '20 at 11:02
  • Do you think there are more problems or just this? – Alexandra Dec 12 '20 at 11:04
  • @Alexandra This is the first problem you have, and the suggested duplicate addresses exactly that. So please take the time to read it attentively and correct your code accordingly. There might be other problems in your code, but this has to be solved first. – Thierry Lathuille Dec 12 '20 at 11:04
  • Change the above line to `if 'R1'==line8[where_is_pawn] or 'R2'==line8[where_is_pawn] or 'N1'==line8[where_is_pawn]` etc or even better: `if line8[where_is_pawn] in ['R1', 'R2', 'N1'......]` – IoaTzimas Dec 12 '20 at 11:04
  • That's the thing about programming. Its all about orchestrating lots of moving parts. Its best to fix the first problem you can see. Also its also about isolating everything into the smallest parts they can be in. – quamrana Dec 12 '20 at 11:05
  • I updated the question, i am having the same problem. I think it is about for loop, when i move p3 it should stop and read the next line and do it but it is doing it again and again. – Alexandra Dec 12 '20 at 11:15

0 Answers0