python3的tic tac toe

时间:2016-11-18 05:54:52

标签: python-3.x 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("\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()

2 个答案:

答案 0 :(得分: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游戏,继续保持:)

答案 1 :(得分:0)

https://www.example.com/name

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

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
相关问题