Results 1 to 3 of 3
  1. #1
    ALREEM is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Exclamation Help with Vacuum cleaner code

    The Requirements are:

    1.The machine should move randomly.
    2.It should clean all the dirt and then stop.
    3.The direction of the movement should be in a array-list, so each time the machine should choose randomly 3 orientation from the list as a direction example(N,N,S).
    4. The machine will move according to the direction continually until it reach the edge then it should choose again.
    5. The dirt should be in array-list and once the machine clean one dirt it should be removed from the list until all the dirt from the list be removed then stop.


    The problem I faced that the machine when it reach on of the edges it stuck there and still be in the same position !!! I dont how how or where I can fix it !!

    Java Code:
    
    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] = "d";
            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 (x == -1) {
                    x = 0;
                }
    
            } else if (choices.contains("W")) {
                y--;
                if (y == -1) {
                    y = 0;
                }
    
            } else if (choices.contains("S")) {
                x++;
                if (x == 8) {
                    x = 7;
                }
            } else if (choices.contains("E")) {
                y++;
                if (y == 8) {
                    y = 7;
                }
            }
            System.out.println("choise taste equal" + x + ":" + y);
            return new int[]{x, y};
        }
    
        public boolean Wall(int x, int y) {
    
            if (choices.contains("N") && y == 7) {
                return false;
            } else if (choices.contains("S") && y == 0) {
                return false;
            } else if (choices.contains("W") && x == 0) {
                return false;
            } else if (choices.contains("E") && x == 7) {
                return false;
            } else {
                return true;
            }
        }
    
        public int CountDirt() {
            int count = 0;
            for (int i = 0; i < ROWS; i++) {
                for (int j = 0; j < COLUMNS; j++) {
                    if (board[i][j] == "d") {
                        count++;
                    }
                }
            }
            // System.out.println("the found dirt " + count+" dirts");
            return count;
        }
    
        public void Move() {
            Collections.shuffle(orientation);
            int nowX = 4, nowY = 4;
            int counter = 0;
            int strightCounter = 1;
            int wallx = 0;
            int wally = 0;
            while (CountDirt() > 0) {
                for (int i = 0; i < 3; i++) {
                    choices.add(orientation.get(i));
                    for (int x = 0; x < strightCounter; x++) {
                        for (int y = 0; y < strightCounter; y++) {
    
                            Wall(x,y);
                            System.out.println("Wall" + x + ":" + y);
                            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);
                        }
                    }
                    counter++;
                    System.out.println("CountDirt()==" + CountDirt());
    
    
                }
                choices.clear();
    
            }
    
        }
    
        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.");
        }
    }
    I hope its clear for you, looking for your reply.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ALREEM is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Help with Vacuum cleaner code

    stackoverflow.com: move-random-matrix-88

    ^That post is from my project partner.

    We are still looking for help.

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 10:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  4. java.lang.Error: Cleaner terminated abnormally
    By CaffeineDK in forum Advanced Java
    Replies: 2
    Last Post: 02-18-2009, 04:19 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •