Ejemplo n.º 1
0
def gameStart():
    '''loopsRemaining=loopFlag'''
    for loops in range(0, loopFlag):
        #flipper = True if loopFlag > 1 and loops >= loopFlag/2 else False
        '''loopsRemaining -= 1'''

        global playerMode, s1Wins, s2Wins, ties
        createPlayers()
        currentTurn = GameState(1, cityConnection, playerOne, playerTwo)

        screen.fill(white)
        screen.blit(BackGround.image, BackGround.rect)

        createBoard()
        drawTracks()
        drawGameBoard()
        drawCities()
        running = True
        # game state array for saving every turn
        # GameStateArray = []

        if limitedFlag or extendedFlag or hybridFlag:
            currentTurn.createCSVs(currentdirs, loops + 1, limitedFlag,
                                   extendedFlag, hybridFlag)

        while running:

            drawCities(
            )  # they do not change but sometimes the tracks are drawn over them making the names unreadable so we just redraw them again here

            pygame.draw.rect(
                screen, grey,
                (display_width * 0.075, display_height * 0.01, 1600, 50), 0)
            displayText("Error Msg: ", display_width * 0.04,
                        display_height * 0.04)
            button("Quit", 20, display_width * 0.85, display_height * 0.8, 100,
                   75, red, darkRed, quitGame)

            p1Move = None
            # AI or Human makes its move and stores it in the game state
            if playerMode == 'Human vs AI':  # or type(playerOne) == Human
                # humanMove = playerOne.makeMove(currentTurn, deckArray)  # it did not work
                # I hope the line above works, it may not since it has the human class calling a TTR class function
                p1Move = getHumanMove(
                )  # since the line above did not work I use this
            elif playerMode == 'AI vs AI':
                p1Move = playerOne.makeMove(currentTurn)

            currentTurn.setPlayerMove(playerOne, p1Move[0])
            currentTurn.LastFullAction = p1Move
            currentTurn.LastP = 'playerOne'

            if currentTurn.getP1Move(
            ) == 'claim':  # if a player claims a track the cityConnection and trackDataArray
                # needs to be updated but for the other move options the values are just updated in that player's instance
                x = p1Move[1][0]
                y = p1Move[1][1]
                playerOne.points += getEdgeValue(
                    cityConnection[x][y].getLength())
                cityConnection[x][y].claim(playerOne)
                cityConnection[y][x].claim(
                    playerOne
                )  # this could be wrong so if weird stuff starts happening check this
                # also now update if claiming this track happens to complete a destination card
                for row in range(0, len(trackDataArray)):
                    if type(trackDataArray[row]) != int:
                        if type(trackDataArray[row][0]) != int:
                            if (trackDataArray[row][0].getEdgeData() ==
                                    p1Move[1]):  # .all():
                                claimTrack(
                                    trackDataArray[row][0], row,
                                    "Player one")  # updates track data array
                                break  # ima do my best to explain this quick: because the track data array is filled "wrong" it has some -1 values in
                                # it so row is equal to -1 sometimes and you cannot get edge data of a non track obj

            #print("Player one chose to " + currentTurn.getP1Move())
            # updating the game state based on player one's move
            playerOne.checkDestCardCompletion(
                cityConnection
            )  # this will update the players points if a dcard was completed
            currentTurn.updatePlayerInfo(playerOne)
            currentTurn.updateTracks(cityConnection)

            # GameStateArray.append(currentTurn.returnListedforP())
            # AI makes its move and stores it in the game state
            p2Move = playerTwo.makeMove(currentTurn)
            currentTurn.setPlayerMove(playerTwo, p2Move[0])

            currentTurn.LastFullAction = p2Move
            currentTurn.LastP = 'playerTwo'

            if currentTurn.getP2Move() == 'claim':
                x = p2Move[1][0]
                y = p2Move[1][1]
                playerTwo.points += getEdgeValue(
                    cityConnection[x][y].getLength())
                cityConnection[x][y].claim(playerTwo)
                cityConnection[y][x].claim(
                    playerTwo
                )  # this could be wrong so if weird stuff starts happening check this
                for row in range(0, len(trackDataArray)):
                    if type(trackDataArray[row]) != int:
                        if type(trackDataArray[row][0]) != int:
                            if (trackDataArray[row][0].getEdgeData() ==
                                    p2Move[1]):  # .all():
                                claimTrack(
                                    trackDataArray[row][0], row,
                                    "Player two")  # updates track data array
                                break

            #print("Player two chose to " + currentTurn.getP2Move())
            # updating the game state based on player two's move
            # remember to update trackDataArray when AI makes move since it affects the player (yep i did, that's done above)
            playerTwo.checkDestCardCompletion(
                cityConnection
            )  # this will update the players points if a dcard was completed
            currentTurn.updatePlayerInfo(playerTwo)
            currentTurn.updateTracks(cityConnection)

            # check for deadlock
            if currentTurn.getP1Move() == 'pass' and currentTurn.getP2Move(
            ) == 'pass':
                print(
                    "Since both players passed their turn it is likely the game has reached a deadlock so game ends"
                )
                running = False  # break should also work

            # tests if there are any edges left to be claimed, if not: end game, if there are unclaimed edges then continue
            edgeLeft = False
            for i in range(len(cityConnection)):
                toLeave = False
                for edge in cityConnection[i]:
                    if type(
                            edge
                    ) != int:  # aka if there is an edge between those two cities
                        if edge.occupied == 'False':
                            toLeave = True
                            edgeLeft = True
                            break
                if toLeave:
                    break
            if not edgeLeft:
                print("All tracks have been claimed so the game is over!")
                running = False  # break should also work

            # save each turn for debugging/prototyping
            # currentTurn.writeToNPY()
            if limitedFlag or extendedFlag or hybridFlag:
                currentTurn.writeToCSV(
                    limitedFlag, extendedFlag, hybridFlag
                )  # btw you def want this before you increment the turn

            # GameStateArray.append(currentTurn.returnListedforP())  # used for alex's numpy stuff i think, if not idk what its for
            currentTurn.incrementTurn()
            pygame.display.update()
            clock.tick(120)

        # check which destination cards are not completed and update player score
        for card in playerOne.getDestCards():
            if not card.completed:
                playerOne.points -= card.points
                print(
                    "Player one did not complete the destination card between "
                    + card.city1 + " and " + card.city2 + " thus lost " +
                    str(card.points) + " points.")
        for card in playerTwo.getDestCards():
            if not card.completed:
                playerTwo.points -= card.points
                print(
                    "Player two did not complete the destination card between "
                    + card.city1 + " and " + card.city2 + " thus lost " +
                    str(card.points) + " points.")

        currentTurn.addFinalScores(playerOne, playerTwo)

        if limitedFlag or extendedFlag or hybridFlag:
            currentTurn.writeToCSV(
                limitedFlag, extendedFlag, hybridFlag
            )  # btw you def want this before you increment the turn

        # next lines find and print the winner of the game (all based on points) !!! make it also check for num destination cards completed if score ties
        if playerOne.points > playerTwo.points:
            print("Player one won with " + str(playerOne.points) +
                  " over player two who had " + str(playerTwo.points) + ".")
            s1Wins += 1
        elif playerOne.points < playerTwo.points:
            print("Player two won with " + str(playerTwo.points) +
                  " over player one who had " + str(playerOne.points) + ".")
            s2Wins += 1
        else:  # then test number of destination cards as a tie breaker
            p1DCount = 0
            for dCard in playerOne.destinationCards:
                if dCard.completed:
                    p1DCount += 1
            p2DCount = 0
            for dCard in playerTwo.destinationCards:
                if dCard.completed:
                    p2DCount += 1
            if p1DCount > p2DCount:
                print("Player one won with " + str(playerOne.points) +
                      " over player two who also had " +
                      str(playerTwo.points) +
                      " because player one completed " + str(p1DCount) +
                      " destination cards"
                      " compared to player two who only completed " +
                      str(p2DCount) + " destination cards.")
                s1Wins += 1
            elif p1DCount < p2DCount:
                print("Player two won with " + str(playerTwo.points) +
                      " over player one who also had " +
                      str(playerOne.points) +
                      " because player two completed " + str(p2DCount) +
                      " destination cards"
                      " compared to player one who only completed " +
                      str(p1DCount) + " destination cards.")
                s2Wins += 1
            else:
                print("It was a draw! Both players had " +
                      str(playerTwo.points) + " points and completed " +
                      str(p2DCount) + " destination cards.")
                ties += 1
        #print("PlayerOne uses " + playerOne.strategy + " and playerTwo uses " + playerTwo.strategy)
        if ties == 0:
            if playerMode == 'AI vs AI':
                print("So far " + stratFlag1 + " has won " + str(s1Wins) +
                      " times, and " + stratFlag2 + " has won " + str(s2Wins) +
                      " times.")
            else:
                print("So far player one has won " + str(s1Wins) +
                      " times, and player two has won " + str(s2Wins) +
                      " times.")
        else:
            if playerMode == 'AI vs AI':
                print("So far " + stratFlag1 + " has won " + str(s1Wins) +
                      " times, and " + stratFlag2 + " has won " + str(s2Wins) +
                      " times and there have been " + str(ties) + " ties.")
            else:
                print("So far player one has won " + str(s1Wins) +
                      " times, and player two has won " + str(s2Wins) +
                      " times and there have been " + str(ties) + " ties.")
    '''if loopsRemaining > 0:
        gameStart(loopsRemaining)'''
    if commandlineFlag == 'gui':
        titleScreen()
        quit()
