0

Well... I was having a terrible time getting part of my code working, but I rearranged things and it suddenly started working correctly. Not sure what I did to be honest, so I guess that will be the subject of this question. I'm building a simple text-based card game that uses decks uploaded from two .txt files. It's aimed at Magic: the Gathering, but would probably work with others if people got creative with it. To provide a rough overview, here is how things are arranged:

import random

def shuffle(board1):

def game():
    #board=[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
    #performs most of the actions relating to the game
    board[0]=20
    board[10]=20


def gameboard(board2):
    #displays game board

def draw(board3, numcards, player):
    #draws cards

def upload(deckname):
    #uploads cards from file

def life(board4):
    #asks about which player the life total is changing on, by how much, etc.
    #and then does it

def maketoken(board5):
    #creates tokens, counters, etc. based on user input

def move(board5):
    #accepts user input and moves cards from zone to zone

def play(board6):
    #handles casting spells, using abilities, triggered abilities, etc.

#main body of program is below function definitions

board=[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]

deckname1=input("\nWhat is the name of deck 1?")
deckname2=input("\nWhat is the name of deck 2?")

deck1=upload(deckname1)
deck2=uplaod(deckname2)

board[1]=deck1
board[11]=deck2

#this is where a lot of the other variables get set
game()

(note: most of the code has been removed for brevity and prettiness, as my code is pretty ugly)

I have a college-level C++ background, and just recently decided to pick up ye olde keyboard for the heck of it, so the assignment operator (=) not working the way I expect is driving me CRAZY. Therefore, I was also wondering if there was a way to get the functionality of the C++ '=' in Python, since I upload the decks from .txt files, and want to be through with the upload() function as soon as that's done (I use deck1=upload(deckname) (same for deck2). I want to use 'deck1' and 'deck2' to refill the decks after each game, but if I understand how '=' works in python, entering board[1]=deck1 means board[1] will point to the storage area of deck1 and changes to board[1] will change deck1, BUT I DON'T WANT THAT... GRRRR!!!!!!11). I'm sure there's a solution out there somewhere since it's making me nutty, but I haven't been able to find it. Thanks!!!

edit: This was the error I received when things were set up this way:

Traceback (most recent call last):
  File "C:\Users\inventor487\Desktop\simplepy.py", line 444, in <module>
    game()
  File "C:\Users\inventor487\Desktop\simplepy.py", line 114, in game
    board[1]=deck1
UnboundLocalError: local variable 'board' referenced before assignment

Summary:

  1. Do I need to pass board to the game() function, even if it's set up as a global variable (or at least I thought it was)? Everything seems to work fine when I assign it inside the game() function (commented out to show this). (edit: nevermind... I'm an idiot.)
  2. Does assigning part of board to a value inside game() make it a local variable (e.g. where I have board[0]=20)? (edit: yes, it does apparently...)
4

2 回答 2

0

正如您所发现的,=Python 中的运算符不会像在 C++ 中那样复制对象。如果您想制作一个副本以存储在另一个变量中,您必须明确说明它。

board[1] = deck1[:]  # the slicing operator copies a subset or the whole list

更通用的方法是使用copy模块

import copy
board[1] = copy.copy(deck1)
board[1] = copy.deepcopy(deck1)
于 2013-07-16T20:47:20.140 回答
0

编辑:一个类很容易地解决了所有这些问题,而且更干净。每个区域都有一个单独的变量,等等。让一切都变得更加顺畅。感谢您的帮助,尽管伙计们。非常感激。

于 2013-07-16T05:54:14.077 回答