# Yahtzee: The Last Brick in the Foundation

In the previous section, I talked about how our strong foundation was letting me write code very easily as I expanded my implementation. I need one last piece of foundational code in my hand class. For the Three of a Kind, Four of a Kind, and Chance rolls, the score for the rolls is the total of all dice in the hand. It seems like if I implement one more simple method to count the total of the individual dice in the hand, I’ll have a very solid foundation. Because of the way we have modeled the `SimpleHand`, this is a very simple method to write also (especially if we remember that our actual hand is represented as a list of the individual dice in the hand:

```class YahtzeeHand(SimpleHand):
...
def get_hand_total_score(self):
return sum(self.hand)```

I’ve also updated my unit tests for the `YahtzeeHand` class as follows:

```class TestYahtzeeHand(TestCase):
def test_large_straight(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([2, 1, 5, 4, 3])
self.assertTrue(h.is_large_straight())
h._force_new([2, 1, 6, 4, 3])
self.assertFalse(h.is_large_straight())

def test_small_straight(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([2, 1, 5, 4, 3])
self.assertTrue(h.is_small_straight())
h._force_new([2, 1, 6, 4, 3])
self.assertTrue(h.is_small_straight())
h._force_new([1, 2, 3, 5, 6])
self.assertFalse(h.is_small_straight())
h._force_new([1, 2, 3, 3, 4])
self.assertTrue(h.is_small_straight())

def test_full_house(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([1, 1, 5, 5, 5])
self.assertTrue(h.is_full_house())
h._force_new([2, 3, 2, 3, 2])
self.assertTrue(h.is_full_house())
h._force_new([1, 2, 3, 5, 6])
self.assertFalse(h.is_full_house())
h._force_new([1, 1, 3, 3, 4])
self.assertFalse(h.is_full_house())

def test_is_four_of_a_kind(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([1, 5, 5, 5, 5])
self.assertTrue(h.is_four_of_a_kind())
h._force_new([1, 1, 5, 5, 5])
self.assertFalse(h.is_four_of_a_kind())
h._force_new([5, 1, 5, 5, 5])
self.assertTrue(h.is_four_of_a_kind())

def test_is_triple(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([1, 5, 5, 5, 5])
self.assertTrue(h.is_triple())
h._force_new([5, 1, 1, 5, 5])
self.assertTrue(h.is_triple())
h._force_new([5, 1, 5, 2, 3])
self.assertFalse(h.is_triple())

def test_is_yahtzee(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([5, 5, 5, 5, 5])
self.assertTrue(h.is_yahtzee())
h._force_new([5, 1, 5, 5, 5])
self.assertFalse(h.is_yahtzee())
h._force_new([5, 1, 5, 2, 3])
self.assertFalse(h.is_yahtzee())

def test_get_score_for_number(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([1, 1, 2, 3, 2])
self.assertEqual(2, h.get_score_for_number(1))
self.assertEqual(0, h.get_score_for_number(5))
self.assertEqual(4, h.get_score_for_number(2))

def test_get_hand_total_score(self):
h = YahtzeeHand(seed=TEST_SEED)
h._force_new([1, 1, 2, 3, 2])
self.assertEqual(9, h.get_hand_total_score())```

Now I think I’ve got what I need to put together a simple scoring mechanism… and that will get me close to the point where I can actually write a game!

Scroll to Top