How to resolve the algorithm 24 game step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 24 game step by step in the Swift programming language

Table of Contents

Problem Statement

The 24 Game tests one's mental arithmetic.

Write a program that randomly chooses and displays four digits, each from 1 ──► 9 (inclusive) with repetitions allowed. The program should prompt for the player to enter an arithmetic expression using just those, and all of those four digits, used exactly once each. The program should check then evaluate the expression. The goal is for the player to enter an expression that (numerically) evaluates to 24.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 24 game step by step in the Swift programming language

Source code in the swift programming language

import Darwin
import Foundation

println("24 Game")
println("Generating 4 digits...")

func randomDigits() -> Int[] {
    var result = Int[]();
    for var i = 0; i < 4; i++ {
        result.append(Int(arc4random_uniform(9)+1))
    }
    return result;
}

// Choose 4 digits
let digits = randomDigits()

print("Make 24 using these digits : ")

for digit in digits {
    print("\(digit) ")
}
println()

// get input from operator
var input = NSString(data:NSFileHandle.fileHandleWithStandardInput().availableData, encoding:NSUTF8StringEncoding)

var enteredDigits = Int[]()

var enteredOperations = Character[]()

let inputString = input as String

// store input in the appropriate table
for character in inputString {
    switch character {
        case "1", "2", "3", "4", "5", "6", "7", "8", "9":
            let digit = String(character)
            enteredDigits.append(digit.toInt()!)
        case "+", "-", "*", "/":
            enteredOperations.append(character)
        case "\n":
            println()
        default:
            println("Invalid expression")
    }
}

// check value of expression provided by the operator
var value = Int()

if enteredDigits.count == 4 && enteredOperations.count == 3 {
    value = enteredDigits[0]
    for (i, operation) in enumerate(enteredOperations) {
        switch operation {
            case "+":
                value = value + enteredDigits[i+1]
            case "-":
                value = value - enteredDigits[i+1]
            case "*":
                value = value * enteredDigits[i+1]
            case "/":
                value = value / enteredDigits[i+1]
            default:
                println("This message should never happen!")
        }
    }
}

if value != 24 {
    println("The value of the provided expression is \(value) instead of 24!")
} else {
    println("Congratulations, you found a solution!")
}


  

You may also check:How to resolve the algorithm Range extraction step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Quine step by step in the Batch File programming language
You may also check:How to resolve the algorithm Distribution of 0 digits in factorial series step by step in the Java programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Delphi programming language