-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberGuessing.swift
98 lines (97 loc) · 3.67 KB
/
numberGuessing.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var choices: [Int] = []
var end = false
while !end {
print("Minimum Number (Default - 1)", terminator: ": ")
var rangeMinInput = readLine()
print("Maximum Number (Default - 100)", terminator: ": ")
var rangeMaxInput = readLine()
var rangeMin = Int()
var rangeMax = Int()
if Int(rangeMinInput!) != nil && Int(rangeMaxInput!) != nil && rangeMaxInput != nil && rangeMinInput != nil {
if Int(rangeMinInput!) == Int(rangeMaxInput!) {
rangeMinInput = nil
rangeMaxInput = nil
print("Maximum Number is equal to Minimum Number")
print("Minimum Number is equal to Maximum Number")
print("Numbers set to default")
} else if Int(rangeMinInput!)! > Int(rangeMaxInput!)! {
rangeMinInput = nil
rangeMaxInput = nil
print("Maximum Number is smaller than Minimum Number")
print("Minimum Number is greater than Maximum Number")
print("Numbers set to default")
}
} else if Int(rangeMinInput!) == nil || Int(rangeMaxInput!) == nil || rangeMinInput?.isEmpty == true || rangeMaxInput?.isEmpty == true {
print("Either one or both numbers are invalid")
print("Invalid Number(s) set to default")
}
if let rangeMinInput = rangeMinInput {
if rangeMinInput.isEmpty == true || Int(rangeMinInput) == nil {
rangeMin = 1
} else {
rangeMin = Int(rangeMinInput)!
}
} else {
rangeMin = 1
}
if let rangeMaxInput = rangeMaxInput {
if rangeMaxInput.isEmpty == true || Int(rangeMaxInput) == nil {
rangeMax = 100
} else {
rangeMax = Int(rangeMaxInput)!
}
} else {
rangeMax = 100
}
var tries = 0
let numberToGuess = Int.random(in: rangeMin...rangeMax)
var foundCorrectNumber = false
while !foundCorrectNumber {
tries += 1
print("Guess a number from \(rangeMin) to \(rangeMax)", terminator: ": ")
let guessInput = readLine()
if !guessInput!.isEmpty {
if Int(guessInput!) != nil {
let guess = Int(guessInput!)
// print(guess)
// print(Number(guess))
// print(choices)
// print(choices.contains(guess))
if guess == numberToGuess {
print("Congrats, you got it!")
foundCorrectNumber = true
} else if (choices.contains(guess!)) {
print("Number has been guessed before")
} else if (guess! > rangeMax || guess! < rangeMin) {
print("Out of Range")
} else if (guess! > numberToGuess) {
print("Sorry, guess again!\nNumber is Smaller")
} else if (guess! < numberToGuess) {
print("Sorry, guess again!\nNumber is Larger")
}
choices.append(guess!)
} else {
print("Invalid Input")
}
} else {
print("Invalid Input")
}
}
print("The number was \(numberToGuess)")
if (tries > 1) {
print("You tried \(tries) times")
} else {
print("You tried 1 time")
}
print("Again [Y/N]", terminator: "? ")
let choice = readLine()
if choice!.lowercased().contains("n") || choice!.lowercased().contains("x") || Bool(choice!) == false {
end = true
} else if choice!.lowercased().contains("y") || Bool(choice!) == true {
end = false
print()
choices = []
} else {
end = true
}
}