Marks Guess the Number Game

Description

The App will generate a random number. Enter in what you think the number is, and it will tell you if you need to guess lower or higher. You have 10 tries to get it right.

This app was created in my Intro Programming and Logic class. The assignment was as follows:

Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. Count only as one try if they input the same number multiple times consecutively.



[code language=”objc”] //
// ViewController.swift
// Guess a Number
//
// Created by Mark Hesser on 10/19/17.
// Copyright © 2017 Mark Hesser. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var guessText: UITextField!
@IBOutlet weak var guessButton: UIButton!
@IBOutlet weak var guessLabel1: UILabel!
@IBOutlet weak var guessLabel2: UILabel!
@IBOutlet weak var guessLabel3: UILabel!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var tryCounterLabel: UILabel!
@IBOutlet weak var debugLabel: UILabel!

var randNumber:Int = 0
var tryCounter:Int = 0
var prevGuess:Int = 0
var playerGuess:Int = 0
var tempGuess = ""
var debugOn:Bool = false

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//guessText.keyboardType = UIKeyboardType.numberPad

gameReset()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func GuessBtnPress(_ sender: Any) {
processGuess()
}
@IBAction func gameResetBtn(_ sender: Any) {
gameReset()
}

private func gameReset() {
//Generates Random Number
randNumber = Int(arc4random_uniform(100))+1
tryCounter = 10
guessText.text = ""
guessButton.isEnabled = true
guessText.isEnabled = true
labelReset()
debugMode()
tryCounterLabel.text = "Trys: \(tryCounter)"
}

func labelReset() {
guessLabel1.text = "I’m thinking of a Number"
guessLabel2.text = "between 1 and 100"
guessLabel3.text = "What is it?"
guessLabel1.isHidden = false
guessLabel2.isHidden = false
guessLabel3.isHidden = false
resultLabel.isHidden = true
}

func labelLayout1() {
guessLabel1.isHidden = true
guessLabel2.isHidden = false
guessLabel3.isHidden = true
resultLabel.isHidden = false
}

func labelLayout2() {
guessLabel1.isHidden = true
guessLabel2.isHidden = false
guessLabel3.isHidden = false
resultLabel.isHidden = false
}

private func processGuess() {
debugCheck()
labelReset()
tempGuess = guessText.text!

// if guess is not emtpy or contains text
// Convert to Integar
if Int(tempGuess) != nil {
playerGuess = Int(tempGuess)!
// check the guess
if (playerGuess != prevGuess)
{
if (playerGuess > 0 && playerGuess < 101) { if (playerGuess == randNumber) { playerWin() } else { if (playerGuess > randNumber)
{
resultLabel.text = "Try Again, Guess Lower!"
resultLabel.isHidden = false
loseTry()
}
if (playerGuess < randNumber)
{
resultLabel.text = "Try Again, Guess Higher!"
resultLabel.isHidden = false
loseTry()
}
}
}
else
{
resultLabel.text = "Invalid! Out of Range…"
resultLabel.isHidden = false
loseTry()
}
}
else
{
guessLabel2.text = "You already Guessed that"
guessLabel3.text = "Your last guess was \(prevGuess)"
resultLabel.text = "Enter a different number"
guessLabel1.isHidden = true
guessLabel3.isHidden = false
resultLabel.isHidden = false
}
checkTrys()
}
guessText.text = ""
}

private func playerWin() {
guessLabel2.text = "Congrats, You Win!"
guessLabel3.text = "The number was \(randNumber)"
resultLabel.text = "Play Again?"
labelLayout2()
guessButton.isEnabled = false
guessText.isEnabled = false
}

func checkTrys() {
if tryCounter <= 0 && playerGuess != randNumber {
guessLabel2.text = "Game Over"
guessLabel3.text = "The Number was \(randNumber)"
resultLabel.text = "Play Again?"
labelLayout2()
guessButton.isEnabled = false
guessText.isEnabled = false
}
}

func loseTry() {
tryCounter -= 1
prevGuess = playerGuess
tryCounterLabel.text = "Trys: \(tryCounter)"
guessLabel3.text = "Your last guess was \(prevGuess)"
guessLabel3.isHidden = false
}

func debugCheck() {
if guessText.text == "6275" {
if debugOn == false {
debugOn = true
gameReset()
}
else if debugOn == true {
debugOn = false
gameReset()
}
}
}

func debugMode() { //Turns Debug Text On or Off
if debugOn == true {
debugLabel.isHidden = false
debugLabel.text = "\(randNumber)"
tryCounter = 3
}
else {
debugLabel.isHidden = true
}
}
}
[/code]