检测获胜的比赛中零和十字架 [英] Detect winning game in nought and crosses

查看:170
本文介绍了检测获胜的比赛中零和十字架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道检测游戏来玩圈和十字架的招制胜的最佳方式。来源$ C ​​$ C无所谓,我只是需要一个例子或东西我可以开始。

I need to know the best way to detect a winning move in a game of noughts and crosses. Source code doesn't matter, I just need a example or something I can start with.

我能想出的唯一的事情就是用循环和测试每个方向的一举一动球员做,搜索,例如五连胜。是否有一个更快,更有效的方式?

The only thing I can come up with is to use loops and test every direction for every move a player makes, to search for e.g five in a row. Is there a faster and more efficient way?

推荐答案

真正简单的办法是刚刚从所做的最后一步检查......很明显,没有事先的举动可以赢得比赛,否则你就不会在这里......所以你只需要检查,看看是否有5个(或包含很多)的行/列/对角线周围刚放置的举动。

The real easy solution is to just check from the last move made...obviously, no prior move could have won the game, or you wouldn't be here...so you just need to check to see if there are 5 (or however many) in a row/column/diagonal around the move that was just placed.

例如,如果董事会是这样的,而X标志着最近的举动:

For example, if the board looks like this, and X marks the most recent move:

.............
.............
.............
.............
.....X.......
.............
.............
.............
.............
.............

您不必为C的范围外的检查什么:

You don't need to check anything outside the range of "C":

.C...C...C...
..C..C..C....
...C.C.C.....
....CCC......
.CCCCXCCCC...
....CCC......
...C.C.C.....
..C..C..C....
.C...C...C...
.............

帮助吗? (它看起来像你可能会暗指这在你原来的问题,但我不知道。)

Does that help? (It looked like you might be alluding to this in your original question, but I wasn't sure.)

除此之外,简单的循环将是你最好的朋友。你也许可以做一些微的优化,但(这取决于你的实际的应用程序在做),这可能是不值得的。

Beyond this, simple loops are going to be your best friend. You could probably do some micro-optimization, but (depending on what your actual application is doing) it's probably not worth it.

有一件事要保持跟踪的是,你不能只是从最近的举动寻找许多在连续跳出5在任何方向,因为这一举动可能是一个连胜的中间。所以,我会做这样的事情

One thing to keep track of is that you can't just jump out 5 in any direction from the most recent move looking for that many in a row, because this move might be in the middle of a streak. So I'd do something like

From the new move
    left = how many in a row we have to the left of the lastest move
    right = how many in a row we have to the right of the latest move
    if (left + right + 1 >= 5) then you have a winner

    up = how many in a row we have above the latest move
    down = how many in a row we have below the latest move
    if (up + down + 1 >= 5) then you have a winner

    // repeat for both diagonal directions.

这篇关于检测获胜的比赛中零和十字架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