-5

The assignment is to create a game of Tic Tac Toe using a two dimensional array. I created the board for the game down below, but now what I'm having issues with is letting the user place X's and O's on the board. I must use a two-dimensional array to represent my board. How can I modify the values so that I can do something like what I have below?

So that this

1 | 2 | 3 
----------
4 | 5 | 6 
----------
7 | 8 | 9

Can become something like this

X | X | O 
----------
4 | 5 | 6 
----------
O | 8 | 9

Essentially, I've made the board, how do I make it so that people can play it? Thanks.

public class TicTacToe 
{
    public static void main( String args[]) 
    { 
        int [][]board = new int[3][3];

        int row =0;
        int column =0;
        int i=1;
        for (row=0;row<board.length; row++)
        { 
            for(column=0;column<board[row].length;column++)
            {
                board[row][column]= i;
                i++;
                System.out.print (" "+(board[row][column])+" ");
            }

            System.out.println();
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Wyatt Bush
  • 5
  • 1
  • 2
  • 5
  • 3
    Please put a little more effort into your program before posting a question next time. – Keppil Apr 01 '15 at 18:57
  • You will probably need another 2-d array that is of type String for Xs and Os – ryekayo Apr 01 '15 at 18:57
  • 1
    You could fill your array with a value of three different digits: `0` for empty, `1` for the `O` and `2` for the `X`. Just an idea. While iterating through the array you can just check the value and print the accordant character. – pzaenger Apr 01 '15 at 18:58

2 Answers2

0

You probably better use an ArrayList containing objects representing the letter and the coordinate where it is placed. It is a packed representation and it probably work better for the kind of game you are trying to implement. Don't think the intuitive representation works better for your needing. Anyway, if you relly want to use a two dimension array, why don't use a two dimension array of chars?

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • @Bill assigment should not prevent finding better solutions. If you look at how a chess board is impemented, you will better understand why the intuitive rep is the worst. – Felice Pollano Apr 01 '15 at 19:02
0

I definitely cannot hand you a solution as this appears to be homework and this homework looks to be on the right track just not fully worked out. However, you should listen to your commentators while banging your head against this wall as they have guided you in a very useful direction.

I think your biggest hurdle once you figure out the data structure (i.e. maybe using your array with coded ints?) will be how you let the user interact with this program. Will they be typing a number corresponding to a cell? If this is the case, I would advise you think about how you might look for the value in the array and figure out how those numbers on the screen might correspond to this process. Once you have this, it will be a trivial task of filling your board with Xs and Os.

Come back after you feel you have hit a few more dead ends. But I think you will figure this one out well before that happens.

Good Luck!

david tallon
  • 394
  • 2
  • 7