import Foundation
func containsSeven(_ num: Int) -> Bool {
return String(num).contains("7")
}
func isBoom(_ num: Int) -> Bool {
return num % 7 == 0 || containsSeven(num)
}
func getUserInput(_ prompt: String) -> String {
print(prompt, terminator: "")
return readLine()?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
func processTurn(expected: Int, input: String) -> Bool {
let lower = input.lowercased()
let shouldBoom = isBoom(expected)
if shouldBoom {
if lower != "boom" {
print("\(input) (should be Boom)")
print("YOU LOSE!")
return false
}
} else {
if lower == "boom" {
print("Wrong move! It was \(expected)")
print("YOU LOSE!")
return false
}
guard let value = Int(input), value == expected else {
print("Wrong move! It was \(expected)")
print("YOU LOSE!")
return false
}
}
return true
}
func runGame() {
let startInput = getUserInput("Enter a number from which you want to start the game (-1 to exit): ")
guard let startNum = Int(startInput), startNum != -1 else {
return
}
var current = startNum
while true {
let input = getUserInput("Enter a number (or Boom): ")
if input == "-1" {
print("Game Over!")
break
}
if !processTurn(expected: current, input: input) {
break
}
current += 1
}
}
runGame()
/*
run:
Enter a number from which you want to start the game (-1 to exit): 6
Enter a number (or Boom): 6
Enter a number (or Boom): Boom
Enter a number (or Boom): 8
Enter a number (or Boom): 9
Enter a number (or Boom): 10
Enter a number (or Boom): 11
Enter a number (or Boom): 12
Enter a number (or Boom): 13
Enter a number (or Boom): Boom
Enter a number (or Boom): 15
Enter a number (or Boom): 16
Enter a number (or Boom): Boom
Enter a number (or Boom): 18
Enter a number (or Boom): 19
Enter a number (or Boom): 20
Enter a number (or Boom): 21
21 (should be Boom)
YOU LOSE!
*/