-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock-paper-scissors-game.swift
65 lines (55 loc) · 1.38 KB
/
rock-paper-scissors-game.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
func getUserChoice(userInput: String) -> String {
if userInput == "rock" || userInput == "paper" || userInput == "scissors" {
return userInput
}
else {
return ("You can only enter rock, paper, or scissors. Try again.")
}
}
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"
}
}
func determineWinner(_ userChoice: String, _ compChoice: String) -> String {
var decision: String = "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 == "rock" {
decision = "The computer won"
}
else if compChoice == "paper" {
decision = "The user won"
}
default:
print("Something went wrong")
}
return decision
}
let userChoice = getUserChoice(userInput: "paper")
let compChoice = getComputerChoice()
print("You threw \(userChoice)")
print("The computer threw \(compChoice)")
print(determineWinner(userChoice, compChoice))