- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
对于小型角色扮演游戏,我会创建包含特定数量值的棋盘。
让我举个例子:
board =[[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 15, 0, 15, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 15, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 15, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 15, 0, 0, 0, 0, 0, 15, 0, 0],
[0, 0, 0, 0, 0, 15, 0, 0, 15, 0],
[15, 0, 15, 15, 0, 0, 0, 0, 0, 0]]
这是一个 10 x 10 的棋盘,其中 13 乘以值 15。
为了得到这个结果,我使用了一个在板上随机给出坐标的程序。
b2x = []
b2y = []
for i in range(B):
x = random.randint(0,L-1)
y = random.randint(0,H-1)
b2x.append(x)
b2y.append(y)
board[x][y] = 15
print('x : ',x, ' and y : ', y)
这里一个明显的问题是我们可以得到两个相似的坐标,这将使整数值减一。
事实上,在第一个示例的面板中,我没有值的数量,因为我向程序询问了 15 个值,它返回了 13。
所以我尝试在没有坐标检查的情况下解决这个问题,现在似乎无法正常工作。
for j in range(len(bo2x)):
if (x == b2x[j-1]) and (y == b2y[j-1]):
i -= 1 # affect the for i in range(B) loop
完整代码:
b2x = []
b2y = []
for i in range(B):
x = random.randint(0,L-1)
y = random.randint(0,H-1)
for j in range(len(bo2x)):
if (x == b2x[j-1]) and (y == b2y[j-1]):
i -= 1 # affect the for i in range(B) loop
b2x.append(x)
b2y.append(y)
board[x][y] = 15
print('x : ',x, ' and y : ', y)
结果,经过多次尝试,根本没有任何变化:
random generation
x : 5 and y : 4
x : 1 and y : 3
x : 7 and y : 7
x : 7 and y : 5
x : 0 and y : 7
x : 0 and y : 1
x : 6 and y : 2
x : 3 and y : 6
x : 9 and y : 4
x : 5 and y : 9
x : 6 and y : 8
x : 6 and y : 7
x : 3 and y : 6
x : 3 and y : 7
x : 7 and y : 5
[Finished in 0.2s]
如您所见,行 x : 7 and y : 5
和行 x : 3 and y : 6
在生成中出现了两次。
有人可以帮助我达到我的预期结果吗?
预期结果(概念):
x : 5 and y : 4
x : 1 and y : 3
x : 7 and y : 7
x : 7 and y : 5
x : 0 and y : 7
x : 0 and y : 1
x : 6 and y : 2
x : 3 and y : 6
x : 9 and y : 4
x : 5 and y : 9
x : 6 and y : 8
x : 6 and y : 7
x : 3 and y : 6
this line already exist !
x : 3 and y : 7
x : 7 and y : 5
this line already exist !
x : 1 and y : 3
x : 9 and y : 4
board :
[[0, 15, 15, 0, 0, 0, 0, 15, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 15, 15, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 15, 0, 0, 0, 0, 15],
[0, 0, 15, 0, 0, 0, 0, 15, 15, 0],
[0, 0, 0, 0, 0, 15, 0, 15, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 15, 0, 0, 0, 0, 0]]
最佳答案
您想创建一个 LxB 板。
L, H = 10, 10
这可以通过一行代码实现:
board = [[0] * L for _ in range(H)]
您想在棋盘上的不同位置放置某个数字 15 次。
生成随机坐标,但如果数字已分配给索引字段,则跳过坐标:
count = 15
number = 15
b = []
while count > 0:
x, y = random.randint(0,L-1), random.randint(0,H-1)
if board[y][x] != number:
board[y][x] = number
b.append((x, y))
count -= 1
for row in board:
print(row)
print(b)
关于python - 游戏 : Generate x random and different coordinates in a definited board,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56923970/
在我的项目中,我使用 LocationSearchTable/MKLocalSearch。当用户点击一个项目时,我的添加注释方法在 MapViewController 中被调用: func dropP
我很想知道如何检查字符串是否具有两个坐标的格式,例如: (signed int x,signed int y) 我已经通过搜索找到了一些答案,但我还没有完全理解它们(刚开始使用 C++),我要求一个简
在我正在编写的程序中,我为两个类(坐标和图形)编写了代码,其中一个将另一个作为构造函数参数。当我尝试编译它时,出现以下 Graph.cpp 错误: undefined symbol : “Graph:
我在 OpenGL ES 中绘制变化的条,它有坐标 (x,y,z)。 我想在 Action_Down 上添加一些更改(例如:栏中的颜色)。有 getX(),但此方法返回以像素为单位的 x 坐标,而不是
因为我之前的问题很不清楚,所以我编辑了一下: 我有以下问题: 我想为半径为 r+fcr_size 的空心球体构建一个图案。空心球体的空腔半径应为 r。有了这个图案,我可以在许多不同的球体中心使用它,并
我想使用 Vuforia 检测标记并在其上放置一个 3d 对象。从那时起,我想在我的应用程序中使用 ARKit。我如何知道检测到的标记或 3d 对象的 ARKit 世界变换? 我正在使用相同的 Vie
我有一组纬度/经度坐标,我可以使用它们进行投影,例如 Mollweide 投影。 library(mapproj) set.seed(0) n 180] = my.points$x[my.point
我正在使用 JavaScript 进行视频处理,我做得很好,但我使用的是一种名为 canvasCtx.rect () 的方法,它接收这些参数。 然后我可以从使用 getImageData () 方法绘
我创建了一个新项目作为单 View 应用程序。在 View Controller 中添加了用于将 View 坐标转换为窗口坐标的代码: - (void) dumpFrame { CGRect
简短版本:如何将 SVG 路径添加到 Leaflet map ,以便在 map 坐标更改时(例如缩放更改或滑动时)路径会更新? 长版:你好,我有一个地形image包含建筑轮廓。对图像进行地理校正后,我
我编写了一个代码,使用 astropy 将坐标从地球固定系统转换为惯性坐标系: from astropy import coordinates as coord from astropy import
我的多显示器设置中遇到以下情况: 在此示例中,我希望将窗口精确定位在黄色箭头所示的坐标处。然而,我所拥有的只是 NSView 的坐标,它是跨越整个(更大、更上面)辅助监视器的 NSWindow 的 c
我不知道如何将 Pane 上圆形对象的所有 x 和 y 与鼠标的 x 和 y 进行比较。我正在处理的问题要求我设置它,以便鼠标的二次单击会在放置在其上时删除一个点,我想我可以通过比较圆坐标和鼠标坐标的
我正在尝试将图像中的几个点转换为 OpenCV 中的极坐标。我遇到了名为 cartToPolar 的函数,它会为我的点提供相对于 0,0 作为我的原点的极坐标。但是,我想通过将图像中的另一个点作为原点
是的,我想知道如何检查某个坐标是否在另一个坐标半径内。但这可能是一个很小的差异,因为纬度、经度、半径存储在数据库中,而我们要检查的只是给定的坐标。 示例 数据库表 name | lat
我正在使用带有图层的 map (来自示例): var lonLat = new OpenLayers.LonLat(40.4088576, -86.8576718) .
我在坐标转换方面遇到了一些麻烦。 我在屏幕上有一个已知坐标(x,y)的对象,我想将其转换为世界坐标(x,y,z),因为它会投影在相机的近平面上。 到目前为止,我可以像这样在Z平面上进行投影: var
我有4个足球场点(角点): P1(lat, lon, alt) , P2(lat, lon, alt) , P3(lat, lon, alt) , P4(lat, lon, alt) . 以及球场上的
我有一个交换了纬度和经度位置的 NetCDF 文件。 我通常使用的 Netcdf 的标准方式定义如下: Coordinates: * time (time) datetime64[n
如果 line1 和 lin2 都由 x,y,alpha 定义,其中 x,y 是直线上一点的坐标,alpha 是直线与 x=const 之间的角度,如何找到 line1 和 lin2 相交的点? 我尝
我是一名优秀的程序员,十分优秀!