1

到目前为止,我有一个程序,其中 2 个玩家可以依次单击以放置 X 和 O。我不确定如何让程序识别获胜者/平局。如果你们可以帮助我制作一个以任何方式在屏幕上显示获胜/平局的功能,我会永远爱你们。谢谢。

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()
4

3 回答 3

6

保持数据和数据表示分离。这就是如何。现在你只是在画东西,而不是你应该生成一些比赛场地的表示(例如,盒子的列表和它们的状态,例如,由 p1 选中,由 p2 选中或未选中),然后在需要时使用它来绘制。优势应该立即显而易见 - 如果您知道游戏的状态,确定是否有赢家(以及获胜者是谁)是微不足道的。

于 2012-12-08T17:20:24.103 回答
0

After 3 turns (minimum turns to win) check your 2d array if there is a token next to the last played by adding/substracting one, if found repeat same operation to array indices else break out.

If 2nd control structure is reached break and announce winner.

于 2012-12-08T17:45:00.887 回答
0

游戏中的每一步都应该使用二维数组或字典(值是列表)。然后,您可以检查每种获胜方式。这样,您还可以检查移动是否有效——棋盘上的位置是否被占据。

我还建议使用数字或坐标系来指示运动。

董事会看起来像这样:

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 次检查。

于 2012-12-08T21:36:52.907 回答