0

I have attempted to create Tic Tac Toe and I was able to fill my board and I was able to check row and columns for who won. However, I need some help to check diagonally to see who won. This is what I have so far. I am a beginner so please don't make the code too hard.

Check Method:

public boolean check(String[] [] board)
{
    int j = 0;
    for(int i = 0; i < board.length;i++) //row
    {
        if(board[i][j] == "X" || board[i][j+1] == "X" || board[i][j+2] == "X")
        {
          if(board[i][j] == board[i][j+1] & board[i][j] == board[i][j+2])
          {
              System.out.println("X wins!");
                return true;
          }
        }
    }
    int e = 0;
    for(j = 0; j < board.length;j++) //col
    {
        if(board[e][j] == "X" | board[e + 1][j] == "X"  | board[e + 2][j] == "X" )
        {
          if(board[e][j] == board[e + 1][j] & board[e][j] == board[e + 2][j])
          {
              System.out.println("X, wins");
                return true;
          }
        }
     }
    int d = 0;
    for(int ii = 0; ii < board.length; ii++) //diag
    {
        if(board[d][ii] != null || board[d + 1][ii] != null | board[d + 2][ii] != null)
        {
            if(board[d][ii] == board[d + 1][ii+1] & board[d][ii] == board[d + 2][ii])
            {
                System.out.println("X, wins dig");
                return true;
            }
        }
    }
    int k = 0;
    for(int i = 0; i < board.length;i++) //row
    {
        if(board[i][k] == "O" || board[i][k+1] == "O" || board[i][k+2] == "O")
        {
          if(board[i][k] == board[i][k+1] & board[i][k] == board[i][k+2])
          {
              System.out.println("O wins!");
                return true;
          }
        }
    }
    int z = 0;
    for(k = 0; k < board.length; k++) //col
    {
        if(board[z][k] == "O" | board[z + 1][k] == "O"  | board[z + 2][k] == "O" )
        {
          if(board[z][k] == board[z + 1][k] & board[z][k] == board[z + 2][k])
          {
              System.out.println("O, wins");
                return true;
          }
        }
     }

    return false;
}
Amanda B.
  • 3
  • 7
  • You may also find [this example](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array/409795#409795) helpful – MadProgrammer Mar 07 '17 at 21:46
  • In addition : You try to iterate through your board on your fillX and fillY methods, but you use r1-1 and c1-1. As they are integer defined before, the values will always be the same. (e.g : if i enter 3 for r1 and 3 for c1, you will put 9 times "o" at board[2][2], which is useless) – Namoz Mar 07 '17 at 21:52
  • So you are saying I should change r1 and c1 to just r and c?@Namoz – Amanda B. Mar 07 '17 at 21:56
  • he is pointing that you have useless cycles in fillO and fillX. Just leave inner body of cycles. – Natalia Mar 07 '17 at 22:07
  • In other words, get rid of the for loops in `fillO` and `fillX`. They are causing you to do the exact same thing 9 times. Instead, do the thing just once, outside any loop. – David Choweller Mar 07 '17 at 22:11
  • In answer to your question, you are prompting the user twice for the row and column, once in the `fillX` method, and once in the `fillO` method. Once you do this you never ask the user again, because your methods that ask the user for input are outside the while loop in `main`. – David Choweller Mar 07 '17 at 22:17
  • Also, you can make your `fillX` and `fillO` methods return void. There is no need to pass back the value of the String array board since it is already being modified by the methods. – David Choweller Mar 07 '17 at 22:21
  • Set a break point, and step through the code :) It'll probably become pretty clear what's going wrong. – mrfreester Mar 07 '17 at 23:35

1 Answers1

1

This question has a few things you are trying to do:

First ensure value already not there: (Suggests from the comments take into account)

public void fillX(String[][] board) {
    attemptToFillValue(board, "x");
}

public void fillO(String[][] board) {
    attemptToFillValue(board, "o");
}

private void attemptToFillValue(String[][]board,  String value) {
    do {
        int r = Integer.parseInt(JOptionPane.showInputDialog("What row from 1-3?"));
        int c = Integer.parseInt(JOptionPane.showInputDialog("What column from 1-3?"));
        if (board[r - 1][c - 1] != null) {
            board[r - 1][c - 1] = value;
            return;
        } else {
            //Warn then that value already exists in some way
        }
    } while (true);
}

Regarding the verifying if the board has a winner, I suggest using for loops and do 3 separate checks. Horizontal, Vertical, and Diagonal.

I do not want to be doing the whole assignment for you so leaving it for you

Bojan Petkovic
  • 2,406
  • 15
  • 26
  • I totally understand, plus the challenge of coding is figuring things out. But i get the idea. I will edit my code above because this is what I have so far – Amanda B. Mar 07 '17 at 23:48
  • how do you initialize the board at the start? if you set the values to null it will work. IF you set it to something else, you have to change the check in attemptToFillValue before the return. – Bojan Petkovic Mar 08 '17 at 18:58