如何在连接4中正确检查DiagonalWin

编程入门 行业动态 更新时间:2024-10-24 16:32:57
本文介绍了如何在连接4中正确检查DiagonalWin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我检查垂直胜利并检查水平胜利工作完全没问题,但我不知道如何处理我的支票对角线代码,使其实际检查对角线。一些指导将非常感谢,这是在java中。谢谢。

My check vertical win and check horizontal win work perfectly fine, however i dont know what to do with my check diagonal code to make it actually check diagonal. Some guidance would be much appreciated and this is in java. Thank you.

private boolean checkVerticalWin() { PieceType type = myBoard[myLastPoint.x][myLastPoint.y]; System.out.println("check vert"); for(int j = 0; j < myNumColumns; j++) { for(int i = 0; i < myNumRows; i++) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; if(count == 1) { myWinBegin = new Point(i,j); } } else { myWinBegin = null; count = 0; } System.out.println(count); if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } myWinBegin = null; return false; } private boolean checkHorizontalWin() { System.out.println("test"); PieceType type = myBoard[myLastPoint.x][myLastPoint.y]; for(int i = 0; i < myNumRows; i++) { for(int j = 0; j < myNumColumns; j++) { if(myBoard[i][j] == type && myBoard[i][j] != null) { count++; if (count == 1) { myWinBegin = new Point(i,j); } } else { myWinBegin = null; count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } myWinBegin = null; return false; } private boolean checkDiagonalWin() { PieceType type = myBoard[myLastPoint.x][myLastPoint.y]; for(int i = 0; i < myNumRows; i++) { for (int j = 0; j < myNumColumns; j++) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; myWinBegin = new Point(i,j); } else { count = 0; myWinEnd = new Point(i,j); } if(count == myWinLength) { return true; } } } for(int j = 0; j < myNumColumns; j--) { for (int i = 0; i < myNumRows; i--) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; myWinBegin = new Point(i,j); } else { count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } for(int j = 0; j < myNumColumns; j++) { for (int i = 0; i < myNumRows; i--) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; } else { myWinBegin = new Point(i,j); count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } for(int j = 0; j < myNumColumns; j--) { for (int i = 0; i < myNumRows; i++) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; myWinBegin = new Point(i,j); } else { count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } return false; }

推荐答案

所以基本上,你需要一个开始点,然后你需要确定移动的方向

So basically, you need a start point, you then need to determine in which direction to move

有了这个想法,你可以使用类似......

With that idea in hand, you could use something like...

boolean win = true; for (int count = 0; count < 4; count++) { if (row < myNumRows && row >= 0 && col < myNumColumns && col >= 0) { int test = myBoard[row][col]; if (test != check) { win = false; break; } } else { break; } row += rowDelta; col += colDelta; }

作为基本算法。所有这一切都是从起点检查每个单元格到总共4个单元格,算法按指定的增量/方向移动并在每个单元格匹配检查时继续检查价值。

As the basic algorithm. All this does is checks each cell from a start point, to a total of 4 cells, the algorithm moves by the specified delta/direction and keeps checking while each cell matches the check value.

现在,我用一个简单的方法包装它

Now, I'd wrap this in a simple method

public boolean didWin(int[][] grid, int check, int row, int col, int rowDelta, int colDelta) { boolean win = true; for (int count = 0; count < 4; count++) { if (row < ROWS && row >= 0 && col < COLUMNS && col >= 0) { int test = grid[row][col]; if (test != check) { win = false; break; } else { break; } } row += rowDelta; col += colDelta; } return win; }

这使得呼叫更简单,知道任何一点,你可以做类似......

which makes it simpler to call, know given any point, you can do something like...

int startRow = ...; int startCol = ...; int player = ...; if (didWin(myBoard, player, startRow, startCol, 1, 0) || // Vertical, down didWin(myBoard, 1, startRow, startCol, 0, 1) || // Right didWin(myBoard, 1, startRow, startCol, 0, -1) || // Left didWin(myBoard, 1, startRow, startCol, 1, 1) || // Right/down didWin(myBoard, 1, startRow, startCol, -1, -1) || // Left/Up didWin(myBoard, 1, startRow, startCol, 1, -1) || // Down/Left didWin(myBoard, 1, startRow, startCol, -1, 1) // Up/Right ) { // You be the winner }

nb:我遗漏了检查垂直方向,因为你不太可能真的以这种方式获胜

ps:我会更加懒惰而且会有一个 didWin 方法,它执行了上述检查并返回 true 或 false ,但我懒了

ps: I'd be even more lazy and would just have a didWin method, which did the above checks and returned true or false, but I'm lazy

因此, startRow 和 startCol 将代表您想要的锚点在这个例子中,检查并将代表最后一滴。

So, the startRow and startCol would represent the anchor point around which you want to check and would, in this example, represent the last drop.

此示例使用 int 来表示播放器/ token,但你可以使用任何东西,所有这一切都是将你提供的令牌与数组中的值进行比较

This example uses a int to represent the player/token, but you could use anything, all this does is compares the token you supply with the values in the array

更多推荐

如何在连接4中正确检查DiagonalWin

本文发布于:2023-10-16 11:53:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   如何在   DiagonalWin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!