Skip to content

Instantly share code, notes, and snippets.

@Ropes
Last active December 22, 2015 19:29
Show Gist options
  • Save Ropes/6520052 to your computer and use it in GitHub Desktop.
Save Ropes/6520052 to your computer and use it in GitHub Desktop.
Bohnanza max score calculation. If I understand the game rules correctly, I think I've fixed the mistakes.
from __future__ import print_function, unicode_literals
bean_types = {
'coffee': (24, [4, 7, 10, 12]),
'wax': (22, [4, 7, 9, 11]),
'blue': (20, [4, 6, 8, 10]),
'chili': (18, [3, 6, 8, 9]),
'stink': (16, [3, 5, 7, 8]),
'green': (14, [3, 5, 6, 7]),
'soy': (12, [2, 4, 6, 7]),
'black-eyed': (10, [2, 4, 5, 6]),
'red': (8, [2,3,4,5]),
'garden': (6, [None, 2, 3, None]),
'cocoa': (4, [None, 2, 3, 4]),
}
def score(beans_left, score_list, points):
#print('{} {} {}'.format(beans_left, score_list, points))
if score_list == [] or beans_left == 0:
#print('total... {}'.format(points))
return points
elif score_list[-1] == None or beans_left < score_list[-1]:
return score(beans_left, score_list[:-1], points)
elif beans_left >= score_list[-1]:
return score(beans_left - len(score_list),\
score_list, points + len(score_list))
if __name__ == '__main__':
total_points = 0
for k,v in bean_types.items():
bean_points = score(v[0], v[1], 0)
total_points += bean_points
print('{}: {}'.format(k, bean_points))
#print('Points-to-beans: {}'.format(float(bean_points)/v[0]))
#print('Beans-to-points: {}\n'.format(v[0]/float(bean_points)))
print('Grand Total: {}'.format(total_points))
24 [4, 7, 10, 12] 0
20 [4, 7, 10, 12] 4
16 [4, 7, 10, 12] 8
12 [4, 7, 10, 12] 12
8 [4, 7, 10, 12] 16
8 [4, 7, 10] 16
8 [4, 7] 16
6 [4, 7] 18
6 [4] 18
5 [4] 19
4 [4] 20
3 [4] 21
3 [] 21
coffee: 21
6 [None, 2, 3, None] 0
6 [None, 2, 3] 0
3 [None, 2, 3] 3
0 [None, 2, 3] 6
garden: 6
16 [3, 5, 7, 8] 0
12 [3, 5, 7, 8] 4
8 [3, 5, 7, 8] 8
4 [3, 5, 7, 8] 12
4 [3, 5, 7] 12
4 [3, 5] 12
4 [3] 12
3 [3] 13
2 [3] 14
2 [] 14
stink: 14
4 [None, 2, 3, 4] 0
0 [None, 2, 3, 4] 4
cocoa: 4
12 [2, 4, 6, 7] 0
8 [2, 4, 6, 7] 4
4 [2, 4, 6, 7] 8
4 [2, 4, 6] 8
4 [2, 4] 8
2 [2, 4] 10
2 [2] 10
1 [2] 11
1 [] 11
soy: 11
18 [3, 6, 8, 9] 0
14 [3, 6, 8, 9] 4
10 [3, 6, 8, 9] 8
6 [3, 6, 8, 9] 12
6 [3, 6, 8] 12
6 [3, 6] 12
4 [3, 6] 14
4 [3] 14
3 [3] 15
2 [3] 16
2 [] 16
chili: 16
20 [4, 6, 8, 10] 0
16 [4, 6, 8, 10] 4
12 [4, 6, 8, 10] 8
8 [4, 6, 8, 10] 12
8 [4, 6, 8] 12
5 [4, 6, 8] 15
5 [4, 6] 15
5 [4] 15
4 [4] 16
3 [4] 17
3 [] 17
blue: 17
14 [3, 5, 6, 7] 0
10 [3, 5, 6, 7] 4
6 [3, 5, 6, 7] 8
6 [3, 5, 6] 8
3 [3, 5, 6] 11
3 [3, 5] 11
3 [3] 11
2 [3] 12
2 [] 12
green: 12
10 [2, 4, 5, 6] 0
6 [2, 4, 5, 6] 4
2 [2, 4, 5, 6] 8
2 [2, 4, 5] 8
2 [2, 4] 8
2 [2] 8
1 [2] 9
1 [] 9
black-eyed: 9
22 [4, 7, 9, 11] 0
18 [4, 7, 9, 11] 4
14 [4, 7, 9, 11] 8
10 [4, 7, 9, 11] 12
10 [4, 7, 9] 12
7 [4, 7, 9] 15
7 [4, 7] 15
5 [4, 7] 17
5 [4] 17
4 [4] 18
3 [4] 19
3 [] 19
wax: 19
8 [2, 3, 4, 5] 0
4 [2, 3, 4, 5] 4
4 [2, 3, 4] 4
1 [2, 3, 4] 7
1 [2, 3] 7
1 [2] 7
1 [] 7
red: 7
Grand Total: 136
from __future__ import print_function, unicode_literals
from unittest import TestCase
from bean_score import bean_types, score
class TestBeanScore(TestCase):
def test_red(self):
bean = bean_types['red']
self.assertEqual(score(bean[0], bean[1], 0), 7)
def test_chili(self):
bean = bean_types['chili']
self.assertEqual(score(bean[0], bean[1], 0), 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment