Exemple #1
0
    def test4(self):
        p1 = Player(1)
        p2 = Player(2)

        spiel = Spiel_class.Spiel([], p1, p2)

        k1 = Card('W', 'W', 'S', 'S')
        spiel.make_action(p1, k1, 0, -1, 2)

        k2 = Card('W', 'S', 'S', 'S', 'G')
        spiel.make_action(p2, k2, 1, -1, 0)

        k3 = Card('S', 'O', 'W', 'S')
        spiel.make_action(p1, k3, 0, -2, 2)

        k4 = Card('O', 'S', 'S', 'W')
        spiel.make_action(p2, k4, 0, -3, 3)

        k5 = Card('W', 'W', 'S', 'S')
        spiel.make_action(p1, k5, 1, -3, 1)

        k6 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(p2, k6, 2, -3, 0)

        #display_spielbrett_dict(spiel.cards_set)

        k7 = Card('S', 'S', 'S', 'S', 'G')
        spiel.make_action(p1, k7, 1, -2, 0)

        print('ende')
Exemple #2
0
    def test1(self):

        p1 = Player(1)
        p2 = Player(2)

        spiel = Spiel_class.Spiel([], p1, p2)

        k1 = Card('O', 'O', 'S', 'O', 'O', True)
        spiel.make_action(p1, k1, 1, 0, 0, k1.orte[0])

        k2 = Card('O', 'S', 'S', 'W')
        spiel.make_action(p2, k2, 0, 1, 1, k2.strassen[0])

        k3 = Card('O', 'S', 'S', 'W')
        spiel.make_action(p1, k3, 2, 0, 3, k3.strassen[0])

        k4 = Card('W', 'W', 'S', 'S')
        spiel.make_action(p2, k4, 2, -1, 0, k4.strassen[0])

        k5 = Card('S', 'W', 'S', 'W')
        spiel.make_action(p1, k5, -1, 0, 0, k5.strassen[0])

        k6 = Card('W', 'W', 'S', 'S')
        spiel.make_action(p2, k6, 0, 2, 1, k6.strassen[0])

        k7 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(p1, k7, 2, 1, 0, 'k')

        k8 = Card('S', 'O', 'S', 'S', 'G')

        s = None
        for st in k8.strassen:
            if st.kanten == [3]:
                s = st

        spiel.make_action(p2, k8, 1, -1, 1, s)

        k9 = Card('O', 'S', 'S', 'W')
        spiel.make_action(p1, k9, 1, -2, 0, k9.orte[0])

        #display_spielbrett_dict(spiel.cards_set)

        k10 = Card('S', 'O', 'W', 'S')

        #pos = spiel.calculate_possible_actions(k10, p2)
        #node = Node(True, None, p2.nummer, None)

        #a, b = uct_select(spiel, k10, p2, pos, d=1, root_node=node, t_end=None, rechenzeit=30, c=1.4142, threads=1)

        #print(a, b)

        w = None
        for wi in k10.wiesen:
            if wi.ecken == [4]:
                w = wi
        spiel.make_action(p2, k10, 2, -2, 0, w)

        spiel.final_evaluate()

        self.assertEqual(p2.punkte, 12)
Exemple #3
0
    def test4(self):
        spiel = Spiel_class.Spiel(
            card_class.create_kartenliste([
                'OSSW', 'SWSW', 'SOSSG', 'WWSWK', 'WWSS', 'WWSS', 'OOSOOT',
                'OSSW', 'SOWS', 'OSSW'
            ], False))
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player1, k1, 1, 0, 3)

        k2 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(player2, k2, 1, -1, 1, 'k')

        k3 = Card('S', 'O', 'W', 'S')
        spiel.make_action(player1, k3, 0, 1, 3)

        k4 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player2, k4, 0, -1, 3)

        k5 = Card('W', 'W', 'S', 'S')
        spiel.make_action(player1, k5, 1, 1, 3)

        self.assertEqual(len(spiel.alle_wiesen), 2)
    def test10(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('O', 'S', 'S', 'W')
        pos1 = spiel.calculate_possible_actions(k1, player1)

        self.assertEqual(30, len(pos1))
        spiel.make_action(player1, k1, 0, 1, 1, k1.orte[0])

        k2 = Card('O', 'W', 'W', 'O', 'O', True)
        pos2 = spiel.calculate_possible_actions(k2, player2)

        self.assertEqual(22, len(pos2))
        spiel.make_action(player2, k2, 1, 0, 0, k2.orte[0])

        k3 = Card('O', 'O', 'W', 'O', 'O')

        pos3 = spiel.calculate_possible_actions(k3, player1)

        self.assertEqual(16, len(pos3))
        spiel.make_action(player1, k3, 1, 1, 2)

        #display_spielbrett_dict(spiel.cards_set)

        k4 = Card('W', 'O', 'W', 'O')

        pos4 = spiel.calculate_possible_actions(k4, player2)

        self.assertEqual(23, len(pos4))
Exemple #5
0
    def test_2(self):
        spiel = Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        card1 = Card('W', 'W', 'S', 'S')
        spiel.make_action(player1, card1, 0, -1, 1)

        card2 = Card('O', 'S', 'S', 'O', 'O')
        spiel.make_action(player2, card2, 0, 1, 1)

        card3 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player1, card3, -1, 1, 3)

        card4 = Card('S', 'W', 'S', 'W')
        spiel.make_action(player2, card4, -1, -1, 1)

        card5 = Card('S', 'O', 'W', 'S')
        spiel.make_action(player1, card5, -2, -1, 1)

        card6 = Card('O', 'W', 'W', 'W')
        spiel.make_action(player2, card6, -2, 1, 1)

        card7 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(player1, card7, -2, 0, 0, 'k')

        self.assertEqual(len(spiel.alle_kloester), 1)
        self.assertEqual(player1.meeples, 6)

        card8 = Card('W', 'W', 'W', 'W', 'K')
        spiel.make_action(player2, card8, -1, 0, 0, 'k')

        self.assertEqual(len(spiel.alle_kloester), 1)
        self.assertEqual(player2.meeples, 7)
Exemple #6
0
    def test2(self):
        p1 = Player(1)
        p2 = Player(2)

        spiel = Spiel_class.Spiel([], p1, p2)

        k1 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(p1, k1, 0, -1, 2)

        k2 = Card('O', 'O', 'S', 'O', 'O', True)
        spiel.make_action(p2, k2, 1, 0, 2)

        k3 = Card('S', 'O', 'W', 'S')
        spiel.make_action(p1, k3, -1, 0, 3)

        k4 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(p2, k4, 1, 1, 0)

        k5 = Card('O', 'S', 'S', 'W')
        spiel.make_action(p1, k5, -1, 1, 2)

        #display_spielbrett_dict(spiel.cards_set)

        k6 = Card('S', 'W', 'S', 'W')
        pos = spiel.calculate_possible_actions(k6, p2)

        spiel.make_action(p2, k6, 0, 1, 0)
