Exemplo n.º 1
0
def _quit(code=0):
    """Cleanly closes the game"""
    # In error codes, 0 = clean close, no errors
    # 1 = closed with errors, very bad
    if code == 1:
        log.critical("Game quiting with errors!")
    else:
        log.info("Game quiting...")
    pygame.display.quit()
    pygame.quit()
    exit(code)
Exemplo n.º 2
0
def renderer(playField):
    """
    Inverts and prints out the play field

    :param playField: The play field
    :return: None
    """
    try:
        playField = np.flip(playField, 0)

        for col in range(COLUMN_COUNT):
            for row in range(ROW_COUNT):
                if not TestMode:
                    pygame.draw.rect(screen, (0, 89, 179),
                                     ((col * 100),
                                      (row * 100) + 100, 100, 100))
                else:
                    pygame.draw.rect(screen, (210, 0, 252),
                                     ((col * 100),
                                      (row * 100) + 100, 100, 100))
        for col in range(COLUMN_COUNT):
            for row in range(ROW_COUNT):
                if playField[row][col] == 0:
                    pygame.draw.circle(screen, (0, 0, 0),
                                       ((col * 100) + 50, (row * 100) + 150),
                                       radius)
                if playField[row][col] == 1:
                    pygame.draw.circle(screen, (206, 22, 48),
                                       ((col * 100) + 50, (row * 100) + 150),
                                       radius)
                if playField[row][col] == 2:
                    pygame.draw.circle(screen, (255, 201, 23),
                                       ((col * 100) + 50, (row * 100) + 150),
                                       radius)
        playField = np.flip(playField, 0)
        return True
    except Exception as e:
        log.critical("Renderer failed with error {}".format(e))
        _quit(1)
Exemplo n.º 3
0
def _input(playField, turn, pos):
    """
    Gets the player's move and validate it

    param playField: the play field
    :param turn: the current turn
    :return: the column the player chose
    """
    # If AI is enabled, this if statement will call ai to give a column number
    if TestMode:
        if AIMode and (turn % 2 == 0):
            return _get_AI_move(playField)
        return random.randint(0, ROW_COUNT)
    if turn % 2 == 0 and AIMode:
        log.debug("Polling AI code for its move...")
        col = _get_AI_move(playField)
        """
        # todo: call some function that'll return a column number
        col = 0 # todo: remove this line
        return  # todo: remove this line and uncomment and edit the line below
        col = ["some function to get a value off the AI"]
        """

        ### SANITY CHECKS ###
        if col is None:
            log.critical("AI returned Null value")
            _quit(1)  # exit with an error condition
        try:
            int(col)
        except ValueError:
            log.critical("AI didnt return an int")
            _quit(1)  # exit with an error condition
        if col > COLUMN_COUNT - 1:
            log.critical("AI returned an impossible position")
            _quit(1)  # exit with an error condition
        else:
            # value from the AI should be known good now, it can be used safely
            log.debug("AI is putting its piece on column {}".format(col))
            return col

    else:
        # if AIMode is not enabled, or its player 1, take input
        if turn % 2 == 0 and DataGatherMode:
            col = random.sample([0, 1, 2, 3, 4, 5, 6], 1)
            while not _validate_move(playField, col):
                col = random.sample([0, 1, 2, 3, 4, 5, 6], 1)
            log.debug("Randomiser clicked at {}|{} = column: {}".format(
                pos[0], pos[1], col))
        else:
            posx = pos[0]
            col = int(math.floor(posx / 100))
            if col > COLUMN_COUNT - 1:
                return None
            log.debug("Player clicked at {}|{} = column: {}".format(
                pos[0], pos[1], col))
        return col
Exemplo n.º 4
0
def renderer(playField):
    """
    Inverts and prints out the play field

    :param playField: The play field
    :return: None
    """
    global hueHSV
    screen.fill((0, 0, 0))
    try:
        playField = np.flip(playField, 0)

        for col in range(COLUMN_COUNT):
            for row in range(ROW_COUNT):
                if not TestMode:
                    pygame.draw.rect(screen, (0, 89, 179),
                                     ((col * 100),
                                      (row * 100) + 100, 100, 100))
                else:
                    RGB = hsv_to_rgb(hueHSV, 1, 1)
                    if hueHSV == 361:
                        hueHSV = 0
                    pygame.draw.rect(screen, RGB,
                                     ((col * 100),
                                      (row * 100) + 100, 100, 100))
        for col in range(COLUMN_COUNT):
            for row in range(ROW_COUNT):
                if playField[row][col] == 0:
                    pygame.draw.circle(screen, (0, 0, 0),
                                       ((col * 100) + 50, (row * 100) + 150),
                                       radius)
                if playField[row][col] == 1:
                    pygame.draw.circle(screen, (206, 22, 48),
                                       ((col * 100) + 50, (row * 100) + 150),
                                       radius)
                if playField[row][col] == 2:
                    pygame.draw.circle(screen, (255, 201, 23),
                                       ((col * 100) + 50, (row * 100) + 150),
                                       radius)
        playField = np.flip(playField, 0)

        ### User interface junk
        # Create all needed surfaces
        textSurfaces = {
            "title":
            small_text.render("Connect 4", True, (255, 255, 255)),
            "NullSpacer":
            small_text.render("Wooo im invisible", False, (0, 0, 0)),
            "turnNum":
            small_text.render("Turn: " + str(turn), True, (255, 255, 255)),
            "AIMode":
            small_text.render("AI: %s" % ("Active" if AIMode else "Disabled"),
                              True, (255, 255, 255)),
            "TestMode":
            small_text.render(
                "TestMode: %s" % ("Active" if TestMode else "Disabled"), True,
                (255, 255, 255)),
        }

        # starting positions
        posX = ROW_COUNT * 100 + 200
        posY = 125
        # Iterate through all surfaces
        for key, value in textSurfaces.items():
            rect = textSurfaces[key].get_rect()
            rect.center = (posX, posY)
            posY += 25
            screen.blit(textSurfaces[key], rect)

        return True
    except Exception as e:
        log.critical("Renderer failed with error {}".format(e))
        _quit(1)