# move random matrix 8*8

• (2 Pages)
• 1
• 2

## 20 Replies - 1403 Views - Last Post: 03 December 2011 - 03:30 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257695&amp;s=1b8b6965c1761658cb3a246fc624daaa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

# move random matrix 8*8

Posted 28 November 2011 - 03:52 PM

Hii ,

I have list of orientaion
```String orientaion[] = {"N", "E", "W", "S"};
```

and I want to take random values from the list, 3 values each time.

I tried this, but it didn't work

```
public void Move() {
int Counter = 0;
Random r = new Random();
while (Counter < 1000) {
for(r.nextInt(orientaion.length())){

}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: move random matrix 8*8

### #2 g00se

• D.I.C Lover

Reputation: 2900
• Posts: 12,366
• Joined: 20-September 08

## Re: move random matrix 8*8

Posted 28 November 2011 - 04:38 PM

Try something like

```List<String> orientation = Arrays.asList(new String[] {"N", "E", "W", "S"} );
List<String> choices = new ArrayList<String>(3);
Collections.shuffle(orientation);
for(int i = 0;i<3;i++) {
}
```

This post has been edited by g00se: 28 November 2011 - 04:38 PM

### #3 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 29 November 2011 - 01:41 PM

Hi ,

I want the machine to move random inside the room (matrix 8*8)and clean the dirt, The movement of the machine is depend in choosing randomly from the orientation list { N, S, E, W} the direction of the machin will be for example {N,N,S} it will be repeated untill the machine face the wall then is should choose again.

I tried this : but it give me errors

```import java.util.*;

public class test {

private static String[][] board;
private static final int ROWS = 8;
private static final int COLUMNS = 8;
int moves = 0;
List<String> orientation = Arrays.asList(new String[]{"N", "E", "W", "S"});
List<String> choices = new ArrayList<String>(3);

public test() {
//String state = "d";
board = new String[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
board[i][j] = " ";

}
}
board[4][4] = "#";
board[0][2] = "d";
board[4][7] = "d";
board[1][5] = "d";
board[6][6] = "d";
board[4][0] = "d";
}

public String toString() {
String r = "";
for (int i = 0; i < ROWS; i++) {

for (int j = 0; j < COLUMNS; j++) {
r += "[" + board[i][j] + "]";
}
r += "\n";
}
return r;
}

public int[] gostright(int x, int y) {
if (choices.contains("N")) {
x--;
}
if (choices.contains("W")) {
y--;
}
if (choices.contains("S")) {
x++;
}
if (choices.contains("E")) {
y++;
}
return new int[]{x, y};
}

public void Move() {
Collections.shuffle(orientation);
int nowX = 0, nowY = 0;
int counter = 0;
int strightCounter = 4;
while (counter < 5) {
for (int i = 0; i < 3; i++) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < strightCounter; y++) {

board[nowX][nowY] = "1";
int[] pos = gostright(nowX, nowY);
nowX = pos[0];
nowY = pos[1];
board[nowX][nowY] = "#";
moves++;
System.out.println(orientation.get(i));
}
}
counter++;
System.out.println("#######test########");
}

}
}

public static void main(String[] args) {
test nnn = new test();
System.out.println("The Vacuum Cleaner starting to clean the room");
System.out.println(nnn.toString());
nnn.Move();
System.out.println("Mission is done, the room is clean.");
}
}
```

any idea , what I can edit to work correctly ???

### #4 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: move random matrix 8*8

Posted 29 November 2011 - 01:48 PM

And what errors do you get?

### #5 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 29 November 2011 - 01:52 PM

its only print the board, without the move of machine

This is the Output:

Quote

The Vacuum Cleaner starting to clean the room
[ ][ ][d][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][d][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[d][ ][ ][ ][#][ ][ ][d]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][d][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at test.Move(test.java:73)
at test.main(test.java:89)
Java Result: 1

### #6 SwiftStriker00

• No idea why my code works

Reputation: 435
• Posts: 1,602
• Joined: 25-December 08

## Re: move random matrix 8*8

Posted 29 November 2011 - 01:59 PM

ArrayIndexOutOfBoundsException means you tried to access an index of an array that dident exist:

an array has 4 elements, this means the index will be from 0-3. That means if you say array[-1] or array[4], you will get that exception thrown

### #7 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 29 November 2011 - 02:04 PM

SwiftStriker00, on 29 November 2011 - 01:59 PM, said:

ArrayIndexOutOfBoundsException means you tried to access an index of an array that dident exist:

an array has 4 elements, this means the index will be from 0-3. That means if you say array[-1] or array[4], you will get that exception thrown

Ok, But where the error, or what line cause this error.

### #8 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: move random matrix 8*8

Posted 29 November 2011 - 02:08 PM

First time when you call this int[] pos = gostright(nowX, nowY); you passes 0,0 as parameters. And you first you add choices.add(orientation.get(i)); and for the first time it is "N". So in gostright() you decrease x from 0 to -1 and return them as -1,0.
So pos[0] = -1, and you said
```nowX = pos[0]; //assign -1 to it
```
and then use it as a parameter, so array out of bounds

### #9 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: move random matrix 8*8

Posted 29 November 2011 - 02:18 PM

Topics with the same related code merged.
Please avoid duplicate topics for the same problem.

### #10 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 30 November 2011 - 05:14 PM

Hii

I have array contains the position of dirts
``` int[][] Dirt = {{4, 2}, {0, 5}, {7, 1}, {3, 3}};
```

once the machine clean one dirt it should be removed from the list until all the dirt from the list be removed then stop.

``` public void Move() {
Collections.shuffle(orientation);
int nowX = 4, nowY = 4;
int[] block = {-1, -1};
int counter = 0;
int strightCounter = 1;
int x = 0 ; int y=0;
while (!(Dirt.length==0)) {
//               for (int x = 0; x < strightCounter; x++) {
//                   for (int y = 0; y < strightCounter; y++) {

for (int i = 0; i < 3; i++) {

board[nowX][nowY] = "1";
int[] pos = gostright(nowX, nowY);
nowX = pos[0];
nowY = pos[1];
System.out.println("" + nowX + ":" + nowY);
System.out.println("nowX and nowY" + board[nowX][nowY]);
board[nowX][nowY] = "#";
moves++;

System.out.println(toString());
System.out.println(orientation.get(i));
System.out.println("Choices "+choices);
System.out.println("# move"+moves);
if(Wall(nowX,nowY)){
break;
}
}
//                }
counter++;

//            }
}
}
```

How I can compare between 2 arrays (pos and dirt), and if equal remove elemant from the (dirt) ??

I try this one, but did not work

```for (int z = 0; z < 3; z++) {
if(board[Dirt[z][0]][Dirt[z][1]].equals()) {){
block[0] = pos[0];
block[1] = pos[1];

}
}
```

### #11 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 01 December 2011 - 01:37 PM

any suggestion??

### #12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11058
• Posts: 41,459
• Joined: 27-December 08

## Re: move random matrix 8*8

Posted 01 December 2011 - 08:44 PM

Arrays are static in size. You cannot remove elements from an array. Rather than checking to see if the size of the array is 0, take a different approach. Each space you clean, change it's value to flag it as clean and increment a counter. From there, choose a random direction, so long as the space chosen isn't clean. Proceed to that space. When counter == numSpaces, then the cleaning is done.

### #13 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 02 December 2011 - 03:22 PM

macosxnerd101, on 01 December 2011 - 08:44 PM, said:

Arrays are static in size. You cannot remove elements from an array. Rather than checking to see if the size of the array is 0, take a different approach. Each space you clean, change it's value to flag it as clean and increment a counter. From there, choose a random direction, so long as the space chosen isn't clean. Proceed to that space. When counter == numSpaces, then the cleaning is done.

Nice idea,,, But the instructor require that the program stop when it clean all dirts. I tried to create a new array with size = size -1 and then copy the relevant values (minus the deleted row) into the new array.

```int[][] Dirt = {{4, 2}, {0, 5}, {7, 1}, {3, 3}};
int[][] temp = Dirt;
int size = Dirt.length - 1;

public void Move() {
Collections.shuffle(orientation);
int nowX = 4, nowY = 4;
int counter = 0;
int strightCounter = 1;
Dirt = new int[size][2];
int newIndex = 0;
int x = 0;
int y = 0;
while (!(Dirt.length == 0)) {

for (int i = 0; i < 3; i++) {

board[nowX][nowY] = "1";
int[] pos = gostright(nowX, nowY);
nowX = pos[0];
nowY = pos[1];
System.out.println("" + nowX + ":" + nowY);
System.out.println("nowX and nowY" + board[nowX][nowY]);
board[nowX][nowY] = "#";
moves++;
for (int z = 0; z < temp.length; z++) {
int[] oneDimension = temp[z];
if (pos[0] == oneDimension[0] && pos[1] == oneDimension[1]) {
continue;
}else{
Dirt[newIndex][0] = oneDimension[0];
Dirt[newIndex][1] = oneDimension[1];
newIndex++;
}
}
System.out.println("The array After removing : ");
for (int s = 0; s < Dirt.length; s++) {
System.out.println(Dirt[s][0] + "," + Dirt[s][1]);
}

System.out.println(toString());
System.out.println(orientation.get(i));
System.out.println("Choices " + choices);
System.out.println("# move" + moves);
if (Wall(nowX, nowY)) {
break;
}
}

counter++;

}
}

```

This the output :

Quote

[ ][ ][ ][ ][ ][ ][ ][ ]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]

choise taste equal5:4
5:4
nowX and nowY
at test.Move(test.java:121)
at test.main(test.java:149)
Java Result: 1

### #14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11058
• Posts: 41,459
• Joined: 27-December 08

## Re: move random matrix 8*8

Posted 02 December 2011 - 03:26 PM

Quote

But the instructor require that the program stop when it clean all dirts.

So how does my idea not do that? It's easier and more efficient than resizing the arrays. In fact, by resizing the arrays, you mess up the adjacencies.

### #15 alberwaaz

Reputation: 0
• Posts: 19
• Joined: 25-October 11

## Re: move random matrix 8*8

Posted 02 December 2011 - 03:41 PM

macosxnerd101, on 02 December 2011 - 03:26 PM, said:

Quote

But the instructor require that the program stop when it clean all dirts.

So how does my idea not do that? It's easier and more efficient than resizing the arrays. In fact, by resizing the arrays, you mess up the adjacencies.

For example, there are 3 dirt in the room. The program will continue even if it cleans the 3 dirt.
and that will increase the number of moves inside the room.