连接4,检查赢家算法 [英] Connect 4, check for winner algorithm

查看:135
本文介绍了连接4,检查赢家算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一些Java code来实现连接4场比赛。当玩家放置四个芯片成一排,无论是水平,垂直或斜成功者声明。很显然,我可以写一些对循环和每次检查一个胜利者,但希望更多的优雅做一些建议。我在想将所有获胜组合一些数据结构,只是检查所做的最后一步是参与的组合,但我不知道这是可能的,或如何实现它。我是新来的Java所以任何提示或使用什么数据结构或者如何实现将是非常美联社preciated建议。谢谢

I'm writing some Java code to implement the Connect 4 game. A winner is declared when a player places four chips in a row, either horizontally, vertically or diagonally. Obviously I could write some for loops and check for a winner each time but would like some advice on doing it more elegantly. I was thinking about adding all winning combinations to some data structure and just checking the combinations the last move made is involved in but I'm not sure if that is possible or how to implement it. I am new to Java so any tips or advice on what data structure to use or how to implement would be much appreciated. Thanks

编辑:好了,可能有人请告诉我,从哪里开始实施的家伙在这里回答:的算法来检查连四场

Ok, could someone please advise me on where to start to implement the guys answer here: algorithm to check a connect four field

推荐答案

无论什么时候,以检查胜利条件,你需要做一些对整个板循环或递归。只要你的循环停止检查在任何方向,只要它不再是一个成功的条件(例如,如果你检查左到右,后2次迭代你会发现不同的颜色),那么它应该就可以了。

No matter what, in order to check for victory condition, you'll need to do some sort of looping or recursion on the whole board. As long as your loop stops checking in any direction as soon as it stops being a winning condition (e.g. if you check left to right and after 2 iterations you find a different color) then it should be fine.

这种优化的方法是只检查的胜利条件时,新的动作玩法,那么你只需要检查那些周围的移动,而不是整个董事会。  如果您需要检查一个完整的电路板,而不是转由转,然后进一步的步骤将是保持的出场动作列表,并做你检查的第一往前走,那么你就可以马上停止为获胜此举是发挥

A way of optimizing this would be to only check for victory conditions when new moves are played, then you only need to check those around that move and not the whole board. If you need to check a complete board and not turn-by-turn then a further step would be to keep a list of moves played and do your checking from the first move forward, then you can stop as soon as the winning move was played.

这篇关于连接4,检查赢家算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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