Skip to content

Commit 0fb2aa0

Browse files
committed
A little restructure, even when using minimax it should check the winner boxes at the end of the game correctly
1 parent 21a8f93 commit 0fb2aa0

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

entities/boxes.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ func EnableBoxes() bool {
5757
}
5858
for _, box := range boxes {
5959
box.disable = false
60+
box.WinStyle(false)
6061
}
6162
return true
6263
}
6364

64-
func ColorBoxes(boxes ...*BoxHolder) bool {
65+
func ColorBoxes(boxes []*BoxHolder) bool {
6566
if boxes == nil {
6667
return false
6768
}
6869
for _, box := range boxes {
69-
box.ChangeStyle()
70+
box.WinStyle(true)
7071
}
7172
return true
7273
}
@@ -75,7 +76,7 @@ func ColorBoxes(boxes ...*BoxHolder) bool {
7576
// each box has its own inputevents too.
7677
// The function returns if the end of the game reached and the winner.
7778

78-
func IsTerminal() (bool, int) {
79+
func IsTerminal() (bool, int, []*BoxHolder) {
7980
winPatterns := [8][3]int{
8081
{0, 1, 2},
8182
{3, 4, 5},
@@ -91,28 +92,33 @@ func IsTerminal() (bool, int) {
9192
if boxes[pattern[0]].content != ' ' &&
9293
boxes[pattern[0]].content == boxes[pattern[1]].content &&
9394
boxes[pattern[1]].content == boxes[pattern[2]].content {
94-
ColorBoxes(boxes[pattern[0]], boxes[pattern[1]], boxes[pattern[2]])
95-
// check if O
95+
96+
// winning boxes
97+
winners := []*BoxHolder{
98+
boxes[pattern[0]],
99+
boxes[pattern[1]],
100+
boxes[pattern[2]],
101+
}
102+
96103
if boxes[pattern[0]].content == 'O' {
97-
return true, 1
104+
return true, 1, winners
98105
}
99-
// check if X
100-
return true, -1
106+
return true, -1, winners
101107
}
102108
}
103109

104110
// check if there are empty boxes
105111
for i := range boxes {
106112
if boxes[i].content == ' ' {
107-
return false, 0
113+
return false, 0, nil
108114
}
109115
}
110116
// no winners - End of the game
111-
return true, 0
117+
return true, 0, nil
112118
}
113119

114120
func Minimax(isMaximizing bool) int {
115-
terminal, score := IsTerminal()
121+
terminal, score, _ := IsTerminal()
116122
if terminal {
117123
return score
118124
}

entities/boxholder.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func (b *BoxHolder) GetContent() rune {
4444
}
4545

4646
func CheckGame(gc *game.GameContext, b *BoxHolder) bool {
47-
status, win := IsTerminal()
47+
status, win, winners := IsTerminal()
48+
ColorBoxes(winners)
4849
gc.Logs.AddLine(fmt.Sprintf("Place: %c | Box: %d", b.content, b.ID))
4950
if status {
5051
if win != 0 {
@@ -164,11 +165,8 @@ func (b *BoxHolder) Update(gs *game.GameContext) {
164165
}
165166
}
166167

167-
func (b *BoxHolder) ChangeStyle() {
168-
if !b.visible {
169-
return
170-
}
171-
b.winnerBox = !b.winnerBox
168+
func (b *BoxHolder) WinStyle(def bool) {
169+
b.winnerBox = def
172170
}
173171

174172
func (b *BoxHolder) Draw(gs *game.GameContext) {

0 commit comments

Comments
 (0)