Beispiel #1
0
def getBestMoves(node, depth, guess=0):
    board = boardRep.TicTacToeBoard(node)
    bestMoves = set()
    if board.isOver():
        return bestMoves
    sm = board.successorMap()
    nodeValue = mtdf(node, guess, depth)
    for move in sm.keys():
        child = sm[move]
        childValue = iterativeDeepening(child)
        if childValue == nodeValue:
            bestMoves.add(move)
    return bestMoves
Beispiel #2
0
def evaluate(node, player, verbose=0):
    board = boardRep.TicTacToeBoard(node)
    return board.evaluate(player, verbose)
Beispiel #3
0
def getChildren(node):
    board = boardRep.TicTacToeBoard(node)
    sm = board.successorMap()
    return sm.values()
Beispiel #4
0
def isTerminal(node):
    board = boardRep.TicTacToeBoard(node)
    return board.isOver()
Beispiel #5
0
def isMaximizing(node):
    board = boardRep.TicTacToeBoard(node)
    return board.whoseTurn() == AIPLAYER
Beispiel #6
0
        i=raw_input(prompt)

        return int(i)


if __name__=="__main__":
    
    import boardRep
    import randomPlayer

    p1=HumanTicTacToePlayer(1)
    #p2=HumanTicTacToePlayer(2)
    p2=randomPlayer.RandomTicTacToePlayer(2)


    b=boardRep.TicTacToeBoard()

    movingPlayer=0
    players=[p1,p2]
    
    while not b.isOver():
        m=players[movingPlayer].getMove(b)
        if m==0:
            break
        m=m-1

        x=m%3
        y=(m-x)/3
        if b.isLegalMove(x,y,movingPlayer+1):
            b.move(x,y,movingPlayer+1)
            movingPlayer += 1
Beispiel #7
0
def drawBoard(c, xIndex, yIndex, hashVal, boardIndex):
    left = xIndex * boardSlotWidth + leftMargin
    bottom = pageHeight - ((yIndex + 1) * boardSize + bottomMargin)
    c.saveState()
    c.translate(left, bottom)

    c.saveState()
    FONTSIZE = 8
    c.setFont("Helvetica", FONTSIZE)
    labelColor = (1.0, 1.0, 1.0)
    labelBackground = (0.5, 0.5, 0.5)

    c.setFillColor(labelBackground)
    c.rect(0,
           boardBottomMargin,
           boardLeftMargin,
           boardSize - 2 * boardBottomMargin,
           stroke=0,
           fill=1)
    c.rotate(90)
    #label="%d (%d)"%(boardIndex,hashVal)
    label = str(boardIndex)
    w = c.stringWidth(label, "Helvetica", FONTSIZE)

    c.setStrokeColor(labelColor)
    c.setFillColor(labelColor)
    c.drawString((boardSize - w) / 2, -(boardLeftMargin + FONTSIZE) / 2, label)
    c.restoreState()

    c.line(boardLeftMargin, boardBottomMargin + squareSize, boardRightMargin,
           boardBottomMargin + squareSize)
    c.line(boardLeftMargin, boardBottomMargin + 2 * squareSize,
           boardRightMargin, boardBottomMargin + 2 * squareSize)
    c.line(boardLeftMargin + squareSize, boardBottomMargin,
           boardLeftMargin + squareSize, boardSize - boardBottomMargin)
    c.line(boardLeftMargin + 2 * squareSize, boardBottomMargin,
           boardLeftMargin + 2 * squareSize, boardSize - boardBottomMargin)

    b = boardRep.TicTacToeBoard(hashVal)

    for x in range(3):
        for y in range(3):
            si = 3 * y + x
            sv = b.squares[si]
            c.saveState()
            c.translate(boardLeftMargin + pieceMargin + x * squareSize,
                        boardBottomMargin + pieceMargin + y * squareSize)

            if sv == 1:
                c.line(0, 0, pieceSize, pieceSize)
                c.line(0, pieceSize, pieceSize, 0)
            elif sv == 2:
                c.ellipse(0, 0, pieceSize, pieceSize)
            c.restoreState()

    v = mtdf.mtdf(hashVal, 0, 12)
    valueStr = {-1000: "O Wins", 0: "Draw", 1000: "X Wins"}[v]

    sm = b.successorMap()
    bestMoves = mtdf.getBestMoves(hashVal, 12, v)
    bestMoveList = list(bestMoves)

    destList = []
    if sm:
        destSet = set()
        for m in bestMoves:
            destSet.add(1 + boardIndexList.index(sm[m]))
        destList = list(destSet)
        destList.sort()

    for m in bestMoves:
        x = m % 3
        y = (m - x) / 3
        c.saveState()
        c.setFont("Helvetica", pieceSize)
        c.translate(boardLeftMargin + pieceMargin + x * squareSize,
                    boardBottomMargin + pieceMargin + y * squareSize)
        c.drawCentredString(
            pieceSize / 2.0, 0,
            str(1 + destList.index(1 + boardIndexList.index(sm[m]))))
        c.restoreState()

    CAPTIONSIZE = 5.6
    c.setFont("Helvetica", CAPTIONSIZE)

    if b.isOver():
        wtString = "Game Over"
    else:
        wt = b.whoseTurn()
        wtString = {1: "X to move", 2: "O to move"}[wt]

    c.drawString(boardRightMargin, boardSize - boardBottomMargin - CAPTIONSIZE,
                 wtString)
    c.drawString(boardRightMargin,
                 boardSize - boardBottomMargin - 2 * CAPTIONSIZE, valueStr)

    moveStr = ""
    for destIndex in range(len(destList)):
        c.drawString(
            boardRightMargin,
            boardSize - boardBottomMargin - (3 + destIndex) * CAPTIONSIZE,
            "%d : %d" % (destIndex + 1, destList[destIndex]))

    c.restoreState()