diff --git a/exercise-009-rock/src/rock/game.go b/exercise-009-rock/src/rock/game.go index d6a4e87..3034d9d 100644 --- a/exercise-009-rock/src/rock/game.go +++ b/exercise-009-rock/src/rock/game.go @@ -20,8 +20,8 @@ type Game struct { } // Add adds a player to the game -func (g *Game) Add(p *Player) { - g.players = append(g.players, p) +func (g *Game) Add(p Player) { + g.players = append(g.players, &p) g.points = append(g.points, 0) } @@ -43,8 +43,8 @@ func (g *Game) RoundRobin() { b := g.players[j] // Determine Winner - moveA := a.Play() - moveB := b.Play() + moveA := (*a).Play() + moveB := (*b).Play() winner := Winner(moveA, moveB) // Update Scores @@ -63,6 +63,6 @@ func (g *Game) Display() { // For Each Player for i, p := range g.players { - fmt.Printf("%-15s %5d\n", p.Type(), g.points[i]) + fmt.Printf("%-15s %5d\n", (*p).Type(), g.points[i]) } } diff --git a/exercise-009-rock/src/rock/player.go b/exercise-009-rock/src/rock/player.go index 6657f48..4a270ab 100644 --- a/exercise-009-rock/src/rock/player.go +++ b/exercise-009-rock/src/rock/player.go @@ -4,17 +4,83 @@ import ( "math/rand" ) -// Player ... -type Player struct { +// Player interface +type Player interface { + Type() string + Play() int } -// Type returns the type of the player -func (p *Player) Type() string { +// RandoRex player type +type RandoRex struct { +} + +// Type method for RandoRex +func (p *RandoRex) Type() string { return "RandoRex" } -// Play returns a move -func (p *Player) Play() int { +// Play method for RandoRex +func (p *RandoRex) Play() int { choice := rand.Int() % 3 return choice } + +// Obsessed player type +type Obsessed struct { + move int +} + +// Type method for Obsessed +func (p *Obsessed) Type() string { + return "Obsessed" +} + +// Play method for Obsesses +func (p *Obsessed) Play() int { + return p.move +} + +// Flipper player type +type Flipper struct { + move1 int + move2 int +} + +// Type method for Flipper +func (p *Flipper) Type() string { + return "Flipper" +} + +// Play method for Flipper +func (p *Flipper) Play() int { + + // randomly select one of the two fixed moves + decision := rand.Int() % 2 + + var choice int + switch decision { + case 0: + choice = p.move1 + case 1: + choice = p.move2 + } + return choice +} + +// Cyclone player type +type Cyclone struct { + currentMove int // cycles through 0-2 +} + +// Type method for Cyclone +func (p *Cyclone) Type() string { + return "Cyclone" +} + +// Play method for Cyclone +func (p *Cyclone) Play() int { + p.currentMove++ + p.currentMove %= 3 + + return p.currentMove +} diff --git a/exercise-009-rock/src/rock/rock.go b/exercise-009-rock/src/rock/rock.go index ff1ccfb..8dc93a0 100644 --- a/exercise-009-rock/src/rock/rock.go +++ b/exercise-009-rock/src/rock/rock.go @@ -6,9 +6,13 @@ func main() { game := &Game{} // Add Players - game.Add(&Player{}) - game.Add(&Player{}) - game.Add(&Player{}) + game.Add(&RandoRex{}) + 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++ {