Here is my problem:
In Swift I, you used your knowledge at the time to create a High / Low Guessing Game.
This week, you will update (or create a new version of) the game using the knowledge you've now attained.
You will be creating a High/Low guessing game as outlined below:
In this game:
Here is code I have from the original assignment, I need help converting it to match the conditions listed above
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
consoleOutput.isHidden = true
// Do any additional setup after loading the view.
}
//var guess: Int = (question as NSString).integerValue
@IBOutlet weak var consoleOutput: UITextView!
@IBOutlet weak var userGuess: UITextField!
@IBAction func guess(_ sender: Any) {
consoleOutput.isHidden = false
let userInput = Int(userGuess.text!)
var number = Int.random(in: 0...999)
if userInput == number {
consoleOutput.text = "You guessed the right number!"}
if userInput! > number {
consoleOutput.text = "You guessed too high, try again"}
if userInput! < number {
consoleOutput.text = "You guessed too low, try again"}
}
}
swift code
import UIKit
import Foundation
class ViewController: UIViewController {
var number = Int.random(in: 0...999)
var guess = 0
@IBOutlet weak var userGuess: UITextField!
@IBOutlet weak var consoleOutput: UILabel!
enum GuessError: ErrorType {
case Empty
case NotInt
case OutOfBounds
}
enum GuessCase {
case TooHigh
case TooLow
case CorrectGuess
case InvalidGuess
}
func reset() {
consoleOutput.text =
""
userGuess.text =
nil
number = Int.random(in:
0...999)
guess = 0
}
func checkError(guessedNumberText: String?,
generatedNumber: Int) throws -> String {
guard guessedNumberText
!= nil else {
throw GuessError.Empty
}
guard guessedNumberText
!= "" else {
throw GuessError.Empty
}
guard let guessedNumber
= Int(guessedNumberText!) else {
throw GuessError.NotInt
}
guard case 0 ... 999 =
guessedNumber else {
throw GuessError.OutOfBounds
}
return "No error"
}
func checkGuess(guessedNumberText: String?,
generatedNumber: Int) -> GuessCase {
guard generatedNumber
>= guessedNumber else {
return .TooHigh
}
guard generatedNumber
<= guessedNumber else {
return .TooLow
}
guard generatedNumber !=
guessedNumber else {
return .CorrectGuess
}
return
.InvalidGuess
}
@IBAction func guess(_ sender: Any) {
++guess
switch
checkGuess(userGuess.text, generatedNumber: number) {
case .TooHigh:
consoleOutput.text = "Your guess is too high."
case .TooLow:
consoleOutput.text = "Your guess is too low."
case .CorrectGuess:
consoleOutput.text = "You've Won! You guessed"+ String(guess) + "
times!!!"
reset()
case .InvalidGuess:
let statusMessage: String?
do {
try statusMessage = checkError(userGuess.text, generatedNumber:
number)
} catch GuessError.Empty {
statusMessage = "You have to guess to play."
} catch GuessError.NotInt {
statusMessage = "That's not a whole number between 0 and 999"
} catch GuessError.OutOfBounds {
statusMessage = "That's a whole number, but it isn't between 0 and
999"
} catch {
statusMessage = "Something went wrong."
}
consoleOutput.text = statusMessage
}
}
override func viewDidLoad() {
super.viewDidLoad()
number = Int.random(in:
0...999)
guess = 0
// Do any additional
setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any
resources that can be recreated.
}
}
Get Answers For Free
Most questions answered within 1 hours.