示例#1
0
def _load_game_from_file(src_file,
                         team_a,
                         team_b,
                         path_piece_def,
                         xml_file,
                         hidden=False,
                         cant_draw=False):

    entire_game, winner = file_parser.parse_file(src_file)
    if cant_draw:
        winner, kk = _handle_draw(entire_game, winner)

    music = False
    if configure.load_configuration()['music_active'] == '1':
        music = True

    if type(team_a[0]) == str:
        name_team_a = team_a[0]
        name_team_b = team_b[0]
    else:
        name_team_a = filenames.extract_name_expert_system(team_a[0])
        name_team_b = filenames.extract_name_expert_system(team_b[0])

    c_team_a = (name_team_a, team_a[1])
    c_team_b = (name_team_b, team_b[1])

    P = pintarPartida.PintarPartida(src_file, c_team_a, c_team_b, music,
                                    hidden, cant_draw)
    P.run()

    show_dialog_result((team_a[0], team_b[0]), winner)
def _load_game_from_file(src_file, team_a, team_b, path_piece_def, xml_file,
                         hidden=False, cant_draw=False):
                         

    entire_game, winner = file_parser.parse_file(src_file)
    if cant_draw:
        winner,kk = _handle_draw(entire_game, winner)

    music = False
    if configure.load_configuration()['music_active'] == '1':
        music = True

    if type(team_a[0]) == str:
        name_team_a = team_a[0]
        name_team_b = team_b[0]
    else:
        name_team_a = filenames.extract_name_expert_system(team_a[0])
        name_team_b = filenames.extract_name_expert_system(team_b[0]) 

    c_team_a = (name_team_a, team_a[1])
    c_team_b = (name_team_b, team_b[1])

    P = pintarPartida.PintarPartida(src_file, c_team_a, c_team_b, music, hidden, cant_draw)
    P.run()

    show_dialog_result((team_a[0], team_b[0]), winner)
示例#3
0
def run(team_a,
        team_b,
        fast=False,
        dont_log=False,
        hidden=False,
        number_turns=100,
        path_piece_def=xdg_data_path('images/piece-default.png'),
        xml_file=xdg_data_path('layouts/main-layout.xml'),
        get_stats=False,
        cant_draw=False,
        logNameReference=None):
    """
    Runs a game using the system expert teams given. It calls to libguadalete,
    generating the game and parsing the file.
    """
    # Preparamos el motor con los equipos y el número de turnos
    lib = libguadalete.LibGuadalete(team_a[0], team_b[0], number_turns)

    try:
        # Procesamos el juego, obteniendo el fichero de log y el ganador
        out_file, winner = lib.run_game()

    except LibFileError as exc:
        logging.error("ERROR: %s", exc.msg)
        raise GuadaFileError(exc.msg)

    # fast indica si queremos ver solo el resultado (fast = True) o la partida
    # completa
    if not fast:
        # Aquí es donde se llama a la función que gestiona el dibujado del juego
        _load_game_from_file(out_file,
                             team_a,
                             team_b,
                             path_piece_def,
                             xml_file,
                             hidden,
                             cant_draw=cant_draw)

    reason = "normal"
    # Si no puede haber empate
    if cant_draw:
        # Solucionar el empate
        entire_game, winner = file_parser.parse_file(out_file)
        winner, reason = _handle_draw(entire_game, winner)

    # Preparamos la variable de retorno
    res = (winner, reason)

    if get_stats:
        res = (winner, stats.get_game_file_stats(out_file))

    if logNameReference != None:
        logNameReference[0] = out_file

    if dont_log:
        os.remove(out_file)

    return res
def run(team_a, team_b, fast=False, dont_log=False, hidden=False,
        number_turns=100,
        path_piece_def=xdg_data_path('images/piece-default.png'),
        xml_file=xdg_data_path('layouts/main-layout.xml'),
        get_stats=False, cant_draw=False, logNameReference = None):
    """
    Runs a game using the system expert teams given. It calls to libguadalete,
    generating the game and parsing the file.
    """
    # Preparamos el motor con los equipos y el número de turnos
    lib = libguadalete.LibGuadalete(team_a[0], team_b[0], number_turns)
    
    try:
        # Procesamos el juego, obteniendo el fichero de log y el ganador
        out_file, winner = lib.run_game()
        
    except LibFileError as exc:
        logging.error("ERROR: %s", exc.msg)
        raise GuadaFileError(exc.msg)
        
    # fast indica si queremos ver solo el resultado (fast = True) o la partida
    # completa
    if not fast:
        # Aquí es donde se llama a la función que gestiona el dibujado del juego
        _load_game_from_file(out_file, 
                             team_a, team_b,
                             path_piece_def,
                             xml_file, hidden, cant_draw=cant_draw)
    
    reason = "normal"
    # Si no puede haber empate
    if cant_draw:
        # Solucionar el empate
        entire_game, winner = file_parser.parse_file(out_file)
        winner,reason = _handle_draw(entire_game, winner)

    
    # Preparamos la variable de retorno
    res = (winner, reason)
    
    if get_stats:
        res = (winner, stats.get_game_file_stats(out_file))
        

    if logNameReference != None:
        logNameReference[0] = out_file

    if dont_log:
        os.remove(out_file)
        
    return res
