我正在尝试使用python制作一个reversi游戏。这是空白板的代码
#reversi
import random
import sys
board = range(9)
def char_range(start, end, step=1):
for char in range(ord(start), ord(end), step):
yield char
def drawBoard(board):
###this prints out the board
HLINE = ' +-+-+-+-+-+-+-+-+'
VLINE = ' | | | | | | | | |'
print(' a b c d e f g h')
print(HLINE)
for y in range(8):
print(y+1, end = '|.|')
for x in char_range('a','h'):
print('| %s' %(board[x][y]), end='.')
print('|')
print(HLINE)
def resetBoard(board):
#blanks out the board
for x in char_range('a', 'h'):
for y in range(8):
board[x][y] = '.'
#starters
board[c][3] = 'X'
board[c][4] = 'O'
board[d][3] = 'O'
board[d][4] = 'X'
def getNewBoard():
#new board
board = []
for i in range(9):
board.append([' '] * 9)
return board
getNewBoard()
drawBoard(board)
resetBoard(board)
由于某些原因,当我执行它时,我收到此消息:
File "C:\Users\Larry\Desktop\test.py", line 18, in drawBoard
print('| %s' % (board[x][y]), end='.')
IndexError: string index out of range
这是什么意思。我需要更改吗?
char_range()
或
print('| %s' % (board[x][y]), end='.')
答案 0 :(得分:0)
正如wim评论的那样,dict通常是实现这样一个董事会的完美方式
def getNewBoard():
#new board
board = {}
for x in range(8): # or x in 'abcdefgh': if you prefer
for y in range(8):
board[x, y] = ' '
return board
不要忘记将电路板分配到board
board = getNewBoard()