Created
July 17, 2018 03:05
-
-
Save topherPedersen/317fb1be14af62c59c47a9a2c2185029 to your computer and use it in GitHub Desktop.
Command-Line Version of Magic 8 Ball written in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
print("Welcome to Magic 8 Ball!") | |
var keepPlaying = true | |
while keepPlaying == true { | |
// Prompt user to ask Magic 8 Ball a question | |
print("What is your question? ") | |
// We use the variable name '_' underbar below instead of something more | |
// appropriate like 'question' in order to suppress Swift warnings | |
// regarding the use of unused variables. Obviously, Magic 8 Ball | |
// doesn't determine it's responses based on users questions, rather | |
// answers are picked at random. However, we still need to prompt | |
// the user to ask his or her question in order for the game to be fun. | |
_ = readLine()! | |
// Generate random integer to help us pick a random response | |
let randomNumber:UInt32 = arc4random_uniform(20) | |
// Initialize answer variable, then assign a value based on our random number | |
var answer: String | |
switch (randomNumber) { | |
case 0: | |
answer = "It is certain." | |
case 1: | |
answer = "It is decidedly so." | |
case 2: | |
answer = "Without a doubt." | |
case 3: | |
answer = "Yes, definitely." | |
case 4: | |
answer = "You may rely on it." | |
case 5: | |
answer = "As I see it, yes." | |
case 6: | |
answer = "Most Likely." | |
case 7: | |
answer = "Outlook good." | |
case 8: | |
answer = "Yes." | |
case 9: | |
answer = "Signs point to yes." | |
case 10: | |
answer = "Reply hazy try again." | |
case 11: | |
answer = "Ask again later." | |
case 12: | |
answer = "Better not tell you now." | |
case 13: | |
answer = "Cannot predict now." | |
case 14: | |
answer = "Concentrate and ask again." | |
case 15: | |
answer = "Don't count on it." | |
case 16: | |
answer = "My reply is no." | |
case 17: | |
answer = "My sources say no." | |
case 18: | |
answer = "Outlook not so good." | |
case 19: | |
answer = "Very doubtful." | |
default: | |
answer = "ERROR: PC LOAD LETTER" | |
} | |
// Display random response | |
print(answer) | |
// Ask user if he or she would like to play again | |
// Prompt user to ask Magic 8 Ball a question | |
print("Would you like to ask Magic 8 Ball another question (enter 'y' for yes, 'n' for no)? ") | |
let playAgain = readLine()! | |
if playAgain == "y" { | |
keepPlaying = true | |
} else { | |
keepPlaying = false | |
print("Goodbye :)") | |
} | |
} // end while-loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment