Advertisement
Guest User

Swift Struct Soccer

a guest
Mar 23rd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.05 KB | None | 0 0
  1. struct Club {
  2.     var name: String
  3.     var country: String
  4.     var squad: [Player] = []
  5.     var captain: Player?
  6.     var victories: Int
  7.     var draws: Int
  8.     var defeats: Int
  9.     var goalsFor: Int
  10.     var goalsAgainst: Int
  11.     var points: Int
  12.     var playedMatches: Int
  13.    
  14.     init(name: String, country: String) {
  15.         self.name = name
  16.         self.country = country
  17.         self.captain = nil
  18.         squad.append(Player(name: "Reus", position: "RW", age: 26, country: "Germany"))
  19.         squad.removeAll()
  20.         victories = 0
  21.         draws = 0
  22.         defeats = 0
  23.         goalsFor = 0
  24.         goalsAgainst = 0
  25.         points = 0
  26.         playedMatches = 0
  27.     }
  28.    
  29.     mutating func addVictory(goalsFor: Int, goalsAgainst: Int) {
  30.         victories+=1
  31.         points+=3
  32.         playedMatches+=1
  33.         self.goalsFor+=goalsFor
  34.         self.goalsAgainst+=goalsAgainst
  35.     }
  36.    
  37.     mutating func addDefeat(goalsFor: Int, goalsAgainst: Int) {
  38.         defeats+=1
  39.         playedMatches+=1
  40.         self.goalsFor+=goalsFor
  41.         self.goalsAgainst+=goalsAgainst
  42.     }
  43.    
  44.     mutating func addDraw(goals: Int) {
  45.         draws+=1
  46.         points+=1
  47.         playedMatches+=1
  48.         self.goalsFor+=goals
  49.         self.goalsAgainst+=goals
  50.     }
  51.    
  52.     func statusInTable() {
  53.         print("P: \(playedMatches) W: \(victories) D: \(draws) L: \(defeats) GF: \(goalsFor) GA:\(goalsAgainst) GD:\(goalsFor-goalsAgainst) \(points)")
  54.     }
  55. }
  56.  
  57. struct League {
  58.     var name: String
  59.     var country: String
  60.     var currentChampion: Club?
  61.     var teams: [Club] = []
  62.    
  63.     init(name: String, country: String) {
  64.         self.name = name
  65.         self.country = country
  66.         currentChampion = nil
  67.         teams.append(Club(name: "A", country: "A"))
  68.         teams.removeAll()
  69.     }
  70.    
  71.     func match(homeTeam: inout Club, awayTeam: inout Club, goalsForHomeTeam: Int, goalsForAwayTeam: Int) {
  72.         if goalsForHomeTeam > goalsForAwayTeam {
  73.             homeTeam.addVictory(goalsFor: goalsForHomeTeam, goalsAgainst: goalsForAwayTeam)
  74.             awayTeam.addDefeat(goalsFor: goalsForAwayTeam, goalsAgainst: goalsForHomeTeam)
  75.         }
  76.         else if goalsForHomeTeam==goalsForAwayTeam {
  77.             homeTeam.addDraw(goals: goalsForHomeTeam)
  78.             awayTeam.addDraw(goals: goalsForHomeTeam)
  79.         }
  80.         else {
  81.             homeTeam.addDefeat(goalsFor: goalsForHomeTeam, goalsAgainst: goalsForAwayTeam)
  82.             awayTeam.addVictory(goalsFor: goalsForAwayTeam, goalsAgainst: goalsForHomeTeam)
  83.         }
  84.     }
  85.    
  86.     mutating func createLTable() {
  87.         teams.sort {
  88.             $0.points > $1.points
  89.         }
  90.         for club in 0..<teams.count {
  91.             teams[club].statusInTable()
  92.         }
  93.     }
  94. }
  95.  
  96. //for now, totally useless struct and therefore I won't be creating its instances any time soon
  97. struct Player {
  98.     var name: String
  99.     var position: String
  100.     var age: Int
  101.     var country: String
  102. }
  103.  
  104. var premierLeague = League(name: "Premier League", country: "England")
  105.  
  106. var chelsea = Club(name: "Chelsea FC", country: "England")
  107. var arsenal = Club(name: "Arsenal FC", country: "England")
  108. var pool = Club(name: "Liverpool FC", country: "England")
  109. var manUnited = Club(name: "Manchester United FC", country: "England")
  110. premierLeague.teams.append(chelsea)
  111. premierLeague.teams.append(arsenal)
  112. premierLeague.teams.append(pool)
  113. premierLeague.teams.append(manUnited)
  114.  
  115.  
  116. //premierLeague.match(homeTeam: &chelsea, awayTeam: &manUnited, goalsForHomeTeam: 4, goalsForAwayTeam: 0)
  117. //premierLeague.match(homeTeam: &pool, awayTeam: &arsenal, goalsForHomeTeam: 3, goalsForAwayTeam: 3)
  118. //chelsea.statusInTable()
  119. //manUnited.statusInTable()
  120. //pool.statusInTable()
  121. //arsenal.statusInTable()
  122. //premierLeague.createLTable()
  123.  
  124. premierLeague.match(homeTeam: &premierLeague.teams[0], awayTeam: &premierLeague.teams[3], goalsForHomeTeam: 4, goalsForAwayTeam: 0)
  125. premierLeague.match(homeTeam: &premierLeague.teams[2], awayTeam: &premierLeague.teams[1], goalsForHomeTeam: 3, goalsForAwayTeam: 3)
  126.  
  127. premierLeague.createLTable()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement