def test_shifting(self):
        options = {
            "names": ["Max", "Lea", "Jo", "Tim"],
            "type": ["RL", "RL", "RL", "RL"],
            "nu_shift_cards": 2,
            "nu_cards": 10,
            "seed": 13
        }
        my_game = game(options)

        #shifting:
        for i in [1, 4, 0, 5, 3, 14, 6, 38]:
            rewards, round_finished, done = my_game.play_ai_move(i,
                                                                 print_=True)
            state = my_game.getState().flatten().astype(np.int)
            print("\t", rewards, round_finished, done)
            print("\tNext state:")
            my_game.printCurrentState()

        #playing:
        for i in [0, 8, 1, 2, 16, 9]:
            rewards, round_finished, done = my_game.play_ai_move(i,
                                                                 print_=True)
            state = my_game.getState().flatten().astype(np.int)
            print("\t", rewards, round_finished, done)
            print("\tNext state:")
            my_game.printCurrentState()
 def __init__(self, parent=None):
     #self.playingPolicy = PlayingPolicy()
     self.witchesPolicy = WitchesPolicy()
     self.options = {}
     self.options_file_path = "../data/reinforce_options.json"
     with open(self.options_file_path) as json_file:
         self.options = json.load(json_file)
     self.my_game = game(self.options)
Exemple #3
0
    def test_getValidOptions(self):
        print("\ntest_getValidOptions:")
        options = {
            "names": ["Max", "Lea", "Jo", "Tim"],
            "type": ["RL", "RL", "RL", "RL"],
            "nu_shift_cards": 0,
            "nu_cards": 4,
            "seed": 5
        }
        my_game = game(options)
        my_game.reset()

        for play in my_game.players:
            print("\t", play.hand, len(play.hand))

        print("\n\t>>>>Start options:")
        for i in range(4):
            options_start = my_game.getValidOptions(i)
            self.assertEqual(options_start, [0, 1, 2, 3])

        print("\n\t>>>>playing a color at the start: options:")
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[0].hand[1].idx, print_=True)
        opt1 = my_game.getValidOptions(1)
        opt2 = my_game.getValidOptions(2)
        opt3 = my_game.getValidOptions(3)  # does not has the color!
        self.assertEqual(opt1, [1, 3])
        self.assertEqual(opt2, [2, 3])
        self.assertEqual(opt3, [0, 1, 2, 3])

        print("\n\t>>>>playing a joker at the start: options:")
        my_game.reset()
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[1].hand[3].idx, print_=True)
        for i in [0, 2, 3]:
            options = my_game.getValidOptions(i)
            self.assertEqual(options, [0, 1, 2, 3])
            print("\t\t", i, options)

        print("\n\t>>>>playing a joker and color at the start: options:")
        my_game.reset()
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[2].hand[2].idx, print_=True)
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[3].hand[0].idx, print_=True)
        print("Active Player Hand:",
              my_game.players[my_game.active_player].hand)
        opt1 = my_game.getValidOptions(0)  # has no blue!
        opt2 = my_game.getValidOptions(1)
        opt3 = my_game.getValidOptions(2)
        self.assertEqual(opt1, [0, 1, 2, 3])
        self.assertEqual(opt2, [0, 3])
        self.assertEqual(opt3, [0])
        print("\n")
Exemple #4
0
 def test_idx(self):
     options = {
         "names": ["Max", "Lea", "Jo", "Tim"],
         "type": ["RL", "RANDOM", "RL", "RANDOM"],
         "nu_shift_cards": 0,
         "nu_cards": 4,
         "seed": -1
     }
     my_game = game(options)
     for i in range(my_game.nu_cards * my_game.nu_players):
         card = my_game.idx2Card(i)
         self.assertEqual(card.idx, i)
         print(card)
     print("\n")
Exemple #5
0
    def __init__(self, env_config):
        print("Inside InIT WITCHES ENV")
        self.action_space = gym.spaces.Discrete(60)
        self.observation_space = gym.spaces.Discrete(180)

        # Create the game:
        self.options = {}
        self.options_file_path = "../data/reinforce_options.json"
        with open(self.options_file_path) as json_file:
            self.options = json.load(json_file)
        self.my_game = game(self.options)

        print("End of WitchesEnv")
        # Start the first game
        self.reset()
Exemple #6
0
    def test_randomPlay(self):
        test_game = game({
            "names": ["Max", "Lea", "Jo", "Tim"],
            "type": ["RL", "RL", "RL", "RL"],
            "nu_shift_cards": 2,
            "nu_cards": 15,
            "seed": 22
        })
        test_game.reset()
        print("after reset")

        #print Hand of RL player:
        for i in [3, 12, 7, 13, 10, 3, 1, 0]:
            rewards, corr_moves, done = test_game.step(i, print_=True)
            print(i, rewards)
Exemple #7
0
 def test_nuCards(self):
     for j in range(16):
         options = {
             "names": ["Max", "Lea", "Jo", "Tim"],
             "type": ["RANDOM", "RANDOM", "RL", "RANDOM"],
             "nu_shift_cards": 0,
             "nu_cards": j,
             "seed": None
         }
         my_game = game(options)
         print("NuCards:", j)
         for play in my_game.players:
             print(play.hand, len(play.hand))
             self.assertEqual(len(play.hand), j)
     print("\n")
 def test_randomPlay(self):
     test_game = game({
         "names": ["Max", "Lea", "Jo", "Tim"],
         "type": ["RANDOM", "RL", "RANDOM", "RANDOM"],
         "nu_shift_cards": 2,
         "nu_cards": 4,
         "seed": 18
     })
     test_game.reset()
     #print Hand of RL player:
     print(test_game.players[1].hand)
     test_game.playUntilAI(print_=True)
     for i in [3, 12, 7, 13, 10, 15]:
         rewards, corr_moves, done = test_game.stepRandomPlay(i,
                                                              print_=True)
     print(rewards, corr_moves, done)
