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 函数中,我不能在我的参数中使用 size 因为它没有定义,我怎样才能使它可行?

谢谢

4

4 回答 4

1

将可变大小设为全局是一种选择,但您应该将您的函数视为 API,并在游戏主按钮上使用它们。

所以像这样存储输入:

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 回答
1

考虑创建一个类来代表董事会。那么size自然是一个实例变量

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 回答
0

你有(至少)两个选择:

  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 回答
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 回答