Random placement of places in trays and stairs

I am a student working on the game Chutes and Ladders. I use methods to determine how many trays and ladders should be placed on the playing field. I list 10 for each in the basic usage parameters, but I keep getting 6 to 11 spread across the board.

Is something going on with two ways interfering with each other?

Or is there a problem with how I set up the for loop to be randomly allocated?

I'm new to this site, please let me know if you need more clarification, I didn't want to post the whole program here. Thank.

//main
                  ChutesAndLadders cl = new ChutesAndLadders();
                  cl.setBoard(new String[100]);
                  cl.makeChutes(10);
                  cl.makeLadders(10);

//methods
            public String [] board;
            private int chutes, ladders;
            public int position;
            public Random rand = new Random();


        //set board
                    public void setBoard(String [] n){
                        board = n;
                        for(int i = 0; i < board.length; i++)
                            board[i] = "   ";
                    }
        //set and place chutes
                    public void makeChutes(int n){
                        chutes = n;
                        for(int i = 0; i <= chutes; i++)                    
                            board[rand.nextInt(board.length)] = "C" + chutes;

                    }
        //set and place ladders
                    public void makeLadders(int n){
                        ladders = n;
                            int lcell = 0; 
                        for(int i = 0; i <= ladders; i++)
                                 board[rand.nextInt(board.length)] = "L" + ladders;

      

+3


source to share


3 answers


First, you wrote:

for(int i = 0; i <= chutes; i++)                    
    board[rand.nextInt(board.length)] = "C" + chutes;

      

The assignment operator in the loop will start trays + 1 times. (Eleven times in your case.) [Use i < chutes

instead.] It's the same in your ladder code. This explains why you can have up to 11 trays or ladders when your code runs.

Secondly, you don't take the trouble of having the same space assigned to a tray or staircase multiple times. rand.nextInt(board.length)

does not guarantee to get unique values ​​on every run (otherwise it would not be random). This explains why you cannot see up to 11 trays and stairs when the code is executed.

To make it clearer, put a constant value here:



for(int i = 0; i < chutes; i++)                    
    board[11] = "C" + chutes;

      

and note that you end up with one tray (in space eleven) - unless the ladder code overwrites it with the ladder.

Hope it helps.

Good luck!

+6


source


At first glance, my guess is that you are completing overlapping records. Since you are creating a random placement and not checking to see if there is a gutter or staircase in there, you will probably end up with overlaps.

It should be enough to just create a random position and then check if there is something in there before placing. If a collision is found, just generate another random one and repeat until you can place it.



Also, as an aside, it's always a good idea to avoid loops and operators without curly braces. It is very easy to add a second one as in a block and wonder why it is not being executed as part of a block.

+2


source


Your loops have a built in upper limit check, 0 .. 10

- 11 records.

As Mike said, fewer results are caused by collisions, you can prevent them by setting up the board, filling it with the required elements, and then shuffling the board to get the end result, for example:

public void setupBoard(String [] n, int nrLadders, int nrChutes){
    board = n;
    int index = 0;

    while (index < board.length && 0 < nrLadders--) {
        board[index++] = "L" + nrLadders;
    }

    while (index < board.length && 0 < nrChutes--) {
        board[index++] = "C" + nrChutes;
    }

    while (index < board.length) {
        board[index++] = "   ";
    }

    board = Collections.shuffle(Arrays.asList(board)).toArray(new String[board.length]);
}

      

It's like making a deck of cards containing multiple ladder cards, multiple gutter cards, more blank spot cards, and shuffling that deck to get the board.

+1


source







All Articles