Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

009-v1 #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions exercise-009-rock/src/rock/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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])
}
}
78 changes: 72 additions & 6 deletions exercise-009-rock/src/rock/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 7 additions & 3 deletions exercise-009-rock/src/rock/rock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down