Question

I am trying to make the game 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.

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

No correct solution

OTHER TIPS

The problem is in the Board class - when you check for winning lines you can get an ArrayOutOfBounds exception.

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 (i.e. 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 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));
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top