我正在建立一个国际象棋游戏。我的董事会是这样定义的:
def new_board(cls, btype):
def size(x):
return [['___'] * x] * x
...
在这种情况下为x = 8
。
我使用此功能将其打印得很漂亮:
def print_board(cls):
def printl():
for x in range(len(cls.board)):
print(' '*6 + f'{Config.letters[x]}', end='')
print('\n')
printl()
for x in range(len(cls.board)):
print(f'{len(cls.board)-x} {cls.board[x]} {len(cls.board)-x}\n')
printl()
print('\n\n\n\n')
这是结果:
a b c d e f g h
8 ['___', '___', '___', '___', '___', '___', '___', '___'] 8
7 ['___', '___', '___', '___', '___', '___', '___', '___'] 7
6 ['___', '___', '___', '___', '___', '___', '___', '___'] 6
5 ['___', '___', '___', '___', '___', '___', '___', '___'] 5
4 ['___', '___', '___', '___', '___', '___', '___', '___'] 4
3 ['___', '___', '___', '___', '___', '___', '___', '___'] 3
2 ['___', '___', '___', '___', '___', '___', '___', '___'] 2
1 ['___', '___', '___', '___', '___', '___', '___', '___'] 1
a b c d e f g h
如果我尝试将一块放置在特定的索引上,
Config.board[1][3] = 'wK1'
会发生这种情况:
a b c d e f g h
8 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 8
7 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 7
6 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 6
5 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 5
4 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 4
3 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 3
2 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 2
1 ['___', '___', '___', 'wK1', '___', '___', '___', '___'] 1
a b c d e f g h
如何使作品只出现在一个正方形上?
如果您想运行它,这是我的完整代码:
class Config:
letters = tuple('abcdefghijklmnopqrstuvwxyz')
@classmethod
def new_board(cls, btype):
def size(x):
return [['___'] * x] * x
if 'custom' in btype.lower():
btype = int(btype.replace('custom', '').strip())
cls.board = size(btype)
elif btype.lower() == 'default':
cls.board = size(8)
elif btype.lower() == 'extended':
cls.board = size(10)
elif btype.lower() == 'small':
cls.board = size(5)
@classmethod
def print_board(cls):
def printl():
for x in range(len(cls.board)):
print(' '*6 + f'{Config.letters[x]}', end='')
print('\n')
printl()
for x in range(len(cls.board)):
print(f'{len(cls.board)-x} {cls.board[x]} {len(cls.board)-x}\n')
printl()
print('\n\n\n\n')
@classmethod
def tile_convert(cls, x):
if isinstance(x, str):
return cls.letters.index(x)
else:
return cls.letters[x]
class Knight:
def __init__(self, pos, color=None, num=''):
self.x = int(Config.tile_convert(pos[0]))
self.y = int(pos[1])
self.color = color
self.bare = num
self.id_ = f'K{num}'
self.set_id()
self.create()
def __str__(self):
return f'{self.color.capitalize()} Knight {self.bare}'
def set_id(self):
if self.color is not None:
if self.color.lower() in ('black', 'white', 'b', 'w'):
self.id_ = self.color.lower()[0] + self.id_
if self.color.lower() == 'b':
self.color = 'black'
elif self.color.lower() == 'w':
self.color = 'white'
else:
self.color = None
print("Invalid color input. Color not set.")
self.set_id()
else:
self.id_ = '_' + self.id_
def create(self): pass
#Config.board[self.y][self.x] = self.id_
def get_info(self):
print(f'{self}:\n')
print('ID: ', self.id_)
print('Position: ', Config.tile_convert(self.x), self.y, sep='')
print('Color: ', self.color)
def get_moves(self): pass
Config.new_board('default')
Config.print_board()
knight1 = Knight('e3', color='w', num=1)
Config.print_board()
knight1.get_info()
Config.board[1][3] = 'wK1'
Config.print_board()