我创建了一个函数,该函数试图确定位置的坐标是否在棋盘格边界内(包括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)
我觉得我的代码不完整。
答案 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)
假设您的Position
和Piece
函数已正确编码,则问题出在检查算法的语法内。
尝试这样:
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