3

I want to check to see if certain areas of theBoard has a certain value of thePlayer.

public boolean diagonal( char[][] theBoard, char thePlayer) {
    int x = 0;

    while (x <theBoard.length){
      if (thePlayer == theBoard[x][x]) {
        x++;
      }
      else {
        return false;
      }
    }
  • 1
    Your loop is not necessarily ever getting finished. – RaminS Apr 29 '16 at 00:23
  • @Gendarme why not? – No_one_I_am Apr 29 '16 at 00:25
  • Nvm. My bad. I misread. – RaminS Apr 29 '16 at 00:26
  • 1
    I don't think this code would even compile. There may not be a return statement in all cases. – Tim Biegeleisen Apr 29 '16 at 00:26
  • 1
    @TimBiegeleisen Correct. If theBoard's diagonal has `thePlayer` everywhere, then it will exit the loop without a return value. It's not at all clear what the goal of the asker is, nor how they managed to reach this code through their thought process. – nanofarad Apr 29 '16 at 00:28
  • 1
    You are checking if the diagonal is filled with `thePlayer`. If it isn't, you are returning `false`. If this is what you want, then it is fine - except for the fact that you need to return `true` after your while-loop. Just add that. – RaminS Apr 29 '16 at 00:28
  • @hexafraction well spoken. The question and source code are cloudy at best – JayC667 Apr 29 '16 at 00:31
  • Here answered for your question [http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – Thirumal Apr 29 '16 at 00:45

3 Answers3

1

Since you are using a multidimensional array, try using a nested for loop.

public boolean diagonal(char[ ][ ] theBoard, char thePlayer) {
    boolean result = false;
    int x, y; // These will tell you the coordinates of thePlayer if found on theBoard

    for(int i = 0; I < theBoard.length; i++)
        for(int j = 0; j < theBoard[i].length; j++)
            if (thePlayer == theBoard[i][j]) {
                result = true;
                // Add if you want to know coordinates of the player
                x = i;
                y = j;
           }

    return result;
}

With a multidimensional array you need to know the values in both the row and column. Suppose you start at i = 0, if you put in one loop and look for theBoard[i] [i], you will be looking at row 0 col 0, then you add 1 to i, i++, now you are looking at row 1 col 1. You're skipping everything else. That's why you need a nested for loop to iterate over both the row and the column.

Look at the example below...

  0 1 2 3 4 5
0 X X X X X X
1 X X X X X X
2 X X X X X X
3 X X X X X X
4 X X X X X X
5 X X X X X X

If you want to check if thePlayer is only in the diagonal indexes of the array, you can use a single for loop.

public boolean diagonal(char[ ][ ] theBoard, char thePlayer) {
    boolean result = false;
    int x; // This will tell you the coordinates of thePlayer if found on theBoard

    for(int i = 0; I < theBoard.length; i++)
        if (thePlayer == theBoard[i][i]) {
            result = true;
            // Add if you want to know coordinates of the player
            x = i;
       }

    return result;
}

In my opinion, it's easier to use a for loop for things like this. The while loop is essentially the same thing implemented differently, but in this situation the for loop looks cleaner and easier to follow. In the end, it's up to you to decide which one is easier for you to implement.

Saad
  • 175
  • 1
  • 15
0
      for(char[] firstarray  : theBoard){
           for(char ch : firstarray){
         boolean matches = (thePlayer == ch) ? true : false;
         if(matches){
         System.out.println("matchFound");        
         }else{ 
         System.out.println("no match");
         }

         }
         }

since you have a twoDimensional array you need to loop the array twice. you can use a normal for loop bt using a enchanced for loop will be much easier.

Priyamal
  • 2,919
  • 2
  • 25
  • 52
0
public boolean diagonal( char[][] theBoard, char thePlayer) { 
        for(int i = 0; I < theBoard.length; i++)
                for(int j = 0; j < theBoard[i].length; j++)
                        if (thePlayer == theBoard[i][j]) {
                                return = true;
                        }
        return false;
}
Sandun Chathuranga
  • 2,242
  • 2
  • 13
  • 27