Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:如何改进这段代码(TicTacToe)?_Python - Fatal编程技术网

python:如何改进这段代码(TicTacToe)?

python:如何改进这段代码(TicTacToe)?,python,Python,我刚刚开始学习python,这是一个Tictatcoe游戏的代码。 你能提出改进建议吗?另外,如果在后续输入中输入了相同的符号(不使用变量),我如何提醒玩家 你可以忽略玩家一起输入他的符号。 玩家轮换,因此您可以提前一个简单的轮换计数器,并使用适当的符号: pos_matrix=[] player = 'xoxoxoxox' turn = 0 def initGame(): global pos_matrix global turn turn = 0 pos_m

我刚刚开始学习python,这是一个Tictatcoe游戏的代码。 你能提出改进建议吗?另外,如果在后续输入中输入了相同的符号(不使用变量),我如何提醒玩家


你可以忽略玩家一起输入他的符号。 玩家轮换,因此您可以提前一个简单的轮换计数器,并使用适当的符号:

pos_matrix=[]
player = 'xoxoxoxox'
turn = 0

def initGame():
    global pos_matrix
    global turn
    turn = 0
    pos_matrix=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]

def printGame():
    for i in range(3):
        print("{} | {} | {}".format(*pos_matrix[i])) # decompose into its 3 values
        print('--|---|--')
    print("")


def my_tictactoe(pos):
    global turn
    global player

    if pos_matrix[pos[0]-1][pos[1]-1] != ' ':
        print('Invalid input')
        exit(1) # return fail code
    else:
        pos_matrix[pos[0]-1][pos[1]-1]= player[turn] 

    printGame()

    if is_win():
        print('GAME OVER. \n Player with symbol {x} wins!'.format( x = player[turn] ))
        initGame()
    elif turn == 8:
        print('Game Over -  D R A W.')
        initGame()

    else:
        turn += 1


def is_win():
    for i in [0,1,2]:
        if pos_matrix[i][0]==pos_matrix[i][1]==pos_matrix[i][2]!=' ':
            return True
        elif pos_matrix[0][i]==pos_matrix[1][i]==pos_matrix[2][i]!=' ':
            return True
    if pos_matrix[0][0]==pos_matrix[1][1]==pos_matrix[2][2]!=' ':
        return True
    elif pos_matrix[0][2]==pos_matrix[1][1]==pos_matrix[2][0]!=' ':
        return True
    else:
        return False


initGame()
my_tictactoe((1,1))
my_tictactoe((2,1))
my_tictactoe((1,2))
my_tictactoe((2,2))
my_tictactoe((2,3))
my_tictactoe((1,3))
my_tictactoe((3,1))
my_tictactoe((3,2))
my_tictactoe((3,3))
对于代码增强,请开始创建具有一个确切用途的较小函数(请参见
打印板()
),直到每个函数都只有一个OLID(请阅读其他的OLID),它们也有助于获得更干净的代码


编辑:添加了绘制状态和初始游戏()

pos_matrix=[]
player = 'xoxoxoxox'
turn = 0

def initGame():
    global pos_matrix
    global turn
    turn = 0
    pos_matrix=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]

def printGame():
    for i in range(3):
        print("{} | {} | {}".format(*pos_matrix[i])) # decompose into its 3 values
        print('--|---|--')
    print("")


def my_tictactoe(pos):
    global turn
    global player

    if pos_matrix[pos[0]-1][pos[1]-1] != ' ':
        print('Invalid input')
        exit(1) # return fail code
    else:
        pos_matrix[pos[0]-1][pos[1]-1]= player[turn] 

    printGame()

    if is_win():
        print('GAME OVER. \n Player with symbol {x} wins!'.format( x = player[turn] ))
        initGame()
    elif turn == 8:
        print('Game Over -  D R A W.')
        initGame()

    else:
        turn += 1


def is_win():
    for i in [0,1,2]:
        if pos_matrix[i][0]==pos_matrix[i][1]==pos_matrix[i][2]!=' ':
            return True
        elif pos_matrix[0][i]==pos_matrix[1][i]==pos_matrix[2][i]!=' ':
            return True
    if pos_matrix[0][0]==pos_matrix[1][1]==pos_matrix[2][2]!=' ':
        return True
    elif pos_matrix[0][2]==pos_matrix[1][1]==pos_matrix[2][0]!=' ':
        return True
    else:
        return False


initGame()
my_tictactoe((1,1))
my_tictactoe((2,1))
my_tictactoe((1,2))
my_tictactoe((2,2))
my_tictactoe((2,3))
my_tictactoe((1,3))
my_tictactoe((3,1))
my_tictactoe((3,2))
my_tictactoe((3,3))