Ejemplo n.º 2
0
def gameStart():

    global playerMode
    createPlayers(playerMode)
    currentTurn = GameState(0, cityConnection, playerOne, playerTwo)

    screen.fill(white)
    screen.blit(BackGround.image, BackGround.rect)

    createBoard()
    drawTracks()
    drawGameBoard()
    drawCities()
    running = True

    # game state array for saving every turn
    GameStateArray = []

    while running:

        drawCities(
        )  # they do not change but sometimes the tracks are drawn over them making the names unreadable so we just redraw them again here

        pygame.draw.rect(
            screen, grey,
            (display_width * 0.075, display_height * 0.01, 1600, 50), 0)
        displayText("Error Msg: ", display_width * 0.04, display_height * 0.04)
        button("Quit", 20, display_width * 0.85, display_height * 0.8, 100, 75,
               red, darkRed, quitGame)

        p1Move = None
        # AI or Human makes its move and stores it in the game state
        if playerMode == 'Human vs AI':  # or type(playerOne) == Human
            # humanMove = playerOne.makeMove(currentTurn, deckArray)  # it did not work
            # I hope the line above works, it may not since it has the human class calling a TTR class function
            p1Move = getHumanMove(
            )  # since the line above did not work I use this
            currentTurn.setPlayerMove(playerOne, p1Move[0])
        elif playerMode == 'AI vs AI':
            p1Move = playerOne.makeMove(currentTurn)
            currentTurn.setPlayerMove(playerOne, p1Move[0])

        currentTurn.LastFullAction = p1Move
        currentTurn.LastP = 'playerOne'

        if currentTurn.getP1Move(
        ) == 'claim':  # if a player claims a track the cityConnection and trackDataArray
            # needs to be updated but for the other move options the values are just updated in that player's instance
            x = p1Move[1][0]
            y = p1Move[1][1]
            playerOne.points += getEdgeValue(cityConnection[x][y].getLength())
            cityConnection[x][y].claim(playerOne)
            cityConnection[y][x].claim(
                playerOne
            )  # this could be wrong so if weird stuff starts happening check this
            for row in range(0, len(trackDataArray)):
                if type(trackDataArray[row]) != int:
                    if type(trackDataArray[row][0]) != int:
                        if (trackDataArray[row][0].getEdgeData() == p1Move[1]
                            ).all():
                            claimTrack(trackDataArray[row][0],
                                       row)  # updates track data array
                            break  # ima do my best to explain this quick: because the track data array is filled "wrong" it has some -1 values in
                            # it so row is equal to -1 sometimes and you cannot get edge data of a non track obj

        print("Player one chose to " + currentTurn.getP1Move())
        # updating the game state based on player one's move
        currentTurn.updatePlayerInfo(playerOne)
        currentTurn.updateTracks(cityConnection)

        #currentTurn.writeToCSV(playerOne)  # this line is commented out since the method had not been made yet

        GameStateArray.append(currentTurn.returnListedforP())
        # AI makes its move and stores it in the game state
        p2Move = playerTwo.makeMove(currentTurn)
        currentTurn.setPlayerMove(playerTwo, p2Move[0])

        currentTurn.LastFullAction = p2Move
        currentTurn.LastP = 'playerTwo'

        if currentTurn.getP2Move() == 'claim':
            x = p2Move[1][0]
            y = p2Move[1][1]
            playerTwo.points += getEdgeValue(cityConnection[x][y].getLength())
            cityConnection[x][y].claim(playerTwo)
            cityConnection[y][x].claim(
                playerTwo
            )  # this could be wrong so if weird stuff starts happening check this
            for row in range(0, len(trackDataArray)):
                if type(trackDataArray[row]) != int:
                    if type(trackDataArray[row][0]) != int:
                        if (trackDataArray[row][0].getEdgeData() == p2Move[1]
                            ).all():
                            claimTrack(trackDataArray[row][0],
                                       row)  # updates track data array
                            break

        print("Player two chose to " + currentTurn.getP2Move())
        # updating the game state based on player two's move
        #remember to update trackDataArray when AI makes move since it affects the player (yep i did, that's done above)
        currentTurn.updatePlayerInfo(playerTwo)
        currentTurn.updateTracks(cityConnection)

        #currentTurn.writeToCSV(playerTwo)  # this line is commented out since the method had not been made yet

        # check for deadlock
        if currentTurn.getP1Move() == 'pass' and currentTurn.getP2Move(
        ) == 'pass':
            print(
                "Since both players passed their turn it is likely the game has reached a deadlock so game ends"
            )
            break  # running = False should also work

        # tests if there are any edges left to be claimed, if not: end game, if there are unclaimed edges then continue
        edgeLeft = False
        for i in range(len(cityConnection)):
            toLeave = False
            for edge in cityConnection[i]:
                if type(
                        edge
                ) != int:  # aka if there is an edge between those two cities
                    if edge.occupied == 'False':
                        toLeave = True
                        edgeLeft = True
                        break
            if toLeave:
                break
        if not edgeLeft:
            print("All tracks have been claimed so the game is over!")
            break  # running = False should also work

        #save each turn for debugging/prototyping
        # currentTurn.writeToNPY()

        GameStateArray.append(currentTurn.returnListedforP())
        currentTurn.writeToCSV()
        currentTurn.incrementTurn()

        pygame.display.update()
        clock.tick(60)
    #check destination cards and update player score
    # next lines find and print the winner of the game (all based on points) !!! make it also check for num destination cards completed if score ties
    if playerOne.points > playerTwo.points:
        print("Player one won with " + str(playerOne.points) +
              " over player two who had " + str(playerTwo.points) +
              ".")  # add num destination cards completed
    elif playerOne.points < playerTwo.points:
        print("Player two won with " + str(playerTwo.points) +
              " over player one who had " + str(playerOne.points) +
              ".")  # add num destination cards completed
    else:  # then test number of destination cards as a tie breaker
        print("It was a draw! Both players had " + str(playerTwo.points) +
              " points.")

    titleScreen()
    quit()