ええと...コードの一部が機能するのにひどい時間を過ごしていましたが、物事を再配置すると、突然正しく機能し始めました。正直なところ何をしたのかわからないので、それがこの質問の主題になると思います。2 つの .txt ファイルからアップロードされたデッキを使用する単純なテキスト ベースのカード ゲームを構築しています。これは Magic: the Gathering を対象としていますが、人々が創造性を発揮すれば、おそらく他のユーザーと連携することになるでしょう。大まかな概要を提供するために、次のように配置されています。
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()
(注:私のコードはかなり醜いので、コードのほとんどは簡潔さと見栄えのために削除されています)
私は大学レベルの C++ のバックグラウンドを持っていますが、つい最近、古いキーボードを手に入れることにしたばかりなので、代入演算子 (=) が期待どおりに機能しないことに頭がおかしくなりました。したがって、Python で C++ '=' の機能を取得する方法があるかどうかも疑問に思っていました。.txt ファイルからデッキをアップロードし、それが完了したらすぐに upload() 関数を使用したいからです。 (deck1=upload(deckname) を使用します (deck2 も同じです)。各ゲームの後に「deck1」と「deck2」を使用してデッキを補充したいのですが、python で「=」がどのように機能するかを理解していれば、board[1 と入力します。 ]=deck1は、board[1]がdeck1のストレージ領域を指し、board[1]への変更がdeck1を変更することを意味しますが、私はそれを望んでいません... GRRRR!!!!!!11)。それ以来、どこかに解決策があると確信しています。気が狂いそうですが、見つけられませんでした。ありがとう!!!
編集:これは、物事がこのように設定されたときに受け取ったエラーです:
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
概要:
- グローバル変数として設定されている場合でも、game() 関数に board を渡す必要がありますか (または、少なくともそうだと思っていました)? game() 関数内で割り当てると、すべてが正常に機能するようです (これを示すためにコメントアウトされています)。(編集:気にしないでください...私はばかです。)
- ボードの一部を game() 内の値に割り当てると、ローカル変数になりますか (たとえば、board[0]=20 の場合)? (編集:はい、どうやらそうです...)