2012-12-08 46 views
2

到目前爲止,我有一個程序讓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() 

回答

6

保持數據和數據表示的分離。這就是如何。現在你只是在繪製東西,而不是你應該產生一些表現場(例如,列表框和它們的狀態,如在,由p1選中,由p2選中或未選中),然後使用它在需要時繪製。優勢應該立即顯而易見 - 如果你知道遊戲的狀態,確定是否有贏家(以及它是誰)是微不足道的。

+0

我該如何讓程序知道哪個方格被佔用? – user1871071

+1

@ user1871071:您可以使用Python中的2維數組可以列在列表中。即board = [[0,0,0],[0,0,0],[0,0,0]]',其中'0'表示空。要改變它的內容,只需'board [i] [j] = value',其中'i'和'j'是要訪問的地點的水平和垂直位置(aka座標)。 – martineau

+0

你可以在我的代碼中實現這個嗎?我不確定你的意思,我對編程很陌生。 – user1871071

0

經過3回合(最小轉成功),檢查你的二維數組,如果最後一個記號旁邊有一個記號,通過添加/減去一個記錄,如果發現重複相同的操作數組索引其他爆發。

如果第二個控制結構到達休息並宣佈獲勝者。

+0

問題是我不知道如何製作2D數組。 – user1871071

+0

@ a3f獲勝的最低輪次必須是5:玩家1,玩家2,玩家1,玩家2,玩家1.先移動的玩家將獲勝(玩家1)。 –

+1

@ F3AR3DLEGEND理論上的最小值是3,即使其他玩家玩得像延遲一樣,算法也應該檢測到贏家(即:玩,所以他不會阻止其他玩家) @ user1871071我在參加會議並且無法閱讀代碼,因爲看起來你應該在再次嘗試^^之前先閱讀一兩個教程 – a3f

0

隨着遊戲中的每一次移動,都應該使用2D數組或字典(值爲列表)。然後,你可以檢查每種獲勝方式。這樣,您也可以檢查移動是否有效 - 是否拍攝板上的斑點。

我也建議使用數字或座標系來指定移動。

董事會應該是這樣的:

1 2 3 
4 5 6 
7 8 9 

中的數字對應的電路板上的斑點。

例如:

在初始化:

moves = 0 
positions = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9':0} 
# '1' - '9' are the spots on the board. 
# 0 means the spot is empty, 'X' means the spot is taken by 'X', 'O' means the spot is taken by 'O'. You can use any other naming system, but this is simple. 

在移動代碼:

while 1 == 1: # just a loop until the input is valid. See the 'break' statement below 
    new_move = input("X, enter what space to move to: ") 
    if positions[new_move] == 0: # if that board spot is empty 
     moves += 1 #moves = moves + 1 
     positions[new_move] == 'X' # board spot is now occupied by 'X' 
     # code to show the piece on the board 
     if moves >= 5: # least possible moves to win is 5 
     win_check(positions) 
     break 

或者,您可以使用移動的功能,並將它遞歸調用本身直到輸入有效:

def move_X(): 
    new_move = input("X, enter what space to move to: ") 
    if positions[new_move] == 0: # if that board spot is empty 
     moves += 1 #moves = moves + 1 
     positions[new_move] == 'X' # board spot is now occupied by 'X' 
     # code to show the piece on the board 
     if moves >= 5: # least possible moves to win is 5 
     win_check(positions) 
     move_O() # this should be defined similarly to 'move_X' except that it would correlate to 'O'. 
    else: 
     move_X() 

的奪冠檢查方法:

def win_check(positions): 
    if positions['1'] == 'X' and positions['2'] == 'X' and positions['3'] == 'X': 
     return "Winner: X" 
    elif # similar things, checking all of the other ways to win. 

您需要1點if聲明(在開始時)和15條elif語句,有8種方法來贏得每個球員,所以16個檢查必須進行。