检查位置的坐标是否在边界内

时间:2020-07-01 22:46:32

标签: python python-3.x

我创建了一个函数,该函数试图确定位置的坐标是否在棋盘格边界内(包括0和数字之间)

以下是我的尝试:

def position_is_in_game(self, position):


    # Replace self with a Game type object
    one_game = Game()

    #Replace position with an object of type Position
    one_position = Position()

    return one_game.position_is_in_game(one_position)

我觉得我的代码不完整。

2 个答案:

答案 0 :(得分:1)

这是我要检查的位置是否在网格上的方法:

class Game :
    def __init__(self):
        self.n_lignes = 8
        self.n_colonnes = 8

    def position_is_in_game(self, x, y):
        return x > 0 and x < self.n_lignes and y > 0 and y < self.n_colonnes

mygame = Game ()
print (mygame.position_is_in_game(1, 8))

答案 1 :(得分:1)

假设您的PositionPiece函数已正确编码,则问题出在检查算法的语法内。

尝试这样:

def position_is_in_game(position_tuple):
    game = Game()

    game_positions = game.cases.keys()
    for i in game_positions:
        if i == position_tuple:
            return True
        else:
            return False
相关问题