我刚刚在Swift完成了我的tic tac toe游戏的AI,虽然一切正常,但用户在没有“思考”的情况下玩游戏后立即让计算机对手玩游戏似乎有点奇怪。如何为AI的响应添加延迟?
我的AI代码:
func aiTurn() {
if done {
return
}
aiDeciding = true
//Computer: Has 2 in row
if let result = rowcheck(value:0) {
var wherePlayResult = wherePlay(result.location, pattern:result.pattern)
if !isOccupied(wherePlayResult) {
setImgforCircle(wherePlayResult, player: 0)
aiDeciding = false
checkforWin()
return
}
}
//Player: Has 2 in row
if let result = rowcheck(value: 1) {
var wherePlayResult = wherePlay(result.location, pattern:result.pattern)
if !isOccupied(wherePlayResult) {
setImgforCircle(wherePlayResult, player: 0)
aiDeciding = false
checkforWin()
return
}
}
func firstOpen(#isCorner:Bool)->(Int?) {
var spots = isCorner ? [1,3,7,9] : [4,2,8,6]
for spot in spots {
if !isOccupied(spot) {
return spot
}
}
return nil
}
//Computer: Center spot available?
if !isOccupied(5) {
setImgforCircle(5, player: 0)
aiDeciding = false
checkforWin()
return
}
//Computer: Corner available?
if let cornerOpen = firstOpen(isCorner: true) {
setImgforCircle(cornerOpen, player: 0)
aiDeciding = false
checkforWin()
return
}
//Computer: Side spot available?
if let sideOpen = firstOpen(isCorner: false) {
setImgforCircle(sideOpen, player: 0)
aiDeciding = false
checkforWin()
return
}
ResultsLabel.hidden = false
ResultsLabel.text = "Tied!"
Results2.hidden = false
aiDeciding = false
}
func wherePlay(location:String, pattern:String)->(Int) {
var pattern1 = "011"
var pattern2 = "101"
var pattern3 = "110"
switch location {
case "top":
if pattern == pattern1 {
return 1
}
else if pattern == pattern2 {
return 2
}
else {
return 3
}
case "bottom":
if pattern == pattern1 {
return 7
}
else if pattern == pattern2 {
return 8
}
else {
return 9
}
case "middle across":
if pattern == pattern1 {
return 4
}
else if pattern == pattern2 {
return 5
}
else {
return 6
}
case "left down":
if pattern == pattern1 {
return 1
}
else if pattern == pattern2 {
return 4
}
else {
return 7
}
case "middle down":
if pattern == pattern1 {
return 2
}
else if pattern == pattern2 {
return 5
}
else {
return 8
}
case "right down":
if pattern == pattern1 {
return 3
}
else if pattern == pattern2 {
return 6
}
else {
return 9
}
case "left-right diagonal":
if pattern == pattern1 {
return 1
}
else if pattern == pattern2 {
return 5
}
else {
return 9
}
case "right-left diagonal":
if pattern == pattern1 {
return 3
}
else if pattern == pattern2 {
return 5
}
else {
return 7
}
default: return 4
}
}
//=============================================================================================
答案 0 :(得分:0)
我认为最简单的方法是使用dispatch_after延迟调用aiTurn()
函数。
假设它是viewController中的一个方法,它看起来像这样。而不是调用self.aiTurn()
。使用
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC * 2))
dispatch_after(dispatch_time, dispatch_get_main_queue(),{ aiTurn() })
答案 1 :(得分:0)
@IBAction func UIButtonClicked(sender:UIButton){
userMessage.hidden = true
if !plays[sender.tag] && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
checkForWin()
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
delay(0.4) {
self.aiTurn()
// do stuff
}
}