Skip to content

Instantly share code, notes, and snippets.

@tedbow
Created February 7, 2021 14:23
Show Gist options
  • Save tedbow/4f3dab1448436bc747a13ecdfeef744f to your computer and use it in GitHub Desktop.
Save tedbow/4f3dab1448436bc747a13ecdfeef744f to your computer and use it in GitHub Desktop.
swift printer
// Test your 🔁 skills!
var printer :String
var current: Int = 1
var upperLimit: Int = 40
var goUp: Bool = false
for _ in 1...100 {
printer = ""
let change: Int = Int.random(in: 2...7)
switch current {
case 1:
goUp = true
case upperLimit:
goUp = false
default:
goUp = Int.random(in:1...2) == 2
}
goUp = Int.random(in:1...5) == 2 ? !goUp : goUp
current = goUp ? current + change : current - change
switch current {
case let x where x < 1:
current = 1
case let x where x > upperLimit:
current = upperLimit
default:
break
}
for _ in 1...current {
printer += "*"
}
print(printer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment