Implementing Hint Functionality for a 24‑Point Game in Swift
This article explains how to design and code a hint feature for a 24‑point puzzle app on iOS, covering random number generation, validation of solvable sets, recursive expression search, expression‑to‑string conversion, optimization of parentheses, and provides complete Swift source code with detailed algorithmic explanations.
Background
The author set out to build a "24‑point" puzzle app and investigated existing apps, noting that most monetize through ads and in‑app purchases for hints. The article shares a complete solution for implementing the hint function, showing both the algorithm and the Swift code.
Solution Overview
The hint feature consists of two main steps: (1) determine whether a given set of four numbers can produce 24, and (2) generate the arithmetic expression that yields 24 when possible.
Step 1 – Checking Solvability
Given four numbers [a, b, c, d] , the algorithm recursively picks any two numbers, applies each of the four operators (+, -, *, /), and replaces the pair with the result, repeating until a single number remains. If the final number is within a small epsilon of 24, the set is solvable.
Key points:
Operators + and * are commutative, so duplicate calculations are avoided.
Division by zero is prevented and a tolerance is used for floating‑point comparison.
Step 2 – Generating the Expression
During the recursive search the code records each operation as a string like (a + b) = result . After a successful search the list of step strings is merged into a single expression by replacing intermediate results with their originating sub‑expressions, ensuring each step is used only once.
Two helper functions are provided:
func generateExpressStr(from list: [String]) -> String { ... }and the main solver:
class Solution {
let epsilon = 0.001
let target = 24.0
let ops = ["+", "-", "*", "/"]
func isEqual24(_ v: Double) -> Bool { return abs(v - target) < epsilon }
func judgePoint24(_ list: [Int]) -> Bool { ... }
func find24(_ cards: [Double], resultExpressList: [String]) -> (Bool, [String]) { ... }
}Expression Optimization
The generated expression may contain redundant parentheses, e.g., (8 * (5 + (6 - 8))) . The article discusses two strategies to simplify the expression by removing unnecessary brackets based on operator precedence.
Complete Code and Demo
The full source code, including the expression‑conversion logic and a demo UI, is available on GitHub: https://github.com/mokong/game24HintDemo . Screenshots show the hint button in action and the final expression displayed to the user.
Conclusion
By combining a recursive 24‑point solver with a systematic expression‑building routine, developers can add a robust hint system to any Swift‑based puzzle game, enhancing user experience while keeping the implementation clear and extensible.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.