【发布时间】:2015-11-20 22:08:22
【问题描述】:
我正在尝试为我正在编写的黑白棋/黑白棋游戏实现一个功能,我认为它的效率非常低。
所以基本上,有一个游戏板具有用户设置的行数和列数(第 1 行从顶部开始,第 1 列从左侧开始)。
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . W W W . . . .
. . . . B B . . . .
. . . . B B B W . .
. . . . . . . B . .
. . . . . . . . . .
. . . . . . . . . .
B 代表黑色,并保存整数 1。 W代表白色,并保存整数2。 一个空白处保存整数 0。
所以 boardArray[7][7] 将返回值 1。(第 8 行第 8 列)
我正在编写一个函数来检查用户输入动作的有效性。假设黑棋手想要将他的棋子插入第 9 行第 5 列。从该位置开始,程序必须检查所有方向(北、东北、东、东南等),以查看是否找到黑棋。如果找到,它将检查两个黑色块之间是否有白色块。如果找到一块白块,那块白块就会变成一块黑块。
目前,我正在以一种极其低效的方式进行尝试。
#north
x = 1
while True:
try:
if boardArray[row-x][col] == 0:
break
elif boardArray[row-x][col] == self._playerTurn:
#function that flips all pieces in between the user inputted location and the location of the same-color piece found
except IndexError:
break
x += 1
#northeast
x = 1
while True:
try:
if boardArray[row-x][col+x] == 0:
break
elif boardArray[row-x][col+x] == self._playerTurn:
#function that flips all pieces in between the user inputted location and the location of the same-color piece found
except IndexError:
break
x += 1
#east
x = 1
while True:
try:
if boardArray[row][col+x] == 0:
break
elif boardArray[row][col+x] == self._playerTurn:
#function that flips all pieces in between the user inputted location and the location of the same-color piece found
except IndexError:
break
x += 1
等等
- 谁能建议我以更有效的方式完成此任务?
- 什么是存储我们必须翻转的棋子位置的好方法?
希望这篇文章有意义!如果你知道奥赛罗游戏的规则会更容易理解。
先谢谢了! - Python新手
【问题讨论】:
标签: python python-3.x