Exemple #9
0
    def test_getState(self):
        print("\ntest_getState:")
        options = {
            "names": ["Max", "Lea"],
            "type": ["RL", "RL"],
            "nu_shift_cards": 0,
            "nu_cards": 8,
            "seed": 111
        }
        my_game = game(options)

        for play in my_game.players:
            print("\t", play.hand, len(play.hand))

        #test binary options:
        options = my_game.players[my_game.active_player].getBinaryOptions(
            my_game.getInColor(), my_game.nu_players, my_game.nu_cards)
        cards = my_game.state2Cards(options)
        print(options, cards)
Exemple #10
0
    def test_step(self):
        #TODO add assert here
        options = {
            "names": ["Max", "Lea", "Jo", "Tim"],
            "type": ["RL", "RANDOM", "RL", "RANDOM"],
            "nu_shift_cards": 0,
            "nu_cards": 4,
            "seed": 5
        }
        my_game = game(options)
        my_game.reset()
        for play in my_game.players:
            print(play.hand, len(play.hand))
        #RL plays first card:
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[0].hand[0].idx, print_=True)

        print(rewards, round_finished, gameOver)
        print("\n")
Exemple #11
0
def worker(remote, parent_remote):
    parent_remote.close()
    my_game = game(["Tim", "Bob", "Lena", "Anja"])
    while True:
        cmd, data = remote.recv()
        if cmd == 'step':
            reward, done, info = my_game.step(data)
            remote.send((reward, done, info))
        elif cmd == 'reset':
            print("inside reset!! This is never entered????!!!!!")
            my_game.reset()
            pGo, cState, availAcs = my_game.getState()
            remote.send((pGo, cState))
        elif cmd == 'getCurrState':
            pGo, cState, availAcs = my_game.getState()  # in here!
            #print("in getCurrState:", availAcs, cState)
            remote.send((pGo, cState, availAcs))
        elif cmd == 'close':
            remote.close()
            break
        else:
            print("Invalid command sent by remote")
            break
#For using the GUI:
#from game_gui import gui

#gui.create_widget()
#Collect Data here! (Input State, Output reward)
# Do it directly here or just train on best moves always?
# Train with rewards at the end of game or?
#getPlayerState(self, playeridx):
# TODO: --> Parameter Searching !

num_games = 1
start_time = datetime.datetime.now()
my_game = game(["Tim", "Bob", "Frank", "Lea"],
               ai_player=["NN", "NN", "NN", "NN"],
               expo_constant=[600, 600, 600, 600],
               depths=[300, 300, 300, 300],
               iterations=[100, 100, 100, 100])
total_rewards = np.zeros((my_game.nu_players, ))
nu_errors = 0  # errors of NN tried to play an invalid move

for i in range(0, num_games):
    game_end = False
    round_finished = False
    shift_round = True
    shift_idx = 0
    factor_shift = 1  # factor for shift round only!
    state = (my_game.getGameState())
    current_player = my_game.active_player

    while not game_end:
Exemple #13
0
    def test_getState(self):
        print("\ntest_getState:")
        options = {
            "names": ["Max", "Lea", "Jo", "Tim"],
            "type": ["RL", "RL", "RL", "RL"],
            "nu_shift_cards": 0,
            "nu_cards": 4,
            "seed": 5
        }
        my_game = game(options)
        my_game.reset()

        for play in my_game.players:
            print("\t", play.hand, len(play.hand))
        #test binary options:
        options = my_game.players[my_game.active_player].getBinaryOptions(
            my_game.getInColor(),
            my_game.nu_players,
            my_game.nu_cards,
            shifting_phase=my_game.shifting_phase)
        cards = my_game.state2Cards(options)
        self.assertEqual(str(cards),
                         "[13 of G_5, 12 of R_8, J of R_11, 12 of Y_12]")
        print(options, cards)

        on_table, on_hand, played = my_game.getmyState(my_game.active_player,
                                                       my_game.nu_players,
                                                       my_game.nu_cards)
        for i, j in zip([on_table, on_hand, played],
                        ["on_table", "on_hand", "played"]):
            print(j, i, my_game.state2Cards(i))

        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[0].hand[0].idx, print_=True)
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[1].hand[0].idx, print_=True)
        on_table, on_hand, played = my_game.getmyState(my_game.active_player,
                                                       my_game.nu_players,
                                                       my_game.nu_cards)
        for i, j in zip([on_table, on_hand, played],
                        ["on_table", "on_hand", "played"]):
            print(j, i, my_game.state2Cards(i))

        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[2].hand[1].idx, print_=True)
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[3].hand[2].idx, print_=True)
        on_table, on_hand, played = my_game.getmyState(my_game.active_player,
                                                       my_game.nu_players,
                                                       my_game.nu_cards)
        for i, j in zip([on_table, on_hand, played],
                        ["on_table", "on_hand", "played"]):
            print(j, i, my_game.state2Cards(i))

        ##Testing additional State:
        print("\t\t>>>>Add State testing:")
        for play in my_game.players:
            print("\t", play.hand, len(play.hand))
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[2].hand[0].idx, print_=True)
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[3].hand[0].idx, print_=True)
        rewards, round_finished, gameOver = my_game.play_ai_move(
            my_game.players[0].hand[0].idx, print_=True)
        for i in range(4):
            print("Add state:", i, my_game.getAdditionalState(i),
                  "win idx, free of B G R Y")