diff --git a/exercise-009-rock/src/rock/game.go b/exercise-009-rock/src/rock/game.go index d6a4e87..1822200 100644 --- a/exercise-009-rock/src/rock/game.go +++ b/exercise-009-rock/src/rock/game.go @@ -15,12 +15,12 @@ const ( // Game ... type Game struct { - players []*Player + players []Player points []int } // Add adds a player to the game -func (g *Game) Add(p *Player) { +func (g *Game) Add(p Player) { g.players = append(g.players, p) g.points = append(g.points, 0) } @@ -33,7 +33,7 @@ func (g *Game) RoundRobin() { n := len(g.players) - // For Each Player + // For Each RandoRex for i := 0; i < n-1; i++ { // For Each Opponent diff --git a/exercise-009-rock/src/rock/player.go b/exercise-009-rock/src/rock/player.go index 6657f48..a70da8a 100644 --- a/exercise-009-rock/src/rock/player.go +++ b/exercise-009-rock/src/rock/player.go @@ -4,17 +4,65 @@ import ( "math/rand" ) -// Player ... -type Player struct { +type Player interface { + Type() string + Play() int +} + +type Flipper struct { + MoveA int + MoveB int +} + +func (p Flipper) Type() string { + return "Flipper" +} + +func (p Flipper) Play() int { + moves := []int{p.MoveA,p.MoveB} + return moves[rand.Int() % len(moves)] +} + +type Obsessed struct { + Move int +} + +func (p Obsessed) Type() string { + return "Obsessed" +} + +func (p Obsessed) Play() int { + return p.Move +} + +type Cyclone struct { + MoveCount int +} + +func (p Cyclone) Type() string{ + return "Cyclone" +} + +func (p Cyclone) Play() int { + p.MoveCount++ + moves := []int{Rock,Paper,Scissors} + return moves[p.MoveCount % len(moves)] + +} + + + +// RandoRex ... +type RandoRex struct { } // Type returns the type of the player -func (p *Player) Type() string { +func (p RandoRex) Type() string { return "RandoRex" } // Play returns a move -func (p *Player) Play() int { +func (p RandoRex) Play() int { choice := rand.Int() % 3 return choice } diff --git a/exercise-009-rock/src/rock/rock.go b/exercise-009-rock/src/rock/rock.go index ff1ccfb..7243252 100644 --- a/exercise-009-rock/src/rock/rock.go +++ b/exercise-009-rock/src/rock/rock.go @@ -6,9 +6,12 @@ func main() { game := &Game{} // Add Players - game.Add(&Player{}) - game.Add(&Player{}) - game.Add(&Player{}) + game.Add(&RandoRex{}) + game.Add(&RandoRex{}) + game.Add(&Flipper{Rock,Paper}) + game.Add(&Obsessed{Paper}) + game.Add(&Obsessed{Scissors}) + game.Add(&Cyclone{}) // A Thousand Round-Robins! for i := 0; i < 1000; i++ {