Home > Mobile >  Make sure a String[] contains 5 letter "A" at different index
Make sure a String[] contains 5 letter "A" at different index

Time:11-26

So i'm trying to make a program which create and print an array made of 29 index with 5 of those being "A" and the 24 others being "-". I've ran across the problem where it's either giving me an outofbound error or just not putting 5 "A" on the board.

Here is what i've tried so far.

 static String[] placeRandomAppleAroundBoard(String[] board) throws  java.lang.ArrayIndexOutOfBoundsException{ //ADD "A" TO RANDOM INDEX IN BOARD
        int x = 5;
        while (checkForAInArray(board)==false) {
            for (int i = 0; i < x; i  ) {
                int j = (int)(Math.random()*board.length);
                while(board[j-1].equals("A") || board[j 1].equals("A")){ //Make sure there's no A beside another "A"
                    j = (int)(Math.random()*board.length);
                }
                board[j] = "A";
            }
            x = CountAInArray(board);
        }
        return board;
    }

    static boolean checkForAInArray(String[] board){ //Make sure there is 5 "A" in the program
        int countOfA = 0;
        
        for (int i = 0; i < board.length; i  ) {
            if(board[i].equals("A")){
                countOfA  ;
            }
        }

        if(countOfA==5){
            return true;
        }
        else{
            return false;
        }
    }

    static int CountAInArray(String[] board){ //Control the number of time the For-loop of 

placeRandomAppleAroundBoard

iterate, which is 5 based and then change depending on how much "A" there's on the board

        int countOfA = 0;
        
        for (int i = 0; i < board.length; i  ) {
            if(board[i].equals("A")){
                countOfA  ;
            }
        }

        if(countOfA==0){
            return 5;
        }
        else{
            return 5 - countOfA;
        }
    }

So as you can see, i've tried to control the number of time the ForLoop add "A" in the board, that didn't work. I've also tried check for the number of "A" already present in the board but that didn't seem to do the tricks

I am expecting a board printed like so :

static void printboard(String[] board){
        System.out.println(board[0]   "|"   board[1]   "|"   board[2]   "|"   board[3]   "|"   board[4]   "|"   board[5]   "|"   board[6]   "|"   board[7]   "|"   board[8]   "|"   board[9]);
        System.out.println(board[10]   "|"   board[11]   "|"   board[12]   "|"   board[13]   "|"   board[14]   "|"   board[15]   "|"   board[16]   "|"   board[17]   "|"   board[18]   "|"   board[19]);
        System.out.println(board[20]   "|"   board[21]   "|"   board[22]   "|"   board[23]   "|"   board[24]   "|"   board[25]   "|"   board[26]   "|"   board[27]   "|"   board[28]   "|"   board[29]);
    }

Thanks for you're help!!

CodePudding user response:

So, i've found the trick ! ahahah, pretty simple actually, here it is :

static String[] placeRandomAppleAroundBoard(String[] board) throws java.lang.ArrayIndexOutOfBoundsException{
        int iterationNumber = checkForNumberOfAInArray(board);
        for (int i = 0; i < iterationNumber; i  ) {
            int j = (int)(Math.random()*board.length);
            
            while(board[j-1].equals("A") || board[j 1].equals("A") || board[j].equals("A")){
                j = (int)(Math.random()*board.length);
            }
            board[j] = "A";
        }
    return board;
}

static int checkForNumberOfAInArray(String[] board){
    int countOfA = 0;

    for (String x : board) {
        if(x == "A"){
            countOfA  ;
        }
    }

    System.out.println(countOfA);
    return (5 - countOfA);
}

And then, you basically use the second function to iterate X number of time with the first function.

CodePudding user response:

What about:

        String[] board = new String[29];
        Arrays.fill(board, "-");
        for(int i = 0;i < 5;i  ) {  
            board[i] = "A";
        }
        Collections.shuffle(Arrays.asList(board));
        System.out.println(Arrays.toString(board));
  • Related