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

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