Reputation: 11
Im making a chess game where the board is represented by a 2d array. However the chess pieces is a different width than spaces, so they get displaced when i try to print it like this:
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♟
♟ ♟ ♟ ♟ ♟ ♟ ♟
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
Here is the print function:
def print_board():
y = ["♖", "♜", "♗", "♝", "♘", "♞", "♕", "♛", "♔","♚", "♙", "♟"," "]
for i in range(8):
tempListe = "".join(board[i])
z=0
for x in ["T","t","L","l","H","h","D","d","K","k","P","p","_"]:
if x in tempListe:
tempListe = tempListe.replace(x,y[z])
z+=1
for j in range(8):
print("{0:2}".format(tempListe[j]),end="")
print("")
print_board()
I've tried many different things, but everything seems to end up the same. How can i fix this without the spaces between pieces becoming too large?
Upvotes: 1
Views: 446
Reputation: 15877
You're trying to present tabular data without resorting to the horizontal tab character (because it produces rather wide columns). But your output device has different width for space and the chess symbols, so using spaces won't work. If this is in an otherwise monospace text, it is most likely that your display device is falling back to another font because the primary font is missing the chess pieces, and this difference would be the source of the width issues.
There are special spaces for a few widths, including digit width, but (as far as I can tell) there's no specific width defined for chess symbols. My first thought was if they could be combined with a particularly wide space, but Unicode doesn't seem to have combining spaces. This leaves two basic options: use a layout system for this task, such as HTML tables, or use a monospace font which includes both your spacing and chess symbols. The latter is most easily done in your own terminal, or can be requested on your own webpages using webfont, but there are many places you don't have control, including individual web browsers. Stack Overflow in particular doesn't permit tables in its Markdown, although chess.stackexchange.com seems to have a javascript chessboard renderer (instructions).
Another (uglier) workaround is to use other formatting capabilities to render chess pieces in the empty spaces, but colored such that they are invisible. This will not work on fonts where the pieces have their own color, as emojis do. It will also wreak havoc on screen readers, or copying the text.
Upvotes: 0
Reputation: 23443
T,t,L,l,H,h,D,d,K,k,P,p,_= "♖♜♗♝♘♞♕♛♔♚♙♟ "
board = [
[T,L,H,D,K,H,L,T],
[P,P,P,P,P,P,P,P],
[_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_],
[p,p,p,p,p,p,p,p],
[t,l,h,d,k,h,l,t]
]
board2 = [
T,L,H,D,K,H,L,T,
P,P,P,P,P,P,P,P,
_,_,_,_,_,_,_,_,
_,_,_,_,_,_,_,_,
_,_,_,_,_,_,_,_,
_,_,_,_,_,_,_,_,
p,p,p,p,p,p,p,p,
t,l,h,d,k,h,l,t
]
def printBoard():
for e in board:
for ee in e:
print(ee, end='')
print()
And the code could continue with another layout like this:
def printBoard2():
b = "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
"{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}".format(*board2)
return b
board2[36] = p
board[4][3] = T
printBoard()
print("\n\n")
print(printBoard2())
At the end the output is this
The first
♖♗♘♕♔♘♗♖
♙♙♙♙♙♙♙♙
♖
♟♟♟♟♟♟♟♟
♜♝♞♛♚♞♝♜
The second layout
♖ ♗ ♘ ♕ ♔ ♘ ♗ ♖
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♟
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
♜ ♝ ♞ ♛ ♚ ♞ ♝ ♜
On the promt it look aligned, but here (in the html) does not. I'll put an image of it below.
Upvotes: 1