Skip to content

Commit

Permalink
hotfix: fix bug when terminal-game called CurrentPlayer when game was…
Browse files Browse the repository at this point in the history
…n't running and caused panic
  • Loading branch information
gucio321 committed Mar 30, 2022
1 parent 43c652f commit 405ec74
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
9 changes: 4 additions & 5 deletions internal/terminalgame/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/gucio321/go-clear"
"github.com/gucio321/terminalmenu/pkg/menuutils"

"github.com/gucio321/tic-tac-go/pkg/core/board/letter"
"github.com/gucio321/tic-tac-go/pkg/core/players/player"
"github.com/gucio321/tic-tac-go/pkg/game"
)
Expand Down Expand Up @@ -42,13 +41,13 @@ func NewTTG(w, h, chainLen byte, player1Type, player2Type player.Type) *TTG {
func (t *TTG) Run() {
endGame := make(chan bool, 1)

t.Game.Result(func(l letter.Letter) {
t.Game.Result(func(p *player.Player) {
// handle game end
switch l {
case letter.LetterNone:
switch p {
case nil:
fmt.Println("DRAW")
default:
fmt.Println(t.CurrentPlayer().Name() + " won")
fmt.Println(p.Name() + " won")
}

if err := menuutils.PromptEnter("Press ENTER to continue "); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Game struct {
isRunning bool

onContinue func()
resultCB func(letter.Letter)
resultCB func(*player.Player)
userActionCB func() int
}

Expand All @@ -42,7 +42,7 @@ func Create(p1type, p2type player.Type) *Game {
isRunning: false,
board: board.Create(defaultBoardW, defaultBoardH, defaultChainLen),
onContinue: func() {},
resultCB: func(letter.Letter) {},
resultCB: func(*player.Player) {},
userActionCB: func() int {
panic("Tic-Tac-Go: game.(*Game): user action callback is not set!")
},
Expand Down Expand Up @@ -101,7 +101,7 @@ func (g *Game) UserAction(cb func() int) {

// Result returns true if game is ended. in addition, it returns its result.
// if LetterNone returned - it means that DRAW reached.
func (g *Game) Result(resultCB func(letter.Letter)) *Game {
func (g *Game) Result(resultCB func(*player.Player)) *Game {
g.resultCB = resultCB

return g
Expand Down Expand Up @@ -150,13 +150,13 @@ func (g *Game) Run() {
if ok, _ := g.Board().IsWinner(g.players.Current().Letter()); ok {
g.onContinue()
g.isRunning = false
g.resultCB(g.players.Current().Letter())
g.resultCB(g.players.Current())

return
} else if g.Board().IsBoardFull() {
g.onContinue()
g.isRunning = false
g.resultCB(letter.LetterNone)
g.resultCB(nil)

return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/giuwidget/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/AllenDang/giu"

"github.com/gucio321/tic-tac-go/pkg/core/board"
"github.com/gucio321/tic-tac-go/pkg/core/board/letter"
"github.com/gucio321/tic-tac-go/pkg/core/players/player"
"github.com/gucio321/tic-tac-go/pkg/game"
)

Expand Down Expand Up @@ -56,7 +56,7 @@ func (g *GameWidget) newState() *gameState {
buttonClick: make(chan int),
}

state.game.Result(func(l letter.Letter) {
state.game.Result(func(p *player.Player) {
_, state.winningCombo = state.currentBoard.GetWinner()
state.gameEnded = true
})
Expand Down

0 comments on commit 405ec74

Please sign in to comment.