Browse Source

Moved strategies to seperate package

master
Jeremy Kescher 7 months ago
parent
commit
a78854c62b
9 changed files with 53 additions and 47 deletions
  1. +7
    -5
      cmd/convict.go
  2. +12
    -13
      cmd/main.go
  3. +3
    -3
      cmd/strategies/experience.go
  4. +3
    -3
      cmd/strategies/fluke.go
  5. +3
    -3
      cmd/strategies/loyal.go
  6. +19
    -0
      cmd/strategies/strategy.go
  7. +3
    -3
      cmd/strategies/switch.go
  8. +3
    -3
      cmd/strategies/tattletale.go
  9. +0
    -14
      cmd/strategy.go

+ 7
- 5
cmd/convict.go View File

@ -1,17 +1,19 @@
package cmd
import strategies2 "git.kescher.at/jeremy.kescher/sim01-in-go/cmd/strategies"
type Convict struct {
strategy Strategy
strategy strategies2.Strategy
}
func (c *Convict) isConfessing() bool {
return isConfessing(&c.strategy)
return strategies2.IsConfessing(&c.strategy)
}
func (c *Convict) afterMath(otherConfessed bool) {
c.strategy.afterMath(otherConfessed)
c.strategy.AfterMath(otherConfessed)
}
func (c *Convict) getNewStrategyInstance() Strategy {
return c.strategy.getNewInstance()
func (c *Convict) getNewStrategyInstance() strategies2.Strategy {
return c.strategy.GetNewInstance()
}

+ 12
- 13
cmd/main.go View File

@ -1,6 +1,7 @@
package cmd
import (
"git.kescher.at/jeremy.kescher/sim01-in-go/cmd/strategies"
"math/rand"
"os"
"reflect"
@ -9,8 +10,8 @@ import (
)
var (
strategies = []string{"Experience", "Fluke", "Loyal", "Switch", "Tattletale"}
strategyCount = len(strategies)
strategyArr = []string{"Experience", "Fluke", "Loyal", "Switch", "Tattletale"}
strategyCount = len(strategyArr)
eachStrategyCount = 5000
convictCount = strategyCount * eachStrategyCount
roundsFactor = 5
@ -20,8 +21,6 @@ var (
// automatically called on import
func init() {
rand.Seed(time.Now().UnixNano())
switchChances[0] = 0.0
switchChances[1] = 1.0
parseAndApplyArgs()
}
@ -66,26 +65,26 @@ func parseAndApplyArgs() {
func convictList() []Convict {
var convicts []Convict
addCounts := make(map[string]int)
for _, strategy := range strategies {
for _, strategy := range strategyArr {
addCounts[strategy] = eachStrategyCount
}
keys := reflect.ValueOf(addCounts).MapKeys()
for len(convicts) < convictCount {
strategyString := keys[rand.Intn(len(keys))].String() //nolint:gosec
var strategy Strategy
var strategy strategies.Strategy
switch strategyString {
case "Experience":
strategy = NewExperience()
strategy = strategies.NewExperience()
case "Fluke":
strategy = NewFluke()
strategy = strategies.NewFluke()
case "Loyal":
strategy = NewLoyal()
strategy = strategies.NewLoyal()
case "Switch":
strategy = NewSwitch()
strategy = strategies.NewSwitch()
case "Tattletale":
strategy = NewTattletale()
strategy = strategies.NewTattletale()
}
count := addCounts[strategyString]
@ -113,7 +112,7 @@ func Main() {
}
statistics := make(map[string]int)
for _, strategy := range strategies {
for _, strategy := range strategyArr {
statistics[strategy] = 0
}
for _, convict := range convicts {
@ -122,6 +121,6 @@ func Main() {
pairList := rankByValue(statistics)
for _, pair := range pairList {
println(pair.k, "represents", pair.v, "convicts")
println(pair.k, "\b:", pair.v, "convicts")
}
}

cmd/experience.go → cmd/strategies/experience.go View File


cmd/fluke.go → cmd/strategies/fluke.go View File


cmd/loyal.go → cmd/strategies/loyal.go View File


+ 19
- 0
cmd/strategies/strategy.go View File

@ -0,0 +1,19 @@
package strategies
import "math/rand"
type Strategy interface {
probability() float64
AfterMath(otherConfessed bool)
GetNewInstance() Strategy
String() string
}
func IsConfessing(s *Strategy) bool {
return rand.Float64() < (*s).probability() //nolint:gosec
}
func init() {
switchChances[0] = 0.0
switchChances[1] = 1.0
}

cmd/switch.go → cmd/strategies/switch.go View File


cmd/tattletale.go → cmd/strategies/tattletale.go View File


+ 0
- 14
cmd/strategy.go View File

@ -1,14 +0,0 @@
package cmd
import "math/rand"
type Strategy interface {
probability() float64
afterMath(otherConfessed bool)
getNewInstance() Strategy
String() string
}
func isConfessing(s *Strategy) bool {
return rand.Float64() < (*s).probability() //nolint:gosec
}

Loading…
Cancel
Save