Python更新列表 - 更改在函数

时间:2018-02-01 17:00:51

标签: python python-3.x

我使用python3创建了一个简单的Tic-Tac-Toe游戏。游戏本身的工作原理完全正常,我遇到的唯一问题是重置功能没有正确擦拭板。它设置值,将其打印出来,然后在下一个操作中使用旧值。

问题的关键在于,当你选择,是的,你想再次玩游戏时,游戏会立即结束,因为它使用了之前游戏中的最终棋盘。我尝试了几个不同的东西,但最终得到了相同的结果。这是范围问题,代码中存在明显的错误。问题应该包含在reset()函数中,其他所有内容都存在于上下文中。

#Tic-Tac-Toe Simulator

from random import *

board = [' ']*9
end = 0
abc = ['A', 'B', 'C']
count = 0
action = str

def render():
    print('3 ' + board[0] + '|' + board[1] + '|' + board[2])
    print('  -----')
    print('2 ' + board[3] + '|' + board[4] + '|' + board[5])
    print('  -----')
    print('1 ' + board[6] + '|' + board[7] + '|' + board[8])
    print('  A B C\n')

def winCheck():
    print(board)
    if board[0] == board[1] == board[2] != ' ':
        return board[0]
    elif board[3] == board[4] == board[5] != ' ':
        return board[3]
    elif board[6] == board[7] == board[8] != ' ':
        return board[6]
    elif board[0] == board[3] == board[6] != ' ':
        return board[0]
    elif board[1] == board[4] == board[7] != ' ':
        return board[1]
    elif board[2] == board[5] == board[8] != ' ':
        return board[2]
    elif board[0] == board[4] == board[8] != ' ':
        return board[0]
    elif board[2] == board[4] == board[6] != ' ':
        return board[2]
    else:
        return 0

def convert(location):
    coords = list(location.upper())
    if coords[0] == 'A' and coords[1] == '3':
        return 0
    if coords[0] == 'A' and coords[1] == '2':
        return 3
    if coords[0] == 'A' and coords[1] == '1':
        return 6
    if coords[0] == 'B' and coords[1] == '3':
        return 1
    if coords[0] == 'B' and coords[1] == '2':
        return 4
    if coords[0] == 'B' and coords[1] == '1':
        return 7
    if coords[0] == 'C' and coords[1] == '3':
        return 2
    if coords[0] == 'C' and coords[1] == '2':
        return 5
    if coords[0] == 'C' and coords[1] == '1':
        return 8

def move(letter, location):
    board[convert(location)] = letter.upper()

def spotCheck(location):
    if board[convert(location)] == ' ':
        return False
    else:
        return True

def reset():
    board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
    print(board)
    end = 0
    abc = ['A', 'B', 'C']
    count = 0
    action = str

def main():
    count = 0
    letter = str
    turn = 'x'
    while letter not in ['x', 'o']:
        letter = str(input("Would you like to play as 'X' or 'O': ")).lower()
    print("Please enter moves in the form of coordinates, such as A2 or C1")
    render()
    while winCheck() == 0:
        if letter == turn:
            action = str(input('Enter move: '))
            while spotCheck(action) == True:
                action = str(input('Space occupied, try again: '))
            move(letter, action)
        else:
            x = randint(0, 2)
            y = randint(1, 3)
            x = abc[x]
            action = x + str(y)
            while spotCheck(action) == True:
                x = randint(0, 2)
                y = randint(1, 3)
                x = abc[x]
                action = x + str(y)
            move(turn, action)
        render()
        if turn == 'x':
            turn = 'o'
        else:
            turn = 'x'
        count = count + 1
        if count == 9:
            break
    if winCheck() == 'X':
        print('X has won the game!')
    if winCheck() == 'O':
        print('O has won the game!')
    if winCheck() == 0:
        print('The game is a tie!')

main()
if input("Would you like to play again? ").lower() in ['y','yes']:
    reset()
    main()

1 个答案:

答案 0 :(得分:0)

您正在创建与全局变量具有相同名称的新本地变量。按名称读取变量(如果你没有在相同名称的函数作用域中声明它们),如果你想修改它们,“解释”到python你的意思是全局的:

def reset():
    global board  
    global end  
    global abc  
    global count  
    global action 
    board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
    print(board)
    end = 0
    abc = ['A', 'B', 'C']
    count = 0
    action = str   # where does str come from?

在此处阅读更多内容:Use of "global" keyword in Python