算法来检查连四场算法、连四场

2023-09-11 01:54:40 作者:神明也愛过

我不知道什么是检查在连四场分出胜负的最好方法。

I'm wondering what's the best way to check for a winner on a connect four field.

我感兴趣的是你们的想法,以及是否有一些知名的算法这样的问题?

I'm interested in what you guys think and whether there is some "well-known" algorithm for this sort of problems?

解决方法:

我实现了在Python Ardavan的哈希表的解决方案。

I implemented Ardavan's hash-table solution in Python.

我让曾经的算法运行在各个领域。最佳的检查时间与我的执行是0.047毫秒,最坏的0.154毫秒,平均0.114毫秒对我的英特尔(R)酷睿(TM)2双核处理器T9600 @ 2.80GHz的。这是速度不够快,我的需要,并且算法似乎整齐的我。

I let the algorithm run over every field once. The best checking time with my implementation was 0.047 ms, the worst 0.154 ms and the average 0.114 ms on my Intel(R) Core(TM)2 Duo CPU T9600 @ 2.80GHz. This is fast enough for my needs, and the algorithm seems neat to me.

推荐答案

每个单元只能归因于12获胜组合的最大数量。 (4横,四纵4对角线)。每个组合将有4个单元,包括正在审议的之一。这些数字将要为所述细胞更靠近边要低得多。因此,这将是有意义的pre-编译这些组合和存储相关的细胞,它可以使一个单一的播放赢家的哈希散列。这样,每个单元后的球员,你只需拔出相关组合/细胞检查,如果它是一个胜利者。

Each cell can only attribute to a maximum number of 12 winning combinations. (4 horizontal, 4 vertical and 4 diagonal). Each combination would have 4 cells including the one under consideration. And these numbers are going to be much lower for the cells closer to the sides. So it would make sense to pre-compile these combinations and store a hash of hash of related cells which can make a single play a winner. This way after each cell is player you simply pull out the related combinations/cells to check if it's a winner.