-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockpaperscissors.swift
62 lines (55 loc) · 1.7 KB
/
rockpaperscissors.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
//Function project from Codecademy
func getUserChoice(userInput: String)-> String {
//this checks three different inputs
if userInput == "rock"||userInput == "paper"||userInput == "scissors"{
return userInput
}else{
return "You can only enter rock, paper, or scissors. Try again."
}
}
//print(getUserChoice(userInput: "rock")) checks function
func getComputerChoice()-> String{
let randomNumber = Int.random(in: 0...2)
switch randomNumber {
case 0:
return "rock"
case 1:
return "paper"
case 2:
return "scissors"
default:
return "Something went wrong"
}
}
//print(getComputerChoice()) checks function
func determineWinner(_ userChoice: String,_ compChoice: String) -> String{
var decision = "It's a tie"
switch userChoice {
case "rock":
if compChoice == "paper"{
decision = "The Computer won!"
} else if compChoice == "scissors" {
decision = "The User won!"
}
case "paper":
if compChoice == "rock" {
decision == "The User won!"
}else if compChoice == "scissors" {
decision = "The Computer won!"
}
case "scissors":
if compChoice == "scissors" {
decision == "The Computer won!"
}else if compChoice == "paper" {
decision == "The User won!"
}
default:
"something went wrong 🤷🏾♀️"
}
return decision
}
let userChoice = getUserChoice(userInput: "paper")//user input to change result
let compChoice = getComputerChoice()
print("You threw \(userChoice)")
print("The computer threw \(compChoice)")
print(determineWinner(userChoice, compChoice))