{
  "id": 16317085,
  "title": "Tic Tac Toe in SwiftUI, Done Properly: Value-Type State, Win Detection and Minimax AI",
  "url": "https://urgent.news/2026/10/24/tic-tac-toe-in-swiftui-done-properly-value-type-state-win-detection",
  "topic": "ai",
  "section": "AI",
  "published": "2026-10-24T00:47:11.000Z",
  "source": {
    "name": "HackerNoon",
    "slug": "hackernoon",
    "url": "https://hackernoon.com/tic-tac-toe-in-swiftui-done-properly-value-type-state-win-detection-and-minimax-ai?source=rss"
  },
  "original_language": "en",
  "account": "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.\n\nThe 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.\n\nThe 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.\n\nTesting 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.\n\nThe 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.\n\nOverall, 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.",
  "summary": "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",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}