-
Notifications
You must be signed in to change notification settings - Fork 0
/
EatBest.py
100 lines (80 loc) · 4.33 KB
/
EatBest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import random
# parameters of this game
maxWeight = 250
turn = 3
# greeting
print('\n\n\tWelcome To BestEat!\n\n')
# collection of fruits having weight with calorie
fruitsCollection = {'A': {'weight': 100, 'calorie': 100}, 'B': {'weight': 50, 'calorie': 100},
'C': {'weight': 25, 'calorie': 75}, 'D': {'weight': 12, 'calorie': 36},
'E': {'weight': 100, 'calorie': 400}, 'F': {'weight': 50, 'calorie': 150},
'G': {'weight': 25, 'calorie': 50}, 'H': {'weight': 12, 'calorie': 12}}
# fruit name mapping with number
fruitNameNumberMapping = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H'}
# calorie weight ratio of fruits
calorieWeightRation = {}
fruits = fruitsCollection.keys()
for fruit in fruits:
calorieWeightRation[fruit] = fruitsCollection[fruit]['calorie'] / fruitsCollection[fruit]['weight']
# game playing
playerScore = 0
playerRemainingWeight = 250
computerScore = 0
computerRemainingWeight = 250
for i in range(turn):
playerFruitNumber = []
playerFruitCollection = {}
playerFruitNumber = random.sample(range(1, 8), 3)
computerFruitNumber = []
computerFruitCollection = {}
computerCalorieWeightRatio = [0] * 8
computerFruitNumber = random.sample(range(1, 8), 3)
# player's turn
for index in playerFruitNumber:
playerFruitCollection[fruitNameNumberMapping[index]] = {'weight': fruitsCollection[fruitNameNumberMapping[index]]['weight'],
'calorie': fruitsCollection[fruitNameNumberMapping[index]]['calorie']}
print('\t Player (Human)')
print('---------------------------')
print('Fruit Name' + '\t' + 'Weight' + '\t\t' + 'Calorie')
for index in playerFruitNumber:
print(fruitNameNumberMapping[index], '\t\t\t', playerFruitCollection[fruitNameNumberMapping[index]]['weight'],
'\t\t\t', playerFruitCollection[fruitNameNumberMapping[index]]['calorie'])
# choice handling of player
playerSelectionOptions = playerFruitCollection.keys()
playerChoise = input('Enter a fruit name: ')
playerChoise = playerChoise.capitalize()
if playerChoise in playerSelectionOptions and (playerRemainingWeight - fruitsCollection[playerChoise]['weight']) >= 0:
playerRemainingWeight -= fruitsCollection[playerChoise]['weight'];
playerScore += fruitsCollection[playerChoise]['calorie']
else:
print('\nPlayer\'s Remaining Weight Exceeded!\n')
# computer's turn
for index in computerFruitNumber:
computerFruitCollection[fruitNameNumberMapping[index]] = {'weight': fruitsCollection[fruitNameNumberMapping[index]]['weight'],
'calorie': fruitsCollection[fruitNameNumberMapping[index]]['calorie']}
computerCalorieWeightRatio[index] = calorieWeightRation[fruitNameNumberMapping[index]]
print('\n\t Computer (AI)')
print('---------------------------')
print('Fruit Name' + '\t' + 'Weight' + '\t\t' + 'Calorie')
for index in computerFruitNumber:
print(fruitNameNumberMapping[index], '\t\t\t', computerFruitCollection[fruitNameNumberMapping[index]]['weight'],
'\t\t\t', computerFruitCollection[fruitNameNumberMapping[index]]['calorie'])
# choice handling of computer
computerSelectionOptions = computerFruitCollection.keys()
maxCalorieIndex = computerCalorieWeightRatio.index(max(computerCalorieWeightRatio))
computerChoice = fruitNameNumberMapping[maxCalorieIndex]
if computerChoice in computerSelectionOptions and (computerRemainingWeight - fruitsCollection[computerChoice]['weight']) >= 0:
computerRemainingWeight -= fruitsCollection[computerChoice]['weight'];
computerScore += fruitsCollection[computerChoice]['calorie']
else:
print('\nComputer\'s Remaining Weight Exceeded!\n')
print('Player\'s Score: ', playerScore)
print('Player\'s Remaining Weight: ', playerRemainingWeight)
print('\nComputer\'s Score: ', computerScore)
print('Computer\'s Remaining Weight: ', computerRemainingWeight)
if playerScore > computerScore:
print('\n\nWinner - Player! Congrats!')
elif playerScore < computerScore:
print('\n\nWinner - Computer! Congrats!')
else:
print('\n\nScore Label! Match Draw')