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:
- 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.)
- 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...)