ハードウェア用の python 用の 2D ボード ゲームを作成しています。
ボード サイズの整数を入力するようにユーザーに依頼しました。たとえば、7. 投稿する前に少し変更しました (重要なもののみを表示)。関数は次のようなものです
def asksize():
while True:
ask=raw_input("Input board size: ")
try:
size=int(ask)
return size
except ValueError:
print "Please enter a integer"
ボードサイズが可変なので、他の関数で可変サイズを再利用する必要があり、ユーザーの移動が有効かどうかを確認するために使用します。変数を再利用するにはどうすればよいですか?
def checkmove(move):
#move is sth like eg. A1:B2
move=move.split(":") #I split it so it becomes ['A','1']['B','2']
if size>=int(move[0][1]) and int(move[0][1])>=1 and size>=int(move[1][1]) and int(move[1][1])>=1: #for example if board size is 7, this is to check whether user input is between 1 to 7 within the board
return True
else:
return False
私の checkmove 関数では、サイズが定義されていないため、引数にサイズを使用できません。どうすれば実行可能にできますか?
ありがとう