Files
work_cation/views/gamesViews.go
T

107 lines
2.0 KiB
Go

package views
import (
"errors"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"slices"
"sync"
)
func TenChinaGameView() {
myApp := fyne.CurrentApp()
myWindow := myApp.NewWindow("Cross Flag Game")
gridWrap := container.NewGridWrap(fyne.NewSize(100, 100))
for i := 0; i < 9; i++ {
//var itemIndex = i
var item = widget.NewButton("", func() {})
if i > 6 {
item.SetIcon(theme.ConfirmIcon())
} else {
item.SetIcon(theme.CancelIcon())
}
gridWrap.Add(container.NewBorder(nil, nil, nil, nil, item))
}
myWindow.SetContent(container.NewScroll(gridWrap))
myWindow.Resize(fyne.NewSize(320, 318))
myWindow.CenterOnScreen()
myWindow.Show()
}
type TenGame struct {
lock sync.Mutex
playerMax int
CurrentRoundPlayer int
players [][]int
items []*widget.Button
winCallback func(int)
}
func (t *TenGame) Play(userIndex int, pos int) error {
t.lock.Lock()
defer t.lock.Unlock()
if t.CurrentRoundPlayer != userIndex {
return errors.New("not your turn")
}
t.play(pos)
t.CurrentRoundPlayer++
if t.CurrentRoundPlayer+1 > t.playerMax {
t.CurrentRoundPlayer = 0
}
return nil
}
/*
1, 2, 3
4, 5, 6
7, 8, 9
*/
func (t *TenGame) play(pos int) {
// TODO
playerIntList := t.players[t.CurrentRoundPlayer]
playerIntList = append(playerIntList, pos)
// 刷新数据
if t.CurrentRoundPlayer == 1 {
t.items[pos].SetIcon(theme.ConfirmIcon())
} else {
t.items[pos].SetIcon(theme.CancelIcon())
}
// check 胜利 判断
slices.Sort(playerIntList)
}
func (t *TenGame) winCheck(playerIntList []int) bool {
for _, i := range playerIntList {
var iI = i
if t.checkLine(playerIntList, iI, 1) ||
t.checkLine(playerIntList, iI, 2) ||
t.checkLine(playerIntList, iI, 4) {
return true
}
// todo slices.Delete()
}
return false
}
func (t *TenGame) checkLine(playerIntList []int, iI, add int) bool {
for {
iI += add
if iI > 9 {
return true
}
if !slices.Contains(playerIntList, iI) {
return false
}
}
}