做Python-Zelle图形tic-tac-toe游戏直到一个玩家得到10分

2024-04-16 17:49:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让电脑和玩家一起玩,直到其中一个得了10分:

from graphics import *
board=[[0,0,0],[0,0,0],[0,0,0]]#as bourd
window=GraphWin("Tic Tac Toe",700,700)
L0=Line(Point(250,50),Point(250,650)).draw(window)
L1=Line(Point(450,50),Point(450,650)).draw(window)
L2=Line(Point(50,250),Point(650,250))
L2.draw(window)
L3=Line(Point(50,450),Point(650,450))
L3.draw(window)
xTurn=True
num=0
while num<9:
    b=window.getMouse()
    pa,pb=int((b.x-50)/200)+1,int((b.y-50)/200)+1
    if board[pb-1][pa-1]==0:
        num+=1
        if xTurn:
            tex="X"
            xTurn=False
        else:
            tex="O"
            xTurn=True
        h=Text(Point(pa*150+50*(pa-1),pb*150+50*(pb-1)),tex).draw(window)
        h.setSize(36)
        if xTurn:
            h.setFill("blue")
            board[pb-1][pa-1]=1
        else:
            h.setFill("red")
            board[pb-1][pa-1]=2
    if num>4:
         if (board[0][0]==1 and board[0][1]==1 and board[0][2]==1) or(board[1][0]==1 and board[1][1]==1 and board[1][2]==1) or(board[2][0]==1 and board[2][1]==1 and board[2][2]==1):
                 print(" O is winner")
                 break
         elif (board[0][0]==2 and board[0][1]==2 and board[0][2]==2) or(board[1][0]==2 and board[1][1]==2 and board[1][2]==2) or (board[2][0]==2 and board[2][1]==2 and board[2][2]==2):
                 print(" X is winner")
                 break

         elif (board[0][0]==2 and board[1][0]==2 and board[2][0]==2) or(board[0][1]==2 and board[1][1]==2 and board[2][1]==2) or (board[0][2]==2 and board[1][2]==2 and board[2][2]==2):
                 print(" X is winner")
                 break
         elif (board[0][0]==1 and board[1][0]==1 and board[2][0]==1) or(board[0][1]==1 and board[1][1]==1 and board[2][1]==1) or (board[0][2]==1 and board[1][2]==1 and board[2][2]==1):
                 print(" O is winner")
                 break
         elif (board[0][0]==1 and board[1][1]==1 and board[2][2]==1) or(board[0][2]==1 and board[1][1]==1 and board[2][0]==1):
                 print(" O is winner")
                 break
         elif (board[0][0]==2 and board[1][1]==2 and board[2][2]==2) or(board[0][2]==2 and board[1][1]==2 and board[2][0]==2):
                 print(" X is winner")
                 break         
if num>=9:
    print("There is no winner!")

Tags: orandboardifiswindownumpoint
1条回答
网友
1楼 · 发布于 2024-04-16 17:49:46

我把你的程序拆开,重新组合起来,使之合理化,并允许它在退出前玩十个游戏。它在退出时打印十场比赛的结果。这并不是你所追求的,但我相信它能为你提供做你想做的事情所需的工具:

from graphics import *
from time import sleep

PLAYERS = ['Draw', 'O', 'X']
DRAW = PLAYERS.index('Draw')

COLORS = ['black', 'blue', 'red']

EMPTY = 0

window = GraphWin("Tic Tac Toe", 700, 700)

Line(Point(250, 50), Point(250, 650)).draw(window)
Line(Point(450, 50), Point(450, 650)).draw(window)
Line(Point(50, 250), Point(650, 250)).draw(window)
Line(Point(50, 450), Point(650, 450)).draw(window)

turn = PLAYERS.index('X')
scores = [0] * len(PLAYERS)
tokens = []

while sum(scores) < 10:
    for token in tokens:
        token.undraw()

    board = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]]

    squares_played = 0

    while squares_played < 9:
        point = window.getMouse()
        x, y = int((point.x - 50) / 200), int((point.y - 50) / 200)

        if board[y][x] == EMPTY:
            squares_played += 1
            board[y][x] = turn
            text = PLAYERS[turn]

            token = Text(Point(200 * x + 150, 200 * y + 150), text)
            token.setSize(36)
            token.setFill(COLORS[turn])
            token.draw(window)

            tokens.append(token)

            turn = len(PLAYERS) - turn

        if squares_played > 4:
            if EMPTY != board[0][0] == board[0][1] == board[0][2]:
                print("{} is winner".format(PLAYERS[board[0][0]]))
                scores[turn] += 1
                break
            elif EMPTY != board[1][0] == board[1][1] == board[1][2]:
                print("{} is winner".format(PLAYERS[board[1][1]]))
                scores[turn] += 1
                break
            elif EMPTY != board[2][0] == board[2][1] == board[2][2]:
                print("{} is winner".format(PLAYERS[board[2][2]]))
                scores[turn] += 1
                break

            elif EMPTY != board[0][0] == board[1][0] == board[2][0]:
                print("{} is winner".format(PLAYERS[board[0][0]]))
                scores[turn] += 1
                break
            elif EMPTY != board[0][1] == board[1][1] == board[2][1]:
                print("{} is winner".format(PLAYERS[board[1][1]]))
                scores[turn] += 1
                break
            elif EMPTY != board[0][2] == board[1][2] == board[2][2]:
                print("{} is winner".format(PLAYERS[board[2][2]]))
                scores[turn] += 1
                break

            elif EMPTY != board[0][0] == board[1][1] == board[2][2]:
                print("{} is winner".format(PLAYERS[board[1][1]]))
                scores[turn] += 1
                break
            elif EMPTY != board[0][2] == board[1][1] == board[2][0]:
                print("{} is winner".format(PLAYERS[board[1][1]]))
                scores[turn] += 1
                break

    if squares_played >= 9:
        print("There is no winner!")
        scores[DRAW] += 1

    sleep(2)

for index, player in enumerate(PLAYERS):
    print("{}: {}".format(player, scores[index]))

我的重做依赖于一些并行数组,这比no数组要好,但应该演变成真正的数据结构。你知道吗

相关问题 更多 >