-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprog1.py
More file actions
36 lines (31 loc) · 960 Bytes
/
prog1.py
File metadata and controls
36 lines (31 loc) · 960 Bytes
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
import random
import numpy as np
# action = 0 <- players decides to stick
# action = 1 <- players decides to hit (draw)
class State:
dealercard = random.randint(1,10)
playersum = random.randint(1,21)
def drawcard(current):
if random.randint(1,3) < 3:
current += random.randint(1,10)
else:
current -= random.randint(1,10)
return current
def step(state, action):
if(action == 0): # player decides to stick
while(state.dealercard < 17):
state.dealercard = drawcard(state.dealercard)
if(state.dealercard > 21 or state.dealercard < 1):
return "terminal", +1.0
if state.dealercard == state.playersum:
return "terminal", 0.0
elif state.dealercard > state.playersum:
return "terminal", -1.0
else:
return "terminal", +1.0
else: # player decides to hit and draw another card
state.playersum = drawcard(state.playersum)
if state.playersum < 1 or state.playersum > 21:
return "terminal", -1.0
else:
return state, 0