项目Projects

猜数字

Q10

五子棋

Q56

1024

Q57

黑白棋

Q58

扫雷

Q59

阅读题

阅读下面的代码如何解决五子棋验证

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
public class Game { int[][] board; int size = 15; boolean overFiveAt(int x, int y) { int[][] directions = {{0, -1}, {1, -1}, {1, 0}, {1, 1}}; for (int i = 0; i < directions.length; i++) { int[] direction = directions[i]; if (overFiveByDirection(x, y, direction[0], direction[1])) { return true; } } return false; } boolean overFiveByDirection(int x, int y, int dx, int dy) { int count = 0; count += countOnDirection(x, y, dx, dy); count += countOnDirection(x, y, -dx, -dy); count --; return count >= 5; } int countOnDirection(int x, int y, int dx, int dy) { int referenceToken = board[y][x]; int count = 1; while (true) { x += dx; y += dy; if (!inBoard(x, y)) { break; } int token = board[y][x]; if (token == referenceToken) { count ++; } else { break; } } return count; } boolean inBoard(int x, int y) { return x >= 0 && x < size && y >= 0 && y < size; }}

ZZAX 微信公众

文档一更新,立刻告诉你