英文:
Try to code a simple Tic Tac Toe from a Udemy course. What did I do wrong?
问题
下面是我用于简单井字游戏的代码。我遇到了以下错误,不知道该如何解决。在Jupyter笔记本中,这个程序是可以运行的,但是当我尝试在VSCode中运行脚本时,就会出现错误。我需要指导如何修复这个错误。
***错误如下: ***
Traceback (most recent call last):
File "x:\Python\Python_Bootcamp\Complete-Python-3-Bootcamp-master\04-Milestone Project - 1\TIC_TAC_TOE.py", line 105, in <module>
display_board(board)
File "x:\Python\Python_Bootcamp\Complete-Python-3-Bootcamp-master\04-Milestone Project - 1\TIC_TAC_TOE.py", line 8, in display_board
print(board[1] + '|' + board[2] + '|' + board[3])
~~~~~^^^
IndexError: list index out of range
### 井字游戏 ###
import random
# 显示游戏的游戏板
def display_board(board):
# print('\n'*100) # 只让您看到游戏板的一个版本
print(board[1] + '|' + board[2] + '|' + board[3])
print('-----')
print(board[4] + '|' + board[5] + '|' + board[6])
print('-----')
print(board[7] + '|' + board[8] + '|' + board[9])
# 选择玩家1是X还是O
`def player_input():
marker = ''
while marker != 'X' and marker != 'O':
marker = input('玩家1选择 X 或 O: ')
player1 = marker.upper()
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return(player1, player2)`
# 在游戏板上占据位置并放置标记
def place_marker(board,marker,position):
board[position] = marker
# 检查是否获胜
def win_check(board, mark):
return ((board[7] == mark and board[8] == mark and board[9] == mark) or # 底线横排
(board[4] == mark and board[5] == mark and board[6] == mark) or # 中间横排
(board[1] == mark and board[2] == mark and board[3] == mark) or # 顶部横排
(board[7] == mark and board[4] == mark and board[1] == mark) or # 左侧竖排
(board[2] == mark and board[5] == mark and board[8] == mark) or # 中间竖排
(board[3] == mark and board[6] == mark and board[9] == mark) or # 右侧竖排
(board[3] == mark and board[5] == mark and board[7] == mark) or # 对角线
(board[9] == mark and board[5] == mark and board[1] == mark)) # 对角线
# 随机决定是玩家1还是玩家2先开始
`def choose_first():
flip = random.randint(0,1)
if flip == 0:
return ('玩家1')
else:
return ('玩家2')`
# 检查位置是否可用
def space_check(board, position):
return board[position] == ' ' # 如果空格为空,则返回值为True
# 检查游戏板是否已满
def full_board_check(board):
for i in range(1,10):
if space_check(board,i):
return False # False 表示该位置为空
else:
return True # True 表示游戏板已满
# 玩家选择他们在游戏板上的下一个位置的功能
`def player_choice(board):
position = 0
while position not in [1,2,3,4,5,6,7,8,9]:
position = int(input('选择您的位置: '))
return position
def replay():
choice = input('您想再玩一次吗?')
return choice == 'Yes'`
### 游戏设置 ###
`print('欢迎来玩井字游戏!')
while True:
board = [' '*10]
player1_marker,player2_marker = player_input()
turn = choose_first()
print(turn + '将先行')
game_on = ''
play_game = input('您准备好玩了吗?是或否: ')
if play_game.lower()[0] == 'y':
game_on == True
else:
game_on == False
# 玩家1回合
if turn == '玩家1':
# 显示游戏板
display_board(board)
# 选择位置
position = player_choice(board)
# 在选择的位置放置标记
place_marker(board,player1_marker,position)
# 检查是否获胜
if win_check(board,player1_marker) == True:
display_board(board)
print('玩家1赢得了比赛!')
game_on = False
# 检查是否为平局
else:
if full_board_check(board):
display_board(board)
print('平局!'
英文:
Below is the code I used for my simple Tic Tac Toe game. I receive the following error I have no idea how to get around. This program does work in Jupyter notebook, but when I try to run the script in VScode, the error appears. I need guidance on how I can fix this error.
***THE ERROR IS AS FOLLOWS: ***
Traceback (most recent call last):
File "x:\Python\Python_Bootcamp\Complete-Python-3-Bootcamp-master-Milestone Project - 1\TIC_TAC_TOE.py", line 105, in <module>
display_board(board)
File "x:\Python\Python_Bootcamp\Complete-Python-3-Bootcamp-master-Milestone Project - 1\TIC_TAC_TOE.py", line 8, in display_board
print(board[1] + '|' + board[2] + '|' + board[3])
~~~~~^^^
IndexError: list index out of range
TIC TAC TOE GAME
`
import random
`
Display the board for the game
`def display_board(board):
# print('\n'*100) # Lets you only see one version of the board
print(board[1] + '|' + board[2] + '|' + board[3])
print('-----')
print(board[4] + '|' + board[5] + '|' + board[6])
print('-----')
print(board[7] + '|' + board[8] + '|' + board[9])`
Choose whether player 1 is X or O
`def player_input():
marker = ''
while marker != 'X' and marker != 'O':
marker = input('Player 1 choose X or O: ')
player1 = marker.upper()
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return(player1, player2)`
Takes a position on the board and puts a marker on position
`def place_marker(board,marker,position):
board[position] = marker`
Check to see if win
`def win_check(board, mark):
return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the bottom
(board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
(board[1] == mark and board[2] == mark and board[3] == mark) or # across the top
(board[7] == mark and board[4] == mark and board[1] == mark) or # down the left column
(board[2] == mark and board[5] == mark and board[8] == mark) or # down the middle
(board[3] == mark and board[6] == mark and board[9] == mark) or # down the right side
(board[3] == mark and board[5] == mark and board[7] == mark) or # diagonal
(board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal`
Random flip to see if Player 1 or Player 2 goes first
`def choose_first():
flip = random.randint(0,1)
if flip == 0:
return ('Player 1')
else:
return ('Player 2')`
Check to see if position freely available
`def space_check(board, position):
return board[position] == ' ' # If space is empty the return value will be True`
Check to see if the board is full
`def full_board_check(board):
for i in range(1,10):
if space_check(board,i):
return False # Fales meaning the space is empty of marker
else:
return True # True meaning the board is full`
Function for player to choose their next position on the board
`def player_choice(board):
position = 0
while position not in [1,2,3,4,5,6,7,8,9]:
position = int(input('Choose your position: '))
return position
def replay():
choice = input('Do you want to play again?')
return choice == 'Yes'`
GAME SETUP
`print('Welcome to Tic Tac Toe!')
while True:
board = [' '*10]
player1_marker,player2_marker = player_input()
turn = choose_first()
print(turn + ' will go first')
game_on = ''
play_game = input('Are you ready to play Yes or No?: ')
if play_game.lower()[0] == 'y':
game_on == True
else:
game_on == False
# Player 1 Turn
if turn == 'Player 1':
# Show board
display_board(board)
# Choose a position
position = player_choice(board)
# Place marker on choosen position
place_marker(board,player1_marker,position)
# Check if they won
if win_check(board,player1_marker) == True:
display_board(board)
print('Player 1 has won the game!')
game_on = False
# Check to see if Tie
else:
if full_board_check(board):
display_board(board)
print('TIE GAME')
break
# If there's no tie turn to Player 2
else:
turn = 'Player 2'
# Player2's turn.
if turn == 'Player 2':
# Show board
# display_board(board)
# Show position
position = player_choice(board)
#Place marker on position
place_marker(board,player2_marker,position)
# Check to see if win
if win_check(board,player2_marker) == True:
display_board(board)
print('Player 2 has won the game!')
game_on = False
# Check to see if Tie
else:
full_board_check(board)
display_board(board)
print('TIE GAME')
break
else:
turn = 'Player 1'
# If players do not want to play again, quit
if not replay():
break`
I've tried to comment out different lines of code to test some of the board configurations. I can get the board to show, but once I try to play a full game I get an error.
答案1
得分: 1
你初始化了board
:
board = [' '*10]
所以board
是只有一个元素的列表(一个包含10个空格的字符串)。因此索引1并不存在。
也许你想要写:
board = 10*[' ']
这样你就会得到一个包含10个元素的列表,每个元素都是空格 ' '
(10个空格)。
我测试了一下,使用你的代码是可以的,我成功进行了我的第一步。
英文:
You have init
board = [' '*10]
so board is a list of only one element (one string with 10 spaces). So index 1 does not exists.
maybee you wanted to write:
board = 10*[' ']
so you have a list with 10 elements ' ' (10 times one blank/space).
I tested it and it works with your code, and I managed to play my first move.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论