示例#1
0
def main(numSquares, oldGameFileName):
    #Set up graphical and logical game boards
    t, scrn = drawBoard(numSquares)
    row = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    board = []
    for i in range(15):
        board.append(row[:])
    #If old game specified, read and set it
    if oldGameFileName != "":
        inFile = open(oldGameFileName, "r")
        fileNextPlayer = inFile.readline()  #Read next player
        fileNextPlayer = fileNextPlayer.strip()  #Remove \n
        row = 0
        for line in inFile:
            line = line[:-1]
            for col in range(len(line)):
                if line[col] != 'e':
                    board[row][col] = line[col]
                    drawPlayer(t, col, row, {
                        'b': 'blue',
                        'g': 'green'
                    }[line[col]])
                else:
                    board[row][col] = ''
            row += 1
        inFile.close()
    #Pick the starting player
    if oldGameFileName != "":
        player = fileNextPlayer
    else:
        player = ["blue", "green"][random.randint(0, 1)]

    #Play game
    move = ""
    while move not in ["QUIT", "Quit", "quit"] and not win(t, scrn, board)[0]:
        if player == "blue":
            move = P1.getValidMove(t, scrn, board, player)
            print(player, move)
        else:
            move = P2.getValidMove(t, scrn, board, player)
            print(player, move)
        if move not in ["QUIT", "Quit", "quit"]:
            row = (ord(move[0])) - 65
            col = int(move[1:])
            drawPlayer(t, col, row, player)
            board[row][col] = player[0]
            if player == "blue":
                player = "green"
            else:
                player = "blue"

    #Display the winner or save the partial game
    d = {"g": "green", "b": "blue"}
    if win(t, scrn, board)[0]:
        print("Winner is", d[win(t, scrn, board)[1]])
    elif move in ["QUIT", "Quit", "quit"]:
        saveFileName = input(
            "Enter a name for the game file, or just hit enter for no save => "
        )
        if saveFileName != "":
            print("Saving game")
            outFile = open(saveFileName, "w")
            outFile.write(player + '\n')
            for row in board:
                outString = ""
                for col in row:
                    if col == "":
                        outString = outString + 'e'
                    else:
                        outString = outString + col
                outFile.write(outString + '\n')
            outFile.close()
            print("Game saved")
        else:
            print("Game abandoned")
    scrn.clearscreen()
    return win(t, scrn, board)[1]
def checkersMain(inFileName, redWinCount, blackWinCount):
    wn, bob, board, player = setupGame(inFileName)
    gameOver = False
    if player == "b":
        move = THE_PLAYER.getValidMove(copy.deepcopy(board),
                                       player)  #get the first move
    else:
        move = P2.getValidMove(copy.deepcopy(board),
                               player)  #get the first move
    while move.lower() != "quit" and not gameOver:  #Start alternate play
        if player == "b":
            playerColor = "black"
        else:
            playerColor = "red"
        if GRAPHICS: wn.tracer(False)
        if STEP:
            input("Press return to watch the selected move for " +
                  playerColor + " ")
        FROMRow, FROMCol, TORow, TOCol = parseMove(
            move)  #parse move into locations
        if abs(FROMRow - TORow) == 1:  #move, not a jump
            if SLOW_DOWN: time.sleep(1)
            playerToken = board[FROMRow][
                FROMCol]  #save the player form current location (regular checker or king)
            removeCheckerGraphicalAndLogical(
                bob, FROMCol, FROMRow, board)  #remove the checker to be moved
            if SLOW_DOWN: time.sleep(.5)
            placeCheckerGraphicalAndLogical(
                bob, TOCol, TORow, board,
                playerToken)  #place the moved checker in its new location
        else:  #jump, not a move
            reps = move.count(":")
            for i in range(reps):
                FROMRow, FROMCol, TORow, TOCol = parseMove(move)
                playerToken = board[FROMRow][
                    FROMCol]  #save the player form current location (regular checker or king)
                if GRAPHICS: wn.tracer(False)
                if SLOW_DOWN:
                    time.sleep(1)
                removeCheckerGraphicalAndLogical(
                    bob, FROMCol, FROMRow,
                    board)  #remove the checker to be moved
                if SLOW_DOWN:
                    time.sleep(.5)
                placeCheckerGraphicalAndLogical(
                    bob, TOCol, TORow, board, playerToken
                )  #place the jumping checker in its new location
                if SLOW_DOWN:
                    wn.tracer(True)
                    time.sleep(1)
                    wn.tracer(False)
                removeCheckerGraphicalAndLogical(
                    bob, (FROMCol + TOCol) // 2, (FROMRow + TORow) // 2,
                    board)  #remove the jumped checker
                if SLOW_DOWN:
                    time.sleep(.5)
                if GRAPHICS: wn.tracer(True)
                move = move[3:]
        if GRAPHICS: wn.tracer(True)
        #printBoard(board)
        player, playerColor = swapPlayer(player)
        gameOver, winningPlayer = win(board)
        if not gameOver:
            if player == "b":
                start = time.time()
                move = THE_PLAYER.getValidMove(copy.deepcopy(board),
                                               player)  #get the first move
                stop = time.time()
            else:
                start = time.time()
                move = P2.getValidMove(copy.deepcopy(board),
                                       player)  #get the first move
                stop = time.time()
        if stop - start > 1.0:
            print("Match forfeited by", playerColor)
            return redWinCount, blackWinCount
    if move.lower() != "quit":
        #print(winningPlayer +" won the game in a smashing victory!")
        if winningPlayer == "red":
            redWinCount += 1
        else:
            blackWinCount += 1
        return redWinCount, blackWinCount
    else:
        fileName = input(
            "Enter a file name to save the current state of the game, or just hit enter to quit without saving the game => "
        )
        if fileName != "":
            saveGame(fileName, board, player)
