Advertisement
jamesxjay

Untitled

Sep 3rd, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. // James Jay
  2.  
  3. import Foundation
  4. import UIKit
  5.  
  6. enum RPS: Printable {
  7.  
  8. case Rock
  9. case Paper
  10. case Scissor
  11.  
  12. //initializes one of them
  13. init() {
  14. let randomNumber = arc4random() % 3
  15. let randomNumber2 = arc4random() % 3
  16.  
  17. if randomNumber == 0 {
  18. self = RPS.Rock
  19. } else if randomNumber == 1 {
  20. self = RPS.Paper
  21. } else {
  22. self = RPS.Scissor
  23. }
  24.  
  25. }
  26.  
  27. var description: String {
  28. switch (self) {
  29. case RPS.Rock:
  30. return "Rock"
  31. case RPS.Paper:
  32. return "Paper"
  33. case RPS.Scissor:
  34. return "Scissors"
  35.  
  36. }
  37. }
  38.  
  39. }
  40.  
  41.  
  42. enum Outcome {
  43.  
  44. case Win
  45. case Lose
  46. case Tie
  47.  
  48. var Description: String{
  49. switch(self){
  50. case .Win:
  51. return "Win"
  52. case .Lose:
  53. return "Lose"
  54. case .Tie:
  55. return "Tie"
  56. }
  57. }
  58. }
  59.  
  60.  
  61.  
  62. struct RPSMatch{
  63. var playerMove: RPS
  64. var opponentMove: RPS
  65.  
  66. var outcome: Outcome{
  67.  
  68. if(playerMove.description == opponentMove.description){
  69. return .Tie
  70. }
  71. else if(playerMove.description == "Paper" && opponentMove.description == "Rock"){
  72. return .Win
  73. }
  74.  
  75. else if(playerMove.description == "Scissor" && opponentMove.description == "Paper"){
  76. return .Win
  77. }
  78. else if(playerMove.description == "Rock" && opponentMove.description == "Scissor"){
  79. return .Win
  80. }
  81. else {
  82. return .Lose
  83. }
  84. }
  85.  
  86. var description: String{
  87. get{
  88. //use playerMove.description to change enum to string.
  89. switch(playerMove){
  90. case .Rock:
  91. switch(outcome){
  92. case .Win:
  93. return "Rock vs Scissors. Win"
  94. case .Lose:
  95. return "Rock vs Paper. Lose"
  96. case .Tie:
  97. return "Rock vs Rock. Tie"
  98. }//rock switch
  99. case .Paper:
  100. switch(outcome){
  101. case .Win:
  102. return "Paper vs Rock. Win"
  103. case .Lose:
  104. return "Paper vs Scissors. Lose"
  105. case .Tie:
  106. return "Paper vs Paper. Tie"
  107. }//paper switch
  108. case .Scissor:
  109. switch(outcome){
  110. case .Win:
  111. return "Scissors vs Paper. Win"
  112. case .Lose:
  113. return "Scissors vs Rock. Lose"
  114. case .Tie:
  115. return "Scissors vs Scissors. Tie"
  116. }
  117. }
  118. }
  119. }
  120.  
  121.  
  122. }
  123.  
  124.  
  125.  
  126. let playerMove = RPS()
  127. print("player one has chosen \(playerMove.description)")
  128.  
  129. let opponentMove = RPS()
  130. print("player two has chosen \(opponentMove.description)")
  131.  
  132.  
  133. let playerHand = playerMove
  134. let opponentHand = opponentMove
  135.  
  136. var match = RPSMatch(playerMove: playerHand, opponentMove: opponentHand)
  137.  
  138. match.description
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement