首页 / JAVA / Java:对角线获胜4
Java:对角线获胜4
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java:对角线获胜4,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4246字,纯文字阅读大概需要7分钟。
内容图文
![Java:对角线获胜4](/upload/InfoBanner/zyjiaocheng/662/1da8034c09db4e1db1c24b20abe4549d.jpg)
我最近做了一个Connect4游戏,当它向右对角线连接时,我的Connect4没有赢得比赛.而且它仅在对角线连接到左侧时才适用于某些组合.坐标:-左上:(0,0),左下:(5,0),右上:(0,6),右下:(5,6). Connect4板为6 x 7.
问题:向左斜连接仅适用于某些组合.但是,没有任何对角线连接正确的工作.
/** A method of winning diagonally towards the left side when playing connect 4 two player*/
/** Giving the new method with all the possible possibilities for a user to win diagonally-left */
public static void diagWinningLeft() {
for (int x = 5; x > 2; x--) { // Checks to see if same colored pegs are lining diagonally to the left
for (int y = 6; y > 3; y--) {
if (board[x][y] == 1 && board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1) {
JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-left in a row in " +(countForRed)+ " turns!");
b.drawLine(x,y,x-3,y-3);
}
if (board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1 && board[x - 4][y - 4] == 1) {
JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-left in a row in " +(countForRed)+ " turns!");
b.drawLine(x,y,x-3,y-3);
}
if (board[x][y] == 2 && board[x - 1][y - 1] == 2 && board[x - 2][y - 2] == 2 && board[x - 3][y - 3] == 2) {
JOptionPane.showMessageDialog(null, playerNames[1]+ " has connected four diagonally-left in a row in " +(countForYellow)+ " turns!");
b.drawLine(x,y,x-3,y-3);
}
}
}
}
/** Another method of winning diagonally towards the right side when playing connect 4 two player*/
/** Giving the new method with all the possible possibilities for a user to win diagonally-right*/
public static void diagWinningRight() {
for (int x = 0; x < 2; x++) { // Check to see if same colored pegs are lining diagonally to the right
for (int y = 0; y < 3; y++) {
if (board[x][y] == 1 && board[x + 1][y + 1] == 1 && board[x + 2][y + 2] == 1 && board[x + 3][y + 3] == 1) {
JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-right in a row in " +(countForRed)+ " turns!");
}
if (board[x][y] == 2 && board[x + 1][y + 1] == 2 && board[x + 2][y + 2] == 2 && board[x + 3][y + 3] == 2) {
JOptionPane.showMessageDialog(null, playerNames[1]+" has connected four diagonally-right in a row in " +(countForYellow)+ " turns!");
}
}
}
}
解决方法:
原谅我没有直接回答问题,但这是可以帮助您解决问题并最终获得更好的代码和以后编写代码的更好功能的东西.
将“ if”条件的逻辑提取到单独的方法中,可以更轻松地自己考虑该逻辑,并使您可以独立于程序的其余部分对其进行测试.
所以代替:
if (board[x][y] == 1 && board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1) {
JOptionPane.showMessageDialog(...)
}
… 采用:
if(isDiagonalLeft(x,y,1) { ... }
…和…
boolean isDiagonalLeft(int x, int y, int player) {
return board[x][y] == player &&
board[x - 1][y - 1] == player &&
board[x - 2][y - 2] == player &&
board[x - 3][y - 3] == player
}
现在,您可以在isDiagonalLeft()上运行单元测试以确保其正常工作.也就是说,一个小程序可以设置一个板并运行isDiagonalLeft(),以确保在各种情况下都能给出正确的答案.这似乎是一项额外的工作,但是大多数尝试它的人都知道,通过尽早发现错误可以节省工作量.
您要做的是将游戏逻辑与表示代码(JOptionPane)分开,这样当您只想练习游戏逻辑时,表示代码就不会受到影响.稍后在编程研究中,您将遇到将这些甚至更多的方法分离的方法,例如Model-View-Controller模型.
如果您需要在Stack Overflow上提出问题,则像这样拉出逻辑也将有所帮助-通过将游戏逻辑与Swing分开,可以向对Swing一无所知的潜在回答者开放问题.
而且,您可以为每个玩家重复使用此方法,而不必将逻辑复制到两个位置.
如果不起作用,请在您的IDE中使用调试器来逐步调试它.
现在,您已经完成了此操作,可以优化该方法,以便计算机执行减量运算,而不是由程序员执行减量运算.
boolean isDiagonalLeft(int x, int y, int player) {
for(int i = 0; i<4; i++) {
if(board[x-i][y-i] != player) {
return false;
}
}
return true;
}
…并且可以对其进行泛化,使其覆盖对角线的两个方向:
boolean isDiagonal(int x, int y, int player, boolean direction) {
int dirUnit = direction ? -1 : 1;
for(int i = 0; i<4; i++) {
if(board[x-i][y + dirUnit] != player) {
return false;
}
}
return true;
}
…所以现在您可以在4个地方重用该方法;对于每个玩家和每个方向.
如果遇到无法在GUI中使用的情况,请进行单元测试,以按GUI中的方式设置电路板,并在其上运行isDiagonal().如果测试通过,则说明问题出在其他地方.如果测试失败,则可以使用调试器和方法的代码来使其通过.
内容总结
以上是互联网集市为您收集整理的Java:对角线获胜4全部内容,希望文章能够帮你解决Java:对角线获胜4所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。