tictactoe-in-go/README.md

89 lines
5.2 KiB
Markdown
Raw Normal View History

# TICTACTOE in GO
#### Video Demo: <URL HERE>
#### Description:
2024-05-01 17:07:54 +00:00
This Tic-Tac-Toe game is a simple single-player experience where the player takes on a computer-controlled opponent. The goal of the game is to make a horizontal, vertical, or diagonal line of their chosen symbol (X or O) on a 3x3 grid. The game ends when one player successfully achieves three marks in a row or when all spaces are filled, resulting in a draw if neither player has achieved this condition.
## Prerequisites
`Go` programming language installed on your machine: To run the TicTacToe application, you need to have the Go programming language installed on your computer. You can download and install Go from the official website (https://golang.org/dl/). Follow the instructions for your specific operating system to set up the Go environment.
`Fyne` framework for building cross-platform GUI applications: The application uses the Fyne framework to create the graphical user interface (GUI) for the TicTacToe game. You can install the Fyne framework using the following command:
2024-05-02 15:29:42 +00:00
```bash
2024-05-01 17:07:54 +00:00
go get fyne.io/fyne/v2@latest
2024-05-02 15:29:42 +00:00
go install fyne.io/fyne/v2/cmd/fyne@latest
```
2024-05-01 17:07:54 +00:00
## How to run the application
1. Clone the repository:
Open your terminal and run the following command to clone the TicTacToe repository to your local machine:
```bash
git clone https://gitea.fabelous.app/fabel/TicTacToe.git`
2024-05-01 17:07:54 +00:00
cd tictactoe
```
2. Install dependencies:
- To ensure that all the required packages are installed, run the following command in your terminal within the project directory:
``` bash
go mod tidy
```
- This will download and install any missing dependencies specified in the go.mod file. 2. Run the application: Execute the main Go file to start the TicTacToe game application:
``` bash
go run main.go
```
- Build the executable: After installing the dependencies, you can build an executable file for your operating system using the go build command:
```bash
# Windows:
fyne package -os windows -icon ./resources/icon.png
# Linux:
fyne package -os linux -icon ./resources/icon.png # gives you a .tar.xz file
# Android
fyne package -os android -appID com.example.myapp -icon ./resources/icon.png
2024-05-01 17:07:54 +00:00
# MacOS:
fyne package -os macos -icon ./resources/icon.png # not tested
# IOS
fyne package -os ios -appID com.example.myapp -icon ./resources/icon.png # not tested
2024-05-01 17:07:54 +00:00
```
2024-05-01 17:07:54 +00:00
This will create an executable file named "tictactoe" in your project's directory that you can run to start the TicTacToe game application.
## Game Functionality
### The TicTacToe game application offers a range of features to create an enjoyable gaming experience for players:
- A graphical user interface (GUI) built using the Fyne framework, which allows for a visually appealing and responsive gameplay experience.
- 3x3 grid board: The game features a traditional TicTacToe grid consisting of nine spaces, where players take turns marking an empty space with their chosen symbol.
- Real-time gameplay: Players can interact with the game in real-time by clicking on the buttons representing the empty spaces on the grid.
- `AI opponent`: The application includes a simple `AI opponent` that plays the game using the minimax algorithm, providing a challenge for human players.
- Game over detection: The game detects when a player has won by making three marks in a row or if all spaces are filled without a winner, and updates the game status accordingly.
`Restart` button: A `Restart` button is available to start a new game after the previous game ends, allowing for continuous play without needing to restart the application.
## Code Structure
The TicTacToe application's code is organized into several Go files, each responsible for specific aspects of the application:
`main.go`:
This file serves as the main entry point for the application. It initializes the Fyne app and creates the game window.
`window.go`:
The `window.go` file is responsible for creating the main window of the Tic-Tac-Toe game using the Fyne library. This file defines a single function, `CreateWindow`, which sets up the application, creates a new window, and organizes the user interface elements. The result is returned as an instance of fyne.Window.
`minimax.go`:
This file contains the implementation of the minimax algorithm. The function minimax takes the current state of the game board, the depth of the algorithm, whether it is the maximizing player's turn or not, and alpha and beta values for alpha-beta pruning. It returns the bestNext Move for the AI Player.
`check.go`:
This file contains utility functions to check if a game state meets certain conditions. The function PlayerHasWon checks whether a player has won the game by having all their symbols in a row, column, or diagonal. The function BoardisFull checks if the game board is full and there are no more moves left.
`gameloop.go`:
`gameloop.go` contains the game loop and GUI logic for the Tic-Tac-Toe application. It uses the Fyne library to create a user interface with a grid of buttons representing the Tic-Tac-Toe board. The file includes functions to create the button grid, manage button interactions, update the game board, and handle player and computer moves. The main game loop function Gameloop handles these actions and ensures that the game progresses smoothly, alternating between the human player's move and the computer's move.