I created board, but I've confused with pawns.
board = [[(x,y) for x in range(1,9)] for y in range(1,9)]
for k in range(len(board)):
print('| |' * len(board))
I want to see something like that:
| || || || || || || || |
|P||P||P||P||P||P||P||P|
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
|P||P||P||P||P||P||P||P|
| || || || || || || || |
But which way can I choose to put pawns on the boards? Should I use double for loops to check if coordinates equal or exist alternative way to?
def pawn():
coordinates = board[1][1]
return coordinates
for i in range(len(board)):
for j in range(8):
if board[i][j] == pawn():
print('|P|' * len(board))
else:
print('| |')
Something like that could work:
import numpy as np
board = np.empty((8,8)).tolist()
for k in range(len(board)):
if k == 1 or k ==6:
board[k]= ['|p|','|p|','|p|','|p|','|p|','|p|','|p|','|p|']
else:
board[k]= ['|x|','|x|','|x|','|x|','|x|','|x|','|x|','|x|']
[print(''.join(b)) for b in board ]
but I'm not sure if it is what you wanted. In that case board is a 2D matrix which is shape like the board. if it is for chess, you need a 'P' for white and a 'p' for black.
The initial position of pawns is row 2 and 7. You can check the for
loop index and set their positions. Something like:
board = [[(x,y) for x in range(1,9)] for y in range(1,9)]
pawnRows = [1, 6] # 0 based index
for k in range(len(board)):
if k in pawnRows:
print('|P|' * len(board))
else:
print('|X|' * len(board))
Making it more general, you can use an array
or pandas.DataFrame
to remember the state of the board
Setting up the pieces
piece = collections.namedtuple('piece', ('value', 'mark'))
EMPTY = piece(0, '|X|')
PAWN = piece(1, '|P|')
pieces = {p.value: p for p in (EMPTY, PAWN)}
You can define as much pieces as you want, and even give the separate colours
boardsize = (8,8)
board = pd.DataFrame(data=[[1] * boardsize[0]] * boardsize[1]) * EMPTY.value
pawn_positions = [(1, 1), (3, 4), (7, 6)]
for pawn in pawn_positions:
board.loc[pawn] = PAWN.value
Results in something like this
0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
3 0 0 0 0 1 0 0 0
4 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 1 0
for row_nr, row in board.applymap(lambda x: pieces[x].mark).iterrows():
print(''.join(row) + '\n')
gives
|X||X||X||X||X||X||X||X|
|X||P||X||X||X||X||X||X|
|X||X||X||X||X||X||X||X|
|X||X||X||X||P||X||X||X|
|X||X||X||X||X||X||X||X|
|X||X||X||X||X||X||X||X|
|X||X||X||X||X||X||X||X|
|X||X||X||X||X||X||P||X|
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.