简体   繁体   中英

How can I print a 1D array to be 3 x 3 and how can I have an RNG computer check values and to keep searching for an empty space?

I have been working on a 1D Tic Tac Toe project for a while now, and I am still fairly new to coding, so I have a couple questions/issues.

  • To start off, I am having issues with printing the board as a 1D String array. Primarily setting it up in the three x three fashion with the 'blanks' represented as '-'.

     //global variables static int ArrayLength = 9; static String[] board = new String[ArrayLength]; static int maxVal = ArrayLength; static void PrntBoard() { for (int cntr = '-'; cntr < maxVal; cntr++) { System.out.println(board[cntr]); } } 
  • I am also experiencing issues with my Computer Moves, as I keep getting errors with the computer unable to wrap around the array to eventually find an empty space and checking if a space is available in the first place.

    static void CompMove() {

     int space = 0; //keep asking till they get an empty one //have the comp random pick a spot space = RNG.nextInt(9); //check while (board[space].equals('X') || board[space].equals('O')); { space = RNG.nextInt(9)-1; } //fill in the game board with the valid position if (board[space].equals('-')) { board[space].equals('O'); PrntBoard(); } int lastspace = space; } 

Any help would be greatly appreciated, as I am still, quite frankly, a novice and do not possess much knowledge in coding. Thank you.

  1. First, please take a look on Java Language Basic to learn some basic concept.
  2. For the printBoard method, the for condition should start from 0 to maxVal , instead of using char '-' , which is converted to int 45 , check out Primitive Data Types for details.
  3. For the CompMove method, the equals comparison fails as you are comparing String(board[space]) with Character('X') , which is of different types. Please note that 'X' is different to "X" in Java.
  4. To fill in the game board with the valid position, board[space].equals('O'); is not what you need, equals only do the comparison and not setting value. Use board[space] = "O"); instead.

Please try the following code to verify the above points.

  public static void main(String[] args) {
    System.out.println("'-' converted to int is " + (int) '-');
    System.out.println("\"X\".equals('X')? " + "X".equals('X'));
    String[] board = new String[9];
    for (int i = 0; i < 9; i++) {
      board[i] = "-";
    }
    System.out.println("Before change\t" + Arrays.toString(board));
    board[0] = "O";
    System.out.println("After change\t" + Arrays.toString(board));
  }

Finally, please try to follow the Java Naming Convention , method and variable name should start with lower case.

In First example you have error in for loop. Should be:

for(int cntr=0; cntr<board.length; cntr++) {
        System.out.println(board[cntr]);
    }

In your case int cntr='-' is this same as 'int cntr=92`(because you cast char '-' to int and char '-' is number 92) but you have initialized array with 9 positions length.

In second example you have to remove semicolon next to while loop. Should be:

while (board[space].equals('X') || board[space].equals('O')){
        space = RNG.nextInt(9)-1;
    }

in your case while (board[space].equals('X') || board[space].equals('O')); you will never get into brakets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM