I have recently started work on a chess program. I coded the program to take a 2d array that I made and decode it to form a chess board using ascii characters and colorama. I coded it up so that R=Rook, B=Bishop, P=Pawn, N=Knight, K=King, Q=Queen. There are some extra characters but they are only necessary for parsing and other stuff. Here is my code:
from colorama import Fore, Back
pos=[
["u3wR", "wN", "wB", "wQ", "uwK", "wB", "wN", "u2wR"],
["uwP", "uwP", "uwP", "uwP", "uwP", "uwP", "uwP", "uwP"],
["E", "E", "E", "E", "E", "E", "E", "E"],
["E", "E", "E", "E", "E", "E", "E", "E"],
["E", "E", "E", "E", "E", "E", "E", "E"],
["E", "E", "E", "E", "E", "E", "E", "E"],
["ubP", "ubP", "ubP", "ubP", "ubP", "ubP", "ubP", "ubP"],
["u3bR", "bN", "bB", "bQ", "ubK", "bB", "bN", "u2bR"]
]
def print_board(pside):
if pside=="white":
for i in range(7, -1, -1):
print("", end="n")
for j in range(0, 7):
if "K" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♚", end="")
else:
print(Fore.BLACK + Back.BLACK + "♚", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♔", end="")
else:
print(Fore.WHITE + Back.BLACK + "♔", end="")
if "Q" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♛", end="")
else:
print(Fore.BLACK + Back.BLACK + "♛", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♕", end="")
else:
print(Fore.WHITE + Back.BLACK + "♕", end="")
if "R" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♜", end="")
else:
print(Fore.BLACK + Back.BLACK + "♜", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♖", end="")
else:
print(Fore.WHITE + Back.BLACK + "♖", end="")
if "B" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♝", end="")
else:
print(Fore.BLACK + Back.BLACK + "♝", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♗", end="")
else:
print(Fore.WHITE + Back.BLACK + "♗", end="")
if "N" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♞", end="")
else:
print(Fore.BLACK + Back.BLACK + "♞", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♘", end="")
else:
print(Fore.WHITE + Back.BLACK + "♘", end="")
if "P" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♟", end="")
else:
print(Fore.BLACK + Back.BLACK + "♟", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♙", end="")
else:
print(Fore.WHITE + Back.BLACK + "♙", end="")
if "E" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + " ", end="")
else:
print(Fore.BLACK + Back.BLACK + " ", end="")
else:
for i in range(0, 8):
print("", end="n")
for j in range(7, -1, -1):
if "K" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♚", end="")
else:
print(Fore.BLACK + Back.BLACK + "♚", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♔", end="")
else:
print(Fore.WHITE + Back.BLACK + "♔", end="")
if "Q" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♛", end="")
else:
print(Fore.BLACK + Back.BLACK + "♛", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♕", end="")
else:
print(Fore.WHITE + Back.BLACK + "♕", end="")
if "R" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♜", end="")
else:
print(Fore.BLACK + Back.BLACK + "♜", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♖", end="")
else:
print(Fore.WHITE + Back.BLACK + "♖", end="")
if "B" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♝", end="")
else:
print(Fore.BLACK + Back.BLACK + "♝", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♗", end="")
else:
print(Fore.WHITE + Back.BLACK + "♗", end="")
if "N" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♞", end="")
else:
print(Fore.BLACK + Back.BLACK + "♞", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♘", end="")
else:
print(Fore.WHITE + Back.BLACK + "♘", end="")
if "P" in pos[i][j]:
if "b" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + "♟", end="")
else:
print(Fore.BLACK + Back.BLACK + "♟", end="")
else:
if (j + i)%2==0:
print(Fore.WHITE + Back.WHITE + "♙", end="")
else:
print(Fore.WHITE + Back.BLACK + "♙", end="")
if "E" in pos[i][j]:
if (j + i)%2==0:
print(Fore.BLACK + Back.WHITE + " ", end="")
else:
print(Fore.BLACK + Back.BLACK + " ", end="")
print_board("white")
When I run the code, I do get the appropriate output, but the ascii chars kind of get merged together and form 2 characters of space from 2 ascii chars, where 1 ascii char itself takes up 2 characters of space. I think StackOverflow will format it automatically, but here is the output in text:
♜♞♝♛♚♝♞
♟♟♟♟♟♟♟
♙♙♙♙♙♙♙
♖♘♗♕♔♗♘
This is an image:
How do I fix this? Can anyone help?