//*****************************************************************
//
// 代理:delegate
// 代理是一种设计模式,它允许类或结构体将一些需要他们负责的功能交给其他的类型
// 代理的实现:定义协议来封装那些需要被代理的函数和方法,使其遵循者拥有这些被
// 代理的函数和方法
//
//*****************************************************************
//
// 下面的协议和类是前期准备
//
//**************************
protocol ProtocolRandom {
func random() -> Double
}
class Dice {
let sides: Int
let generator: ProtocolRandom
init(sides: Int,generator: ProtocolRandom) {
self.sides= sides
self.generator= generator
}
func roll() -> Int{
return Int(generator.random()* Double(sides)+ 1 )
}
}
class Gener: ProtocolRandom {
var lastRandom = 42.0
let m = 1234.56,a = 2233.11, c = 451.23
func random() -> Double{
lastRandom = (lastRandom * a + c) % m
return lastRandom/ m
}
}
//**************************
//
// 下面是两个基于色子游戏的协议
// 1.DiceGame协议可以在任意含有色子的游戏中实现
// 2.DiceGameDelegate协议用来追踪DiceGame的游戏过程
//
//**************************
protocol DiceGame {
var dice: Dice{ get }
func play()
}
//**************************************************************
//
// DiceGameDelegate提供了3个方法跟踪游戏过程,分别是开始,每轮摇色及结束
//
//**************************************************************
protocol DiceGameDelegate {
func gameDidStart (game: DiceGame)
func game(game: DiceGame,didStartNewTurnWithDiceRoll diceRoll: Int)
func gameDidEnd(game: DiceGame)
}
//**************************************************************
//
// delegate是一个遵循DiceGameDelegate协议的可选属性
// 在func()方法中调用代理方法gameDidStart、game和gameDidEnd进行游戏跟踪
//
//**************************************************************
class SnakesAndLadders: DiceGame{
let finalSquare = 25
let dice = Dice(sides:7, generator: Gener())
var square = 0
var board: [Int]
init() {
board = [Int](count:finalSquare + 1,repeatedValue: 0 )
board[03]= +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14]= -10; board[19] = -11; board[22] = -02; board[24] = -08
}
var delegate: DiceGameDelegate?
func play() {
square = 0
delegate?.gameDidStart(self)
gameLoop: while square != finalSquare{
let diceRoll = dice.roll()
delegate?.game(self,didStartNewTurnWithDiceRoll: diceRoll)
switch square + diceRoll {
case finalSquare: breakgameLoop
case let newSquare wherenewSquare > finalSquare: continue gameLoop
default:
square+= diceRoll
square+= board[square]
}
}
delegate?.gameDidEnd(self)
}
func test(){ println("Testing!")}
}
//**************************************************************
//
// DiceGameTracker实现了DiceGameDelegate协议的方法要求,用于跟踪游戏过程
// 跟踪时,传递被跟踪对象DiceGame过来给DiceGameTracker调用,也就是代理DiceGame
// 进行另外的工作(游戏跟踪)
//
//**************************************************************
class DiceGameTracker: DiceGameDelegate{
var numberOfTurns = 0
func gameDidStart(game: DiceGame){
numberOfTurns = 0
if game isSnakesAndLadders {
println("Starteda new game of 蛇与梯子!")
}
if letgameon = game as? SnakesAndLadders{
gameon.test()
}
println("the game is usinga \(game.dice.sides)-sides dice")
}
func game(game: DiceGame,didStartNewTurnWithDiceRoll diceRoll: Int) {
++numberOfTurns
println("Rollednumber = \(diceRoll)")
}
func gameDidEnd(game: DiceGame){
println("The game lastedfor \(numberOfTurns) turns")
}
}
let tracker = DiceGameTracker()
let game = SnakesAndLadders()
game.delegate = tracker
game.play()
//运行结果:
//Started a new game of 蛇与梯子!
//Testing!
//thegame is using a 7-sides dice
//Rollednumber = 3
//Rollednumber = 4
//Rollednumber = 2
//Rollednumber = 4
//Rollednumber = 3
//Rollednumber = 4
//Rollednumber = 1
//Rollednumber = 6
//Rollednumber = 4
//Thegame lasted for 9 turns