【问题标题】:How do I get my Go-Moku game to keep looping?如何让我的 Go-Moku 游戏继续循环播放?
【发布时间】:2014-02-15 01:51:41
【问题描述】:

我正在尝试制作游戏 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();
        }
    }
}
 }

谢谢

【问题讨论】:

  • 你清楚 while 结构的使用吗?因为你对它们的使用确实区分了玩家回合对我来说没有多大意义。

标签: java gomoku


【解决方案1】:

问题出在 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();
        }
    }
}

你也可以简化main方法:

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;
    }
}

除此之外,还有其他地方可能会出错(即,如果用户输入的行或列是负数或大于棋盘大小),玩家可以一遍又一遍地输入相同的坐标。

【讨论】:

  • 我采纳了您对代码的建议,但运行它时并没有改变结果。
【解决方案2】:
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 total1 = 1;
    int total2 = 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;
      ++total1;
    }
    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;
      ++total1;
    }
    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;
      ++total2;
    }
    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;
      ++total2;
    }
    return ((total1 >= 5) || (total2 >= 5));
  }

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2015-07-18
  • 2015-10-01
  • 2022-11-25
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多