mardi 21 mars 2017

Java: taking a random element from Arraylist, checking it, removing if necessary and taking another random element from Arraylist

I am very new to Java and I'm having so much trouble with this code I want to write. I'll be happy for any help or pointers.

I am writing a random move for a Nim game.

I have some integers 1-5 which represent my rows.

I want to get randomly one number from 1 to 5, not including 0 because it will throw me an exception later (1-5 correspond to elements in my other class (Board) so I can't have it be 0).

I want to check if the row with that number has any unmarked sticks, if it doesn't I want to remove it from the arrayList, go back and take a random row again (but this time it's 1-4), etc until I find a row that has some unmarked sticks.

Then I check if there are unmarked stick after that stick, and make my move which is marking a sequence of unmarked sticks.

I wrote this code:

private Move produceRandomMove(Board board) {

    List<Integer> myRows = Arrays.asList(1, 2, 3, 4, 5);  // array of 1-5

    Random myRandom = new Random();  // allow to make random stuff

    while (true) {
        int randomRowIndex = myRandom.nextInt(myRows.size());  // random from 1 to 5
        int randomRow = myRows.get(randomRowIndex);  // element can't be 0 (I hope)
        for (int stick = 1; stick <= board.getRowLength(randomRow); stick++) {
            if (board.isStickUnmarked(randomRow, stick)) {  // if stick is unmarked
                int left = stick;
                int right = stick + 1;
                while (board.isStickUnmarked(randomRow, right)) {  // while the right stick is unmarked or exists
                    // it returns True
                    right = stick++;
                }  // we exit the loop when stick is marked or doesn't exist
                return new Move(randomRow, left, right - 1);  // since the "last stick" that makes us exit we
                // make right - 1.

            }

        } // get here if none of the sticks in the row are unmarked

        myRows.remove(randomRowIndex);  // remove row if it doesn't have empty sticks

    } // there should eventually be a row with some empty sticks so should never get here

}

I keep getting this error:

Exception in thread "main" java.lang.UnsupportedOperationException

at java.util.AbstractList.remove(AbstractList.java:161)

at Player.produceRandomMove(Player.java:163)

... (some more similar rows)

I keep trying to find my error, I think it is something with the index, but I went over my code and I don't understand where it goes wrong...

For reference, this is my Board's class' function that I am using to "check" the row:

/**
 * Given an index to the stick position (row and number in row - counting from the left side), 
 * this method returns true if the stick is unmarked, and false if it is marked, or if the input is
 * out of bounds.
 */

public boolean isStickUnmarked(int row,int stickNum){

    if(row<1 || row>NUM_OF_ROWS || stickNum<1 || stickNum>gameBoard[row-1].length)

        return false;

    return(gameBoard[row-1][stickNum-1]==1);

}




Aucun commentaire:

Enregistrer un commentaire