Example of Sudoku game developed by IOS

Time:2022-1-19

This example shares the specific code of IOS Sudoku game for your reference. The specific contents are as follows

1、 Interface design

See the figure below. Figure 1 is the main page after the user opens the program. Click “start game” to enter the game page (Figure 2 and figure 3). At the top of the game interface is a display bar, which displays the currently selected button of the player. When the player wants to fill in the number in the nine palace grid, just click the number control 1-9 below, the number currently selected by the player will be automatically displayed on the display bar, and then click the position that the player wants to fill in to fill in the control.

The bottom three buttons are submit, eraser and tag controls. When the player clicks the “submit” button, the system will automatically settle the game score for the player; When players click “eraser”, they can erase the numbers at the specified position on the Jiugong grid; When players click “mark”, they can add a small red flag as a mark to the grid on the Jiugong grid. There is a navigation bar at the top of the game interface. Clicking the house icon will return to the main page.

2、 Realize

1. First build the portal interface, including an image view and button, add a viewcontroller as the game interface, add segue between the portal interface and the game interface, and jump to the game interface when you click “start game”.

2. Add necessary controls for the game interface, use 9×9 buttons to realize the Jiugong grid, and add 9 digital buttons and 3 functional controls. In order to facilitate users’ use, a display bar needs to be added. The user presses the button to know the position coordinates that the player wants to fill in the numbers on the nine palaces, and the button is identified by tag.

3. Association code. Associate the Jiugong grid, 9 number buttons and 3 function controls with the code, and create a new board class. The board class includes Jiugong grid game progress and answer calculation.

//
//  ViewController.swift
//
import UIKit
 
class ViewController: UIViewController {
 
    var inputNumber: String = " "
    var board = Board()
    var oldx: Int = 0
    var oldy: Int = 0
    var oldValue: Int = 0
    @IBOutlet weak var display: UILabel!
    
    @IBAction func inNumber(_ sender: UIButton) {
        if let  num = sender.currentTitle{
            switch num {
            case "" :inputNumber = "" ;display.text = ""
            case "1" :inputNumber = "1" ;display.text = "1"
            case "2" :inputNumber = "2" ;display.text = "2"
            case "3" :inputNumber = "3" ;display.text = "3"
            case "4" :inputNumber = "4" ;display.text = "4"
            case "5" :inputNumber = "5" ;display.text = "5"
            case "6" :inputNumber = "6" ;display.text = "6"
            case "7" :inputNumber = "7" ;display.text = "7"
            case "8" :inputNumber = "8" ;display.text = "8"
            case "9" :inputNumber = "9" ;display.text = "9"
            default:
                break
            }
        }
    }
 
    @IBAction func setNumber(_ sender: UIButton) {
        let pushTag: Int = sender.tag
        let hightTag: Int = pushTag / 10
        let lowTag: Int = pushTag % 10
        if board.question[hightTag - 1][lowTag - 1] == 0 {
            sender.setTitle(inputNumber, for: UIControl.State.normal)
            board.change(x: hightTag - 1, y: lowTag - 1, value: Int(inputNumber) ?? 0)
            oldx = hightTag - 1
            oldy = lowTag - 1
            oldValue = Int(inputNumber) ?? 0
        }
    }
    
    @IBAction func eraser(_ sender: UIButton) {
        inputNumber = " "
        display. Text = "eraser"
    }
 
    @IBAction func finish(_ sender: UIButton) {
        if board.check() {
            display.text = "You Win!"
        } else {
            display.text = "Score: \(board.score)"
        }
    }
    
    @IBAction func backHome(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        display.text = "begin"
    }
}

//
//  Board.swift
//
import UIKit
class Board{
    var score: Int = 0
    var question: [[Int]] = [[0,0,0,1,8,0,9,3,0],
                             [1,8,6,0,0,0,0,0,7],
                             [0,5,0,7,4,0,0,0,0],
                             [5,0,4,0,0,0,3,0,6],
                             [0,0,0,3,9,4,0,0,2],
                             [2,3,0,0,0,0,0,1,0],
                             [0,0,9,0,0,3,0,0,8],
                             [0,0,2,9,0,5,7,0,0],
                             [0,7,0,0,0,8,0,9,0],]
    var nowBoard: [[Int]] = [[0,0,0,1,8,0,9,3,0],
                             [1,8,6,0,0,0,0,0,7],
                             [0,5,0,7,4,0,0,0,0],
                             [5,0,4,0,0,0,3,0,6],
                             [0,0,0,3,9,4,0,0,2],
                             [2,3,0,0,0,0,0,1,0],
                             [0,0,9,0,0,3,0,0,8],
                             [0,0,2,9,0,5,7,0,0],
                             [0,7,0,0,0,8,0,9,0],]
    var answer: [[Int]] = [[4,2,7,1,8,6,9,3,5],
                           [1,8,6,5,3,9,2,4,7],
                           [9,5,3,7,4,2,6,8,1],
                           [5,9,4,8,2,1,3,7,6],
                           [7,6,1,3,9,4,8,5,2],
                           [2,3,8,6,5,7,4,1,9],
                           [6,1,9,4,7,3,5,2,8],
                           [8,4,2,9,1,5,7,6,3],
                           [3,7,5,2,6,8,1,9,4],]
    func getNum(x: Int, y: Int) -> Int {
        return nowBoard[x][y]
    }
    func change(x: Int, y: Int, value: Int) {
        nowBoard[x][y] = value
    }
    func check() -> Bool {
        var countCorrect: Float = 0
        var countEmpty: Float = 0
        for i in 0...8{
            for j in 0...8 {
                if nowBoard[i][j] == answer[i][j] {
                    countCorrect += 1
                }
                if question[i][j] == 0 {
                    countEmpty += 1
                }
            }
        }
        //score = Int(((countQues -  countCorrect) / countQues) * 100)
        score = Int( ((countCorrect +  countEmpty - 81) / countEmpty) * 100 )
        if nowBoard == answer {
            score = 100
            return true
        } else {
            return false
        }
    }
}

3、 Commissioning (with GIF)

4、 Evaluation

Advantages: simple program, small amount of code and no complex algorithm.

Disadvantages: the game disk is relatively fixed, and the random generation of Sudoku disk, automatic generation of Sudoku solution and generation of different difficulty are not realized.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.