Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player {
- private static let DEFAULT_NAME: String = "Player"
- private static var s_lastId: Int = 0
- private static func getNextId() -> Int {
- s_lastId += 1
- return s_lastId
- }
- private var _id:Int
- private var _name:String
- private var _score:Int = 0
- private var _level:Int = 1
- var name:String {
- get{
- return _name
- }
- set {
- if newValue.count > 0 {
- _name = newValue
- }
- }
- }
- init(myName:String) {
- if myName.count == 0 {
- _name = Player.DEFAULT_NAME
- } else {
- _name = myName
- }
- _id = Player.getNextId()
- }
- func getScore() -> Int {
- return _score
- }
- func getLevel() -> Int {
- return _level
- }
- func gameWon() {
- changeScore(_score + 5)
- }
- func gameLost() {
- changeScore(_score - 2)
- }
- func changeScore(_ newScore:Int) {
- _score = newScore
- updateLevel()
- }
- private func updateLevel() {
- if _score < 10 {
- _level = 1
- } else if _score < 20 {
- _level = 2
- } else {
- _level = 3
- }
- }
- }
- let player1 = Player(myName: "Brigitte")
- let player2 = Player(myName: "Francis")
- player1.gameWon()
- player1.gameWon()
- player1.gameLost()
- player1.changeScore(15)
- let player3 = Player(myName: "Yves")
- print("Player3 : \(player3.name)") // lecture de la variable (dans name > get{})
- player3.name = "Jackie" // écriture de la variable (dans name > set{})
Advertisement
Add Comment
Please, Sign In to add comment