python3的tic tac toe

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3的tic tac toe相关的知识,希望对你有一定的参考价值。

如何在这个tic tac toe游戏中添加一个得分计数器:

我需要能够保持tic tac toe游戏的得分,以便用户(它的2人游戏)可以连续玩可变游戏并且程序将为他们保持得分。该程序已经允许用户继续玩这样他们可以连续玩多个游戏但是该程序不会跟踪玩家x和o的分数或者关系的数量。我如何添加到这个以便托盘玩家x的胜利,玩家的胜利以及关系数量

def drawboard(board):
    print('   |   |')
    print(' ' + str(board[7]) + ' | ' +str( board[8]) + ' | ' + str(board[9]))
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + str(board[4]) + ' | ' + str(board[5]) + ' | ' + str(board[6]))
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + str(board[1]) + ' | ' + str(board[2]) + ' | ' + str(board[3]))
    print('   |   |')
def draw(board):
    print(board[7], board[8], board[9])
    print(board[4], board[5], board[6])
    print(board[1], board[2], board[3])
    print()
def t(board):
    while True:
            try:
                    x = int(input())
                    if x in board:
                            return x
                    else:
                            print("
Space already taken. Try again")
            except ValueError:
                    print("
That's not a number. enter a space 1-9")
def GO(win,board):
    for x, o, b in win:
            if board[x] == board[o] == board[b]:
                    print("Player {0} wins!
".format(board[x]))
                    print("Congratulations!
")
                    return True
    if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
            print("The game ends in a tie
")
            return True
def tic_tac_toe():
    board = [None] + list(range(1, 10))
    win = [(1, 2, 3),(4, 5, 6),(7, 8, 9),(1, 4, 7),(2, 5, 8),(3, 6, 9),(1, 5, 9),(3, 5, 7),]
    for player in 'XO' * 9:
            drawboard(board)
            if GO(win,board):
                    break
            print("Player {0}".format(player))
            board[t(board)] = player
            print()
def main():
    while True:
            tic_tac_toe()
            if input("Play again (y/n)
") != "y":
                    break
main()
答案

看到代码已经有点乱,仍然没有答案,我建议一个快速和肮脏的解决方案。

您可以在所有函数之外定义全局变量,例如:

scoreboard = {"X": 0, "O": 0, "T": 0}

然后你只需增加GO函数的分数。

def GO(win,board):
    for x, o, b in win:
        if board[x] == board[o] == board[b]:
            print("Player {0} wins!
".format(board[x]))
            print("Congratulations!
")
            scoreboard[board[x]] += 1
            return True
    if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
        print("The game ends in a tie
")
        scoreboard["T"] += 1
        return True

只需打印scoreboard的分数,无论你想要什么。

但是,我建议学习如何使代码更具可读性。它将帮助您更轻松地编写更难的程序。这将有助于避免像这样的快速和肮脏的解决方案,并使很多困难的事情落实到位,而不需要太多的努力。

在任何情况下,恭喜写一个有效的TTT游戏,继续保持:)

另一答案
def GO(win,board):
    for x, o, b in win:
        if board[x] == board[o] == board[b]:
            print("Player {0} wins!
".format(board[x]))
            print("Congratulations!
")
            return True
    if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
        print("The game ends in a tie
")
        # return False if there is no winner 
        return False
def tic_tac_toe():
    board = [None] + list(range(1, 10))
    win = [(1, 2, 3),(4, 5, 6),(7, 8, 9),(1, 4, 7),(2, 5, 8),(3, 6, 9),(1, 5, 9),(3, 5, 7),]
    for player in 'XO' * 9:
        drawboard(board)
        if GO(win,board):
            # returns the winner if there is one    
            return player
        elif GO(win, board) is False:
            # returns False if winner is a tie     
            return False  
        print("Player {0}".format(player))
        board[t(board)] = player
        print()

首先,您应该为不同的游戏结果创建唯一的输出值,而不是仅为True。然后,您可以根据函数调用的值在while循环中保持分数。

def main():
   count_x = 0
   count_o = 0 
    while True:
        score = tic_tac_toe()
        if score == 'X':
            count_x += 1
        elif score == 'O':
            count_o += 1 
        print("The running score is " + '('+ str(count_x), str(count_y) +')')
        if input("Play again (y/n)
") != "y":
            break
main()

以上是关于python3的tic tac toe的主要内容,如果未能解决你的问题,请参考以下文章

Tic-tac-toe使用python tkinter

html Tic Tac Toe

html Tic Tac Toe决赛

html Tic Tac Toe

Design Tic-Tac Toe

简单的Tic-Tac-Toe