Skip to content
Merged
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
18 changes: 9 additions & 9 deletions internal/game/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ func TestInOrderPlayer(t *testing.T) {
if !p.Play(game) {
t.Error("expected play to succeed on empty board")
}
if game.LastPlayedPosition != 1 {
t.Errorf("got position %d, want 1", game.LastPlayedPosition)
if game.LastPlayedPosition != 0 {
t.Errorf("got position %d, want 0", game.LastPlayedPosition)
}

if !p.Play(game) {
t.Error("expected play to succeed")
}
if game.LastPlayedPosition != 2 {
t.Errorf("got position %d, want 2", game.LastPlayedPosition)
if game.LastPlayedPosition != 1 {
t.Errorf("got position %d, want 1", game.LastPlayedPosition)
}
}

func TestInOrderPlayerFullBoard(t *testing.T) {
p := NewInOrderPlayer()
game := NewTicTacToe(3, 3, 3)
for i := 1; i <= 9; i++ {
for i := 0; i < 9; i++ {
game.Play(i)
}
if p.Play(game) {
Expand Down Expand Up @@ -148,8 +148,8 @@ func TestPlayerSetName(t *testing.T) {

func TestTicTacToeString(t *testing.T) {
game := NewTicTacToe(3, 3, 3)
game.Play(1) // X at 1
game.Play(5) // O at 5
game.Play(0) // X at 0
game.Play(4) // O at 4

s := game.String()
if !strings.Contains(s, "X") {
Expand All @@ -165,8 +165,8 @@ func TestTicTacToeString(t *testing.T) {

func TestTicTacToeBoardState(t *testing.T) {
game := NewTicTacToe(3, 3, 3)
game.Play(1) // X
game.Play(2) // O
game.Play(0) // X
game.Play(1) // O

state := game.BoardState()
if state[0] != 1 {
Expand Down
15 changes: 5 additions & 10 deletions internal/game/inorder_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ func NewInOrderPlayer() *InOrderPlayer {
}

func (p *InOrderPlayer) Play(game *TicTacToe) bool {
nextPosition := 0
for {
nextPosition++
v := game.ValueAt(nextPosition)
if v == CellInvalid {
return false
} else if v == CellEmpty {
break
for pos := 0; pos < game.NumRows*game.NumColumns; pos++ {
if game.ValueAt(pos) == CellEmpty {
game.Play(pos)
return true
}
}
game.Play(nextPosition)
return true
return false
}
18 changes: 9 additions & 9 deletions internal/game/menace_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@ func NewMenaceGame(game *TicTacToe) *MenaceGame {
func (mg *MenaceGame) SetOutcome(outcome GameOutcome) {
switch outcome {
case OutcomeWin:
mg.BoardOdds[mg.CurrentPosition-1] += 3
mg.BoardOdds[mg.CurrentPosition] += 3
mg.TotalOdds += 3
case OutcomeLose:
if mg.TotalOdds == 1 {
// Do not remove the last bead
} else {
mg.BoardOdds[mg.CurrentPosition-1] -= 1
mg.BoardOdds[mg.CurrentPosition] -= 1
mg.TotalOdds -= 1
}
case OutcomeDraw:
mg.BoardOdds[mg.CurrentPosition-1] += 1
mg.BoardOdds[mg.CurrentPosition] += 1
mg.TotalOdds += 1
}
}

func (mg *MenaceGame) PickCurrentPosition() int {
if mg.fullBoard() {
return 0
return -1
}
mg.CurrentPosition = mg.CalculatePosition(mg.RollDice())
return mg.CurrentPosition
}

func (mg *MenaceGame) RollDice() int {
if mg.fullBoard() {
return 0
return -1
}
return rand.Intn(mg.TotalOdds) + 1
}

func (mg *MenaceGame) CalculatePosition(diceRoll int) int {
if mg.fullBoard() {
return 0
return -1
}

for i := 0; i < len(mg.BoardOdds); i++ {
diceRoll -= mg.BoardOdds[i]
if diceRoll <= 0 {
return i + 1
return i
}
}
return 0
return -1
}

func (mg *MenaceGame) fullBoard() bool {
Expand All @@ -75,7 +75,7 @@ func (mg *MenaceGame) fullBoard() bool {
func (mg *MenaceGame) ResetOdds() {
mg.BoardOdds = make([]int, 9)
copy(mg.BoardOdds, initialBeads[:])
mg.CurrentPosition = 0
mg.CurrentPosition = -1
mg.updateOdds()
}

Expand Down
Loading