Exemple #7
0
 def test7(self):
     spiel = Spiel_class.Spiel(
         card_class.create_kartenliste([
             'OSSW', 'SWSW', 'SOSSG', 'WWSWK', 'WWSS', 'WWSS', 'OOSOOT',
             'OSSW', 'SOWS', 'OSSW', 'SWSW', 'WOWOOT', 'WOWO', 'OWWOO',
             'WWWWK', 'OOSOO'
         ], False))
     player1 = Player(1)
     player2 = Player(2)
    def test1(self):
        player1 = Player(1)
        player2 = Player(2)

        spiel = Spiel_class.Spiel([])

        k1 = Card('S', 'W', 'S', 'W')
        pos = spiel.calculate_possible_actions(k1, player1)

        self.assertEqual(12, len(pos))
    def test5(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k2 = Card('O', 'W', 'W', 'O', 'O')
        spiel.make_action(player1, k2, 1, 0, 0, k2.orte[0])

        k3 = Card('W', 'O', 'W', 'O')
        pos = spiel.calculate_possible_actions(k3, player2)

        self.assertEqual(len(pos), 15)
Exemple #10
0
    def test3(self):
        spiel = Spiel_class.Spiel(
            card_class.create_kartenliste([
                'SOWS', 'SWSW', 'WWSS', 'WWSWK', 'OOSOOT', 'OSSW', 'SOSSG',
                'OSSW', 'WWSS', 'OSSW'
            ], False))
        player1 = Player(1)
        player2 = Player(2)

        k1 = spiel.cards_left.pop(0)
        spiel.make_action(player2, k1, 1, 0, 2, k1.orte[0])

        k2 = spiel.cards_left.pop(0)
        spiel.make_action(player1, k2, 2, 0, 1, k2.strassen[0])

        k3 = spiel.cards_left.pop(0)
        spiel.make_action(player2, k3, 0, 1, 3, k3.strassen[0])

        k4 = spiel.cards_left.pop(0)
        spiel.make_action(player1, k4, 1, -1, 2, 'k')

        k5 = spiel.cards_left.pop(0)
        spiel.make_action(player2, k5, 3, 0, 1, k5.orte[0])

        k6 = spiel.cards_left.pop(0)
        spiel.make_action(player1, k6, 1, -2, 1, k6.strassen[0])

        k7 = spiel.cards_left.pop(0)

        strasse = None
        for s in k7.strassen:
            if s.kanten == [0]:
                strasse = s

        spiel.make_action(player2, k7, 3, 1, 1, strasse)

        k8 = spiel.cards_left.pop(0)
        spiel.make_action(player1, k8, 2, -2, 3, k8.orte[0])

        k9 = spiel.cards_left.pop(0)

        w = [wi for wi in k9.wiesen if wi.ecken == [7]][0]
        spiel.make_action(player2, k9, 1, -3, 2, w)

        #display_spielbrett_dict(spiel.cards_set)

        k10 = spiel.cards_left.pop(0)
        spiel.make_action(player1, k10, 0, -2, 0, k10.orte[0])

        spiel.final_evaluate()

        self.assertEqual(player1.punkte, 19)
        self.assertEqual(player2.punkte, 16)
    def test4(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k2 = Card('O', 'W', 'W', 'O', 'O')
        spiel.make_action(player1, k2, 1, 0, 0)

        k3 = Card('W', 'O', 'W', 'O')
        pos = spiel.calculate_possible_actions(k3, player2)

        #display_spielbrett_dict(spiel.cards_set)

        self.assertEqual(len(pos), 16)
    def test_1(self):
        spiel = Spiel_class.Spiel(Kartenliste)

        # startvorgaben
        card1 = card_class.Card("W", "O", "O", "W", "O")
        spiel.cards_set.update({(1, 1): card1})

        ort1 = Ort((1, 1), [1, 2])
        wiese2 = Wiese((1, 1), [4, 5, 7])
        spiel.alle_orte.append(ort1)
        spiel.alle_wiesen.append(wiese2)

        spiel.cards_set[(1, 1)].ecken = {
            4: wiese2,
            5: wiese2,
            6: None,
            7: wiese2
        }
        spiel.cards_set[(1, 1)].kanten = {0: None, 1: ort1, 2: ort1, 3: None}

        spiel.possible_coordinates = [(0, 1), (1, 0), (0, -1), (-1, 0), (1, 2),
                                      (2, 1)]

        #display_spielbrett_dict(spiel.cards_set)

        k = card_class.Card("S", "O", "O", "S", "O")
        #draw_card(k)

        goal = [(0, -1, 0, k.strassen[0]), (0, -1, 0, k.orte[0]),
                (0, -1, 1, k.strassen[0]), (0, -1, 1, k.orte[0]),
                (2, 1, 1, k.strassen[0]), (2, 1, 1, k.orte[0]),
                (2, 1, 2, k.strassen[0]), (2, 1, 2, k.orte[0]),
                (1, 0, 2, k.orte[0]), (1, 0, 2, k.strassen[0]),
                (0, -1, 0, None), (0, -1, 1, None), (1, 0, 2, None),
                (2, 1, 1, None), (2, 1, 2, None), (1, 0, 2, k.wiesen[0]),
                (1, 0, 2, k.wiesen[1]), (2, 1, 1, k.wiesen[0]),
                (2, 1, 1, k.wiesen[1]), (2, 1, 2, k.wiesen[0]),
                (2, 1, 2, k.wiesen[1]), (0, -1, 0, k.wiesen[0]),
                (0, -1, 0, k.wiesen[1]), (0, -1, 1, k.wiesen[0]),
                (0, -1, 1, k.wiesen[1])]

        player1 = Player(1)
        pos_act = spiel.calculate_possible_actions(k, player1)
        self.assertEqual(25, len(pos_act))

        for tup in spiel.calculate_possible_actions(k, Player(1)):
            print(tup)
            self.assertTrue(tup in goal)
Exemple #13
0
    def test3(self):
        spiel = Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('W', 'W', 'W', 'W', 'K')
        spiel.make_action(player1, k1, -1, 0, 0, 'k')

        k2 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(player2, k2, -1, 1, 1, 'k')

        #plot_cards.display_spielbrett_dict(spiel.cards_set)

        spiel.final_evaluate()

        self.assertEqual(player1.punkte, 3)
        self.assertEqual(player2.punkte, 3)
Exemple #14
0
    def test5(self):
        p1 = Player(1)
        p2 = Player(2)

        spiel = Spiel_class.Spiel([], p1, p2)

        k1 = Card('S', 'W', 'S', 'W')
        spiel.make_action(p1, k1, 0, -1, 0, k1.strassen[0])

        k2 = Card('O', 'S', 'S', 'W')

        w = None
        for wi in k2.wiesen:
            if sorted(wi.ecken) == [4, 5, 7]:
                w = wi

        spiel.make_action(p2, k2, 0, 1, 0, w)
        print('ende')
Exemple #15
0
    def test_1(self):
        spiel = Spiel(Kartenliste)
        card1 = Card('W', 'W', 'W', 'W', 'K')
        player1 = Player(1)

        spiel.make_action(player1, card1, -1, 0, 1, 'k')

        self.assertEqual(player1.meeples, 6)
        self.assertEqual(len(spiel.alle_kloester), 1)
Exemple #16
0
    def test2(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('W', 'S', 'S', 'W')
        spiel.make_action(player1, k1, 0, 1, 1, k1.strassen[0])

        k2 = Card("S", 'W', 'S', 'W')
        spiel.make_action(player2, k2, -1, 0, 0, k2.strassen[0])

        k3 = Card('W', 'S', 'S', 'W')
        spiel.make_action(player1, k3, -1, 1, 0, None)

        # display_spielbrett_dict(spiel.cards_set)

        spiel.final_evaluate()

        self.assertEqual(player1.punkte, 4)
        self.assertEqual(player2.punkte, 4)
Exemple #17
0
    def test7(self):
        p1 = Player(1)
        p2 = Player(2)

        spiel = Spiel_class.Spiel([], p1, p2)

        k1 = Card('O', 'S', 'S', 'O', 'O')
        spiel.make_action(p2, k1, 1, 0, 0, k1.strassen[0])

        k2 = Card('W', 'W', 'S', 'S')
        spiel.make_action(p1, k2, 1, -1, 2)

        k3 = Card('W', 'W', 'S', 'S')
        spiel.make_action(p2, k3, 2, -1, 1)

        k4 = Card('W', 'S', 'S', 'S', 'G')
        spiel.make_action(p1, k4, 2, 0, 0)

        self.assertEqual(p2.meeples, 7)
        self.assertEqual(p2.punkte, 5)
    def test11(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('S', 'O', 'S', 'S', 'G')

        spiel.make_action(player1, k1, 1, 0, 2, k1.orte[0])

        k2 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player2, k2, 0, -1, 2, k2.orte[0])
        pos = spiel.calculate_possible_actions(k2, player2)

        k3 = Card('W', 'W', 'S', 'S')
        wiese = None
        for w in k3.wiesen:
            if sorted(w.ecken) == sorted([4, 5, 7]):
                wiese = w
        spiel.make_action(player1, k3, 1, -1, 2, wiese)

        k4 = Card('S', 'W', 'S', 'W')
        spiel.make_action(player2, k4, 0, 1, 0, k4.strassen[0])

        k5 = Card('O', 'O', 'S', 'O', 'O', True)
        wiese = [w for w in k5.wiesen if w.ecken == [6]][0]
        spiel.make_action(player1, k5, 2, -1, 1, wiese)

        k6 = Card('O', 'S', 'S', 'W')
        wiese = [w for w in k6.wiesen
                 if sorted(w.ecken) == sorted([4, 5, 7])][0]
        spiel.make_action(player2, k6, 0, -2, 0, wiese)

        k7 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player1, k7, 2, 0, 2, k7.orte[0])

        k8 = Card('S', 'O', 'W', 'S')
        pos = spiel.calculate_possible_actions(k8, player2)

        print('hi')
Exemple #19
0
    def test6(self):
        spiel = Spiel_class.Spiel(
            card_class.create_kartenliste([
                'OSSW', 'SWSW', 'SOSSG', 'WWSWK', 'WWSS', 'WWSS', 'OOSOOT',
                'OSSW', 'SOWS', 'OSSW'
            ], False))
        player1 = Player(1)
        player2 = Player(2)

        k0 = Card('S', 'W', 'W', 'S')
        spiel.make_action(player1, k0, 0, 1, 2)

        k1 = Card('W', 'W', 'S', 'S')
        spiel.make_action(player2, k1, 0, 2, 1)

        k2 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(player1, k2, 1, 2, 0)

        k3 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player2, k3, 1, 1, 2)

        self.assertEqual(1, 1)
Exemple #20
0
    def test5(self):
        spiel = Spiel_class.Spiel(
            card_class.create_kartenliste([
                'OSSW', 'SWSW', 'SOSSG', 'WWSWK', 'WWSS', 'WWSS', 'OOSOOT',
                'OSSW', 'SOWS', 'OSSW'
            ], False))
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('O', 'S', 'S', 'W')
        spiel.make_action(player1, k1, 1, 0, 3)

        k2 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(player2, k2, 1, -1, 1, 'k')

        k3 = Card('W', 'W', 'S', 'W', 'K')
        spiel.make_action(player1, k3, 0, 1, 1, 'k')

        k4 = Card('W', 'W', 'S', 'S')
        spiel.make_action(player2, k4, 0, -1, 2)

        a = sorted([
            list(v) for v in [w.alle_teile.items() for w in spiel.alle_wiesen]
        ])
        b = [[((1, 0), [5])],
             [((1, 0), [7, 4, 6]), ((1, -1), [5, 6, 7, 4]),
              ((0, -1), [6, 7, 4, 5]), ((0, 0), [5, 6, 4, 7]),
              ((0, 1), [5, 6, 7, 4])]]

        self.assertEqual(a, b)

        w = [w for w in spiel.alle_wiesen if len(w.alle_teile) != 1][0]

        for c in spiel.cards_set:
            if c != (1, 0):
                for ecke in spiel.cards_set[c].ecken:

                    self.assertEqual(spiel.cards_set[c].ecken[ecke], w)
    def test9(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('W', 'W', 'S', 'S')
        pos1 = spiel.calculate_possible_actions(k1, player1)

        self.assertEqual(len(pos1), 24)
        spiel.make_action(player1, k1, 0, 1, 3)

        k2 = Card('S', 'O', 'S', 'W')
        pos2 = spiel.calculate_possible_actions(k2, player2)

        self.assertEqual(len(pos2), 40)
        wiese = [w for w in k2.wiesen if w.ecken == [5, 6]][0]

        spiel.make_action(player1, k2, 1, 1, 1, wiese)

        k3 = Card('O', 'O', 'S', 'O', 'O', True)
        pos3 = spiel.calculate_possible_actions(k3, player1)

        self.assertEqual(len(pos3), 18)
    def test3(self):

        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('O', 'O', 'S', 'O', 'O', True)
        spiel.make_action(player2, k1, 1, 0, 0, k1.orte[0])

        k2 = Card('O', 'W', 'W', 'O', 'O')
        spiel.make_action(player1, k2, 1, 1, 2, k2.wiesen[0])

        k3 = Card('W', 'W', 'W', 'W', 'K')
        pos = spiel.calculate_possible_actions(k3, player2)

        #display_spielbrett_dict(spiel.cards_set)

        goal = [(-1, 0, 0, k3.wiesen[0]), (-1, 0, 0, None), (-1, 0, 0, 'k'),
                (1, 2, 0, None), (1, 2, 0, 'k')]

        self.assertEqual(len(pos), len(goal))
        for tup in pos:
            self.assertTrue(tup in goal)
    def test7(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('S', 'W', 'S', 'W')
        pos1 = spiel.calculate_possible_actions(k1, player1)

        self.assertEqual(len(pos1), 12)
        spiel.make_action(player1, k1, 0, 1, 0, k1.strassen[0])

        k2 = Card('O', 'W', 'W', 'O', 'O')
        pos2 = spiel.calculate_possible_actions(k2, player2)

        self.assertEqual(len(pos2), 24)
        spiel.make_action(player2, k2, 1, 0, 0, k2.orte[0])

        #display_spielbrett_dict(spiel.cards_set)

        k3 = Card('W', 'W', 'S', 'S')
        pos3 = spiel.calculate_possible_actions(k3, player1)

        self.assertEqual(len(pos3), 44)
        spiel.make_action(player1, k3, 0, 2, 3)

        k4 = Card('O', 'S', 'S', 'W')
        pos4 = spiel.calculate_possible_actions(k4, player2)

        self.assertEqual(len(pos4), 46)
        spiel.make_action(player2, k4, 1, 2, 2)

        k4 = Card('W', 'O', 'W', 'O')
        pos5 = spiel.calculate_possible_actions(k4, player1)

        #display_spielbrett_dict(spiel.cards_set)

        self.assertEqual(len(pos5), 31)
    def test8(self):
        """fast der gleiche wie test7, nur mit meeple mehr in vorletzem Zug"""
        spiel = Spiel_class.Spiel(Kartenliste)
        player1 = Player(1)
        player2 = Player(2)

        k1 = Card('S', 'W', 'S', 'W')
        pos1 = spiel.calculate_possible_actions(k1, player1)

        self.assertEqual(len(pos1), 12)
        spiel.make_action(player1, k1, 0, 1, 0, k1.strassen[0])

        k2 = Card('O', 'W', 'W', 'O', 'O')
        pos2 = spiel.calculate_possible_actions(k2, player2)

        self.assertEqual(len(pos2), 24)
        spiel.make_action(player2, k2, 1, 0, 0, k2.orte[0])

        #display_spielbrett_dict(spiel.cards_set)

        k3 = Card('W', 'W', 'S', 'S')
        pos3 = spiel.calculate_possible_actions(k3, player1)

        self.assertEqual(len(pos3), 44)
        spiel.make_action(player1, k3, 0, 2, 3)

        k4 = Card('O', 'S', 'S', 'W')
        pos4 = spiel.calculate_possible_actions(k4, player2)

        self.assertEqual(len(pos4), 46)
        spiel.make_action(player2, k4, 1, 2, 2, k4.orte[0])

        k5 = Card('W', 'O', 'W', 'O')
        pos5 = spiel.calculate_possible_actions(k5, player1)

        self.assertEqual(len(pos5), 30)
    def test_2(self):
        spiel = Spiel_class.Spiel(Kartenliste)
        card0 = card_class.Card("S", "O", "S", "W")
        card1 = card_class.Card("S", "S", "W", "W")
        card2 = card_class.Card("O", "O", "O", "O", "O", True)
        card3 = card_class.Card("W", "O", "O", "W")
        card4 = card_class.Card("W", "O", "W", "O")

        k = card_class.Card("O", "S", "S", "O", "O", True)

        strasse0 = Strasse((0, 0), [0, 2])
        strasse0.koordinaten_plus_oeffnungen.update({(0, -1): [1]})
        strasse0.koordinaten_plus_oeffnungen[(0, 0)].remove(2)

        ort0 = Ort((0, 0), [1])
        ort1 = Ort((1, 1), [1])
        ort2 = Ort((2, 0), [1])
        ort0.koordinaten_plus_oeffnungen.update({
            (1, 0): 2,
            (1, 1): [],
            (2, 0): []
        })
        ort0.koordinaten_plus_oeffnungen[(0, 0)].remove(1)

        # meeple in ort2
        ort2.besitzer = Player(2)

        spiel.cards_set = {
            (0, 0): card0,
            (0, -1): card1,
            (1, 0): card2,
            (1, 1): card3,
            (2, 0): card4
        }
        spiel.possible_coordinates = [(0, 1), (-1, 0), (-1, -1), (0, -2),
                                      (1, -1), (2, -1), (3, 0), (2, 1), (1, 2)]

        goal = [(1, -1, 1, k.strassen[0]),
                (1, -1, 1, k.orte[0], (1, -1, 1, None)), (3, 0, 0, None),
                (3, 0, 0, k.strassen[0]), (3, 0, 3, None),
                (3, 0, 3, k.strassen[0])]
def uct_vs_uct(counter):

    logfile = open('../log/logfile{}'.format(counter), 'w+')
    logfile.write('NEUES SPIEL')

    player1 = Player(1, 'ai')
    player2 = Player(2, 'ai')

    # player1.punkte = 3

    d = {player1: player2, player2: player1}

    # zum probieren
    # von einer random-Kartenliste die ersten 10 Karten
    #spiel = Spiel(create_kartenliste(karteninfoliste)[:10], player1, player2)

    spiel = Spiel(create_kartenliste(determinized_karteninfoliste, False),
                  player1, player2)

    # select startspieler
    current_player = random.choice([player1, player2])

    mcts = MCTS((player1, player2))
    mcts.root = Node(True, None, current_player.nummer)

    game_is_running = True
    while game_is_running:

        logfile.write("\n\n\nNEUER ZUG: Player{} ist am Zug\n".format(
            current_player.nummer))
        logfile.write(
            'Aktuell hat player1 {} Punkte und player2 {} Punkte.\n'.format(
                player1.punkte, player2.punkte))
        #display_spielbrett_dict(spiel.cards_set)
        current_card = spiel.cards_left[0]

        print('\nDie nachste Karte ist [{0}, {1}, {2}, {3}, {4}, {5}]'.format(
            current_card.info[0], current_card.info[1], current_card.info[2],
            current_card.info[3], current_card.mitte, current_card.schild))
        logfile.write(
            '\nDie nachste Karte ist [{0}, {1}, {2}, {3}, {4}, {5}]'.format(
                current_card.info[0], current_card.info[1],
                current_card.info[2], current_card.info[3], current_card.mitte,
                current_card.schild))
        #draw_card(current_card)
        logfile.write('\nSie enthält folgende moegliche Meeplepositionen:')
        logfile.write('\nOrte: ')
        for o in current_card.orte:
            logfile.write("{}: {}  ".format(o.name, o.kanten))
        logfile.write('\nStrassen: ')
        for s in current_card.strassen:
            logfile.write("{}: {}  ".format(s.name, s.kanten))
        logfile.write('\nWiesen: ')
        for w in current_card.wiesen:
            logfile.write("{}: {}  ".format(w.name, w.ecken))

        pos = spiel.calculate_possible_actions(current_card, current_player)

        # wenn es moegliche anlegestellenn (fuer den aktuellen Spieler) gibt, (es gibt fuer einen spieler auf jeden Fall
        # eine Anlegemoeglichkeit, wenn es fuer den anderen auch eine gibt)
        if pos:
            if current_player.nummer == 1:
                #logfile.write('\nPlayer1 ist am Zug')

                mcts.root = mcts.find_next_move(spiel)

                # l_a_K auf die gespielt werden soll
                if mcts.root.action[3] is None:
                    landschaft = None
                elif mcts.root.action[3] == 'k':
                    landschaft = 'k'
                else:
                    l_dict = {
                        'o': current_card.orte,
                        's': current_card.strassen,
                        'w': current_card.wiesen
                    }
                    landschaft = [
                        l for l in l_dict[mcts.root.action[3]]
                        if l.name == mcts.root.action[4]
                    ][0]

                spiel.make_action(
                    current_player, current_card, mcts.root.action[0],
                    mcts.root.action[1], mcts.root.action[2],
                    landschaft)  #######################################

                if mcts.root.action[3] is not None:
                    # action_ausgabe = 'k' if mcts.root.action[2] == 'k' else mcts.root.action[2]
                    logfile.write(
                        "\n\nDie AI setzt einen Meeple auf {}{}.".format(
                            mcts.root.action[3], mcts.root.action[4]))
                elif mcts.root.action[3] == 'k':
                    logfile.write(
                        "\nDie AI setzt einem Meeple auf das Kloster.")
                else:
                    logfile.write("\nDie AI setzt keinen Meeple.")

                logfile.write(
                    "\nDie AI setzt die Karte an ({}, {}) und rotiert sie {} mal"
                    .format(mcts.root.action[0], mcts.root.action[1],
                            mcts.root.action[2]))

                # gesetzte Karte loeschen
                del spiel.cards_left[0]

                if len(spiel.cards_left) == 0:
                    game_is_running = False

                # spieler wechseln
                current_player = d[current_player]

            else:
                # player2
                #logfile.write('Player2 ist am Zug')

                mcts.root = mcts.find_next_move(spiel)

                # l_a_K auf die gespielt werden soll
                if mcts.root.action[3] is None:
                    landschaft = None
                elif mcts.root.action[3] == 'k':
                    landschaft = 'k'
                else:
                    l_dict = {
                        'o': current_card.orte,
                        's': current_card.strassen,
                        'w': current_card.wiesen
                    }
                    landschaft = [
                        l for l in l_dict[mcts.root.action[3]]
                        if l.name == mcts.root.action[4]
                    ][0]

                spiel.make_action(
                    current_player, current_card, mcts.root.action[0],
                    mcts.root.action[1], mcts.root.action[2],
                    landschaft)  #######################################

                if mcts.root.action[3] is not None:
                    # action_ausgabe = 'k' if mcts.root.action[2] == 'k' else mcts.root.action[2]
                    logfile.write(
                        "\n\nDie AI setzt einen Meeple auf {}{}.".format(
                            mcts.root.action[3], mcts.root.action[4]))
                elif mcts.root.action[2] == 'k':
                    logfile.write(
                        "\nDie AI setzt einem Meeple auf das Kloster.")
                else:
                    logfile.write("\nDie AI setzt keinen Meeple.")

                logfile.write(
                    "\nDie AI setzt die Karte an ({}, {}) und rotiert sie {} mal"
                    .format(mcts.root.action[0], mcts.root.action[1],
                            mcts.root.action[2]))

                # gesetzte Karte loeschen
                del spiel.cards_left[0]

                if len(spiel.cards_left) == 0:
                    game_is_running = False

                # spieler wechseln
                current_player = d[current_player]

        else:
            print("ES GIBT FUER DIESE KARTE KEINE ANLEGESTELLE")

            # gesetzte Karte loeschen
            del spiel.cards_left[0]

            if len(spiel.cards_left) == 0:
                game_is_running = False

    spiel.final_evaluate()
    logfile.write(
        "\nSpielende: Player1 hat {} Punkte, Player2 hat {} Punkte.".format(
            player1.punkte, player2.punkte))
    logfile.close()
Exemple #27
0
    def test_1(self):

        player1 = Player(1)
        player2 = Player(2, 'ai')
        d = {player1: player2, player2: player1}

        kartenliste = create_kartenliste(['SOWS', 'WWSS', 'SOSSG', 'OSSW', 'OOSOOT', 'OSSW', 'WWSWK', 'OSSW'], False)
        spiel = Spiel(kartenliste, player1, player2)

        mcts = MCTS([player1, player2])
        mcts.root = Node(True, None, player1.nummer)

        current_player = player1
        #display_spielbrett_dict(spiel.cards_set)

        # spiel geht los
        k1 = spiel.cards_left.pop(0)
        spiel.make_action(current_player, k1, 1, 0, 2, k1.orte[0])

        # tree updaten
        mcts.root = Node(True, (1, 0, 2, 'o', 1), player2.nummer, mcts.root)

        current_player = d[current_player]
        #display_spielbrett_dict(spiel.cards_set)


        # AI Zug 1
        proposed_node = mcts.find_next_move(spiel)
        spiel.make_action(current_player, spiel.cards_left[0], 0, 1, 0, spiel.cards_left[0].strassen[0])

        for node in mcts.root.children:
            if node.action == (0, 1, 0, 's', 1):
                mcts.root = node
                break
            #print('\n', node.action)
            #print(node.wins,'/', node.visits)

        del spiel.cards_left[0]
        current_player = d[current_player]
        #display_spielbrett_dict(spiel.cards_set)

        self.assertEqual(7, player1.meeples)
        self.assertEqual(6, player2.meeples)

        # neuer human Zug
        k3 = spiel.cards_left.pop(0)

        wiese = None
        nr = None
        for w in k3.wiesen:
            if w.ecken == [5, 6]:
                wiese = w
                nr = w.name

        spiel.make_action(current_player, k3, 2, 0, 3, wiese)

        # tree updaten
        for node in mcts.root.children:
            if node.action == (2, 0, 3, 'w', nr):
                mcts.root = node

        current_player = d[current_player]
        #display_spielbrett_dict(spiel.cards_set)


        # AI Zug 2
        proposed_node = mcts.find_next_move(spiel)

        wiese = None
        for w in spiel.cards_left[0].wiesen:
            if w.ecken == [4, 5, 7]:
                wiese = w
        spiel.make_action(current_player, spiel.cards_left[0], -1, 1, 0, wiese)

        for node in mcts.root.children:
            if node.action == (-1, 1, 0, 'w', 1):
                mcts.root = node
            #print('\n', node.action)
            #print(node.wins,'/', node.visits)

        del spiel.cards_left[0]
        current_player = d[current_player]
        #display_spielbrett_dict(spiel.cards_set)

        self.assertEqual(5, player2.meeples)
        self.assertEqual(6, player1.meeples)

        # neuer human Zug
        k5 = spiel.cards_left.pop(0)

        spiel.make_action(current_player, k5, 2, 1, 1, k5.orte[0])

        # tree updaten
        for node in mcts.root.children:
            if node.action == (2, 1, 1, 'o', 1):
                mcts.root = node

        current_player = d[current_player]
        #display_spielbrett_dict(spiel.cards_set)

        # AI Zug 3
        proposed_node = mcts.find_next_move(spiel)
        spiel.make_action(current_player, spiel.cards_left[0], -1, 2, 2, spiel.cards_left[0].orte[0])

        for node in mcts.root.children:
            if node.action == (-1, 2, 2, 'o', 1):
                mcts.root = node
            # print('\n', node.action)
            # print(node.wins,'/', node.visits)

        del spiel.cards_left[0]
        current_player = d[current_player]
        #display_spielbrett_dict(spiel.cards_set)

        # neuer human Zug
        k7 = spiel.cards_left.pop(0)

        spiel.make_action(current_player, k7, 1, 1, 3, 'k')

        # tree updaten
        for node in mcts.root.children:
            #print(node.action)
            if node.action == (1, 1, 3, 'k', 1):
                mcts.root = node

        current_player = d[current_player]
        display_spielbrett_dict(spiel.cards_set)

        self.assertEqual(4, player1.punkte)
        self.assertEqual(4, player2.punkte)

        self.assertEqual(4, player1.meeples)
        self.assertEqual(5, player2.meeples)

        # AI Zug 4
        for c in spiel.cards_left:
            print(c.matrix)
        proposed_node = mcts.find_next_move(spiel)
Exemple #28
0
def player_vs_ucb(kartenliste=None):

    player1 = Player(1)
    player2 = Player(2, 'ai')

    #player1.meeples = 3
    #player2.meeples = 3

    #player1.punkte = 3

    d = {player1: player2, player2: player1}

    if kartenliste is None:
        spiel = Spiel(create_kartenliste(karteninfoliste))
    else:
        spiel = Spiel(kartenliste)

    #select startspieler
    current_player = player2



    #####
    #k1 = Card('O', 'W', 'W', 'O', 'O')
    #spiel.make_action(k1, (1, 0), 0, player1, k1.orte[0])

    #k2 = Card('O', 'W', 'W', 'O', 'O')
    #spiel.make_action(k2, (1, 0), 0, player1)
    #####


    game_is_running = True
    while game_is_running:

        print('player1 hat {} Punkte, player2 hat {} Punkte.'.format(player1.punkte, player2.punkte))

        # display spielbrett
        display_spielbrett_dict(spiel.cards_set)

        current_card = spiel.draw_card()
        #print('Alle moeglichen actions:')
        #print(spiel.calculate_possible_actions(current_card, current_player))

        print('\nDie nachste Karte ist [{0}, {1}, {2}, {3}, {4}, {5}]'.format(current_card.info[0], current_card.info[1], current_card.info[2], current_card.info[3], current_card.mitte, current_card.schild))

        draw_card(current_card)
        print('Sie enthält folgende moegliche Meeplepositionen:')
        print('Orte:')
        for o in current_card.orte:
            print(o.name, o.kanten)
        print('Strassen:')
        for s in current_card.strassen:
            print(s.name, s.kanten)
        print('Wiesen:')
        for w in current_card.wiesen:
            print(w.name, w.ecken)

        pos = spiel.calculate_possible_actions(current_card, current_player)

        if pos:

            if current_player.art == 'human':
                 #gib move ein
                    inp = input('Bitte gib deine Aktion an:')

                    inp_split = inp.split(' ')
                    ungueltig = True
                    action = None
                    try:
                        if inp_split[3][0] == 'o':
                            o = [a for a in current_card.orte if a.name == int(inp_split[3][1])]
                            action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), o[0])
                        elif inp_split[3][0] == 's':
                            s = [a for a in current_card.strassen if a.name == int(inp_split[3][1])]
                            action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), s[0])
                        elif inp_split[3][0] == 'w':
                            w = [a for a in current_card.wiesen if a.name == int(inp_split[3][1])]
                            action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), w[0])
                        elif inp_split[3][0] == 'k':
                            action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), 'K')
                        else:
                            action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), None)
                    except IndexError or ValueError:
                        pass

                    #falls move unguelig:
                    if action in pos:
                        ungueltig = False
                    while ungueltig:
                        print("illegaler Move")
                        inp = input('Bitte gib deine Aktion an:')
                        inp_split = inp.split(' ')
                        try:
                            if inp_split[3][0] == 'o':
                                o = [a for a in current_card.orte if a.name == int(inp_split[3][1])]
                                action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), o[0])
                            elif inp_split[3][0] == 's':
                                s = [a for a in current_card.strassen if a.name == int(inp_split[3][1])]
                                action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), s[0])
                            elif inp_split[3][0] == 'w':
                                w = [a for a in current_card.wiesen if a.name == int(inp_split[3][1])]
                                action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), w[0])
                            elif inp_split[3][0] == 'k':
                                action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), 'K')
                            else:
                                action = (int(inp_split[0]), int(inp_split[1]), int(inp_split[2]), None)
                        except IndexError or ValueError:
                            pass
                        if action in pos:
                            ungueltig = False


                    spiel.make_action(current_card, (action[0], action[1]), action[2], current_player, action[3])

                    #spieler wechseln
                    current_player = d[current_player]

                    if not spiel.cards_left:
                        game_is_running = False

            else:

                # ai
                child_nodes = [Node(action) for action in pos]

                t = 0
                t_end = 500

                # player stats in real game
                current_player_stats = (current_player.meeples, current_player.punkte)
                other_player_stats = (d[current_player].meeples, d[current_player].punkte)

                # loop as long as time is left:
                while t < t_end:

                    spiel_copy = deepcopy(spiel)
                    current_card_copy = deepcopy(current_card)

                    #player zurueckaendern:
                    """die spieler, die beim kopieren vom Spiel veraendert wurden werden hier wieder zurueckgesetzt"""
                    l = [spiel_copy.alle_orte, spiel_copy.alle_wiesen, spiel_copy.alle_strassen]
                    for landart in l:
                        for instance in landart:
                            if instance.meeples:

                                new_d = {current_player: 0, d[current_player]: 0}
                                for player in instance.meeples:
                                    for global_player in new_d:
                                        if global_player.nummer == player.nummer:
                                            new_d[global_player] = instance.meeples[player]
                                instance.meeples = new_d
                                instance.update_besitzer()

                    # spieler von kloestern zuruecksetzen
                    for global_kloster in spiel.alle_kloester:
                        for kloster in spiel_copy.alle_kloester:
                            if kloster.umgebungs_koordinaten == global_kloster.umgebungs_koordinaten:
                                kloster.besitzer = global_kloster.besitzer

                    #current_card_copy = deepcopy(current_card)

                    current_node = max(child_nodes, key=lambda nod: nod.calculate_UCB1_value(t))

                    meeple_pos = 'K'

                    if isinstance(current_node.action[3], Ort_auf_Karte):
                        for ort in current_card_copy.orte:
                            if ort.name == current_node.action[3].name:
                                meeple_pos = ort
                                break
                    elif isinstance(current_node.action[3], StasseAufKarte):
                        for strasse in current_card_copy.strassen:
                            if strasse.name == current_node.action[3].name:
                                meeple_pos = strasse
                                break
                    elif isinstance(current_node.action[3], WieseAufKarte):
                        for wiese in current_card_copy.wiesen:
                            if wiese.name == current_node.action[3].name:
                                meeple_pos = wiese
                                break

                    spiel_copy.make_action(current_card_copy, (current_node.action[0], current_node.action[1]), current_node.action[2], current_player, meeple_pos)

                    # play random
                    winner = spiel_copy.play_random1v1(d[current_player], current_player)

                    current_node.visits += 1
                    if winner == current_player:
                        current_node.wins += 1
                    elif winner == 0:
                        current_node.wins += 0.5

                    # spieler nach random-spiel wieder auf ihre ausgangswerte setzen
                    current_player.meeples = current_player_stats[0]
                    current_player.punkte = current_player_stats[1]

                    d[current_player].meeples = other_player_stats[0]
                    d[current_player].punkte = other_player_stats[1]

                    # erste Karte nach dem Spiel wieder resetten
                    #current_card.ecken, current_card.kanten, current_card.orte, current_card.orte_kanten,\
                    #current_card.strassen, current_card.strassen_kanten, current_card.wiesen, current_card.wiesen_kanten = card_stats[0], card_stats[1], card_stats[2], card_stats[3], card_stats[4], card_stats[5], card_stats[6], card_stats[7]

                    t += 1


                #spiel.make_action(max(child_nodes, key=lambda nod: nod.wins).action)
                action = max(child_nodes, key=lambda nod: nod.wins).action
                spiel.make_action(current_card, (action[0], action[1]), action[2], current_player, action[3])

                if action[3] is not None:
                    action_ausgabe = 'K' if action[3] == 'K' else action[3].name
                    print("\nEin Meeple wird auf {} mit Namen {} gesetzt".format(action[3], action_ausgabe))
                else:
                    print("\nEs wird kein Meeple gesetzt")

                print("\nDie AI setzt die Karte an ({}, {}) und rotiert sie {} mal".format(action[0], action[1], action[2]))


                # switch players
                current_player = d[current_player]

                if not spiel.cards_left:
                    game_is_running = False

        else:
            continue

    spiel.final_evaluate()
    print("Spielende: Player1 hat {} Punkte, Player2 hat {} Punkte.".format(player1.punkte, player2.punkte))
