Skip to content

Commit

Permalink
End of Giving hints chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph committed Apr 17, 2018
1 parent a8c3a9d commit fbfbf3d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Randomizly/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,26 @@
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dNs-CT-3Ea">
<rect key="frame" x="166" y="251" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="dNs-CT-3Ea" firstAttribute="centerX" secondItem="EMQ-wp-Qq5" secondAttribute="centerX" id="1nD-X3-Kse"/>
<constraint firstItem="DEF-Za-xcl" firstAttribute="centerX" secondItem="EMQ-wp-Qq5" secondAttribute="centerX" id="B53-j1-8jl"/>
<constraint firstItem="DEF-Za-xcl" firstAttribute="top" secondItem="dNs-CT-3Ea" secondAttribute="bottom" constant="8" id="Lf1-kI-Geo"/>
<constraint firstItem="xkH-DR-YWk" firstAttribute="centerY" secondItem="EMQ-wp-Qq5" secondAttribute="centerY" id="avA-CV-1Ot"/>
<constraint firstItem="xkH-DR-YWk" firstAttribute="centerX" secondItem="EMQ-wp-Qq5" secondAttribute="centerX" id="q9b-JE-5Gm"/>
<constraint firstItem="xkH-DR-YWk" firstAttribute="top" secondItem="DEF-Za-xcl" secondAttribute="bottom" constant="8" id="r3g-5e-yQY"/>
</constraints>
<viewLayoutGuide key="safeArea" id="oNi-7b-3OX"/>
</view>
<connections>
<outlet property="hintLabel" destination="dNs-CT-3Ea" id="sD1-Zv-VR6"/>
<outlet property="inputField" destination="DEF-Za-xcl" id="gY8-3c-SmA"/>
</connections>
</viewController>
Expand Down
23 changes: 20 additions & 3 deletions Randomizly/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,33 @@ class GameViewController: UIViewController {

@IBOutlet weak var inputField: UITextField!

@IBOutlet weak var hintLabel: UILabel!

let model = RandomizlyModel()

override func viewDidLoad() {
super.viewDidLoad()
self.hintLabel.isHidden = true
}

@IBAction func guess(_ sender: Any) {
if let input = self.inputField.text, let number = Int(input) {
let correct = self.model.guess(number)
let result = self.model.guess(number)
let tries = self.model.tries
if correct == true {
switch result {
case .correct:
self.showAlert(title: "Victory", message: "You guessed the number correctly! Tries needed: \(tries)")
self.hintLabel.isHidden = true

case .tooLow:
self.hintLabel.text = "👆 HIGHER 📈"
self.hintLabel.isHidden = false

case .tooHigh:
self.hintLabel.text = "👇 LOWER 📉"
self.hintLabel.isHidden = false
}
print("Correct: \(correct), tries: \(tries)")
print("Result: \(result), tries: \(tries)")
} else {
print("no input")
self.showAlert(title: "Error", message: "Enter a number!")
Expand Down
16 changes: 14 additions & 2 deletions Randomizly/RandomizlyModel.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Foundation

enum GuessAnswer {
case correct
case tooLow
case tooHigh
}

class RandomizlyModel {
private var number = 0
private(set) var tries = 0
Expand All @@ -12,8 +18,14 @@ class RandomizlyModel {
self.number = Int(arc4random_uniform(6) + 1 )
}

func guess(_ guess: Int) -> Bool {
func guess(_ guess: Int) -> GuessAnswer {
self.tries += 1
return self.number == guess
if number == guess {
return .correct
} else if number > guess {
return .tooLow
} else {
return .tooHigh
}
}
}

0 comments on commit fbfbf3d

Please sign in to comment.