aboutsummaryrefslogtreecommitdiff
path: root/challenge-052/user-person/python/ch-2.py
blob: da402bb7db0484e8e085c6e3b0b9422ea51badd4 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3

###########################################################################
# script name: ch-2.py                                                    #
#                                                                         #
# https://github.com/user-person                                          #
#                                                                         #
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/         #
#                                                                         #
# Lucky Winner                                                            #
# Suppose there are following coins arranged on a table in a line in      #
# random order                                                            #
#                                                                         #
# 1, 50p, 1p, 10p, 5p, 2, 2p                                              #
#                                                                         #
# Suppose you are playing against the computer. Player can only pick one  #
# coin at a time from either ends. Find out the lucky winner, who has     #
# the larger amounts in total?20p, order.                                 #
#                                                                         #
###########################################################################

# Coins total 3.88. 2 > 1.88 . If a player gets the L2 coin they win the game.

# Given sufficiently intelligent players, whoever gets the first turn wins the game.

import random
import readline

coins = [ '1p', '2p', '5p', '10p', '20p', '50p', 'L1', 'L2' ]

random.shuffle(coins)

coinVal = { '1p' : 0.01, '2p' : 0.02, '5p' : 0.05, '10p' : 0.10, \
                '20p' : 0.20, '50p' : 0.50, 'L1' : 1.00, 'L2' : 2.00 }

bank = { 'player' : 0, 'computer' : 0 }

turn = random.choice([True,False])

def l2Index():
    ret = -1
    for i in range(len(coins)-1):
        if coins[i] == 'L2':
           ret = i
           break
    return ret

def takeCoin(choice,who):
    if choice == 'f':
        bank[who] += coinVal[ coins.pop(0) ]
    else:                                    # "If you ain't first, you're last."
        bank[who] += coinVal[ coins.pop(-1) ]

    print(who + ': ' + '{:.2f}'.format(bank[who]) + '\n')

def playerChoice():
    fl = ''
    loop = True

    prompt = "Type 'f' to choose the first coin. Type 'l' to choose the last coin. Type 'q' to quit:" ;
    if len(coins) > 1:
        print(prompt)

    while loop:
        
        loop = False
        
        if len(coins) == 1:     # Don't ask when there's only one choice.
            takeCoin( 'f', 'player')
            continue

        fl = input('> ')

        if fl == 'f' or fl == 'l':
            takeCoin( fl, 'player')
        elif fl == 'q':
            exit()
        else:
            print('Invalid choice')
            loop = True

def chooseGreater():
    if coinVal[ coins[0] ] > coinVal[ coins[-1] ] :
        takeCoin('f','computer')
    else:
        takeCoin('l','computer')

def computerChoice():
    
    # Grabs L2 off the end when available
    # Doesn't grab the item before L2 to free it up for player to win.
    # Otherwise, grabs whichever end is greater.
    # It doesn't always get the highest points, but it wins when that's possible.

    ind = l2Index()

    if len(coins) == 3:         # Without this statement computer always chooses last (third)
        chooseGreater()         # when protecting L2 ( e.g. [first], L2, [last] )
                                # even if first is greater.
    else:

        if ind == 0 or ind == len(coins)-2:
            takeCoin('f','computer')

        elif ind == len(coins)-1 or ind == 1:
            takeCoin('l','computer')

        else:
            chooseGreater()

while len(coins):
    for c in coins:
        print(c,end=' ')
    print()
    
    if turn:
        playerChoice()
        turn = False
    else:
        computerChoice()
        turn = True

if bank['computer'] > bank['player']:
    print('Computer Wins. Computer: L ' + '{:.2f}'.format(bank['computer']) \
              + ' Player: L ' + '{:.2f}'.format(bank['player']) + '\n')
else:
    print('Player Wins. Player: L ' + '{:.2f}'.format(bank['player']) \
                  + ' Computer: L ' + '{:.2f}'.format(bank['computer']) + '\n')