Guest User

Untitled

a guest
Oct 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. //HW07 Alex Tikhomirov
  2.  
  3. import Foundation
  4.  
  5. //1
  6. print("\n-------------#1-------------\n")
  7. enum Value {
  8. case digital(Int)
  9. case jack(String)
  10. case queen(String)
  11. case king(String)
  12. case ace(String)
  13. }
  14.  
  15. enum Suits: String {
  16. case heart
  17. case spade
  18. case club
  19. case diamond
  20. }
  21.  
  22. struct Card {
  23. var suit:Suits
  24. var value: Value
  25. func description() -> String {
  26. return "\(self.suit) \(self.value)"
  27. }
  28. }
  29.  
  30. let cardArray: [Card]
  31. cardArray = [Card.init(suit: .diamond, value: .digital(6)), Card.init(suit: .diamond, value: .ace("ase")), Card.init(suit: .club, value: .king("king"))]
  32. for card in cardArray {
  33. print(card.description())
  34. }
  35.  
  36. //2
  37. print("\n-------------#2-------------\n")
  38. class Player {
  39. enum Direction {
  40. case up
  41. case down
  42. case left
  43. case right
  44. }
  45. var name: String
  46. var position: (x: Int, y: Int)
  47. init(name: String,_ position:(x: Int, y: Int) = (x: 0, y: 0)) {
  48. self.name = name
  49. self.position = position
  50. return
  51. }
  52. func move(direction: Direction) {
  53. switch direction {
  54. case .up:
  55. self.position.y -= 1
  56. case .down:
  57. self.position.y += 1
  58. case .left:
  59. self.position.x -= 1
  60. case .right:
  61. self.position.x += 1
  62. }
  63. }
  64. func makeRandomMove() {
  65. var direction: Direction = .left
  66. let dir = arc4random_uniform(3)
  67. switch dir {
  68. case 0:
  69. direction = .up
  70. case 1:
  71. direction = .down
  72. case 2:
  73. direction = .left
  74. case 3:
  75. direction = .right
  76. default:
  77. break
  78. }
  79. self.move(direction: direction)
  80. }
  81. }
  82. let a = Player(name: "A")
  83. for _ in 0...5 {
  84. a.makeRandomMove()
  85. print(a.position)
  86. }
  87.  
  88. //3
  89. print("\n-------------#3-------------\n")
  90. protocol Wallpost {
  91. func messageForSharing()
  92. }
  93.  
  94. class Status: Wallpost {
  95. func messageForSharing() {
  96. //body function
  97. }
  98. }
  99.  
  100. class Post: Wallpost {
  101. func messageForSharing() {
  102. //body function
  103. }
  104. }
  105.  
  106. //4
  107. print("\n-------------#4-------------\n")
  108.  
  109. protocol Vehicle {
  110. var speed: Int {get set}
  111. var capacity: Int {get set}
  112. var cost: Int {get set}
  113. func calculation(distance: Int, people: Int) -> (cost: Int, time: Int)
  114. }
  115. extension Vehicle {
  116. var cost: Int {return 10}
  117. }
  118.  
  119. class Transfer {
  120. var speed: Int
  121. var capacity: Int
  122. var cost: Int
  123. init(speed: Int, capacity: Int, cost: Int) {
  124. self.speed = speed
  125. self.capacity = capacity
  126. self.cost = cost
  127. }
  128. func calculation(distance: Int, people: Int) -> (cost: Int, time: Int) {
  129. var time: Int = 0
  130. let tm = distance / speed //flight time
  131. time = people / capacity
  132. if people % capacity > 0 {
  133. time += 1
  134. }
  135. if time > 1 {
  136. time *= 2 //number of transportations with returns
  137. }
  138. time *= tm
  139. return (cost: people * self.cost, time: time)
  140. }
  141. }
  142.  
  143. class Airplane: Transfer, Vehicle {
  144. }
  145.  
  146. class Ship: Transfer, Vehicle {
  147. }
  148.  
  149. class Helicopter: Transfer, Vehicle {
  150. }
  151.  
  152. class Car: Transfer, Vehicle {
  153. }
  154. let airplane = Airplane(speed: 650, capacity: 150, cost: 10)
  155. let ship = Ship(speed: 30, capacity: 3000, cost: 5)
  156. let helicopter = Helicopter(speed: 300, capacity: 10, cost: 15)
  157. let car = Car(speed: 120, capacity: 4, cost: 0)
  158.  
  159. let vehicle = [airplane, ship, helicopter, car]
  160. for veh in vehicle {
  161. print(veh.calculation(distance: 2000, people: 10))
  162. }
Add Comment
Please, Sign In to add comment