0

ハードウェア用の 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 関数では、サイズが定義されていないため、引数にサイズを使用できません。どうすれば実行可能にできますか?

ありがとう

4

4 に答える 4

1

ボードを表すクラスを作成することを検討してください。その場合、サイズは当然インスタンス変数です

class Board(object):

    def asksize(self):           
        while True:
            ask=raw_input("Input board size: ")  
            try:
                self.size=int(ask)
                return        
            except ValueError: 
                print "Please enter a integer"   

    def checkmove(self, move):    
        #move is sth like eg. A1:B2
        move=move.split(":") #I split it so it becomes ['A','1']['B','2']  
        if self.size>=int(move[0][1]) and int(move[0][1])>=1 and self.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 


# Use it like this
board = Board()
board.asksize()
move = some_function_that_returns_a_move()
board.checkmove(move)
于 2012-10-03T10:13:47.893 に答える
1

可変サイズをグローバルにすることはオプションですが、関数を API と見なし、ゲームのメイン bucle で使用する必要があります。

したがって、次のように入力を保存します。

size = asksize() #store the data on to variable called size
#then just call your check move
checkmove(x)

とにかく、これは悪い練習です。ゲーム コード内の関数を介して変数を渡すことをお勧めします。

#definitions
def asksize():
    #your code here
def checkmove(move, size):
    #your code here


#game set up here
size = asksize()
#more set up stuff
#end of game set up
#Game Main code

while True: #main game bucle
#Game stuff here
checkmove(move, size)
#more game stuff
于 2012-10-03T09:59:33.467 に答える
0

または単に「サイズ」をグローバル変数として作成します。とにかく、python は次に高いスコープで変数を検索します。次のように試すことができます:

size = 0
def asksize():
    global size
    while True:
        ask=raw_input("Input board size: ")
        try:
            size=int(ask)
            return size

        except ValueError:
            print "Please enter a integer"


def checkmove(move):
    global size
    # 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
于 2016-06-10T08:51:42.920 に答える
0

(少なくとも) 2 つの選択肢があります。

  1. size関数のパラメーターとして渡しcheckmoveます。
  2. size= asksize()関数を呼び出しcheckmoveます。

サイズをパラメーターとして渡す方が理にかなっていると思います。そうすれば、同じ関数を AI プレーヤーに再利用できるからです...

あれは:

def checkmove(move, size):
    # rest of definition

# main bit of code

size = asksize()
checkmove(move, size)
于 2012-10-03T09:48:53.137 に答える