Urgent.News

What's breaking now, across hundreds of outlets.

AI

Tic Tac Toe in SwiftUI, Done Properly: Value-Type State, Win Detection and Minimax AI

The smallest game worth architecting: an immutable board, testable rules, a view that just renders state, and the classic AI you can bolt on in 40 lines

Tic Tac Toe in SwiftUI, Done Properly: Value-Type State, Win Detection and Minimax AI

Tic Tac Toe, a classic game often used to introduce programming concepts, has been implemented in SwiftUI with a focus on proper architecture. The implementation includes an immutable game board, testable rules, a view that only renders the game state, and a classic AI called minimax, which can be added with just 40 lines of code.

The game's architecture is divided into three main components: Game (struct), GameViewModel (SwiftUI view model), and GameView (SwiftUI view). The Game struct contains all the rules, such as whose turn it is, legal moves, the winner, and draw condition. The GameViewModel owns a Game instance, applies moves, triggers AI, and publishes the game state. The GameView renders the game state, sends taps, and does not contain any rules.

The Game struct uses value types to ensure immutability, making it easy to test, copy (undo is free), and hand copies to the AI for exploring future moves without altering the original game. The model is defined using enums for Player, Outcome, and lines, alongside a struct for Game that encapsulates all the game logic. The Game struct provides methods to play a move, undo a move, and check for legal moves and outcomes of the game.

Testing the rules is straightforward due to the pure nature of the model. The test cases cover various scenarios like row win, diagonal win for O, a draw, illegal moves, and undoing a move. These tests run quickly and independently, providing a safety net if new features are added later.

The GameView is a SwiftUI view that renders the game state as a grid and forwards taps to the view model. The view model is an ObservableObject that manages the game state and mode. It includes a published property for the game instance, which allows the view to react to changes in the game. The view model also handles the logic for the computer's move using the minimax algorithm when needed.

Overall, this implementation demonstrates a clean and maintainable approach to building a Tic Tac Toe game with SwiftUI, emphasizing the importance of proper architecture even for simple games.

Rewritten by urgent.news; may contain errors — check the original.

Source: hackernoon.com

Read the original at hackernoon.com →

Report a problem with this story

More in AI

Benedict Oramah: Africa Must Build Its Own AI Models, Not Just Supply the Data

Former President of Afreximbank, Dr. Benedict Oramah, has urged Africa to build its own artificial intelligence models rather than remain a supplier of data to foreign technology firms, stressing the

  • Africa must build its own AI models, not just supply data
  • Dr. Benedict Oramah emphasizes economic independence
  • Nigeria's cheap gas can power data centers and create tech parks

More from Saturday 24 October →