Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Python Help![solved] (http://www.programmingforums.org/showthread.php?t=1518)

JoeUser Dec 12th, 2004 4:00 PM

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()


Overmind Dec 12th, 2004 4:08 PM

btw, there is a python forum, under scripting :)

JoeUser Dec 12th, 2004 4:41 PM

Really? Sorry. Didn't see it.
I've made some progress, I now understand that b=a just makes b another name for a. Unfortunately, I havn't gotten any further than that on how to fix this problem.
b = a[:] doesn't work on the type of dictionary arrays I'm using. Anybody know how to copy dictionary arrays (ex: a = {'a':1,'b':2,'c':3} )?

Overmind Dec 12th, 2004 4:57 PM

Quote:

I now understand that b=a just makes b another name for a
Well I wouldn't put it like that...it asigns a' s value to b.

JoeUser Dec 12th, 2004 7:00 PM

Ahh you're right, I didn't make myself clear. Let me clarify, I now understand that array b=array a just makes array b another name for array a. That's only true of arrays.

The question remains, how do you make an independent copy of a definition array?

And admin thanks for moving to the right forum.

thechristelegacy Dec 12th, 2004 9:49 PM

I'm not quite sure what you're asking, but if you're trying to copy from dictionary to another variable all it is is this.
:

>>>a = {'a':1,'b':2,'c':3}
>>>b = a
>>>print b
{'a':1,'b':2,'c':3}


As you can see, b is an exact copy of a. If that doesn't answere your question, keep asking.

JoeUser Dec 12th, 2004 10:55 PM

Yes I know! But take this piece of code for example
:

>>> a = {'a':1,'b':2,'c':3}
>>> b = a
>>> print a
{'a': 1, 'c': 3, 'b': 2}
>>> b['a'] = 9999
>>> print a
{'a': 9999, 'c': 3, 'b': 2}

When you assign the value of one array to the value of another, really you are just creating another name for the same array. See how when I modified b, a was also modified? It's like calling

Arrays in python act strangely, they act more like methods than variables. The question is, how do I copy one array into an independent array? With normal arrays (such as a = [1,2,3]), you would do this by "splicing" it with b = a[:] . But with definition arrays (a = {'a':1,'b':2,'c':3}) splice doesn't work.

JoeUser Dec 13th, 2004 7:06 PM

I figured it out. With normal arrays, you create an independent array with
:

arrayA = arrayB[:]
but with dictionary arrays, use
:

arrayA = arrayB.copy()


All times are GMT -5. The time now is 5:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC