public static void displayBoard(final char[][] theBoard) {
        // note the final parameter - we won't change the board, just print it.

        // Need to format the board display to look like this:
        //  | |
        // -----
        //  | |
        // -----
        //  | |
        System.out.println("Welcome to a game of TicTacToe\nPlayer 1 will go first, and then Player 2 after that.");
        for (int row = 0; row < board_size; row++) {
            for (int col = 0; col < board_size; col++) {
               System.out.print(theBoard[row][col]);                   // row always comes first in a 2d array
            }
            
            System.out.println("| |\n  -----\n");
        }
    }

this is my code. I need to format the board display to look like this:
| |
-----
| |
-----
| |
System.out.println("| |\n -----\n"); makes the table, but is not correct because the Xs and Os just appear before the | | and not inside or to the right of the slots like they are supposed to.

Recommended Answers

All 16 Replies

this is what my output currently looks like:
Welcome to a game of TicTacToe
Player 1 will go first, and then Player 2 after that.
XXO| |
-----

OOX| |
-----

XOX| |
-----

Which row do you want your piece to be placed? 1, 2, or 3?

I need the X's and O's to be outside AND inside the | |

Use formatted output then

String[] test = new String[]{"X","","O"};
System.out.printf(" %s | %s | %s ",test[0],test[1],test[2]);

what are the numbers after test for?

do you have AIM or anything i can contact you with?

Here is the other class that is getting the input from the user as to where to place to X's and O's.

import java.util.Scanner;

public class TicTacToePlayerJonathanSilverberg {

    public void getMove(char[][] theBoard, char myPiece) {

       


        Scanner sc = new Scanner(System.in);




        System.out.println("Which row do you want your piece to be placed? 1, 2, or 3?");
        int myRow = sc.nextInt();
        System.out.println("Which column do you want your piece to be placed?1, 2, or 3?");
        int myCol = sc.nextInt();

        theBoard[myRow][myCol] = myPiece;


    }
}

You see, one input is for which row you want the X to be placed and the second input is for which column do you want the X to be placed. I have my board printed with the rows and columns. But as you can see from my previous post, the X's and O's are being printed to the left of the board and not ON the board.

what are the numbers after test for?

Those are the array indexes. "test" was just a small String array.

1.
      String[] test = new String[]{"X","","O"};
   2.
      System.out.printf(" %s | %s | %s ",test[0],test[1],test[2]);

ok that is good except instead of "X","","O"
I need it to be theBoard[row][col] because the row and column are being input from a seperate class

Yes, those two lines are an example. As you said, you need to use theBoard[][].

so how would I do that? Im still learning java.

Im wondering if you had an example of how to put theBoard[][] in the place of "X","","O"

No, that part is up to you. I think my example comes plenty close. Anything more is essentially doing that part of your assignment for you.

I was playing with it all day yesterday and couldn't figure it out.

String[] test = new String[]{theBoard[row][col]}; // doesnt work

String[] test = new String[]{"","","", + theBoard[row][col]}; // doesnt work

Come on I have been really trying to do what your telling me and keep getting errors.

String[] test = new String[]{"X","","O"};
            System.out.printf(" %s | %s | %s ",test[row],test[col]);

something like that? I'm still getting an error when it runs.

The "test" array has nothing at all to do with your code. It's just an array I used to put some data in so you could see how to use the formatted output here

System.out.printf(" %s | %s | %s ",test[row],test[col]);

Each of those "%s" spots is just a place holder for a string value, which you specify in order after the format.

So I need to make the string value = theBoard[row][col] correct? Ive been trying to do that...not working.
String[] theBoard[row][col] = new String[]{"X","","O"}; not working.... I am completely lost, we haven't gone of strings that much.

I think we might be talking about doing different things...

sorry I am very new to using strings... :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.