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

EX 09 #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

EX 09 #154

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
4 changes: 2 additions & 2 deletions exercise-009-rock/src/rock/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
53 changes: 50 additions & 3 deletions exercise-009-rock/src/rock/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,63 @@ import (
)

// Player ...
type Player struct {
type Player interface {
Type() string
Play() int
}

type RandoRex struct {
}

// Type returns the type of the player
func (p *Player) Type() string {
func (r *RandoRex) Type() string {
return "RandoRex"
}

// Play returns a move
func (p *Player) Play() int {
func (r *RandoRex) Play() int {
choice := rand.Int() % 3
return choice
}

type Flipper struct {
move1 int
move2 int
}

func (f *Flipper) Type() string {
return "Flipper"
}

func (f *Flipper) Play() int {
moves := []int{f.move1, f.move2}
return moves[rand.Intn(2)]
}

type Obsessed struct {
move int
}

func (o *Obsessed) Type() string {
return "Obsessed"
}

func (o *Obsessed) Play() int {
return o.move
}

type Cyclone struct {
count int
}

func (c *Cyclone) Type() string {
return "RandoRex"
}

func (c *Cyclone) Play() int {
moves := []int{Rock, Paper, Scissors}
move := moves[c.count%3]
c.count++
return move

}
9 changes: 6 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,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++ {
Expand Down