def player_vs_uct():

    player1 = Player(1)
    player2 = Player(2, 'ai')

    #player1.punkte = 3

    d = {player1: player2, player2: player1}

    #zum probieren
    #spiel = Spiel(create_kartenliste(determinized_short_karteninfoliste, False), player1, player2)

    spiel = Spiel(create_kartenliste(speed_test_karteninfoliste, False),
                  player1, player2)
    #spiel = Spiel(create_kartenliste(test_karteninfolist, False), player1, player2)      #['OSSW', 'WWSS', 'OSSW', 'WWSWK']

    #select startspieler
    current_player = player2  #random.choice((player1, player2))
    print('Der Startspieler ist Player{}'.format(current_player.nummer))

    mcts = MCTS((player1, player2), spiel.play_random1v1,
                spiel.calculate_possible_actions)
    mcts.root = Node(True, None, current_player.nummer)

    game_is_running = True
    while game_is_running:

        print(
            '\n\nNEUER ZUG: Aktuell hat player1 {} Punkte und player2 {} Punkte.\n'
            .format(player1.punkte, player2.punkte))

        display_spielbrett_dict(spiel.cards_set)
        current_card = spiel.cards_left[0]

        print('Die nachste Karte ist [{0}, {1}, {2}, {3}, {4}, {5}]'.format(
            current_card.info[0], current_card.info[1], current_card.info[2],
            current_card.info[3], current_card.mitte, current_card.schild))
        draw_card(current_card)
        print('Sie enthält folgende moegliche Meeplepositionen:')
        print('Orte:')
        for o in current_card.orte:
            print(o.name, o.kanten)
        print('Strassen:')
        for s in current_card.strassen:
            print(s.name, s.kanten)
        print('Wiesen:')
        for w in current_card.wiesen:
            print(w.name, w.ecken)

        pos = spiel.calculate_possible_actions(current_card, current_player)

        # wenn es moegliche anlegestellenn (fuer den aktuellen Spieler) gibt, (es gibt fuer einen spieler auf jeden Fall
        # eine Anlegemoeglichkeit, wenn es fuer den anderen auch eine gibt)
        if pos:
            if current_player.art == 'human':

                #gib move ein
                inp = input('Bitte gib deine Aktion an:')
                inp_split = inp.split(' ')

                ungueltig = True
                action = None
                try:
                    if inp_split[3][0] == 'o':
                        o = [
                            a for a in current_card.orte
                            if a.name == int(inp_split[3][1])
                        ]
                        action = (int(inp_split[0]), int(inp_split[1]),
                                  int(inp_split[2]), o[0])
                    elif inp_split[3][0] == 's':
                        s = [
                            a for a in current_card.strassen
                            if a.name == int(inp_split[3][1])
                        ]
                        action = (int(inp_split[0]), int(inp_split[1]),
                                  int(inp_split[2]), s[0])
                    elif inp_split[3][0] == 'w':
                        w = [
                            a for a in current_card.wiesen
                            if a.name == int(inp_split[3][1])
                        ]
                        action = (int(inp_split[0]), int(inp_split[1]),
                                  int(inp_split[2]), w[0])
                    elif inp_split[3][0] == 'k':
                        action = (int(inp_split[0]), int(inp_split[1]),
                                  int(inp_split[2]), 'k')
                    else:
                        action = (int(inp_split[0]), int(inp_split[1]),
                                  int(inp_split[2]), None)
                except IndexError or ValueError:
                    print('ERROR CATCHED')

                # falls move unguelig:
                if action in pos:
                    ungueltig = False
                while ungueltig:
                    print("illegaler Move")
                    inp = input('Bitte gib deine Aktion an:')
                    inp_split = inp.split(' ')
                    try:
                        if inp_split[3][0] == 'o':
                            o = [
                                a for a in current_card.orte
                                if a.name == int(inp_split[3][1])
                            ]
                            action = (int(inp_split[0]), int(inp_split[1]),
                                      int(inp_split[2]), o[0])
                        elif inp_split[3][0] == 's':
                            s = [
                                a for a in current_card.strassen
                                if a.name == int(inp_split[3][1])
                            ]
                            action = (int(inp_split[0]), int(inp_split[1]),
                                      int(inp_split[2]), s[0])
                        elif inp_split[3][0] == 'w':
                            w = [
                                a for a in current_card.wiesen
                                if a.name == int(inp_split[3][1])
                            ]
                            action = (int(inp_split[0]), int(inp_split[1]),
                                      int(inp_split[2]), w[0])
                        elif inp_split[3][0] == 'k':
                            action = (int(inp_split[0]), int(inp_split[1]),
                                      int(inp_split[2]), 'k')
                        else:
                            action = (int(inp_split[0]), int(inp_split[1]),
                                      int(inp_split[2]), None)
                    except IndexError or ValueError:
                        pass
                    if action in pos:
                        ungueltig = False

                spiel.make_action(current_player, current_card, action[0],
                                  action[1], action[2], action[3])

                # gespielte action formulieren
                if action[3]:
                    lak_id = inp_split[3][0]
                    lak_name = 1 if inp_split[3][0] == 'k' else action[3].name
                else:
                    lak_id = None
                    lak_name = None

                node_action = (action[0], action[1], action[2], lak_id,
                               lak_name)

                # root anpassen

                # falls die aktuelle root_node bereits Kinder hat
                if mcts.root.children:
                    for child in mcts.root.children:

                        # wenn die action von der child-node der gespielten entspricht
                        if child.action == node_action:  ###
                            mcts.root = child
                            break

                # another player made the first move of the game, or the node has no visits yet
                else:
                    p_num = 1 if current_player.nummer == 2 else 2
                    mcts.root = Node(True, node_action, p_num, mcts.root)

            # AI-PLayer
            else:
                #mcts.root = mcts.find_next_move(spiel)

                # erstelle Liste mit den root_nodes_kopien fuer welche die Trees aufgebaut wurden
                t_start = time.time()
                root_copies = [deepcopy(mcts.root) for i in range(4)]

                # multiprocessing
                pool = multiprocessing.Pool()
                roots = pool.starmap(calculate_tree,
                                     zip(root_copies, repeat(spiel, 4)))

                pool.close()
                pool.join()
                # ermittle die neue child-Node
                mcts.root = get_best_child(roots)

                t_ende = time.time()

                print(
                    f'Es wurden {t_ende - t_start} Sekunden gebraucht, um die naechste Node auszurechnen.'
                )

                # l_a_K auf die gespielt werden soll
                if mcts.root.action[3] is None:
                    landschaft = None
                elif mcts.root.action[3] == 'k':
                    landschaft = 'k'
                else:
                    l_dict = {
                        'o': current_card.orte,
                        's': current_card.strassen,
                        'w': current_card.wiesen
                    }
                    landschaft = [
                        l for l in l_dict[mcts.root.action[3]]
                        if l.name == mcts.root.action[4]
                    ][0]

                spiel.make_action(
                    current_player, current_card, mcts.root.action[0],
                    mcts.root.action[1], mcts.root.action[2],
                    landschaft)  #######################################

                # falls die AI-Aktion einen Meeple auf eine Landschaft (ausser Kloster) setzt
                if mcts.root.action[3] is not None:
                    print("\nDie AI setzt einen Meeple auf {}{}.".format(
                        mcts.root.action[3], mcts.root.action[4]))
                elif mcts.root.action[3] == 'k':
                    print("\nDie AI setzt einem Meeple auf das Kloster.")
                else:
                    print("\nDie AI setzt keinen Meeple.")

                print(
                    "Die AI setzt die Karte an ({}, {}) und rotiert sie {} mal"
                    .format(mcts.root.action[0], mcts.root.action[1],
                            mcts.root.action[2]))

            # Vorbereitung fuer naechsten Zug
            # gesetzte Karte loeschen
            del spiel.cards_left[0]

            if len(spiel.cards_left) == 0:
                game_is_running = False

            # spieler wechseln
            current_player = d[current_player]

        # wenn es fuer die gezogene Karte keine Anlegestelle gibt
        else:
            print("ES GIBT FUER DIESE KARTE KEINE ANLEGESTELLE")

            # gesetzte Karte loeschen
            del spiel.cards_left[0]

            if len(spiel.cards_left) == 0:
                game_is_running = False

    spiel.final_evaluate()
    print("\nSpielende: Player1 hat {} Punkte, Player2 hat {} Punkte.".format(
        player1.punkte, player2.punkte))
Exemple #30
0
def levelOne():

    role = ' '

    #stats for Knight
    Knight = "Knight"
    kHP = 100
    kAD = 20
    kDef = 30

    #stats for Assassin
    Assassin = "Assasssin"
    aHP = 50
    aAD = 50
    aDef = 10

    # gets name for player
    name = input("So you're back again, eh? What was your name again?\n")

    # make sure player chooses correct role
    while (True):
        role = input("Choose your role (Assassin or Knight )\n")
        role = role.lower()
        if (role == 'assassin') or (role == "knight"):
            break

    # sets players stats based on the role selected
    if role == "assassin":
        p1 = Player(name, Assassin, aHP, aAD, aDef)
        print('''You are an assassin.
                 Your stats are:''')
        print("HP: ", p1.HP, "\n")
        print("AD: ", p1.AD, "\n")
        print("DEF: ", p1.Def, "\n")
    else:
        p1 = Player(name, Knight, kHP, kAD, kDef)
        print('''You are a knight.
                 Your stats are:''')
        print("HP: ", p1.HP, "\n")
        print("AD: ", p1.AD, "\n")
        print("DEF: ", p1.Def, "\n")

    print("Now that we've got you set up, let's start our adventure.\n")
    #asks player if they wish to continue to level two
    start = input("Shall we?(y or n)\n")
    if 'y' in start:
        levelTwo()
    else:
        #player chose not to continue so offer player choice to change
        #information or quit the game
        choice = input('''What would you like to do?
                1. Change name
                2. Change role
                3. Quit\n''')
        if choice == '1':
            p1.name = input('Enter your new name\n')

        elif choice == '2':
            p1.role = input('Enter your new role\n')
            if p1.role == "knight":
                p1.HP = kHP
                p1.AD = kAD
                p1.Def = kDef
            else:
                p1.HP = aHP
                p1.AD = aAD
                p1.Def = aDef

        else:
            print('Ok! See you later!')
 def __init__(self, checker, tiebreak, lookahead):
     ''' Constructs a new AIPlayer object
     '''
     Player.__init__(self, checker)
     self.tiebreak = tiebreak
     self.lookahead = lookahead