def _handle_draw(output_file):
    """
    Handle the draw if the game cant draw
    """
    entire_game, winner = file_parser.parse_file(output_file)

    if not winner == 0:
        return winner
    else:  # if it's a draw
        num_a = 0
        num_b = 0
        _sum = 0
        final_board = entire_game[:-1]
        _num = len(final_board)
        for i in range(_num):
            for j in range(_num):
                _value = final_board[i][j]
                _sum = _sum + _value
                if not _value == 0:
                    if _value > 0:
                        num_a = num_a + 1
                    else:
                        num_b = num_b + 1
        if not _sum == 0:  # a team has more _sum of values than the other
            if _sum > 0:
                return 1
            else:
                return -1
        else:  # both has the same _sum of values
            if not num_a == num_b:  # a team has more pieces than the other
                if num_a > num_b:
                    return 1
                else:
                    return -1
            else:  # both have the same number of pieces.

                return -1  # B team is in disvantage
def _load_game_from_file(src_file, team_a, team_b, path_piece_def, xml_file,
                         hidden=False, cant_draw=False):
    entire_game, winner = file_parser.parse_file(src_file)
    if cant_draw:
        winner = _handle_draw(src_file)

    if winner == 0:
        print u'Empate'
    elif winner == 1:
        print u'Ganó %s' % team_a[0]
    else:
        print u'Ganó %s' % team_b[0]

    music = False
    if configure.load_configuration()['music_active'] == '1':
        music = True

    pygame.init()

    xml_layout = layout.Layout(xml_file)

    screen = pygame.display.set_mode(xml_layout.get_window_size())
    pygame.display.set_caption(xml_layout.get_window_title())

    if music:
        _music_path = xdg_data_path('music/walking_on_old_stones.ogg')
        mixer.music.load(_music_path)
        mixer.music.play()

    res_game = game.Game(entire_game, team_a[1],
                         team_b[1], path_piece_def, hidden=hidden)

    img_board = res_game.draw_board().convert()

    xml_layout.init((team_a[1], team_a[0]), (team_b[1], team_b[0]), img_board)
    rects = xml_layout.get_buttons_rects()

    pygame.display.set_icon(xml_layout.get_favicon())

    screen.blit(xml_layout.get_surface(), (0, 0))
    pygame.display.flip()

    band_pos = False
    clock = pygame.time.Clock()
    while True:
        clock.tick(50)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                if music:
                    mixer.music.stop()
                pygame.display.quit()
                show_dialog_result((team_a[0], team_b[0]), winner)
                return winner
            elif event.type == pygame.KEYDOWN:
                if event.key == 275:
                    surface = next_turn(res_game, xml_layout)
                    screen.blit(surface, (0, 0))
                    pygame.display.flip()
                if event.key == 276:
                    surface = previous_turn(res_game, xml_layout)
                    screen.blit(surface, (0, 0))
                    pygame.display.flip()
            elif event.type == pygame.MOUSEMOTION:
                res = get_collision(event.pos, rects)
                if res != '':
                    if band_pos == False:
                        surface = xml_layout.get_surface((res, 2))
                        screen.blit(surface, (0, 0))
                        pygame.display.flip()
                        band_pos = True
                else:
                    if band_pos == True:
                        surface = xml_layout.get_surface()
                        screen.blit(surface, (0, 0))
                        pygame.display.flip()
                        band_pos = False
            elif event.type == pygame.MOUSEBUTTONUP:
                res = get_collision(event.pos, rects)
                if event.button == 1 and res != '':
                    if res == 'button_exit':
                        if music:
                            mixer.music.stop()
                        pygame.display.quit()
                        show_dialog_result((team_a[0], team_b[0]), winner)
                        return winner
                    else:
                        if res == 'button_left_2':
                            surface = first_turn(
                                res_game, xml_layout, (res, 1))
                            screen.blit(surface, (0, 0))
                            pygame.display.flip()
                        elif res == 'button_left_1':
                            surface = previous_turn(
                                res_game, xml_layout, (res, 1))
                            screen.blit(surface, (0, 0))
                            pygame.display.flip()
                        elif res == 'button_right_1':
                            surface = next_turn(
                                res_game, xml_layout, (res, 1))
                            screen.blit(surface, (0, 0))
                            pygame.display.flip()
                        elif res == 'button_right_2':
                            surface = last_turn(
                                res_game, xml_layout, (res, 1))
                            screen.blit(surface, (0, 0))
                            pygame.display.flip()