-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
93 lines (87 loc) · 1.55 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// main.swift
// Hangman
//
// Created by Aaron Ivie on 11/14/20.
//
var words = ["rainbow", "computer", "science", "programming",
"python", "mathematics", "player", "condition",
"reverse", "water", "board", "geeks"]
var man1 = " o"
var man2 = """
o
|
"""
var man3 = """
o
|-
"""
var man4 = """
o
-|-
"""
var man5 = """
o
-|-
\\
"""
var man6 = """
o
-|-
/\\
"""
var word = words.randomElement()!
var guesses = Set<Character>()
var chances = 6
var unguessed:Int
while chances >= 0{
unguessed = word.count
for char in word{
if guesses.contains(char) {
print(char, terminator:"")
unguessed-=1
if unguessed==0{
print("\rYou win!")
print("Word is \(word)")
break
}
}else{
print("_", terminator:"")
}
}
switch chances {
case 6:
print("",terminator:"")
case 5:
print()
print(man1)
case 4:
print()
print(man2)
case 3:
print()
print(man3)
case 2:
print()
print(man4)
case 1:
print()
print(man5)
case 0:
print("\rOut of chances, you lose.")
print("The word was \(word).")
print(man6)
break
default:
print("invalid chance number")
break
}
print("\r\rGuess a letter: ", terminator:"")
let guess = Character(readLine()!)
if word.contains(guess){
guesses.insert(guess)
}else{
chances-=1
print("\rWrong, you have", chances, "more chances.\r")
}
}