示例#3
0
def getMove(board, player, player1, player2):  #Player 1 is Red
    global moveCounter
    moveCounter += 1
    invalid = True
    validJumps = listSingleJumps(board, player)
    validJumps += listMultipleJumps(board, player, validJumps)
    validMoves = listValidMoves(board, player)
    startTime = time.time()
    while invalid:
        'Main', 'Dumb', 'Julia', 'Courtney'
        if player == "b":
            if player2 == 'Main':
                move = Main.getValidMove(copy.deepcopy(board), player)
            elif player2 == 'Dumb':
                move = Dumb.getValidMove(copy.deepcopy(board), player)
            elif player2 == 'Julia':
                move = Julia.getValidMove(copy.deepcopy(board), player)
            elif player2 == 'Courtney':
                move = Courtney.getValidMove(copy.deepcopy(board), player)
            elif player2 == 'Manual':
                move = Manual.getValidMove(copy.deepcopy(board), player)
            elif player2 == 'P1':
                move = P1.getValidMove(copy.deepcopy(board), player)
            elif player2 == 'P2':
                move = P2.getValidMove(copy.deepcopy(board), player)
        else:
            if player1 == 'Main':
                move = Main.getValidMove(copy.deepcopy(board), player)
            elif player1 == 'Dumb':
                move = Dumb.getValidMove(copy.deepcopy(board), player)
            elif player1 == 'Julia':
                move = Julia.getValidMove(copy.deepcopy(board), player)
            elif player1 == 'Courtney':
                move = Courtney.getValidMove(copy.deepcopy(board), player)
            elif player1 == 'Manual':
                move = Manual.getValidMove(copy.deepcopy(board), player)
            elif player1 == 'P1':
                move = P1.getValidMove(copy.deepcopy(board), player)
            elif player1 == 'P2':
                move = P2.getValidMove(copy.deepcopy(board), player)
        if len(validJumps) > 0:
            if move in validJumps:
                invalid = False
            else:
                # print (player, 'must take jump, not ',move)
                if time.time() - startTime > 2 and not 'Manual' in [
                        player1, player2
                ]:
                    print('Timeout due to Missing Jump:', move)
                    # input('Press enter')
                    return ('Timeout')
        else:
            if move in validMoves:
                invalid = False
            else:
                print(player, 'entered invalid move of: ', move)
    # print ('Player %s made the move %s in %3.2f seconds'%(player,move,clock(startTime)))
    if player == 'r':
        p1time[0] += time.time() - startTime
        p1time[1] += 1
        if time.time() - startTime >= .98:
            p1time[2] += 1
    else:
        p2time[0] += time.time() - startTime
        p2time[1] += 1
        if time.time() - startTime >= .98:
            p2time[2] += 1

    return move