mardi 27 novembre 2018

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.

Aucun commentaire:

Enregistrer un commentaire