用python写一个国际象棋棋盘的8*8数组
时间: 2023-05-18 09:03:48 浏览: 771
用JavaScript编写黑白棋
可以使用以下代码来创建一个8*8的国际象棋棋盘数组:
```
chess_board = [[0 for x in range(8)] for y in range(8)]
# 设置棋盘上的黑色棋子
chess_board[0][0] = "r"
chess_board[0][1] = "n"
chess_board[0][2] = "b"
chess_board[0][3] = "q"
chess_board[0][4] = "k"
chess_board[0][5] = "b"
chess_board[0][6] = "n"
chess_board[0][7] = "r"
for i in range(8):
chess_board[1][i] = "p"
# 设置棋盘上的白色棋子
chess_board[7][0] = "R"
chess_board[7][1] = "N"
chess_board[7][2] = "B"
chess_board[7][3] = "Q"
chess_board[7][4] = "K"
chess_board[7][5] = "B"
chess_board[7][6] = "N"
chess_board[7][7] = "R"
for i in range(8):
chess_board[6][i] = "P"
# 打印棋盘
for row in chess_board:
print(row)
```
这段代码会输出以下结果:
```
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P']
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
```
这个棋盘数组中,小写字母表示黑色棋子,大写字母表示白色棋子,"p"表示兵,"r"表示车,"n"表示马,"b"表示象,"q"表示后,"k"表示王。
阅读全文