python3的tic tac toe

问题描述 投票:-1回答:2

如何在这个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("\nSpace already taken. Try again")
            except ValueError:
                    print("\nThat'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!\n".format(board[x]))
                    print("Congratulations!\n")
                    return True
    if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
            print("The game ends in a tie\n")
            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)\n") != "y":
                    break
main()
python-3.x tic-tac-toe
2个回答
0
投票

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

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

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!\n".format(board[x]))
            print("Congratulations!\n")
            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\n")
        scoreboard["T"] += 1
        return True

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

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

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


0
投票
def GO(win,board):
    for x, o, b in win:
        if board[x] == board[o] == board[b]:
            print("Player {0} wins!\n".format(board[x]))
            print("Congratulations!\n")
            return True
    if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
        print("The game ends in a tie\n")
        # 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)\n") != "y":
            break
main()
© www.soinside.com 2019 - 2024. All rights reserved.