Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 47 of 47

Thread: Rush hour game

  1. #26
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    ok so i got some help from someone.
    i know have this question: can i get rid of the visual part? because i don't need that just now
    i only need it in console atm

     
    class Car
    {
        private int xPos;
        private int yPos;
        private int width;
        private int height;
        Color color;
     
        Car(int x, int y, int w, int h, Color color)
        {
            this.xPos = x;
            this.yPos = y;
            this.width = w;
            this.height = h;
            this.color = color;
        }
     
        public void setX(int xPos){
            this.xPos = xPos;
        }
     
        public int getX(){
            return xPos;
        }
     
        public void setY(int yPos){
            this.yPos = yPos;
        }
     
        public int getY(){
            return yPos;
        }
     
        public int getWidth(){
            return width;
        }
     
        public int getHeight(){
            return height;
        }
     
        public boolean contains(Point p)
        {
            return new Rectangle(xPos, yPos, width, height).contains(p);
        }
     
        public void paintCar(Graphics g)
        {
            g.setColor(color);
            g.fillRect(xPos,yPos,width,height);
            g.setColor(Color.BLACK);
            g.drawRect(xPos,yPos,width,height);
        }
    }
     
    public class RhTest extends JPanel
    {
        // Very easy to add othe cars to the board, just specify position, size and colour
        // Double array to enter x position, y position, width and height
        int[][] dims = {
                // x    y    w    h
                {  50, 100, 100,  50 }, {   0,   0, 100,  50 }, { 250,   0, 50, 150 },
                { 150,  50,  50, 150 }, {   0,  50,  50, 150 }, {   0, 200, 50, 100 },
                { 200, 200, 100,  50 }, { 100, 250, 150,  50 }
        };    // Colours of the cars are entered here
        Color[] colors = {
                Color.red, Color.green, Color.yellow, Color.blue,
                Color.pink, Color.orange, Color.cyan, Color.black
        };
        Car[] cars;
        int selectedIndex;
        boolean drag = false;
        // added here
        boolean mouseInside, collide;
        // end of add
        final int OFFSET = 1;
     
        public RhTest() {
            initCars();
            setBorder(BorderFactory.createLineBorder(Color.black));
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    Point p = e.getPoint();
                    // Check to see if user has clicked on a car
                    for(int j = 0; j < cars.length; j++) {
                        if(cars[j].contains(p)) {
                            selectedIndex = j;
                            drag = true;
                            break;
                        }
                    }
                }
     
                public void mouseReleased(MouseEvent e) {
                    drag = false;
                }
            });
     
            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    if(drag) {
                        moveCar(e.getX(),e.getY());
                    }
                }
            });
        }
     
        private void moveCar(int x, int y){
            final int CURR_X = cars[selectedIndex].getX();
            final int CURR_Y = cars[selectedIndex].getY();
            final int CURR_W = cars[selectedIndex].getWidth();
            final int CURR_H = cars[selectedIndex].getHeight();
            final int OFFSET = 1;
     
            if ((CURR_X!=x) || (CURR_Y!=y)) {
                // The car is moving, repaint background
                // over the old car location.
                repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
                // Update coordinates.
                if (CURR_W > CURR_H)
                {
                    cars[selectedIndex].setX(x);
                }
                if (CURR_H > CURR_W)
                {
                    cars[selectedIndex].setY(y);
                }
     
                // Repaint the car at the new location.
                repaint(cars[selectedIndex].getX(), cars[selectedIndex].getY(),
                        cars[selectedIndex].getWidth()+OFFSET,
                        cars[selectedIndex].getHeight()+OFFSET);
            }
        }
     
        private void initCars() {
            cars = new Car[colors.length];
            for(int j = 0; j < cars.length; j++) {
                int x = dims[j][0];
                int y = dims[j][1];
                int w = dims[j][2];
                int h = dims[j][3];
                cars[j] = new Car(x, y, w, h, colors[j]);
            }
        }
     
        public Dimension getPreferredSize() {
            return new Dimension(300,300);
        }
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for(int j = 0; j < cars.length; j++)
                cars[j].paintCar(g);
            // added here
            if(collide)
                g.drawString("Collision!", 10, 20);
            //end of add
     
        }
    }
     
     
    public class Main {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
     
        private static void createAndShowGUI() {
            JFrame f = new JFrame("The Rush Hour Game");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new RhTest());
            f.setSize(317,337);
            f.setVisible(true);
        }
    }

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    Are you asking how to move the logic from the paintComponent() method to use a println()?

    Sometimes it is easier to start from the beginning than to try to rewrite code that is written for a completely different environment.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    yes i think that is what i need.
    you are probably right but i just don't have enough time to start from the begining again

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    It could take longer to rewrite a GUI app to run in a console. Your choice of course.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    i dont know how to start working on both so idk :/

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    Why both?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    i mean i dont know how to take out the GUI and i also don't know how i should start all over

    --- Update ---

    How can i change the values of an array and stock it in another?

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    change the values of an array
    Use an assignment statement:
    theArray[theIndex] = value;  // change array's contents at index element

    stock it in another
    Can you explain what that means?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    i mean that after you change the values, the program should print the new array and you should be able to keep doing this. like you are moving the cars 1 block at a time and take a picture after every move

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    the program should print the new array
    The Arrays class's deepToString() method is useful for printing out the contents of a 2 dim array for debugging:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));

    If the print out is for a user to see, you'll have to write some loops and use print()
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    i'm almost finished now, but when i try to get an extern file (the level) i get this error:
    --Error: File Not Found--
    The data file could not be found. Check thefile name and try again.

    where should the file be?

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    where should the file be
    Where the program is looking for it to be.
    To see where the program is looking for the file, Create a File object with the same path to the file that is currently being used and print out the value of the File class's get absolute path method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    if this is my game board
    and each number represents a car
    88= the border of the board
    10= an open space

    What should i write to make the numbers(cars) move and fill the other space with '10'??

     {int[][] board = { {8,88,88,88,88,88,88,8},
                    {8,22,22,22,37,23,36,8},
                    {8,33,34,34,37,23,36,8},
                    {8,33,10,45,45,23,10,11},
                    {8,30,30,36,10,10,10,8},
                    {8,10,39,36,10,31,31,8},
                    {8,10,39,35,35,32,32,8},
                    {8,88,88,88,88,88,88,8}
            };

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    make the numbers(cars) move
    Define the rules for moving a car. If a car is at 3,4 what other locations are legal moves? There are 8 locations immediately adjacent.
    What if the other location is not empty (has a value other than 10)?

    fill the other space with '10'??
    Not sure where the "other space" is?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #40
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    i mean the space that becomes empty if a car is moved

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    If you know where the car was located before it was moved, that would be the place that is empty after the car was moved.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    i know but as i said, i have no idea how to write this. i started lessons only 1 month ago

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    Write what? Describe in simple steps what you are trying to write.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #44
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    -ask the player which car he wants to move
    -ask the player in which direction
    -show the gameboard where the car is moved --> so let the number move and fill the open space with 10

  20. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    Which step is the problem?
    Take the first step: What problems do you have?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #46
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rush hour game

    the problem is that i don't know how to move the number in the array when the player calls it.
    the program asks the number and the direction but then it doesnt do anything else

  22. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rush hour game

    how to move the number in the array when the player calls it.
    That wasn't the step to work on. I don't see that step in the list of steps in post #44.
    Are you adding new steps to that list? If so, please post the new list of steps.

    You need to work on one step at a time. The first step was:
    -ask the player which car he wants to move

    How will the program do that?
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  2. Replies: 0
    Last Post: December 9th, 2012, 09:45 PM
  3. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  4. Help with Clock (advance and reverse day,hour,minute)
    By whyld4900 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 13th, 2011, 02:39 AM
  5. Array month/day/hour/min/sec incomplete java code
    By mightyking in forum Collections and Generics
    Replies: 12
    Last Post: September 18th, 2011, 08:46 PM