How to resolve the algorithm Guess the number/With feedback step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Guess the number/With feedback step by step in the Swift programming language

Table of Contents

Problem Statement

Write a game (computer program) that follows the following rules:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number/With feedback step by step in the Swift programming language

Source code in the swift programming language

import Cocoa

var found = false

let randomNum = Int(arc4random_uniform(100) + 1)

println("Guess a number between 1 and 100\n")

while (!found) {
    var fh = NSFileHandle.fileHandleWithStandardInput()
    
    println("Enter a number: ")
    let data = fh.availableData
    let str = NSString(data: data, encoding: NSUTF8StringEncoding)
    if (str?.integerValue == randomNum) {
        found = true
        println("Well guessed!")
    } else if (str?.integerValue < randomNum) {
        println("Good try but the number is more than that!")
    } else if (str?.integerValue > randomNum) {
        println("Good try but the number is less than that!")
    }
}


  

You may also check:How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sudoku step by step in the Pascal programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Quackery programming language
You may also check:How to resolve the algorithm A+B step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Amb step by step in the langur programming language