Je tente de faire un jeu reversi en utilisant python. c'est le code pour un tableau blancQue signifie un index de chaîne hors plage?
#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)
pour une raison quand je l'exécute je reçois ce message:
File "C:\Users\Larry\Desktop\test.py", line 18, in drawBoard
print('| %s' % (board[x][y]), end='.')
IndexError: string index out of range
ce que cela signifie. dois-je changer
char_range()
ou
print('| %s' % (board[x][y]), end='.')
Cela signifie que la chaîne n'est pas aussi grand que vous le pensez et vous ne pouvez pas indexer après sa fin. – JBernardo