-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
96 lines (77 loc) · 2.46 KB
/
Contents.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
93
94
import Foundation
struct Card {
var Color: String // holds the color values associated with the card (Red, Blue, or Green)
var Roll: Int // holds a random value that represents the side of the die that will trigger that card
}
class Deck {
var cards = [Card]()
init() {
for _ in 1...10 {
let card = Card(Color: "Blue", Roll: Int(arc4random_uniform(2) + 1)) // Blue cards should have a roll value of either 1 or 2
cards.append(card)
}
for _ in 1...10 {
let card = Card(Color: "Red", Roll: Int(arc4random_uniform(2) + 3)) // Red cards should have a roll value of either 3 or 4
cards.append(card)
}
for _ in 1...10 {
let card = Card(Color: "Green", Roll: Int(arc4random_uniform(3) + 4)) // Green cards should have a roll value between 4 and 6
cards.append(card)
}
}
func removeTop() -> Card {
let topCard = cards.count - 1
let temp = cards[topCard]
cards.remove(at: topCard)
return temp
}
func isEmpty() -> Bool {
if cards.count == 0 {
return true
}
else {
return false
}
}
func shuffle() -> Any {
for _ in 1 ... 30 {
let randomNum1 = Int(arc4random_uniform(UInt32(cards.count - 1)))
let randomNum2 = Int(arc4random_uniform(UInt32(cards.count - 1)))
let temp = cards[randomNum1]
cards[randomNum1] = cards[randomNum2]
cards[randomNum2] = temp
}
return deck.cards
}
}
var deck = Deck()
class Player {
var name: String
var hand: [Card] = []
init(name: String) {
self.name = name
}
func draw(deck: Deck) -> Card {
let drawCard = deck.removeTop()
self.hand.append(drawCard)
return drawCard
}
func rollDice() -> Int { // random number between 1 and 6
let diceValue = Int(arc4random_uniform(5) + 1)
return diceValue
}
func matchCards(roll: Int, color: String) -> Int {
var matchCount = 0
for i in hand {
if (roll == i.Roll && color == i.Color) {
matchCount = matchCount + 1
}
}
return matchCount
}
}
// test case
var player1: Player = Player(name: "John")
deck.shuffle()
print(player1.draw(deck: deck))
print(player1.matchCards(roll: 5, color: "Green"))