# Help with Tic Tac Toe, 2D arrays?

• 01-16-2014, 09:48 AM
Propinquity
Help with Tic Tac Toe, 2D arrays?
Hello!
So what I'm trying to do is use 2D arrays to print out the following output:

+-+-+-+
| | | |
+-+-+-+
| | | |
+-+-+-+
| | | |
+-+-+-+
I'm having difficulty figuring this out. I don't know how to incorporate the formatting into the program. I understand how to get the X and O into the array, but I don't know how to make the box look like that.

So far what I have is this:
Code:

public tictactoe(){
board = new char[3][3];
for (int row = 0; row < 3; row ++){
for (int col =  0; col < 3; col++){
board[row][col] = ' ';
}
}
}

• 01-16-2014, 03:35 PM
jim829
Re: Help with Tic Tac Toe, 2D arrays?
What you have shown makes no sense in terms of what you want. Personally, I would just print the X's and O's separated by a space. However, you should treat the X's and O's separately on the board, using spaces(' ') where no move exists. Then have special display method which takes the array and simply prints the moves appropriately inside the boxes. And don't use a proportional font. Use a fixed-width font. Here's a hint:
Code:

System.out.println("|" + "X" + "|");
Regards,
Jim
• 01-16-2014, 03:59 PM
gimbal2
Re: Help with Tic Tac Toe, 2D arrays?
Another tip that for me personally helped a lot when I was learning: don't try to make something that can draw a 3*3 table; try to make something that draws an X by X table so it works if you put a 3*3 array in there a 6*6 array or even a 10*10 array. That will force you to think about a proper code design right away in stead of spending all your time trying to hammer something together that just happens to spit out exactly what you want at that point and which you would have to completely rebuild if you want something smaller or bigger.

Then do it in steps, one step at a time. Try to draw the array content first and make sure it works for different sizes. Then add horizontal lines. Then add vertical 'lines'. And voila, then you have it. Then draw three differently sized grids under each other; that should be no effort at all anymore because you simply create three arrays and you reuse the exact same method to draw each one.

And guess what: the next time you don't want to make a tic tac toe game but checkers, you'll already have the code to draw the board which you can reuse.