Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 19e878c

Browse files
committed
use Strategy type in place of Bool
1 parent 96d7e11 commit 19e878c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Gym/Blackjack/main.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ let environment = gym.make("Blackjack-v0")
2121
let totalIterations = 10000
2222
let learningPhase = totalIterations * 5 / 100
2323

24+
typealias Strategy = Bool
25+
2426
class BlackjackState {
2527
var playerSum: Int = 0
2628
var dealerCard: Int = 0
@@ -69,7 +71,7 @@ class Solver {
6971
Q[prior.playerSum][prior.dealerCard][prior.useableAce][action] += priorQ + postQ
7072
}
7173

72-
func qLearningStrategy(observation: BlackjackState, iteration: Int) -> Bool {
74+
func qLearningStrategy(observation: BlackjackState, iteration: Int) -> Strategy {
7375
let hitReward = Q[observation.playerSum][observation.dealerCard][observation.useableAce][0]
7476
let stayReward = Q[observation.playerSum][observation.dealerCard][observation.useableAce][1]
7577

@@ -87,11 +89,11 @@ class Solver {
8789
}
8890
}
8991

90-
func randomStrategy() -> Bool {
91-
return Bool.random()
92+
func randomStrategy() -> Strategy {
93+
return Strategy.random()
9294
}
9395

94-
func markovStrategy(observation: BlackjackState) -> Bool {
96+
func markovStrategy(observation: BlackjackState) -> Strategy {
9597
// hit @ 80% probability unless over 18, in which case do the reverse
9698
let flip = Float.random(in: 0..<1)
9799
let threshHold: Float = 0.8
@@ -122,15 +124,15 @@ class Solver {
122124
}
123125
}
124126

125-
func normalStrategy(observation: BlackjackState) -> Bool {
127+
func normalStrategy(observation: BlackjackState) -> Strategy {
126128
if observation.playerSum == 0 {
127129
return true
128130
}
129131
let lookupString = normalStrategyLookup(playerSum: observation.playerSum)
130132
return Array(lookupString)[observation.dealerCard - 1] == "H"
131133
}
132134

133-
func strategy(observation: BlackjackState, solver: SolverType, iteration: Int) -> Bool {
135+
func strategy(observation: BlackjackState, solver: SolverType, iteration: Int) -> Strategy {
134136
switch solver {
135137
case .random:
136138
return randomStrategy()

0 commit comments

Comments
 (0)