0
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.

2
Contributors
16
Replies
17
Views
8 Years
Discussion Span
Last Post by jnthn205
0

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 | |

0

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.

Edited by jnthn205: n/a

0

what are the numbers after test for?

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

0
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

0

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

0

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.

0

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.

0
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.

Edited by jnthn205: n/a

0

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.

0

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.

Edited by jnthn205: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.