Example #1
0
    def test_IsLeaf(self):
        a = Battle.GenerateBattles([Battle.Contestant("0")])
        self.assertTrue(a.IsLeaf())

        b = Battle.GenerateBattles(
            [Battle.Contestant("1"),
             Battle.Contestant("2")])
        self.assertFalse(b.IsLeaf())
Example #2
0
 def test_GetNextUndecided(self):
     a = Battle.GenerateBattles([Battle.Contestant("0")])
     self.assertTrue(a.GetNextUndecided() is None)
     b = Battle.GenerateBattles(
         [Battle.Contestant("1"),
          Battle.Contestant("2")])
     self.assertEqual(b.GetNextUndecided(), b)
     b.WinnerIsA()
     self.assertTrue(b.GetNextUndecided() is None)
     self.assertTrue(b.IsDecided())
Example #3
0
    def test_IsDecided(self):
        a = Battle.GenerateBattles([Battle.Contestant("0")])
        self.assertTrue(a.IsDecided())

        b = Battle.GenerateBattles(
            [Battle.Contestant("1"),
             Battle.Contestant("2")])
        self.assertFalse(b.IsDecided())
        b.WinnerIsA()
        self.assertTrue(b.IsDecided())
Example #4
0
    def test_RemoveWinner(self):
        c1 = Battle.Contestant("1")
        c2 = Battle.Contestant("2")
        c3 = Battle.Contestant("3")

        main = Battle.GenerateBattles([c1, c2, c3])

        # c1 vs. c2
        c = main.GetNextUndecided()
        # c1 winner
        c.WinnerIsA()

        # c1 vs. c3
        d = main.GetNextUndecided()
        # c1 winner
        d.WinnerIsA()

        self.assertTrue(main.IsDecided())
        self.assertEqual(main.getWinner(), c1)
        main = main.RemoveWinner()
        self.assertFalse(main.IsDecided())

        # winner is c3
        main.WinnerIsB()
        self.assertTrue(main.IsDecided())
        self.assertEqual(main.getWinner(), c2)

        self.assertTrue(main.IsDecided())
        main = main.RemoveWinner()

        self.assertTrue(main.IsDecided())
        self.assertEqual(main.getWinner(), c3)
Example #5
0
def startBattle(contestants):
    """ This is a typical workflow for the Battle class: 
	1. Generate a Battle using a list of contestants
	2. As long as the Current battle is not a Single contestant (a Leaf in the graph) :
	  a. check if there is any undecided battle between contestant before we can select a winner for the current battle
	  b. if there is one, fetch it and elect the winner for the battle using WinnerIsA() or WinnerIsB()
	  c. If the current battle is decided (ie a winner has been elected), fetch the winner and create a new graph battle without him.
	     This become the new current battle
	"""
    listOfResults = []

    battle = Battle.GenerateBattles(contestants)

    while not battle.IsLeaf():
        undec = battle.GetNextUndecided()
        if (undec != None):
            a = undec.a.getWinner()
            b = undec.b.getWinner()
            # decide which contestant is stronger depending on its number
            if int(a.id) < int(b.id):
                undec.WinnerIsA()
            else:
                undec.WinnerIsB()
        if battle.IsDecided():
            listOfResults.append(battle.getWinner())
            battle = battle.RemoveWinner()

    # append the last element, when b is leaf
    listOfResults.append(battle.getWinner())
    return listOfResults
Example #6
0
    def test_GetWinner(self):
        c1 = Battle.Contestant("1")
        c2 = Battle.Contestant("2")
        c3 = Battle.Contestant("3")

        a = Battle.GenerateBattles([c1, c2, c3])

        c = a.GetNextUndecided()
        c.WinnerIsA()
        self.assertEqual(c.getWinner(), c1)
Example #7
0
 def generateBattle(self, path):
     """ Generate the main battle structure with the different Contestants and the associated images """
     contestants = []
     for file_name in glob.glob(os.path.join(path, "*.jpg")):
         im = Image.open(file_name)
         fullIm = im.copy()
         im.thumbnail(PREVIEW_SIZE, Image.ANTIALIAS)
         contestants.append(Battle.Contestant(file_name, im, fullIm))
     self.setLabel("Number of images loaded: %d" % (len(contestants), ))
     self.currentBest = Battle.GenerateBattles(contestants)
     self.currentBattle = self.currentBest.GetNextUndecided()
Example #8
0
    def test_GenerateBattles(self):
        contestantList = []
        for i in range(5):
            contestantList.append(Battle.Contestant(str(i)))

        mainBattle = Battle.GenerateBattles(contestantList)

        # Check Topology
        self.assertFalse(mainBattle.IsDecided())
        self.assertFalse(mainBattle.a.IsLeaf())
        self.assertFalse(mainBattle.a.a.IsLeaf())
        self.assertTrue(mainBattle.a.a.a.IsLeaf())
        self.assertTrue(mainBattle.b.IsLeaf())