四成一排的逻辑 [英] Four in a row logic
问题描述
我目前工作的一个基本的四连胜的比赛为我自己,但我宁愿停留在它背后的逻辑。
I'm currently working on a basic four in a row game for myself, but I'm rather stuck at the logic behind it.
目前我有这样的多维数组重新presents板
Currently I have this multi-dimensional array that represents the board
[
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
]
0
将重新present一个空的插槽,而 1
和 2
重present的球员。
因此,让后说,而你得到这个数组:
0
would represent an empty slot, while 1
and 2
represent a player.
So let's say after a while you get this array:
[
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 2, 2, 2, 0],
[0, 1, 2, 2, 1, 2, 0]
]
我怎么能写出逻辑来检查是否有四连胜? 它的计算水平和垂直的似乎比较容易(虽然还是找出最好的方式),但我将如何做到这一点的对角线?
How can I write the logic to check if there are four in a row? Calculating it for horizontal and vertical ones seem rather easy (although still figuring out the best way), but how would I do this for diagonal lines?
推荐答案
最好的办法是大概划分搜索空间分为四种:
Best bet is to probably divide the search space into four:
- 垂直;
- 水平;
- 右键和向下;
- 右键及以上。
然后限制你的起点和终点坐标的基础上的方向。
then limit your starting and ending coordinates based on the direction.
例如,假设你的数组是板[行= 0-5] [COL = 0-6]
与板[0] [0]
在左上角。
For example, let's say your array is board[row=0-5][col=0-6]
with board[0][0]
at the top left.
第一纵(环都包括在的两个的这个伪code结尾):
First vertical (loops are inclusive at both ends in this pseudo-code):
for row = 0 to 2:
for col = 0 to 6:
if board[row][col] != 0 and
board[row][col] == board[row+1][col] and
board[row][col] == board[row+2][col] and
board[row][col] == board[row+3][col]:
return board[row][col]
这限制了可能性只有那些不扩展超出板的边缘,一个问题大多数解决方案都当他们通过检查每个单元,并从那里走出去在所有方向上简单地启动。通过这一点,我的意思是有没有检查点3的起始行,仅仅是因为这将涉及到行3,4,5和6(后者不存在)。
This limits the possibilities to only those that don't extend off the edge of the board, a problem most solutions have when they simplistically start by checking each cell and going out in all directions from there. By that, I mean there's no point checking a start row of 3, simply because that would involve rows 3, 4, 5 and 6 (the latter which does not exist).
同样,对于水平:
for row = 0 to 5:
for col = 0 to 3:
if board[row][col] != 0 and
board[row][col] == board[row][col+1] and
board[row][col] == board[row][col+2] and
board[row][col] == board[row][col+3]:
return board[row][col]
有关向右和向下,其次是右和上:
For right and down, followed by right and up:
for row = 0 to 2:
for col = 0 to 3:
if board[row][col] != 0 and
board[row][col] == board[row+1][col+1] and
board[row][col] == board[row+2][col+2] and
board[row][col] == board[row+3][col+3]:
return board[row][col]
for row = 3 to 5:
for col = 0 to 3:
if board[row][col] != 0 and
board[row][col] == board[row-1][col+1] and
board[row][col] == board[row-2][col+2] and
board[row][col] == board[row-3][col+3]:
return board[row][col]
现在,你可以真正的合并的这两个通过为COL = 0至3
外循环,只这样做一次,而不是两次但我其实preFER让他们分开(与合适的注释),因此它更容易理解。但是,如果你沉迷于性能,你可以试试:
Now, you could actually combine those two by making for col = 0 to 3
the outer loop and only doing it once rather than twice but I actually prefer to keep them separate (with suitable comments) so that it's easier to understand. However, if you're addicted to performance, you can try:
for col = 0 to 3:
for row = 0 to 2:
if board[row][col] != 0 and
board[row][col] == board[row+1][col+1] and
board[row][col] == board[row+2][col+2] and
board[row][col] == board[row+3][col+3]:
return board[row][col]
for row = 3 to 5:
if board[row][col] != 0 and
board[row][col] == board[row-1][col+1] and
board[row][col] == board[row-2][col+2] and
board[row][col] == board[row-3][col+3]:
return board[row][col]
而不是赢家
之后,如果被发现在四个可能的方向没有胜,只返回 0
1
或 2
。
因此,举例来说,你的样板:
So, for example, your sample board:
row
0 [0, 0, 0, 0, 0, 0, 0]
1 [0, 0, 0, 0, 0, 0, 0]
2 [0, 0, 0, 1, 1, 0, 0]
3 [0, 0, 0, 1, 1, 0, 0]
4 [0, 0, 1, 2, 2, 2, 0]
5 > [0, 1, 2, 2, 1, 2, 0]
^
0 1 2 3 4 5 6 <- col
将检测到的赢家,在右和上环,其中起始细胞是 {5,1}
,因为 {5,1}
, {4,2}
, {3,3}
和 {2,4}
都设置为 1
。
would detect a winner in the right and up loop where the starting cell was {5,1}
because {5,1}
, {4,2}
, {3,3}
and {2,4}
are all set to 1
.
这篇关于四成一排的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!