gpt4 book ai didi

ios - 所有这些 if 语句真的有必要吗?

转载 作者:行者123 更新时间:2023-11-28 11:45:26 28 4
gpt4 key购买 nike

我有一个连接两个房间和走廊的功能。看起来效率很低,但我找不到更好的方法。它是我用于生成地下城的 BSP 算法的一部分。您可以找到完整的算法 here .

public func createHall(left:Room, right:Room) {
// Connects 2 rooms together with hallways

// Reset hallways function to make sure its empty. hallways = [Room]()
hallways = []

// get width and height of first room
let point1 = CGPoint(x: Int.random(in: (left.x1 + 1)..<(left.x2 - 1)),
y: Int.random(in: (left.y1 + 1)..<(left.y2 - 1)))

// get width and height of second room
let point2 = CGPoint(x: Int.random(in: (right.x1 + 1)..<(right.x2 - 1)),
y: Int.random(in: (right.y1 + 1)..<(right.y2 - 1)))

let w = point2.x - point1.x
let h = point2.y - point1.y

if w < 0 {
if h < 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
}
} else if h > 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
}
} else {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
}
} else if w > 0 {
if h < 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
}
} else if h > 0 {
if Double.random(in: 0..<1.0) > 0.5 {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
} else {
hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
}
} else {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
}
} else {
if h < 0 {
hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
} else if h > 0 {
hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
}
}
}

Room 是我编写的自定义类,用于计算矩形及其中心:

class Room {
var x1:Int
var x2:Int
var y1:Int
var y2:Int
var center:CGPoint

init(X: Int, Y: Int, W: Int, H: Int) {
x1 = X
x2 = X + W
y1 = Y
y2 = Y + H
center = CGPoint(x: (x1 + x2) / 2, y: (y1 + y2) / 2)
}
}

我最成功的尝试:

func hCorridor(x1: Int, x2: Int, y: Int) {
for x in min(x1,x2)...max(x1,x2) {
hallways.append(Room(X: y, Y: y, W: 1, H: Int(abs(h))))
}
}

func vCorridor(y1: Int, y2: Int, x: Int) {
for y in min(y1,y2)...max(y1,y2) {
hallways.append(Room(X: y, Y: y, W: Int(abs(w), H: 1))
}
}

// Randomly choose to start with horizontal or vertical corridors
if Double.random(in: 0..<1.0) > 0.5 {
hCorridor(x1: Int(point1.x), x2: Int(point2.x), y: Int(point1.y))
vCorridor(y1: Int(point1.y), y2: Int(point2.y), x: Int(point2.x))
} else {
vCorridor(y1: Int(point1.y), y2: Int(point2.y), x: Int(point1.x))
hCorridor(x1: Int(point1.x), x2: Int(point2.x), y: Int(point2.y))
}

createHall() 函数中的所有这些 if 语句真的有必要吗?如果没有,写它们的更好方法是什么?我所有的尝试都不像 if 语句那样有效。我的尝试给了我死胡同和无法进入的房间。

最佳答案

如果我正确理解你的问题,你

  • 先随机选择左边的两个点point1, point2(resp. right)房间,和
  • 然后用从point1point2的两条“走廊”连接房间,先水平然后垂直,反之亦然。

除了随机选择之外,如果您使用,则不需要 if 语句minabs 计算走廊坐标。类似的东西(内联更多解释):

if Bool.random() {
// Horizontally first, then vertically:
// From point1 to (point2.x, point1.y):
hallways.append(Room(X: Int(min(point1.x, point2.x)), Y: Int(point1.y),
W: Int(abs(point1.x - point2.x)), H: 1))
// From (point2.x, point1.y) to point2:
hallways.append(Room(X: Int(point2.x), Y: Int(min(point1.y, point2.y)),
W: 1, H: Int(abs(point1.y - point2.y))))
} else {
// Vertically first, then Horizontally:
// From point1 to (point1.x, point2.y):
hallways.append(Room(X: Int(point1.x), Y: Int(min(point1.y, point2.y)),
W: 1, H: Int(abs(point1.y - point2.y))))
// From (point1.x, point2.y) to point2:
hallways.append(Room(X: Int(min(point1.x, point2.x)), Y: Int(point2.y),
W: Int(abs(point1.x - point2.x)), H: 1))
}

关于ios - 所有这些 if 语句真的有必要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678708/

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