def runGame():
    currentState = INITIAL_STATE
    print('The Gamemaster says, "Our game type for this match is: ' +
          GAME_TYPE + '"')
    print('The Gamemaster says, "Players, introduce yourselves."')
    print('     (Playing White:) ' + player1.who_am_i())
    print('     (Playing Black:) ' + player2.who_am_i())

    if USE_HTML:
        html.startHTML(player1.moniker(), player1.who_am_i(),
                       player2.moniker(), player2.who_am_i(), GAME_TYPE, K, 1)
    try:
        p1comment = player1.get_ready(INITIAL_STATE, K, 'W', player2.moniker())
    except Exception as e:
        report = 'Player 1 (' + player1.moniker(
        ) + ' failed to prepare, and loses by default.'
        print(report)
        if USE_HTML: html.reportResult(report)
        report = 'Congratulations to Player 2 (' + player2.moniker() + ')!'
        print(report)
        if USE_HTML: html.reportResult(report)
        if USE_HTML: html.endHTML()
        print(e)
        return
    try:
        p2comment = player2.get_ready(INITIAL_STATE, K, 'B', player1.moniker())
    except Exception as e:
        report = 'Player 2 (' + player2.moniker(
        ) + ' failed to prepare, and loses by default.'
        print(report)
        if USE_HTML: html.reportResult(report)
        report = 'Congratulations to Player 1 (' + player1.moniker() + ')!'
        print(report)
        if USE_HTML: html.reportResult(report)
        if USE_HTML: html.endHTML()
        print(e)
        return

    print('The Gamemaster says, "Let\'s Play!"')
    print('The initial state is...')

    currentRemark = "The game is starting."
    if USE_HTML: html.stateToHTML(currentState)

    WhitesTurn = True
    name = None
    global FINISHED
    FINISHED = False
    turnCount = 0
    printState(currentState)
    while not FINISHED:
        who = currentState.whose_turn
        if WhitesTurn:
            playerResult = player1.take_turn(currentState, currentRemark,
                                             TIME_PER_MOVE)
            name = player1.moniker()
            WhitesTurn = False
        else:
            playerResult = player2.take_turn(currentState, currentRemark,
                                             TIME_PER_MOVE)
            name = player2.moniker()
            WhitesTurn = True
        moveAndState, currentRemark = playerResult
        if moveAndState == None:
            FINISHED = True
            continue
        move, currentState = moveAndState
        moveReport = "In turn " + str(
            turnCount) + ", move is by " + who + " to " + str(move)
        print(moveReport)
        utteranceReport = name + ' says: ' + currentRemark
        print(utteranceReport)
        if USE_HTML: html.reportResult(moveReport)
        if USE_HTML: html.reportResult(utteranceReport)
        possibleWin = TTS_win_tester.get_win(currentState, move, K)
        if possibleWin != "No win":
            FINISHED = True
            printState(currentState)
            if USE_HTML: html.stateToHTML(currentState, finished=True)
            print(possibleWin)
            if USE_HTML: html.reportResult(possibleWin)
            if USE_HTML: html.endHTML()
            return
        printState(currentState)
        if USE_HTML: html.stateToHTML(currentState)
        turnCount += 1
        if turnCount == TURN_LIMIT: FINISHED = True
    #printState(currentState)
    #if USE_HTML: html.stateToHTML(currentState)
    who = currentState.whose_turn
    print("Game over; it's a draw.")
    if USE_HTML: html.reportResult("Game Over; it's a draw")
    if USE_HTML: html.endHTML()
示例#2
0
def runGame():
    currentState = INITIAL_STATE
    print('The Gamemaster says, "Players, introduce yourselves."')
    print('     (Playing White:) ' + player1.who_am_i())
    print('     (Playing Black:) ' + player2.who_am_i())

    if USE_HTML:
        gameToHTML.startHTML(player1.moniker(), player1.who_am_i(),
                             player2.moniker(), player2.who_am_i(), GAME_TYPE,
                             K, 1)
    try:
        p1comment = player1.get_ready(INITIAL_STATE, K, 'W', player2.moniker())
    except:
        report = 'Player 1 (' + player1.moniker(
        ) + ' failed to prepare, and loses by default.'
        print(report)
        if USE_HTML: gameToHTML.reportResult(report)
        report = 'Congratulations to Player 2 (' + player2.moniker() + ')!'
        print(report)
        if USE_HTML: gameToHTML.reportResult(report)
        if USE_HTML: gameToHTML.endHTML()
        return
    try:
        p2comment = player2.get_ready(INITIAL_STATE, K, 'B', player1.moniker())
    except:
        report = 'Player 2 (' + player2.moniker(
        ) + ' failed to prepare, and loses by default.'
        print(report)
        if USE_HTML: gameToHTML.reportResult(report)
        report = 'Congratulations to Player 1 (' + player1.moniker() + ')!'
        print(report)
        if USE_HTML: gameToHTML.reportResult(report)
        if USE_HTML: gameToHTML.endHTML()
        return
        return

    print('The Gamemaster says, "Let\'s Play!"')
    print('The initial state is...')

    currentRemark = "The game is starting."
    if USE_HTML: gameToHTML.stateToHTML(currentState)

    WhitesTurn = True
    name = None
    global FINISHED
    FINISHED = False
    turnCount = 0
    printState(currentState)
    while not FINISHED:
        who = currentState.whose_turn
        global CURRENT_PLAYER
        CURRENT_PLAYER = who
        if WhitesTurn:
            playerResult = timeout(player1.take_turn,
                                   args=(currentState, currentRemark,
                                         TIME_PER_MOVE),
                                   kwargs={},
                                   timeout_duration=TIME_PER_MOVE,
                                   default=(None, "I give up!"))
            name = player1.moniker()
            WhitesTurn = False
        else:
            playerResult = timeout(player2.take_turn,
                                   args=(currentState, currentRemark,
                                         TIME_PER_MOVE),
                                   kwargs={},
                                   timeout_duration=TIME_PER_MOVE,
                                   default=(None, "I give up!"))
            name = player2.moniker()
            WhitesTurn = True
        if not playerResult:
            print("No move could be found.")
            break
        moveAndState, currentRemark = playerResult
        if moveAndState == None:
            FINISHED = True
            continue
        move, currentState = moveAndState
        if move == False:
            print("No move could be found.")
            break
        moveReport = "In turn " + str(
            turnCount) + ", move is by " + who + " to " + str(move)
        print(moveReport)
        utteranceReport = name + ' says: ' + currentRemark
        print(utteranceReport)
        if USE_HTML: gameToHTML.reportResult(moveReport)
        if USE_HTML: gameToHTML.reportResult(utteranceReport)
        possibleWin = get_win(currentState, move, K)
        if possibleWin != "No win":
            FINISHED = True
            printState(currentState)
            if USE_HTML: gameToHTML.stateToHTML(currentState, finished=True)
            print(possibleWin)
            if USE_HTML: gameToHTML.reportResult(possibleWin)
            if USE_HTML: gameToHTML.endHTML()
            return
        printState(currentState)
        if USE_HTML: gameToHTML.stateToHTML(currentState)
        turnCount += 1
        if turnCount == TURN_LIMIT: FINISHED = True
    # printState(currentState)
    # if USE_HTML: gameToHTML.stateToHTML(currentState)
    who = currentState.whose_turn
    print("Game over; it's a draw.")
    if USE_HTML: gameToHTML.reportResult("Game Over; it's a draw")
    if USE_HTML: gameToHTML.endHTML()