View Single Post
Old Dec 12th, 2004, 4:00 PM   #1
JoeUser
Newbie
 
Join Date: Dec 2004
Posts: 8
Rep Power: 0 JoeUser is on a distinguished road
I'm fairly new to python. I am making a simple text 2 player chess game currently. It's working like a charm (I love python's array definitions), but the array is acting wierdly. Here's a snippet of what's mystifieing me.
#this just moves the pieces, board() is just a method that prints out 
#a chessboard.
#p is copied into lb
          lb = p
          wMove2 = raw_input("    To: ")
#p is changed
          p[wMove2] = p[wMove1]
          p[wMove1] = " "
      board(p)
#once this code is completed, lb is THE SAME as p, even though p 
#was modified after it was put into lb, board(lb) and board(p) print 
#out the same.
I realize the problem is I don't fully understand python variable properties, but it just doesn't seem logical.

The whole code is here:
(sorry it's messy, I have some long prints)
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
      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()
JoeUser is offline   Reply With Quote