replaced the textLabel with a popup
This commit is contained in:
parent
b0b04d23b1
commit
ae3dfe9e10
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/layout"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
@ -24,14 +25,14 @@ const (
|
|||
var gridContainer *fyne.Container
|
||||
|
||||
// creates the button grid for the tictactoe game
|
||||
func MakeButtonGrid(rows, cols int) *fyne.Container {
|
||||
func MakeButtonGrid(rows, cols int, Window fyne.Window) *fyne.Container {
|
||||
// generates layout and grid container
|
||||
gridLayout := layout.NewGridLayout(cols)
|
||||
gridContainer = container.New(gridLayout)
|
||||
// create the 3x3 Button grid
|
||||
for row := 0; row < rows; row++ {
|
||||
for col := 0; col < cols; col++ {
|
||||
button := CreateButton(row, col)
|
||||
button := CreateButton(row, col, Window)
|
||||
gridContainer.Add(button)
|
||||
}
|
||||
}
|
||||
|
@ -40,13 +41,13 @@ func MakeButtonGrid(rows, cols int) *fyne.Container {
|
|||
}
|
||||
|
||||
// create the button grid and adds key press handler for each grid-button
|
||||
func CreateButton(row, col int) *widget.Button {
|
||||
func CreateButton(row, col int, Window fyne.Window) *widget.Button {
|
||||
button := widget.NewButton(" ", nil)
|
||||
|
||||
button.OnTapped = func() {
|
||||
if button.Text == " " {
|
||||
button.SetText(PLAYER_X)
|
||||
Gameloop(row, col)
|
||||
Gameloop(row, col, Window)
|
||||
}
|
||||
}
|
||||
return button
|
||||
|
@ -65,7 +66,6 @@ func ResetButtonTexts(container *fyne.Container) {
|
|||
{" ", " ", " "},
|
||||
}
|
||||
EnableAllButtons(gridContainer)
|
||||
TextLabel.SetText("TicTacToe")
|
||||
}
|
||||
|
||||
// apply AI opponent text to the button
|
||||
|
@ -75,16 +75,17 @@ func ChangeButtonText(row, col int) {
|
|||
|
||||
// gameloop function for the game process:
|
||||
// human makes a move, AI counters
|
||||
func Gameloop(row, col int) {
|
||||
func Gameloop(row, col int, Window fyne.Window) {
|
||||
const gameOverText string = "Game Over"
|
||||
// starting with the human making a move
|
||||
board[row][col] = PLAYER_X
|
||||
// checking if the game has been won by the human player
|
||||
if gameControl.PlayerHasWon(board, PLAYER_X) {
|
||||
TextLabel.SetText("X Wins")
|
||||
dialog.ShowInformation(gameOverText, "X has won!", Window)
|
||||
}
|
||||
// checking if the game is a draw
|
||||
if gameControl.BoardisFull(board) {
|
||||
TextLabel.SetText("Draw")
|
||||
dialog.ShowInformation(gameOverText, "It's a draw!", Window)
|
||||
}
|
||||
// AI opponent makes a move
|
||||
bestMove := opponent.Minimax(board, 0, true, PLAYER_O)
|
||||
|
@ -94,12 +95,12 @@ func Gameloop(row, col int) {
|
|||
|
||||
// check if AI opponent has won the game
|
||||
if gameControl.PlayerHasWon(board, PLAYER_O) {
|
||||
TextLabel.SetText("O Wins")
|
||||
dialog.ShowInformation(gameOverText, "O has won!", Window)
|
||||
DisableAllButtons(gridContainer)
|
||||
}
|
||||
// checking if the game is a draw
|
||||
if gameControl.BoardisFull(board) {
|
||||
TextLabel.SetText("Draw")
|
||||
dialog.ShowInformation(gameOverText, "It's a draw!", Window)
|
||||
DisableAllButtons(gridContainer)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,8 +10,6 @@ import (
|
|||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
||||
var TextLabel *widget.Label
|
||||
|
||||
// creates a window for the TicTacToe game application.
|
||||
func CreateWindow() fyne.Window {
|
||||
// Create the app.
|
||||
|
@ -22,17 +20,15 @@ func CreateWindow() fyne.Window {
|
|||
// Create a new window for the game application.
|
||||
Window := App.NewWindow("TicTacToe")
|
||||
|
||||
// Set the text of our TextLabel to TicTacToe
|
||||
TextLabel = widget.NewLabel("TicTacToe")
|
||||
// create the 3x3 Button grid
|
||||
matchField := MakeButtonGrid(3, 3)
|
||||
matchField := MakeButtonGrid(3, 3, Window)
|
||||
// Create a new button and a handler function, if it gets pressed.
|
||||
restartButton := widget.NewButton("Restart", func() {
|
||||
ResetButtonTexts(matchField)
|
||||
})
|
||||
|
||||
// vertical box layout containg the top Items, here the textlabel and the button.
|
||||
topItems := container.NewVBox(TextLabel, restartButton)
|
||||
topItems := container.NewVBox(restartButton)
|
||||
|
||||
// Generate content for the window using a border layout with the topItems and matchField as the children
|
||||
content := container.New(layout.NewBorderLayout(topItems, nil, nil, nil), topItems, matchField)
|
||||
|
|
Loading…
Reference in New Issue