到目前爲止,我有一個程序讓2個玩家輪流點擊放置一個X和一個O.我不知道如何讓程序認出勝者/平局。如果你們能夠幫助我製作一個以任何方式在屏幕上顯示勝利/抽獎的功能,我會永遠愛你。謝謝。我如何找到我的Python Tic Tac Toe遊戲的優勝者?
from graphics import *
import sys
def player_o(win, center):
'''
Parameters:
- win: the window
'''
outline_width = 5
circle = Circle(center, boxsize/2)
circle.setOutline('red')
circle.setWidth(outline_width)
circle.draw(win)
def player_x(win, p1x, p1y):
'''
Parameters:
- win: the window
'''
for i in range(2):
deltaX = (-1) ** i * (boxsize/2)
deltaY = (boxsize/2)
line = Line(Point(p1x - deltaX, p1y - deltaY),
Point(p1x + deltaX, p1y + deltaY))
line.setFill('red')
line.setWidth(5)
line.draw(win)
def game():
global win
global boxsize
try:
winsize = int(input("How large would you like the window? (Between 100 and 3000): "))
if winsize < 100 or winsize > 3000:
print("Invalid window size")
quit()
squares = int(input("How many squares per row? (Between 3 and 10):"))
boxsize = winsize/ squares
if squares < 3 or squares > winsize/10:
print("Invalid number")
quit()
except ValueError:
sys.exit("Not a valid number")
win = GraphWin("Tic Tac Toe", winsize, winsize)
for i in range(squares - 1):
hline = Line(Point(0, (winsize/squares) * (i + 1)), Point(winsize, (winsize/squares) * (i + 1)))
hline.draw(win)
vline = Line(Point((winsize/squares) * (i + 1), 0), Point((winsize/squares) * (i + 1), winsize))
vline.draw(win)
for i in range((squares ** 2) // 2):
print("X, click a square.")
p1mouse = win.getMouse()
p1x = p1mouse.getX()
p1y = p1mouse.getY()
player_x(win, p1x, p1y)
print("O, click a square.")
p2mouse = win.getMouse()
p2x = p2mouse.getX()
p2y = p2mouse.getY()
player_o(win, Point(p2x, p2y))
if squares % 2 == 1:
print("X, click a square.")
p1mouse = win.getMouse()
p1x = p1mouse.getX()
ply = p1mouse.getY()
player_x(win, p1x, p1y)
game()
我該如何讓程序知道哪個方格被佔用? – user1871071
@ user1871071:您可以使用Python中的2維數組可以列在列表中。即board = [[0,0,0],[0,0,0],[0,0,0]]',其中'0'表示空。要改變它的內容,只需'board [i] [j] = value',其中'i'和'j'是要訪問的地點的水平和垂直位置(aka座標)。 – martineau
你可以在我的代碼中實現這個嗎?我不確定你的意思,我對編程很陌生。 – user1871071