Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Club {
- var name: String
- var country: String
- var squad: [Player] = []
- var captain: Player?
- var victories: Int
- var draws: Int
- var defeats: Int
- var goalsFor: Int
- var goalsAgainst: Int
- var points: Int
- var playedMatches: Int
- init(name: String, country: String) {
- self.name = name
- self.country = country
- self.captain = nil
- squad.append(Player(name: "Reus", position: "RW", age: 26, country: "Germany"))
- squad.removeAll()
- victories = 0
- draws = 0
- defeats = 0
- goalsFor = 0
- goalsAgainst = 0
- points = 0
- playedMatches = 0
- }
- mutating func addVictory(goalsFor: Int, goalsAgainst: Int) {
- victories+=1
- points+=3
- playedMatches+=1
- self.goalsFor+=goalsFor
- self.goalsAgainst+=goalsAgainst
- }
- mutating func addDefeat(goalsFor: Int, goalsAgainst: Int) {
- defeats+=1
- playedMatches+=1
- self.goalsFor+=goalsFor
- self.goalsAgainst+=goalsAgainst
- }
- mutating func addDraw(goals: Int) {
- draws+=1
- points+=1
- playedMatches+=1
- self.goalsFor+=goals
- self.goalsAgainst+=goals
- }
- func statusInTable() {
- print("P: \(playedMatches) W: \(victories) D: \(draws) L: \(defeats) GF: \(goalsFor) GA:\(goalsAgainst) GD:\(goalsFor-goalsAgainst) \(points)")
- }
- }
- struct League {
- var name: String
- var country: String
- var currentChampion: Club?
- var teams: [Club] = []
- init(name: String, country: String) {
- self.name = name
- self.country = country
- currentChampion = nil
- teams.append(Club(name: "A", country: "A"))
- teams.removeAll()
- }
- func match(homeTeam: inout Club, awayTeam: inout Club, goalsForHomeTeam: Int, goalsForAwayTeam: Int) {
- if goalsForHomeTeam > goalsForAwayTeam {
- homeTeam.addVictory(goalsFor: goalsForHomeTeam, goalsAgainst: goalsForAwayTeam)
- awayTeam.addDefeat(goalsFor: goalsForAwayTeam, goalsAgainst: goalsForHomeTeam)
- }
- else if goalsForHomeTeam==goalsForAwayTeam {
- homeTeam.addDraw(goals: goalsForHomeTeam)
- awayTeam.addDraw(goals: goalsForHomeTeam)
- }
- else {
- homeTeam.addDefeat(goalsFor: goalsForHomeTeam, goalsAgainst: goalsForAwayTeam)
- awayTeam.addVictory(goalsFor: goalsForAwayTeam, goalsAgainst: goalsForHomeTeam)
- }
- }
- mutating func createLTable() {
- teams.sort {
- $0.points > $1.points
- }
- for club in 0..<teams.count {
- teams[club].statusInTable()
- }
- }
- }
- //for now, totally useless struct and therefore I won't be creating its instances any time soon
- struct Player {
- var name: String
- var position: String
- var age: Int
- var country: String
- }
- var premierLeague = League(name: "Premier League", country: "England")
- var chelsea = Club(name: "Chelsea FC", country: "England")
- var arsenal = Club(name: "Arsenal FC", country: "England")
- var pool = Club(name: "Liverpool FC", country: "England")
- var manUnited = Club(name: "Manchester United FC", country: "England")
- premierLeague.teams.append(chelsea)
- premierLeague.teams.append(arsenal)
- premierLeague.teams.append(pool)
- premierLeague.teams.append(manUnited)
- //premierLeague.match(homeTeam: &chelsea, awayTeam: &manUnited, goalsForHomeTeam: 4, goalsForAwayTeam: 0)
- //premierLeague.match(homeTeam: &pool, awayTeam: &arsenal, goalsForHomeTeam: 3, goalsForAwayTeam: 3)
- //chelsea.statusInTable()
- //manUnited.statusInTable()
- //pool.statusInTable()
- //arsenal.statusInTable()
- //premierLeague.createLTable()
- premierLeague.match(homeTeam: &premierLeague.teams[0], awayTeam: &premierLeague.teams[3], goalsForHomeTeam: 4, goalsForAwayTeam: 0)
- premierLeague.match(homeTeam: &premierLeague.teams[2], awayTeam: &premierLeague.teams[1], goalsForHomeTeam: 3, goalsForAwayTeam: 3)
- premierLeague.createLTable()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement