9.記色彩遊戲
import UIKit
enum ButtonColor : Int{
case Red = 1
case Green = 2
case Blue = 3
case Yellow = 4
}
//玩家和電腦
enum WhoseTurn{
case Human
case Computer
}
//與模型相關的對象和變量
let winningNumber : Int = 5 //獲勝條件
var currentPlayer : WhoseTurn = .Computer
var inputs = [ButtonColor]()//按鈕順序存儲
var indexOfNextButtonToTouch : Int = 0 //存儲按鈕的索引
var highlightSquareTime = 1.5 //按鈕顯示時長,越大越久,遊戲越容易
var redButton :UIButton!
var yellowButton :UIButton!
var greenButton :UIButton!
var blueButton :UIButton!
class ViewController: UIViewController,UIAlertViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//與視圖相關的對象和變量
let centerView = UIView()
centerView.frame = CGRect(x: 20, y: 100, width: self.view.frame.width-40, height: self.view.frame.width-40);
centerView.backgroundColor = UIColor.gray
self.view.addSubview(centerView)
redButton = UIButton(type: .custom);//這樣也是能夠的
//設置frame
redButton.frame = CGRect(x: 10, y: 10, width: 128 , height: 128);
//設置按鈕的背景顏色
redButton.backgroundColor = UIColor.red;
redButton.tag = 1
//添加按鈕到視圖控制器
centerView.addSubview(redButton);
redButton.addTarget(self, action:#selector(btnClike(_:)), for: UIControlEvents.touchUpInside);
yellowButton = UIButton(type: .custom)
yellowButton.frame = CGRect(x:redButton.frame.width+50,y:10,width:128,height:128)
yellowButton.backgroundColor = UIColor.yellow
centerView.addSubview(yellowButton)
yellowButton.tag = 4
yellowButton.addTarget(self, action: #selector(btnClike(_:)), for: UIControlEvents.touchUpInside)
greenButton = UIButton(type: .custom)
greenButton.frame = CGRect(x:10,y:redButton.frame.origin.y+redButton.frame.height + 20,width:128,height:128)
greenButton.backgroundColor = UIColor.green
centerView.addSubview(greenButton)
greenButton.tag = 2
greenButton.addTarget(self, action: #selector(btnClike(_:)), for: UIControlEvents.touchUpInside)
blueButton = UIButton(type: .custom)
blueButton.frame = CGRect(x:redButton.frame.width+50,y:redButton.frame.origin.y+redButton.frame.height + 20,width:128,height:128)
blueButton.backgroundColor = UIColor.blue
blueButton.tag = 3
centerView.addSubview(blueButton)
blueButton.addTarget(self, action: #selector(btnClike(_:)), for: UIControlEvents.touchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
startNewGame()
}
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
startNewGame()
}
func buttonByColor(color : ButtonColor) ->UIButton{
switch color {
case .Red:
return redButton
case .Green:
return greenButton
case .Blue:
return blueButton
case .Yellow:
return yellowButton
}
}
func playSequence(index : Int,highlightTime : Double){
currentPlayer = .Computer//當前輪到計算機控制
if index == inputs.count{
currentPlayer = .Human
return
}
let button : UIButton = buttonByColor(color: inputs[index])
let originalColor : UIColor? = button.backgroundColor
let highlightColor : UIColor = UIColor.white
UIView.animate(withDuration: highlightTime,
delay: 0.0,
options: [.curveLinear,.allowUserInteraction,.beginFromCurrentState],
animations: {
button.backgroundColor = highlightColor
},completion: { finished in
button.backgroundColor = originalColor
let newIndex : Int = index + 1
self.playSequence(index: newIndex, highlightTime: highlightTime)//遞歸
})
}
func btnClike(_ sender:UIButton) {
//根據tag肯定觸摸的是哪一個按鈕
let buttonTag : Int = sender.tag
if let colorTouched = ButtonColor(rawValue : buttonTag){//可選鏈,檢查建立的ButtonColor的結果是否爲nil
if currentPlayer == .Computer{
return//只要這個條件爲true,就忽略觸摸
}
if colorTouched == inputs[indexOfNextButtonToTouch]{
//玩家觸摸了正確的按鈕
indexOfNextButtonToTouch+=1
//判斷這一輪是否還有其餘按鈕須要觸摸
if indexOfNextButtonToTouch == inputs.count{
//玩家成功完成了這一輪
if advanceGame() == false{
playerWins()
}
indexOfNextButtonToTouch = 0
}else{
//還有其餘按鈕須要觸摸
}
}else{
//玩家觸摸的按鈕不對
playerLoses()
indexOfNextButtonToTouch = 0
}
}
}
func playerLoses() -> Void{
let winner : UIAlertView = UIAlertView(title:"You Lost!",message:"Sorry!",delegate:self,cancelButtonTitle:nil)
winner.show()
}
func playerWins() -> Void{
let winner : UIAlertView = UIAlertView(title:"You Won!",message:"Congratulation",delegate:self,cancelButtonTitle:nil)
winner.show()
}
func randomButton()-> ButtonColor{
let v : Int = Int(arc4random_uniform(_:UInt32(4))) + 1
let result = ButtonColor(rawValue : v)
return result!
}
func startNewGame() -> Void{
//生成隨機的輸入數組
inputs = [ButtonColor]()
let result : Bool = advanceGame()
print("\(result)")
}
func advanceGame() -> Bool{
var result : Bool = true
if inputs.count == winningNumber{
result = false
}else{
//輸入數組中添加一個隨機數
inputs += [randomButton()]
//亮起一個按鈕或等待玩家開始觸摸按鈕
playSequence(index: 0, highlightTime: highlightSquareTime)
}
return result
}
}