python - 将 2D 坐标转换为 2D 数组中的相应索引
问题描述
我正在编写井字游戏,我管理每个单元格状态的方式是通过这样的数组:
cell_states = [
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1]
]
但是,我游戏中的每个单元格都有一个窗口坐标(x, y)
,(0, 0)
左下角在哪里。如何对函数进行编程,使其根据坐标输入返回二维数组的索引?我在想这样的事情:
index = coords_to_index(x=0, y=0) #(0, 0) should be at the bottom left corner.
print(index) #prints (2, 0), the geometrically corresponding index. This is the part I'm struggling with.
关于如何做到这一点的任何建议?欢迎提出更好的管理单元状态的方法。谢谢!
解决方案
推荐阅读
- ios - 启动应用程序时如何克服此错误?
- javascript - TypeError:无法读取未定义 React JS 的属性“handleClick”
- html - 即使我声明它,我的网站格式也没有间隔
- python - 将数据帧作为时间序列处理时,将新数据帧与旧数据帧进行比较?
- swift - SKAction 对儿童的奇怪行为
- python-3.x - 将值分配给 ndarray 中的中断切片
- javascript - 如何将 Javascript 文件中的值返回到 Node.js,以便写入文件?
- linux - 我想 grep [Nodes]:ALL from
- python - 如何访问在 `with` 语句中创建的变量
- typescript - 打字稿中所有对象文字类型的子类型?