Results 1 to 3 of 3
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default 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:
    Java 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] = ' ';
    	      }
    	    }
    	  }
    Last edited by Propinquity; 01-16-2014 at 09:54 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default 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:
    Java Code:
    System.out.println("|" + "X" + "|");
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default 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.
    Last edited by gimbal2; 01-16-2014 at 04:02 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-03-2012, 12:17 AM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •