示例#1
0
def run(arg_dict):
    plays_history = []
    cuts_history = []
    save_lap_counter = 0

    save_system(arg_dict.output_path, plays_history, True)  # write header
    save_system(arg_dict.cut_history_path, cuts_history, True)  # write header

    for cycle in range(arg_dict.cycles):
        for psw_goal, index in zip(PSW_POOL_COMPLETE, range(len(PSW_POOL_COMPLETE))):
            save_lap_counter += 1

            if arg_dict.optimal_start:
                play_history, cut_history = player.main(['--strategy={}'.format(arg_dict.strategy),
                                                         '--goal={}'.format(psw_goal),
                                                         '--optimal_start',
                                                         '--silent'])
            else:
                play_history, cut_history = player.main(['--strategy={}'.format(arg_dict.strategy),
                                                         '--goal={}'.format(psw_goal),
                                                         '--silent'])

            # Match is lost
            if len(play_history) > 10:
                play_history = play_history[:9]
                play_history.append(MATCH_LOST_PAD)
                cut_history = cut_history[:10]

            # Match is won before 10 attempts
            if len(play_history) < 10:
                to_add = 10 - len(play_history)
                for _ in range(to_add):
                    play_history.append("<pad>")
                    cut_history.append(0)
            play_history.append(psw_goal)
            cut_history.append(psw_goal)

            plays_history.append(play_history)
            cuts_history.append(cut_history)

            # Save the CSV each saving_lap laps
            if save_lap_counter >= arg_dict.saving_lap:
                save_lap_counter = 0
                save_system(arg_dict.output_path, plays_history)
                save_system(arg_dict.cut_history_path, cuts_history)
                plays_history = []
                cuts_history = []

            if not arg_dict.silent:
                print('Cycle = {}, psw{}/{}'.format(cycle, index, len(PSW_POOL_COMPLETE)))
                sys.stdout.flush()

    save_system(arg_dict.output_path, plays_history)
    save_system(arg_dict.cut_history_path, cuts_history)
    print('Database created!')
示例#2
0
def main():
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-u',
                       '--piccolo-url',
                       metavar='URL',
                       help='set the URL of the piccolo server')
    group.add_argument('-x',
                       '--xbee-address',
                       metavar='ADR',
                       help="the address of the xbee radio")
    group.add_argument('-n',
                       '--xbee-network',
                       metavar='BAUD',
                       help="the baudrate of the xbee network")
    parser.add_argument('-d',
                        '--debug',
                        action='store_true',
                        default=False,
                        help="enable debugging output")

    args = parser.parse_args()

    log = logging.getLogger("piccolo")
    if args.debug:
        log.setLevel(logging.DEBUG)
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s %(levelname)s: %(name)s: %(message)s')
    handler.setFormatter(formatter)
    log.addHandler(handler)

    if args.xbee_address != None:
        connection = ('xbee', args.xbee_address)
    elif args.xbee_network != None:
        connection = ('xbee auto', args.xbee_network)
    elif args.piccolo_url != None:
        connection = ('http', args.piccolo_url)
    else:
        connection = None

    player.main(connection)
示例#3
0
def main():

    #written by Efe Arın and Cem Recai Çırak 03.2017
    print("written by Efe Arın and Cem Recai Çırak 03.2017")
    print("")
    print("This mini program plays 128 by itself, using minimax algoritm with alpha beta pruning and configurable independent player-generator intelligence level which adjusts how many future steps could be handled by both opponents")
    print("")
    print("program throws result.txt file so make sure that you have write permition in working directory if not copying the program to another folder then running may help")
    print("")
    print("Play with intelligence levels and see differences. Higher then 10 could take a while. To see 128 on the board player intelligence may set over 15")
    print("")
    generatorIL=int(input("enter intelligence level of generator (ex: 5): "))
    playerIL=int(input("enter intelligence level of player (ex: 10): "))
    # generatorIL=1
    # playerIL=1
    print("")

    #
    def terminate (state):
        for x in range (1,5):
            if move.main(state,x)[-1]:
                return False
        return True
    #
    def show(state):
        print(state[0],state[1],state[2])
        print(state[3],state[4],state[5])
        print(state[6],state[7],state[8])
        print("")
    #
    
    resultFile = open("result.txt", "w")
    resultFile.write("player intelligence: "+str(playerIL)+"\n")
    resultFile.write("generator intelligence: "+str(generatorIL)+"\n")

    state = generator.randomGenerate()
    print("Board is randomly initialized by generator")
    show(state)
    resultFile.write("initial board: "+str(state).strip("[]")+"\n")
   
    turnNumber=0
    start=time.time()
    
    resultFile.write("actions taken by player (1:up, 2:down, 3:left, 4:right) and generator (position of 2 (from position 0 to 8 on the board) in play order starting from player: \n")

    while not terminate(state) :
        turnNumber+=1
        print("turn number ",turnNumber)
        state = player.main(state, playerIL)
        resultFile.write(str(state[-1])+" ")
        show(state)
        state = generator.main(state, generatorIL)
        resultFile.write(str(state[-1])+" ")
        show(state)
    
    stop=time.time()
    totalTime=stop-start

    score = search.utility(state)

    resultFile.write("\nfinal board is: "+str(state).strip("[]")+"\n")
    resultFile.write("score: "+str(score)+"\n")
    resultFile.write("turn number: "+str(turnNumber)+"\n")
    resultFile.write("total time: "+str(totalTime)+"\n")
    resultFile.write("time per turn: "+str(totalTime/turnNumber))
    resultFile.close()

    print("Game is over at score ",score," in ",turnNumber," turns and total time spend is ",totalTime," (apprx. ",totalTime/turnNumber," for each turn).")
    print("")
    print("Result file is created under working directory named as result.txt")
    print("")
    print("Press any key to exit")
    input()
示例#4
0
 def on_press(self):
     self.pressed = True
     print("Host the Ludo")
     player.main()