我從頭開始創建一個連接四個程序的良好實踐,我遇到了checkAlignment()方法的問題,或者什麼是勝利條件。它適用於一些行,但不是全部,它不適用於任何其他方向(垂直,對角向前,對角向後)。Connect中的Java程序問題
public char checkAlignment(int row, int column) {
char color = board[row][column];
char[][] current = getBoard();
// Horizontal Left-to-Right Check - - - - - - - - - -
if (column + 4 <= columns) {
for (int i = 1; i < 4; i++) {
if (current[row][column + i] != color) {
return NONE;
}
}
return color;
}
// Horizontal Right-To-Left Check - - - - - - - -
if (column - 4 > -1) {
for (int i = 1; i < 4; i++) {
if (current[row][column - i] != color) {
return NONE;
}
}
return color;
}
// Vertical Top-To-Bottom Check - - - - - - -
if (row + 4 <= rows) {
for (int i = 1; i < 4; i++) {
if (current[row + i][column] != color) {
return NONE;
}
}
return color;
}
// Vertical Bottom-To-Top Check - - - - - - - -
if (row - 4 > -1) {
for (int i = 1; i < 4; i++) {
if (current[row - i][column] != color) {
return NONE;
}
}
return color;
}
// Main Diagonal Backwards Check - - - - - - - - - -
if (column - 4 > -1 && row - 4 > -1) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row - i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Main Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= columns && row + 4 <= rows) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row + i][column + j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Backwards Check - - - - - - - - -
if (column - 4 > -1 && row + 4 <= rows) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row + i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= columns && row - 4 > -1) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row - i][column + j] != color) {
return NONE;
}
}
}
return color;
}
return NONE;
}
任何人都可以幫我嗎?
編輯/調整後:
public char checkAlignment(int row, int column) {
char color = board[row][column];
char[][] current = getBoard();
// Horizontal Left-to-Right Check
if (column + 4 <= NUM_COLS) {
for (int i = 0; i < 4; i++) {
if (current[row][column + i] != color) {
return NONE;
}
}
return color;
}
// Horizontal Right-To-Left Check
if (column - 4 > -1) {
for (int i = 0; i < 4; i++) {
if (current[row][column - i] != color) {
return NONE;
}
}
return color;
}
// Vertical Top-To-Bottom Check
if (row + 4 <= NUM_ROWS) {
for (int i = 0; i < 4; i++) {
if (current[row + i][column] != color) {
return NONE;
}
}
return color;
}
// Vertical Bottom-To-Top Check
if (row - 4 > -1) {
for (int i = 0; i < 4; i++) {
if (current[row - i][column] != color) {
return NONE;
}
}
return color;
}
// Main Diagonal Backwards Check
if (column - 4 > -1 && row - 4 > -1) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row - i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Main Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= NUM_COLS && row + 4 <= NUM_ROWS) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row + i][column + j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Backwards Check - - - - - - - - -
if (column - 4 > -1 && row + 4 <= NUM_ROWS) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row + i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= NUM_COLS && row - 4 > -1) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row - i][column + j] != color) {
return NONE;
}
}
}
return color;
}
return NONE;
}
我會試一試。我是否也會將「j = 1」更改爲「j = 0」? –
是的,你也需要。 – Chris
我已經添加了調整/編輯版本。它看起來如何? –