Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Dec 12th, 2004, 4:08 PM   #2
Overmind
Professional Programmer
 
Overmind's Avatar
 
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5 Overmind is on a distinguished road
btw, there is a python forum, under scripting
__________________
[SIGPIC][/SIGPIC]
Overmind is offline   Reply With Quote
Old Dec 12th, 2004, 4:41 PM   #3
JoeUser
Newbie
 
Join Date: Dec 2004
Posts: 8
Rep Power: 0 JoeUser is on a distinguished road
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} )?
JoeUser is offline   Reply With Quote
Old Dec 12th, 2004, 4:57 PM   #4
Overmind
Professional Programmer
 
Overmind's Avatar
 
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5 Overmind is on a distinguished road
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.
__________________
[SIGPIC][/SIGPIC]
Overmind is offline   Reply With Quote
Old Dec 12th, 2004, 7:00 PM   #5
JoeUser
Newbie
 
Join Date: Dec 2004
Posts: 8
Rep Power: 0 JoeUser is on a distinguished road
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.
JoeUser is offline   Reply With Quote
Old Dec 12th, 2004, 9:49 PM   #6
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 707
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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.
thechristelegacy is offline   Reply With Quote
Old Dec 12th, 2004, 10:55 PM   #7
JoeUser
Newbie
 
Join Date: Dec 2004
Posts: 8
Rep Power: 0 JoeUser is on a distinguished road
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 is offline   Reply With Quote
Old Dec 13th, 2004, 7:06 PM   #8
JoeUser
Newbie
 
Join Date: Dec 2004
Posts: 8
Rep Power: 0 JoeUser is on a distinguished road
I figured it out. With normal arrays, you create an independent array with
arrayA = arrayB[:]
but with dictionary arrays, use
arrayA = arrayB.copy()
JoeUser is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:43 PM.

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