From 63d9aae0bf10cd45b3aa338fcd72a46ba10da08f Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 19 Jun 2023 18:55:31 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E6=98=93?= =?UTF-8?q?=E4=BA=8E=E8=B0=83=E8=AF=95=E4=BD=BF=E7=94=A8=E7=9A=84=E5=B9=B3?= =?UTF-8?q?=E9=9D=A2=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/geometry/floor_plan.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 utils/geometry/floor_plan.go diff --git a/utils/geometry/floor_plan.go b/utils/geometry/floor_plan.go new file mode 100644 index 0000000..758b9ad --- /dev/null +++ b/utils/geometry/floor_plan.go @@ -0,0 +1,32 @@ +package geometry + +import ( + "strings" +) + +// FloorPlan 平面图 +type FloorPlan []string + +// IsFree 检查位置是否为空格 +func (slf FloorPlan) IsFree(point Point[int]) bool { + return slf.IsInBounds(point) && slf[point.GetY()][point.GetX()] == ' ' +} + +// IsInBounds 检查位置是否在边界内 +func (slf FloorPlan) IsInBounds(point Point[int]) bool { + return (0 <= point.GetX() && point.GetX() < len(slf[point.GetY()])) && (0 <= point.GetY() && point.GetY() < len(slf)) +} + +// Put 设置平面图特定位置的字符 +func (slf FloorPlan) Put(point Point[int], c rune) { + slf[point.GetY()] = slf[point.GetY()][:point.GetX()] + string(c) + slf[point.GetY()][point.GetX()+1:] +} + +// String 获取平面图结果 +func (slf FloorPlan) String() string { + var result string + for _, row := range slf { + result += row + "\n" + } + return strings.TrimSuffix(result, "\n") +}