我将如何使用 ascii 对 python 中的棋子进行验证
How would I place validations on chess pieces in python using ascii
我开始研究 python 的命令行国际象棋。我已经成功地创建了一个 table 像这样:
def setup_grid():
grid = [[' ' for i in range(gridsize)] for i in range(gridsize)] # Creates a grid of 0 of the size set by user
return(grid)
# Displaying the grid to the user
def show_grid(grid):
gridsize = len(grid)
horizontal = ' '+4*gridsize*'-'+'-' # This prints the horizontal borders of the grid
#####################################################
toplabel = ' ' #
for i in string.ascii_lowercase[:gridsize]: # This creates the letters according
toplabel = toplabel+i+' ' # to how big the grid is. This prints the top letters
print '\n'+toplabel+'\n'+horizontal #
#####################################################
#########################################
for idx,i in enumerate(grid): #
row = '{0:2} |'.format(idx+1) #
for j in i: # This creates the numbers
row = row+' '+j+' |' # for the left side of the grid
print row+'\n'+horizontal #
print '' #
#########################################
def play_game():
gridsize = 8
currgrid = [[' ' for i in range(gridsize)] for i in range(gridsize)]
show_grid(currgrid)
grid = []
play_game()
现在我正在努力为白皇后定义像 wq
这样的作品。
玩家选择要移动的棋子的方式是在网格上选择坐标并移动棋子 y 输入新坐标。我不知道的是如何确保棋子按要求移动。我不知道如何验证他们的运动。
着法验证有些复杂,但是,下面是如何验证皇后着法的可能性。首先,您可能希望将游戏布局移动到 class 以便更简单地访问棋盘和棋子。然后,创建一个移动棋子的方法。该方法可以由装饰器包装,装饰器将验证传递给该方法的坐标:
def validate_move(f):
def wrapper(cls, name, x, y):
methods = {'q':cls.__class__.queen_moves} #build dictionary of move accumulators
full_moves = list(methods[name[:-1]](getattr(cls, 'board'), color = name[-1]))
if [x, y] not in full_moves:
print("invalid move")
else:
f(cls, x, y)
return wrapper
class Chess:
pieces = [['r', 'kn', 'b', 'k', 'q', 'b', 'kn', 'r'], ['p']*8]
def __init__(self):
self.board = [[i+'b' for i in b] for b in Chess.pieces]+([['-']*8]*8)+[[i+'w' for i in b] for b in Chess.pieces]
@validate_move
def move_piece(_name, x, y):
a1, b1 = [(i, b) for i in range(8) for b in range(8) if self.board[i][b] == _name]
self.board[x][y] = _name
self.board[a1][b1] = '-'
@staticmethod
def queen_moves(board, color = 'w'):
c1, c2 = [(i, b) for i in range(8) for b in range(8) if board[i][b] == 'q'+color]
_c2, _c1 = c2, c1
while c2 < 8: #check vertically
c2 += 1
if board[c1][c2] != '-':
c2 = _c2
break
yield [c1, c2]
while c2 >= 0: #check vertically
c2 -= 1
if board[c1][c2] != '-':
c2 = _c2
break
yield [c1, c2]
while c1 < 8: #check horizontally
c1 += 1
if board[c1][c2] != '-':
c1 = _c1
break
yield [c1, c2]
while c1 >= 0: #check horizontally
c1 -= 1
if board[c1][c2] != '-':
c1 = _c1
break
yield [c1, c2]
因此,您还有两件事要做:
创建 staticmethod
的完整列表以获得棋盘上所有棋子的完整移动。
在wrapper
中建立methods
字典来存储移动查找器功能对象。
我开始研究 python 的命令行国际象棋。我已经成功地创建了一个 table 像这样:
def setup_grid():
grid = [[' ' for i in range(gridsize)] for i in range(gridsize)] # Creates a grid of 0 of the size set by user
return(grid)
# Displaying the grid to the user
def show_grid(grid):
gridsize = len(grid)
horizontal = ' '+4*gridsize*'-'+'-' # This prints the horizontal borders of the grid
#####################################################
toplabel = ' ' #
for i in string.ascii_lowercase[:gridsize]: # This creates the letters according
toplabel = toplabel+i+' ' # to how big the grid is. This prints the top letters
print '\n'+toplabel+'\n'+horizontal #
#####################################################
#########################################
for idx,i in enumerate(grid): #
row = '{0:2} |'.format(idx+1) #
for j in i: # This creates the numbers
row = row+' '+j+' |' # for the left side of the grid
print row+'\n'+horizontal #
print '' #
#########################################
def play_game():
gridsize = 8
currgrid = [[' ' for i in range(gridsize)] for i in range(gridsize)]
show_grid(currgrid)
grid = []
play_game()
现在我正在努力为白皇后定义像 wq
这样的作品。
玩家选择要移动的棋子的方式是在网格上选择坐标并移动棋子 y 输入新坐标。我不知道的是如何确保棋子按要求移动。我不知道如何验证他们的运动。
着法验证有些复杂,但是,下面是如何验证皇后着法的可能性。首先,您可能希望将游戏布局移动到 class 以便更简单地访问棋盘和棋子。然后,创建一个移动棋子的方法。该方法可以由装饰器包装,装饰器将验证传递给该方法的坐标:
def validate_move(f):
def wrapper(cls, name, x, y):
methods = {'q':cls.__class__.queen_moves} #build dictionary of move accumulators
full_moves = list(methods[name[:-1]](getattr(cls, 'board'), color = name[-1]))
if [x, y] not in full_moves:
print("invalid move")
else:
f(cls, x, y)
return wrapper
class Chess:
pieces = [['r', 'kn', 'b', 'k', 'q', 'b', 'kn', 'r'], ['p']*8]
def __init__(self):
self.board = [[i+'b' for i in b] for b in Chess.pieces]+([['-']*8]*8)+[[i+'w' for i in b] for b in Chess.pieces]
@validate_move
def move_piece(_name, x, y):
a1, b1 = [(i, b) for i in range(8) for b in range(8) if self.board[i][b] == _name]
self.board[x][y] = _name
self.board[a1][b1] = '-'
@staticmethod
def queen_moves(board, color = 'w'):
c1, c2 = [(i, b) for i in range(8) for b in range(8) if board[i][b] == 'q'+color]
_c2, _c1 = c2, c1
while c2 < 8: #check vertically
c2 += 1
if board[c1][c2] != '-':
c2 = _c2
break
yield [c1, c2]
while c2 >= 0: #check vertically
c2 -= 1
if board[c1][c2] != '-':
c2 = _c2
break
yield [c1, c2]
while c1 < 8: #check horizontally
c1 += 1
if board[c1][c2] != '-':
c1 = _c1
break
yield [c1, c2]
while c1 >= 0: #check horizontally
c1 -= 1
if board[c1][c2] != '-':
c1 = _c1
break
yield [c1, c2]
因此,您还有两件事要做:
创建
staticmethod
的完整列表以获得棋盘上所有棋子的完整移动。在
wrapper
中建立methods
字典来存储移动查找器功能对象。