完善聊天功能 初步构建十子棋功能

This commit is contained in:
2024-10-15 22:03:57 +08:00
parent b3cf948fd2
commit 2f8d833006
3 changed files with 57 additions and 38 deletions
+53 -34
View File
@@ -1,9 +1,10 @@
package views package views
import ( import (
"errors" "fmt"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"
"slices" "slices"
@@ -12,22 +13,17 @@ import (
func TenChinaGameView() { func TenChinaGameView() {
myApp := fyne.CurrentApp() myApp := fyne.CurrentApp()
myWindow := myApp.NewWindow("Cross Flag Game") myWindow := myApp.NewWindow("Game")
gridWrap := container.NewGridWrap(fyne.NewSize(100, 100)) gridWrap := container.NewGridWrap(fyne.NewSize(100, 100))
game := NewTenGame(2, func(i int) {
for i := 0; i < 9; i++ { fmt.Println("win", i)
//var itemIndex = i dialog.ShowInformation("游戏结束", fmt.Sprintf("%d 获得胜利", i), myWindow)
var item = widget.NewButton("", func() {}) //myWindow.Close()
if i > 6 { })
item.SetIcon(theme.ConfirmIcon()) for _, i := range game.Items {
} else { gridWrap.Add(container.NewBorder(nil, nil, nil, nil, i))
item.SetIcon(theme.CancelIcon())
}
gridWrap.Add(container.NewBorder(nil, nil, nil, nil, item))
} }
myWindow.SetContent(container.NewScroll(gridWrap)) myWindow.SetContent(container.NewScroll(gridWrap))
myWindow.Resize(fyne.NewSize(320, 318)) myWindow.Resize(fyne.NewSize(320, 318))
myWindow.CenterOnScreen() myWindow.CenterOnScreen()
myWindow.Show() myWindow.Show()
@@ -38,16 +34,37 @@ type TenGame struct {
playerMax int playerMax int
CurrentRoundPlayer int CurrentRoundPlayer int
players [][]int players [][]int
items []*widget.Button Items []*widget.Button
winCallback func(int) winCallback func(int)
} }
func NewTenGame(playerMax int, winCallback func(int)) *TenGame {
game := &TenGame{
playerMax: playerMax,
CurrentRoundPlayer: 0,
players: make([][]int, playerMax),
winCallback: winCallback,
}
var items = make([]*widget.Button, 9)
for i := 0; i < 9; i++ {
var itemIndex = i
var item = widget.NewButton("", func() {
game.Play(0, itemIndex)
})
item.SetIcon(theme.FyneLogo())
items[i] = item
}
game.Items = items
return game
}
func (t *TenGame) Play(userIndex int, pos int) error { func (t *TenGame) Play(userIndex int, pos int) error {
t.lock.Lock() t.lock.Lock()
defer t.lock.Unlock() defer t.lock.Unlock()
if t.CurrentRoundPlayer != userIndex { //if t.CurrentRoundPlayer != userIndex {
return errors.New("not your turn") // return errors.New("not your turn")
} //}
t.play(pos) t.play(pos)
t.CurrentRoundPlayer++ t.CurrentRoundPlayer++
@@ -68,39 +85,41 @@ func (t *TenGame) play(pos int) {
playerIntList = append(playerIntList, pos) playerIntList = append(playerIntList, pos)
// 刷新数据 // 刷新数据
if t.CurrentRoundPlayer == 1 { if t.CurrentRoundPlayer == 1 {
t.items[pos].SetIcon(theme.ConfirmIcon()) t.Items[pos].SetIcon(theme.ConfirmIcon())
} else { } else {
t.items[pos].SetIcon(theme.CancelIcon()) t.Items[pos].SetIcon(theme.CancelIcon())
} }
// check 胜利 判断 // check 胜利 判断
slices.Sort(playerIntList) slices.Sort(playerIntList)
t.players[t.CurrentRoundPlayer] = playerIntList
if t.winCheck(playerIntList) {
t.winCallback(t.CurrentRoundPlayer)
}
} }
func (t *TenGame) winCheck(playerIntList []int) bool { func (t *TenGame) winCheck(playerIntList []int) bool {
for _, i := range playerIntList { fmt.Println("playerIntList:", playerIntList)
var iI = i if isArithmeticSequence(playerIntList, 1) ||
if t.checkLine(playerIntList, iI, 1) || isArithmeticSequence(playerIntList, 2) ||
t.checkLine(playerIntList, iI, 2) || isArithmeticSequence(playerIntList, 3) ||
t.checkLine(playerIntList, iI, 4) { isArithmeticSequence(playerIntList, 4) {
return true return true
} }
// todo slices.Delete()
}
return false return false
} }
func (t *TenGame) checkLine(playerIntList []int, iI, add int) bool { func isArithmeticSequence(numbers []int, diff int) bool {
for { if len(numbers) <= 2 {
iI += add return false
if iI > 9 {
return true
} }
if !slices.Contains(playerIntList, iI) {
for i := 1; i < len(numbers); i++ {
if numbers[i]-numbers[i-1] != diff {
return false return false
} }
} }
return true
} }
+1 -1
View File
@@ -14,7 +14,7 @@ import (
// 卡片展示界面 // 卡片展示界面
func allCardsViews(w fyne.Window) fyne.CanvasObject { func allCardsViews(w fyne.Window) fyne.CanvasObject {
//var items []fyne.CanvasObject //var Items []fyne.CanvasObject
gridWrap := container.NewGridWrap(fyne.NewSize(200, 200)) gridWrap := container.NewGridWrap(fyne.NewSize(200, 200))
for _, baseCard := range repo.BaseCard.FindAll(global.DB) { for _, baseCard := range repo.BaseCard.FindAll(global.DB) {
if info, is := cardTypeMap[baseCard.ToolType]; is { if info, is := cardTypeMap[baseCard.ToolType]; is {
+1 -1
View File
@@ -22,7 +22,7 @@ import (
) )
func mainUserViews(w fyne.Window) fyne.CanvasObject { func mainUserViews(w fyne.Window) fyne.CanvasObject {
//TenChinaGameView() TenChinaGameView()
var userCard = widget.NewCard("", "", nil) var userCard = widget.NewCard("", "", nil)
user1 := repo.User.GetUserInfo(global.DB) user1 := repo.User.GetUserInfo(global.DB)
refresh := func(user *models.Users) { refresh := func(user *models.Users) {