TicTacToe Python帮助

时间:2014-03-16 07:24:53

标签: python

对于我的任务,我必须通过实现后继函数和所有必要的辅助函数来使这个tic tac toe程序工作。在目前的状态下,董事会被抽出,但你看不到X和O,也不能玩游戏。任何人都可以帮助我/解释后继功能吗?

# A *cell* is an integer in the interval [1,10). Cells represent squares
# on the tic tac toe board as pictured below:
#    1|2|3
#    -----
#    4|5|6
#    -----
#    7|8|9
#
# A *game state* is a pair (aka 2-tuple) of the form (S,T) where S and T
# are sets of cells. The game state (S,T) is thought of as the
# configuration in which S is the set of cells occupied by 'X' and T is
# the set of cells occupied by 'O'. For example, the game state ({1,9},{5})
# may be visualized as follows:
#    x| |
#    -----
#     |o|
#    -----
#     | |x

def initialState():
    return (set(),set())

def displayImages(S):
    return background() + cellContents(S)

def background():
    L1 = (200,0,200,600)
    L2 = (400,0,400,600)
    L3 = (0,200,600,200)
    L4 = (0,400,600,400)
    return [L1, L2, L3, L4]

def cellContents(S):
    contents = []
    for cell in S[0]:
        contents.append(cellText("X",cell))
    for cell in S[1]:
        contents.append(cellText("O",cell))
    return contents

def cellText(P,C):
    (x,y) = cellCenter(C)
    return (P,x,y,18)

def cellCenter(C):
    if C == 1:
        return (100,500)
    elif C == 2:
        return (300,500)
    elif C == 3:
        return (500,500)
    elif C == 4:
        return (100,300)
    elif C == 5:
        return (300,300)
    elif C == 6:
        return (500,300)
    elif C == 7:
        return (100,100)
    elif C == 8:
        return (300,100)
    else:
        return (500,100)

def successor(S,P):
    return S

# TPGE GAME ENGINE

from graphics import *

# displaySize() is the size of the display window, (width, height)

def displaySize() :
    return (600,600)

# If x is an image, imageKind(x) is the type of image x is:
# 'circle', 'text', or 'lineSegment'

def imageKind(x):
    if len(x)==3 : return 'circle'
    elif type(x[0])== str :return 'text'
    else : return 'lineSegment'

# If x is an image, convert(x) is the corresponding image in the
# graphics.py library. We turn the screen upside down so that the origin
# is in the lower left corner, so it matches what they learn in algebra
# class.

def convert(x):
    if imageKind(x) == 'circle':
        return convertCircle(x)
    elif imageKind(x) == 'lineSegment':
        return convertLine(x)
    elif imageKind(x) == 'text':
        return convertText(x)


def convertLine(x):
    (W,H) = displaySize()
    P1 = Point(x[0],H - x[1])
    P2 = Point(x[2],H - x[3])
    return Line(P1,P2)

def convertText(x):
    (W,H) = displaySize()
    center = Point(x[1],H-x[2])
    string = x[0]
    size = x[3]
    T = Text(center,string)
    T.setSize(size)
    return T

def convertCircle(x):
    (W,H) = displaySize()
    center = Point(x[0],H-x[1])
    radius = x[2]
    return Circle(center,radius)

# Create a window to play in

display = GraphWin("My game", displaySize()[0], displaySize()[1])

# The main loop
#
# Set the state, draw the display, get a mouse click, set the new state,
# and repeat until the user closes the window.

S = initialState()
images = [convert(x) for x in displayImages(S)]
while(True):
    for x in images: x.draw(display)
    c = display.getMouse()
    click = (c.getX(),displaySize()[1] - c.getY())
    S = successor(S,click)
    for I in images: I.undraw()
    images = [convert(x) for x in displayImages(S)]

1 个答案:

答案 0 :(得分:1)

successor有两个参数:一个是游戏状态,一个是刚刚点击的点。游戏状态在评论中解释:

# A *game state* is a pair (aka 2-tuple) of the form (S,T) where S and T
# are sets of cells. The game state (S,T) is thought of as the
# configuration in which S is the set of cells occupied by 'X' and T is
# the set of cells occupied by 'O'. For example, the game state ({1,9},{5})
# may be visualized as follows:

successor获取当前状态,以及播放器刚刚点击的位置 - 并且必须创建并返回游戏状态。所以,它知道电路板目前是什么样的,它知道玩家选择了哪个位置,并且必须在那里放置X或者O.

由于状态由os S和T组成,因此将状态也称为S可能有点令人困惑,所以让我们从重命名并从中提取S和T开始:

def successor(state, position):
    S, T = state

现在,位置已经是你想要的形式 - 你只需要添加到X位置或O位置。现在,让我们总是将它添加到X位置 - 这不是你最终想要它实现的,但它应该可以帮助你理解这个函数做什么然后你可以以后改进它。

def successor(state, position):
    S, T = state
    S.add(position)

现在我们已经创建了一个新状态,我们需要返回它:

def successor(state, position):
    S, T = state
    S.add(position)
    return S, T

有了这三行,你应该看到的是每次点击一个正方形时,都会在其中放入一个X.您需要处理的下一步是更新此功能,以便在放置X或放置O之间交替。