def run(self): # Starting timer t0 = time.time() print('Timeout is set to ' + str(data.timeout_threshold) + ' seconds') root_node = TreeNode( data.base_stacks, deque([Foundation(Suit(i + 1)) for i in range(data.F)]), deque([Freecell() for i in range(data.C)]), 0) self.frontier.append(root_node) self.visited.add(root_node) solution = None while self.frontier: self.states_checked += 1 # Check timer before continuing timer = time.time() - t0 if timer > data.timeout_threshold: print('Timeout. Node with the maximum depth:') solution = self.max_node break current_node = self.frontier.popleft() if current_node.is_solution(): solution = current_node break self.find_children(current_node) timer = time.time() - t0 self.stop(solution) return timer, solution.depth, self.states_checked
def setUp(self): """Initializes a Foundation pile for Spades.""" self.pile = Foundation(Suit.SPADE)
class TestFoundationPile(unittest.TestCase): """ Tests the functionality of a Foundation pile. """ def setUp(self): """Initializes a Foundation pile for Spades.""" self.pile = Foundation(Suit.SPADE) def tearDown(self): """Does nothing.""" print "" def test_init(self): """Tests the init() function""" print "foundation: test_init ", self.assertEqual(self.pile.suit, Suit.SPADE, "The suit of the Foundation pile was incorrect.") self.assertEqual(self.pile.name, "Foundation-S", "The name of the Foundation pile was incorrect.") def test_maxlen(self): """Tests that the maximum size of a Foundation pile is 13.""" print "foundation: test_maximumSize ", self.assertEqual(self.pile.maxlen, 13, "The maximum length of the Foundation pile was \ incorrect.") def test_enqueueCard(self): """ Tests that enqueue() does not insert a Card at the bottom of a Foundation pile. """ print "foundation: test_enqueueCard ", for i in range(1,6): card = Card(Suit.SPADE, i) card.flip_up() self.pile.enqueue_card(card) self.assertNotIn(card, self.pile, "A Card was enqueued to a Foundation pile.") def test_pushValidCard(self): """Tests that push() accepts a valid Card.""" print "foundation: test_pushValidCard ", append_some_cards(self.pile) for i in range(7,14): card = Card(Suit.SPADE, i) card.flip_up() self.pile.push_card(card) self.assertIn(card, self.pile, "The Foundation pile rejected a valid card.") def test_pushInvalidRankCard(self): """Tests that push() rejects a Card with an invalid rank.""" print "foundation: test_pushInvalidRankCard ", append_some_cards(self.pile) for i in range(8,14): card = Card(Suit.SPADE, i) card.flip_up() self.pile.push_card(card) self.assertNotIn(card, self.pile, "The Foundation pile accepted a card with \ invalid rank.") def test_pushInvalidSuitCard(self): """Tests that push() rejects a Card with an invalid suit.""" print "foundation: test_pushInvalidSuitCard ", invalidSuit = Card(Suit.DIAMOND, Rank.ACE) invalidSuit.flip_up() self.pile.push_card(invalidSuit) self.assertNotIn(invalidSuit, self.pile, "The Foundation pile accepted a card with \ invalid suit.") invalidSuit = Card(Suit.HEART, Rank.ACE) invalidSuit.flip_up() self.pile.push_card(invalidSuit) self.assertNotIn(invalidSuit, self.pile, "The Foundation pile accepted a card with \ invalid suit.") def test_pushAceEmpty(self): """ Tests that an empty Foundation pile accepts an Ace of the proper suit. """ print "foundation: test_pushAceEmpty ", aceS = Card(Suit.SPADE, 1) aceS.flip_up() self.pile.push_card(aceS) self.assertIn(aceS, self.pile, "The empty Foundation pile rejected a valid Ace.") def test_pushNonAceEmpty(self): """Tests that an empty Foundation rejects a non-Ace.""" print "foundation: test_pushNonAceEmpty ", nonAce = Card(Suit.SPADE, 2) nonAce.flip_up() self.pile.push_card(nonAce) self.assertNotIn(nonAce, self.pile, "The empty Foundation pile accepted \ a non-Ace of its suit.") def test_isCompleteTrue(self): """ Tests that is_complete() returns true for a Foundation pile with all 13 cards of the same suit built bottom up. """ print "foundation: test_isCompleteTrue ", append_cards(self.pile) self.assertTrue(self.pile.is_complete(), "The complete Foundation pile doesn't think it's \ complete.") def test_isCompleteFalse(self): """ Tests that is_complete() returns true for a Foundation pile with all 13 cards of the same suit built bottom up. @todo: WRITE TEST """ pass