[英]How do I get my Go-Moku game to keep looping?
I am trying to make the game Go-Moku.我正在尝试制作 Go-Moku 游戏。 The game compiles, but doesn not run properly.游戏可以编译,但不能正常运行。 When I run the game I am able to have the empty board printed and then i am prompted to enter the row and column integers, but once I hit enter to submit the column int, nothing else happens.当我运行游戏时,我可以打印空板,然后系统会提示我输入行和列整数,但是一旦我按 Enter 键提交列 int,其他任何事情都不会发生。
Here is my code:这是我的代码:
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();
}
}
}
}
thanks谢谢
The problem is in the Board
class - when you check for winning lines you can get an ArrayOutOfBounds
exception.问题出在Board
类中 - 当您检查获胜线时,您可能会收到ArrayOutOfBounds
异常。
You can fix it with something like this:你可以用这样的东西来修复它:
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();
}
}
}
You can also simplify the main method:您还可以简化主要方法:
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;
}
}
Apart from that there are still other places where you can get errors (ie if the user enters a row or column that is negative or greater than the board size) and players can enter the same co-ordinates over and over.除此之外,还有其他地方可能会出错(即,如果用户输入的行或列是负数或大于棋盘大小),玩家可以一遍又一遍地输入相同的坐标。
public boolean verticaleWin(int ligne, int colonne, int player) {
int total = 1;
for (int k = ligne - 1; (k >= 0 && k > ligne - 5); k--) {
if (dimension[k][colonne] != player) break;
++total;
}
for (int k = ligne + 1; k < dimension.length && k < ligne + 5; k++) {
if (dimension[k][colonne] != player) break;
++total;
}
return total >= 5;
}
public boolean horizontaleWin(int ligne, int colonne, int player) {
int total = 1;
for (int k = colonne - 1; k >= 0 && k > colonne - 5; k--) {
if (dimension[ligne][k] != player) break;
++total;
}
for (int k = ++colonne; k < dimension.length && k < colonne + 5; k++) {
if (dimension[ligne][k] != player) break;
++total;
}
return total >= 5;
}
public boolean diagonaleWin(int ligne, int colonne, int player) {
int total = 1;
for (int k = ligne - 1, j = colonne + 1; (k >= 0 && k > ligne - 5 && j < dimension.length && j < colonne + 5); k--, j++) {
if (dimension[k][j] != player) break;
++total;
}
for (int k = ligne + 1, j = colonne + 1; (k < dimension.length && k < ligne + 5 && j < dimension.length && j < colonne + 5); k++, j++) {
if (dimension[k][j] != player) break;
++total;
}
for (int k = ligne - 1, j = colonne - 1; (k >= 0 && k > ligne - 5 && j >= 0 && j > colonne - 5); k--, j--) {
if (dimension[k][j] != player) break;
++total;
}
for (int k = ligne + 1, j = colonne - 1; (k < dimension.length && k < ligne + 5 && j >= 0 && j > colonne - 5); k++, j--) {
if (dimension[k][j] != player) break;
++total;
}
return total >= 5;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.