Skip to content

Commit 405ec74

Browse files
committed
hotfix: fix bug when terminal-game called CurrentPlayer when game wasn't running and caused panic
1 parent 43c652f commit 405ec74

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

internal/terminalgame/game/game.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/gucio321/go-clear"
1010
"github.com/gucio321/terminalmenu/pkg/menuutils"
1111

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

45-
t.Game.Result(func(l letter.Letter) {
44+
t.Game.Result(func(p *player.Player) {
4645
// handle game end
47-
switch l {
48-
case letter.LetterNone:
46+
switch p {
47+
case nil:
4948
fmt.Println("DRAW")
5049
default:
51-
fmt.Println(t.CurrentPlayer().Name() + " won")
50+
fmt.Println(p.Name() + " won")
5251
}
5352

5453
if err := menuutils.PromptEnter("Press ENTER to continue "); err != nil {

pkg/game/game.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Game struct {
3232
isRunning bool
3333

3434
onContinue func()
35-
resultCB func(letter.Letter)
35+
resultCB func(*player.Player)
3636
userActionCB func() int
3737
}
3838

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

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

107107
return g
@@ -150,13 +150,13 @@ func (g *Game) Run() {
150150
if ok, _ := g.Board().IsWinner(g.players.Current().Letter()); ok {
151151
g.onContinue()
152152
g.isRunning = false
153-
g.resultCB(g.players.Current().Letter())
153+
g.resultCB(g.players.Current())
154154

155155
return
156156
} else if g.Board().IsBoardFull() {
157157
g.onContinue()
158158
g.isRunning = false
159-
g.resultCB(letter.LetterNone)
159+
g.resultCB(nil)
160160

161161
return
162162
}

pkg/giuwidget/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"github.com/AllenDang/giu"
55

66
"github.com/gucio321/tic-tac-go/pkg/core/board"
7-
"github.com/gucio321/tic-tac-go/pkg/core/board/letter"
7+
"github.com/gucio321/tic-tac-go/pkg/core/players/player"
88
"github.com/gucio321/tic-tac-go/pkg/game"
99
)
1010

@@ -56,7 +56,7 @@ func (g *GameWidget) newState() *gameState {
5656
buttonClick: make(chan int),
5757
}
5858

59-
state.game.Result(func(l letter.Letter) {
59+
state.game.Result(func(p *player.Player) {
6060
_, state.winningCombo = state.currentBoard.GetWinner()
6161
state.gameEnded = true
6262
})

0 commit comments

Comments
 (0)