我正在尝试制作游戏Go-Moku。游戏编译,但运行不正常。 当我运行游戏时,我可以打印空白板,然后提示我输入行和列整数,但是一旦我按Enter键提交列int,就没有其他事情发生了。
这是我的代码:
import java.util.Scanner;
public class GoMoku extends Board{
Scanner input = new Scanner(System.in);
//play turn method
boolean play (int player){
this.printBoard();
System.out.println("It's Player "+ player + "'s turn.");
System.out.print("Choose Row: ");
int row = input.nextInt();
System.out.print("Choose Column: ");
int column = input.nextInt();
return this.place(row, column, player);
}
public static void main(String[] args) {
System.out.println("Welcome to Go-Moku!");
GoMoku gomoku = new GoMoku();
int gameLoop = 1;
//Turn taking Loop
while ( gameLoop != 0) {
//Player 1 Loop
while (gameLoop == 1) {
gameLoop++;
if (gomoku.play(1) == true) gameLoop = 0;
}
//Player 2 Loop
while (gameLoop == 2) {
gameLoop--;
if (gomoku.play(2) == true) gameLoop = 0;
}
}
}
public class Board {
int[][] board = new int[19][19];
//Board Contructor
public Board() {
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
board[i][j] = 0;
}
}
}
//Places token for player
public boolean place(int row, int column, int player) {
if (board[row][column] == 0) {
board[row][column] = player;
}
return this.hasWin(row, column, player);
}
//Checks to see if the player won the game horizontally
public boolean hasHorizontalWin(int row, int column,int player) {
int left = 0;
int right = 0;
//Counts connections made on left side of placement
for (int k = 1; k < 6; k++){
if (board[row - k][column] == player) left++;
}
//Counts connectons made on right side of placement
for (int l = 1; l < 6; l++) {
if (board[row + l][column] == player) right++;
}
int count = left + right;
if (count == 5) {
return true;
} else return false;
}
//Checks to see if the player won the game vertically
public boolean hasVerticalWin(int row, int column, int player) {
int down = 0;
int up = 0;
//Counts connections made on down side of placement
for (int n = 1; n < 6; n++){
if (board[row][column - n] == player) down++;
}
//Counts connections made on up side of placement
for (int p = 1; p < 6; p++) {
if (board[row][column + p] == player) up++;
}
int count = up + down;
if (count == 5) {
return true;
} else return false;
}
//Player wins method
boolean hasWin(int row, int column, int player) {
if (this.hasHorizontalWin(row, column, player) == true ||
this.hasVerticalWin(row, column, player) == true) {
System.out.println("Player " + player + " wins!");
return true;
} else return false;
}
//Prints board as string
public void printBoard(){
for (int m = 0; m < 19; m++){
for (int b = 0 ; b < 19; b++){
if (board[m][b] == 0) System.out.print("-");
if (board[m][b] == 1) System.out.print("o");
if (board[m][b] == 2) System.out.print("x");
}
System.out.println();
}
}
}
}
感谢
答案 0 :(得分:0)
问题出在Board
课程中 - 当您检查获胜行时,您可以获得ArrayOutOfBounds
例外。
您可以使用以下内容修复它:
public class Board {
static final int WIDTH = 19;
static final int HEIGHT = 19;
int[][] board = new int[HEIGHT][WIDTH];
//Board Contructor
public Board() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
board[i][j] = 0;
}
}
}
//Places token for player
public boolean place(int row, int column, int player) {
if (board[row][column] == 0) {
board[row][column] = player;
}
return this.hasWin(row, column, player);
}
//Checks to see if the player won the game horizontally
public boolean hasHorizontalWin(int row, int column,int player) {
int total = 1;
//Counts connections made on left side of placement
for ( int k = row - 1; k >= 0 && k > row - 5; --k ) {
if ( board[k][column] != player ) break;
++total;
}
for ( int k = row + 1; k < HEIGHT && k < row + 5; ++k ) {
if ( board[k][column] != player ) break;
++total;
}
return total >= 5;
}
//Checks to see if the player won the game vertically
public boolean hasVerticalWin(int row, int column, int player) {
int total = 1;
//Counts connections made on left side of placement
for ( int k = column - 1; k >= 0 && k > column - 5; --k ) {
if ( board[row][k] != player ) break;
++total;
}
for ( int k = column + 1; k < WIDTH && k < column + 5; ++k ) {
if ( board[row][k] != player ) break;
++total;
}
return total >= 5;
}
//Player wins method
boolean hasWin(int row, int column, int player) {
if (this.hasHorizontalWin(row, column, player) == true ||
this.hasVerticalWin(row, column, player) == true) {
System.out.println("Player " + player + " wins!");
return true;
} else return false;
}
//Prints board as string
public void printBoard(){
for (int m = 0; m < HEIGHT; m++){
for (int b = 0 ; b < WIDTH; b++){
if (board[m][b] == 0) System.out.print("-");
if (board[m][b] == 1) System.out.print("o");
if (board[m][b] == 2) System.out.print("x");
}
System.out.println();
}
}
}
您还可以简化主要方法:
public static void main(String[] args) {
System.out.println("Welcome to Go-Moku!");
GoMoku gomoku = new GoMoku();
int player = 1;
//Turn taking Loop
while ( true ) {
if ( gomoku.play( player ) ) break;
player = 3 - player;
}
}
除此之外,还有其他地方可能会出现错误(例如,如果用户输入负数或大于电路板大小的行或列),玩家可以一遍又一遍地输入相同的坐标。 / p>