gpt4 book ai didi

Swift:如何在井字游戏中为我的 AI 添加延迟?

转载 作者:行者123 更新时间:2023-11-28 09:20:51 25 4
gpt4 key购买 nike

我刚刚在 Swift 中为我的井字游戏完成了 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

}
}

//=============================================================================================

最佳答案

我认为最简单的方法是使用 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() })

关于Swift:如何在井字游戏中为我的 AI 添加延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24716830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com