尝试编写一个来自Udemy课程的简单井字棋。我做错了什么?

huangapple go评论66阅读模式
英文:

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(&#39;\n&#39;*100) # 只让您看到游戏板的一个版本
print(board[1] + &#39;|&#39; + board[2] + &#39;|&#39; + board[3])
print(&#39;-----&#39;)
print(board[4] + &#39;|&#39; + board[5] + &#39;|&#39; + board[6])
print(&#39;-----&#39;)
print(board[7] + &#39;|&#39; + board[8] + &#39;|&#39; + board[9])


# 选择玩家1是X还是O

`def player_input():
marker = ''

while marker != &#39;X&#39; and marker != &#39;O&#39;:
    marker = input(&#39;玩家1选择 X 或 O: &#39;)
    
    player1 = marker.upper()

    if player1 == &#39;X&#39;:
        player2 = &#39;O&#39;
    else:
        player2 = &#39;X&#39;

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 (&#39;玩家1&#39;)
else:
    return (&#39;玩家2&#39;)`

# 检查位置是否可用

def space_check(board, position):
return board[position] == &#39; &#39; # 如果空格为空,则返回值为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(&#39;选择您的位置: &#39;))

return position

def replay():

choice = input(&#39;您想再玩一次吗?&#39;)

return choice == &#39;Yes&#39;`


### 游戏设置 ###

`print('欢迎来玩井字游戏!')

while True:
board = [' '*10]
player1_marker,player2_marker = player_input()
turn = choose_first()
print(turn + '将先行')
game_on = ''

play_game = input(&#39;您准备好玩了吗?是或否: &#39;)

if play_game.lower()[0] == &#39;y&#39;:
    game_on == True
else:
    game_on == False

    # 玩家1回合
if turn == &#39;玩家1&#39;:

    # 显示游戏板

    display_board(board)

    # 选择位置

    position = player_choice(board)

    # 在选择的位置放置标记

    place_marker(board,player1_marker,position)

    # 检查是否获胜

    if win_check(board,player1_marker) == True:
        display_board(board)
        print(&#39;玩家1赢得了比赛!&#39;)
        game_on = False

    # 检查是否为平局
    else: 
        if full_board_check(board):
            display_board(board)
            print(&#39;平局!&#39;
英文:

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 &quot;x:\Python\Python_Bootcamp\Complete-Python-3-Bootcamp-master-Milestone Project - 1\TIC_TAC_TOE.py&quot;, line 105, in &lt;module&gt;   
    display_board(board)
  File &quot;x:\Python\Python_Bootcamp\Complete-Python-3-Bootcamp-master-Milestone Project - 1\TIC_TAC_TOE.py&quot;, line 8, in display_board
    print(board[1] + &#39;|&#39; + board[2] + &#39;|&#39; + 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(&#39;\n&#39;*100) # Lets you only see one version of the board
    print(board[1] + &#39;|&#39; + board[2] + &#39;|&#39; + board[3])
    print(&#39;-----&#39;)
    print(board[4] + &#39;|&#39; + board[5] + &#39;|&#39; + board[6])
    print(&#39;-----&#39;)
    print(board[7] + &#39;|&#39; + board[8] + &#39;|&#39; + board[9])`

Choose whether player 1 is X or O

`def player_input():
    marker = &#39;&#39;

    while marker != &#39;X&#39; and marker != &#39;O&#39;:
        marker = input(&#39;Player 1 choose X or O: &#39;)
        
        player1 = marker.upper()

        if player1 == &#39;X&#39;:
            player2 = &#39;O&#39;
        else:
            player2 = &#39;X&#39;

    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 (&#39;Player 1&#39;)
    else:
        return (&#39;Player 2&#39;)`

Check to see if position freely available

`def space_check(board, position):
    return board[position] == &#39; &#39; # 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(&#39;Choose your position: &#39;))

    return position

def replay():
    
    choice = input(&#39;Do you want to play again?&#39;)

    return choice == &#39;Yes&#39;`

GAME SETUP

`print(&#39;Welcome to Tic Tac Toe!&#39;)

while True:
    board = [&#39; &#39;*10]
    player1_marker,player2_marker = player_input()
    turn = choose_first()
    print(turn + &#39; will go first&#39;)
    game_on = &#39;&#39;
    
    play_game = input(&#39;Are you ready to play Yes or No?: &#39;)

    if play_game.lower()[0] == &#39;y&#39;:
        game_on == True
    else:
        game_on == False

        # Player 1 Turn
    if turn == &#39;Player 1&#39;:

        # 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(&#39;Player 1 has won the game!&#39;)
            game_on = False

        # Check to see if Tie
        else: 
            if full_board_check(board):
                display_board(board)
                print(&#39;TIE GAME&#39;)
                break

    # If there&#39;s no tie turn to Player 2        
    else:
        turn = &#39;Player 2&#39;

        # Player2&#39;s turn.
    if turn == &#39;Player 2&#39;:

        # 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(&#39;Player 2 has won the game!&#39;)
            game_on = False
        
        # Check to see if Tie
        else:
            full_board_check(board)
            display_board(board)
            print(&#39;TIE GAME&#39;)
            break
    else:
        turn = &#39;Player 1&#39;
    
    # 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 = [&#39; &#39;*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*[&#39; &#39;]

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.

huangapple
  • 本文由 发表于 2023年2月10日 03:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定