I'm new to python. I'm making a simple text-based chess program, and everything works fine except for one function. Whenever I copy array 'p' into array 'lb', if I modify 'p', 'lb' changes with it. I'm not really familiar with python variable properties, and it just mistifies me why array 'lb' would follow 'p'.
That's the part of the program that isn't working.
board(lb) and board(p) are the same even though array "p" was modified after it was copied onto "lg".
The whole code is here, sorry it looks messy, I have some long lines of code.
Code:
#white's move; sets the last board (lb) to position before it changes
#then it modifies the board position (p) to the moves
#only this instance requires a 2nd user input (hence wMove2=raw_input...)
elif wMove1 != "o-o" and wMove1 != "o-o-o":
lb = p
wMove2 = raw_input("Move to where: ")
p[wMove2] = p[wMove1]
p[wMove1] = " "
#board(x) is just a method that changes an array into a
#chessboard and prints it onto the screen
board(p)
That's the part of the program that isn't working.
board(lb) and board(p) are the same even though array "p" was modified after it was copied onto "lg".
The whole code is here, sorry it looks messy, I have some long lines of code.
Code:
def newGame():
p = {'a1':"wR",'b1':"wN",'c1':"wB",'d1':"wQ",'e1':"wK",'f1':"wB",'g1':"wN",'h1':"wR",
'a2':"wP",'b2':"wP",'c2':"wP",'d2':"wP",'e2':"wP",'f2':"wP",'g2':"wP",'h2':"wP",
'a3':" ",'b3':" ",'c3':" ",'d3':" ",'e3':" ",'f3':" ",'g3':" ",'h3':" ",
'a4':" ",'b4':" ",'c4':" ",'d4':" ",'e4':" ",'f4':" ",'g4':" ",'h4':" ",
'a5':" ",'b5':" ",'c5':" ",'d5':" ",'e5':" ",'f5':" ",'g5':" ",'h5':" ",
'a6':" ",'b6':" ",'c6':" ",'d6':" ",'e6':" ",'f6':" ",'g6':" ",'h6':" ",
'a7':"bP",'b7':"bP",'c7':"bP",'d7':"bP",'e7':"bP",'f7':"bP",'g7':"bP",'h7':"bP",
'a8':"bR",'b8':"bN",'c8':"bB",'d8':"bQ",'e8':"bK",'f8':"bB",'g8':"bN",'h8':"bR"}
board(p)
play(p)
#prints the board to the screen
def board(p):
horizLine = " ----------------------------------------"
print ""
print horizLine," JoeUser's 2 Player Chess!"
print " 8|",p['a8'],"|",p['b8'],"|",p['c8'],"|",p['d8'],"|",p['e8'],"|",p['f8'],"|",p['g8'],"|",p['h8'],"|"
print horizLine," Use the grid notation"
print " 7|",p['a7'],"|",p['b7'],"|",p['c7'],"|",p['d7'],"|",p['e7'],"|",p['f7'],"|",p['g7'],"|",p['h7'],"|"," to move pieces."
print horizLine
print " 6|",p['a6'],"|",p['b6'],"|",p['c6'],"|",p['d6'],"|",p['e6'],"|",p['f6'],"|",p['g6'],"|",p['h6'],"|"
print horizLine
print " 5|",p['a5'],"|",p['b5'],"|",p['c5'],"|",p['d5'],"|",p['e5'],"|",p['f5'],"|",p['g5'],"|",p['h5'],"|"
print horizLine," Commands"
print " 4|",p['a4'],"|",p['b4'],"|",p['c4'],"|",p['d4'],"|",p['e4'],"|",p['f4'],"|",p['g4'],"|",p['h4'],"|"
print horizLine," 'new' -New Game"
print " 3|",p['a3'],"|",p['b3'],"|",p['c3'],"|",p['d3'],"|",p['e3'],"|",p['f3'],"|",p['g3'],"|",p['h3'],"|"," 'undo' -Undo Previous Move"
print horizLine
print " 2|",p['a2'],"|",p['b2'],"|",p['c2'],"|",p['d2'],"|",p['e2'],"|",p['f2'],"|",p['g2'],"|",p['h2'],"|"
print horizLine
print " 1|",p['a1'],"|",p['b1'],"|",p['c1'],"|",p['d1'],"|",p['e1'],"|",p['f1'],"|",p['g1'],"|",p['h1'],"|"
print horizLine
print " a b c d e f g h "
print ""
print ""
print ""
print ""
#play
def play(p):
while 1:
#white's move
print "White's move..."
wMove1 = raw_input("Move from: ")
#new game
if wMove1 == "new":
newGame()
#undoes by changing the board position (p) to the last board postition (p)
elif wMove1 == "undo":
p = lb
#handles castling, also assigns last board (lb) before it changes
elif wMove1 == "o-o" or wMove1 == "o-o-o":
lb = p
if wMove1 == "o-o":
p['e1'],p['f1'],p['g1'],p['h1'] = " ", "wR","wK"," "
if wMove1 == "o-o-o":
p['e1'],p['d1'],p['c1'],p['b1'],p['a1'] = " ","wR","wK"," "," "
#white's move; sets the last board (lb) to position before it changes
#then it modifies the board position (p) to the moves
#only this instance requires a 2nd user input (hence wMove2=raw_input...)
elif wMove1 != "o-o" and wMove1 != "o-o-o":
lb = p
wMove2 = raw_input(" To: ")
p[wMove2] = p[wMove1]
p[wMove1] = " "
board(p)
board(lb)
#black's move; repeat above code with black
print "Black's move..."
bMove1 = raw_input("Move from: ")
if bMove1 == "new":
newGame()
elif bMove1 == "undo":
p = lb
elif bMove1 != "o-o" and bMove1 !="o-o-o":
lb = p
bMove2 = raw_input(" To: ")
p[bMove2] = p[bMove1]
p[bMove1] = " "
elif bMove1 == "o-o" or bMove1 == "o-o-o":
lb = p
if bMove1 == "o-o":
p['e8'],p['f8'],p['g8'],p['h8'] = " ", "bR","bK"," "
if bMove1 == "o-o-o":
p['e8'],p['d8'],p['c8'],p['b8'],p['a8'] = " ","bR","bK"," "," "
board(p)
board(lb)
#initializes board and play, starts game
newGame()