函数to_integer()
必须用数字(Empty= 0, White= 1, Black="@")替换Go棋盘上点的颜色(Empty=".", White=", Black="@"),并将棋盘的所有行连接为一行数字,这给出了一个三元数,然后可以转换为整数。
A B C
3 O . O
2 . @ .
1 @ O .
…结果应该是101020210。
现在,我的代码是这样的,但它没有给出那个输出,我使用字典根据符号提取值:
points = {
'E': '.',
'B': '@',
'W': 'O'
}
to_integer ={
'.': '0',
'O': '1',
'@': '2'
}
def to_integer(self):
digit_colour=""
for line in self.to_strings():
for character in line:
print(character)
character = "'" + character + "'"
print(character)
num_colour = self.to_integer.values(character)
digit_colour += num_colour
digit_colour = int(digit_colour)
return digit_colour
b = Board(
3,
["O.O", ".@.", "@O."]
)
print(b.to_integer())
我怎样才能得到想要的输出?
整体代码:
from string import ascii_uppercase as letters
class Board:
#Dictionary created for the colours and the respected symbols
points = {'E': '.', 'B': '@', 'W': 'O'}
to_int={'.':'0','O':'1','@':'2'}
#Constructor
def __init__(self,size=19,from_strings=None):
assert 2 <= size <= 26, "Illegal board size: must be between 2 and 26."
assert type(from_strings) is list,"input is not a list"
assert len(from_strings)==size, "length of input list does not match size"
for i in from_strings:
assert type(i)==str, "row "+i+" is not a string"
assert len(i)==size,"length of row "+i+" does not match size"
#assert i == self.grid[i], "invalid character in row "+i
self.size = size
self.grid = [['E'] * size for _ in range(size)]
self.from_strings = [] if from_strings is None else from_strings
def __str__(self): # creating the grid
padding = ' ' # Creating a variable with a space assigned so that it acts as a padding to the rows that have a single digit
heading = ' ' + ' '.join(letters[:self.size]) # Alphabetical heading is created
lines = [heading] # adding the alphabetical heading into a list named lines to which the rows will be added later
for r, row in enumerate(self.grid):
if len(self.grid) < 10: # for the grid with a size less than 10 to add the space to the start of the row for the single digits to be aligned
if (self.from_strings):
line = " " + f'{self.size - r} ' + ' '.join(self.from_strings[r])
else:
line = " " + f'{self.size - r} ' + ' '.join(self.points[x] for x in row)
lines.append(line)
else: # for the grids that are larger than 9
if r > 9: # for rows 1 to 9 the single digits are aligned according to the first digit from the right of the two digit rows
if (self.from_strings):
line = f'{self.size - r} ' + ' '.join(self.from_strings[r])
else:
line = f'{self.size - r} ' + ' '.join(self.points[x] for x in row)
line = padding + line # adding the space using the variable padding to the row created
lines.append(line) # adding the row to the list of rows
else: # for the rows 10 onwards - as there is no requirement to add a padding it is not added here
if (self.from_strings):
line = f'{self.size - r} ' + ' '.join(self.from_strings[r])
else:
line = f'{self.size - r} ' + ' '.join(self.points[x] for x in row) # creation of the row
lines.append(line) # adding the newly created row to the list of rows
return 'n'.join(lines)
一种简洁的方法是使用单个连接:
codes ={
'.': '0',
'O': '1',
'@': '2'
}
def board_to_int(board):
return ''.join(codes[c] for line in board for c in line)
例如
board = ["O.O", ".@.", "@O."]
print(board_to_int(board))
#101020210
如果你很好奇,int(board_to_int(board),3)
给出了这个三进制数的10进制表示,在这个例子中是7473
。