Question

I'm still in the early stages of writing this program (BreakThrough game), but when attempting to test run the code I've already got, it appears steps are repeated in the do while loop before one loop is ever finished. It seems it runs obj.userMove(row, col, move); three times before completing the loop.

import java.util.Scanner;
import java.util.Random;
class BreakThroughGame {
char board[][];
Random rand = new Random();


BreakThroughGame() {
    board = new char[][]{{' ','1','2','3','4','5','6','7','8', ' '},
        {'1','w','w','w','w','w','w','w','w','1'},
        {'2','w','w','w','w','w','w','w','w','2'}, 
        {'3','_', '_','_', '_','_', '_','_', '_','3'}, 
        {'4','_', '_','_', '_','_', '_','_', '_','4'}, 
        {'5','_', '_','_', '_','_', '_','_', '_','5'}, 
        {'6','_', '_','_', '_','_', '_','_', '_','6'}, 
        {'7','b', 'b','b', 'b','b', 'b','b', 'b','7'},
        {'8','b', 'b','b', 'b','b', 'b','b', 'b','8'},
        {' ','1','2','3','4','5','6','7','8',' '}};


public boolean userMove( int row, int col, String move ) {
    System.out.println("pos = "+board[row][col]);

    if (board[row][col] == 'b') {
        if(move.charAt(0) == 'f' && board[row+1][col] == 'w') {
            System.out.println("Can't move there, try again");
            return false;
        }
        switch (move.charAt(0)){
            case 'f':
                board[row-1][col] = 'b';
                board[row][col] = '_';
                return true;
            case 'r':
                board[row-1][col+1] = 'b';
                board[row][col] = '_';
                return true;
            case 'l':
                board[row-1][col-1] = 'b';
                board[row][col] = '_';
                return true;
        }
    }
    else {
        System.out.println("Invalid position, try again");
        return false;
    }
    return false;
}


public void computerMove() {
    int rm = rand.nextInt(8)+1;
    int cm = rand.nextInt(8)+1;
    int dm = rand.nextInt(2);
    //code goes here

}

public boolean gameOver() {
    //code goes here
    return false;
} 

public void printBoard() {
    for (int row = 0; row <= 9; row++){
        for (int column = 0; column <= 9; column++) {
            System.out.print(board[row][column]+" ");
        }
        System.out.println();
    }
  }
} 

public class game {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    BreakThroughGame obj = new BreakThroughGame();

    do {
        obj.printBoard();
        System.out.println("Enter row position");
        int row = in.nextInt();
        System.out.println("Enter column position");
        int col = in.nextInt();
        System.out.println("Enter move direction");
        String move = in.next();

        obj.userMove(row, col, move);
        System.out.println(obj.userMove(row, col, move));

        if(obj.userMove(row, col, move) == true)
            obj.computerMove();

    }while (obj.gameOver() == false);
  }
}

Here is the output after completing the first loop:

  1 2 3 4 5 6 7 8   
1 w w w w w w w w 1 
2 w w w w w w w w 2
3 _ _ _ _ _ _ _ _ 3
4 _ _ _ _ _ _ _ _ 4 
5 _ _ _ _ _ _ _ _ 5 
6 _ _ _ _ _ _ _ _ 6 
7 b b b b b b b b 7 
8 b b b b b b b b 8 
  1 2 3 4 5 6 7 8   
Enter row position
7
Enter column position
5
Enter move direction
f
pos = b
pos = _
Invalid position, try again
false
pos = _
Invalid position, try again

Now the loop appears to be finally finished, and then starts from the top again

  1 2 3 4 5 6 7 8
1 w w w w w w w w 1 
2 w w w w w w w w 2 
3 _ _ _ _ _ _ _ _ 3 
4 _ _ _ _ _ _ _ _ 4 
5 _ _ _ _ _ _ _ _ 5 
6 _ _ _ _ b _ _ _ 6 
7 b b b b _ b b b 7 
8 b b b b b b b b 8 
  1 2 3 4 5 6 7 8   
Enter row position
Was it helpful?

Solution

You are calling the method 3 times in the loop:

// 1
obj.userMove(row, col, move);

// 2
System.out.println(obj.userMove(row, col, move));

// 3
if(obj.userMove(row, col, move) == true)

I assume you wanted to call it once and use the same result in all 3 places. Assign the result to a variable and then use that variable in these 3 places.

boolean moveResult = obj.userMove(row, col, move);

System.out.println(moveResult);

if(moveResult)

Now it is only called once.

OTHER TIPS

You are calling the method three times:

obj.userMove(row, col, move); //1
    System.out.println(obj.userMove(row, col, move)); //2

    if(obj.userMove(row, col, move) == true) //3

Your loop runs obj.userMove(row, col, move) three times because it is in your loop three times. If you only want it to be run once, assign the result to a new variable:

boolean userMoveResult = obj.userMove(row, col, move);
System.out.println(userMoveResult);
if (userMoveResult == true)
    obj.computerMove();

By the way, you don't need to compare boolean values to true or false in an if or while. You can just use them like the following:

if (userMoveResult) {
    ....
}
while (!obj.gameOver()) { // Note: ! is the negation operator (i.e. NOT)
    ....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top