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.

Results 1 to 10 of 10

Thread: video game project (shorter methods required) HELP!!

  1. #1
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: video game project (shorter methods required) HELP!!

    Do you have a question?


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: video game project (shorter methods required) HELP!!

    When I put the animal in i will have almost 200 lines just setting button text and foreground color.

    any suggestions, on the design.

    I have been doing Java 1 month on and off.
    Please have patience I am a newbie.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: video game project (shorter methods required) HELP!!

    Quote Originally Posted by chris96910 View Post
    When I put the animal in i will have almost 200 lines just setting button text and foreground color.

    any suggestions, I have been doing Java 1 month on and off.
    Please have patience I am a newbie.
    Define 'put the animal in'...we are working with a blank slate here. Sorry, it may be just me but I've got no clue what the goal of your project is, or where the problem lies.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: video game project (shorter methods required) HELP!!

    My apologies,

    I am creating a turn based game.
    Four character player, monster, animal, healer.
    monster moves toward player and attack when adjacent
    animal and healer move at random
    healer increments health when adjacent

    I will post all my code, you can run it and see the output to understand.

    I am still working on putting the animal in the world and moving the healer around.

    please look at the design and be as critical as possible.
    any suggestions on how I may have done the bit with the JButtons different

    keep in mind its not finished.
    sorry if i am to vague i just want the experts to look at what i am doing and make suggestions to improve

    --- Update ---

    GameGUI
    package task1;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.*;
     
    public class GameGui extends JFrame implements ActionListener {
     
        private JButton btnOne = new JButton();
        private JButton btnTwo = new JButton();
        private JButton btnThree = new JButton();
        private JButton btnFour = new JButton();
        private JButton btnFive = new JButton();
        private JButton btnSix = new JButton();
        private JButton btnSeven = new JButton();
        private JButton btnEight = new JButton();
        private JButton btnNine = new JButton();
        private JButton btnTen = new JButton();
        private JButton btnEleven = new JButton();
        private JButton btnTwelve = new JButton();
        private JButton btnThirteen = new JButton();
        private JButton btnFourteen = new JButton();
        private JButton btnFiveteen = new JButton();
        private JButton btnSixteen = new JButton();
        private JTextArea leftArea = new JTextArea(20, 8);
        private JScrollPane leftScrollPane = new JScrollPane(leftArea);
        private JTextArea rightArea = new JTextArea(20, 15);
        private JScrollPane rightScrollPane = new JScrollPane(rightArea);
        private JPanel middlePanel = new JPanel(new GridLayout(4, 4));
        private JPanel bottomPanel = new JPanel(new GridLayout(3, 0));
        private JPanel topPanel = new JPanel();
        private JPanel leftRightPanel = new JPanel(new GridLayout(0, 2));
        private JPanel bottomBtnPanel = new JPanel();
        private JButton moveLeftButton = new JButton("Left");
        private JButton moveRightButton = new JButton("Right");
        private JButton moveTopButton = new JButton("Up");
        private JButton moveBottomButton = new JButton("Down");
        private int[][] locationPlayer = new int[4][4];
        private int[][] monsterPlayer = new int[4][4];
        private int[][] healerPlayer = new int[4][4];
        private int[][] animalPlayer = new int[4][4];
        private World world;
        private Player player = new Player();
        private Monster monster = new Monster();
        private Healer healer = new Healer();
        private Animal animal = new Animal();
        private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        private Date toDayDate = new Date();
     
        public GameGui() throws DescriptionException {
            super("Game GUI");
            locationPlayer[0][0] = 1;
            monsterPlayer[2][3] = 1;
            healerPlayer[1][3] = 1;
            animalPlayer[3][0] = 1;
            world = new World(4, 4);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            rightArea.setText(simpleDateFormat.format(toDayDate) + "\n");
            topPanel.add(moveTopButton);
            leftRightPanel.add(moveLeftButton);
            leftRightPanel.add(moveRightButton);
            bottomBtnPanel.add(moveBottomButton);
     
            bottomPanel.add(topPanel);
            bottomPanel.add(leftRightPanel);
            bottomPanel.add(bottomBtnPanel);
            rightArea.append("Players Health: " + player.getHealth() + "\n");
            rightArea.append("Monsters Health: " + monster.getHealth() + "\n");
     
     
            this.getContentPane().add(getWorldView(), BorderLayout.NORTH);
            this.getContentPane().add(getMiddlePanel(), BorderLayout.CENTER);
            this.getContentPane().add(leftScrollPane, BorderLayout.WEST);
            this.getContentPane().add(rightScrollPane, BorderLayout.EAST);
            this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
     
            btnOne.setText("P");
            btnEight.setText("A");
            btnTwelve.setText("M");
            btnThirteen.setText("H");
     
            btnOne.setForeground(Color.red);
            btnEight.setForeground(Color.red);
            btnTwelve.setForeground(Color.red);
            btnThirteen.setForeground(Color.red);
     
            world.addFromLine(0, "gmmr");
            world.addFromLine(1, "gmrd");
            world.addFromLine(2, "grdd");
            world.addFromLine(3, "gddg");
            world.getPatch(0, 0);
     
            btnOne.setBackground(Color.GREEN);
            btnTwo.setBackground(Color.darkGray);
            btnThree.setBackground(Color.darkGray);
            btnFour.setBackground(Color.BLUE);
            btnFive.setBackground(Color.GREEN);
            btnSix.setBackground(Color.darkGray);
            btnSeven.setBackground(Color.BLUE);
            btnEight.setBackground(Color.ORANGE);
            btnNine.setBackground(Color.GREEN);
            btnTen.setBackground(Color.BLUE);
            btnEleven.setBackground(Color.ORANGE);
            btnTwelve.setBackground(Color.ORANGE);
            btnThirteen.setBackground(Color.BLUE);
            btnFourteen.setBackground(Color.ORANGE);
            btnFiveteen.setBackground(Color.ORANGE);
            btnSixteen.setBackground(Color.WHITE);
     
     
            moveTopButton.addActionListener(this);
            moveLeftButton.addActionListener(this);
            moveRightButton.addActionListener(this);
            moveBottomButton.addActionListener(this);
            setSize(500, 600);
            setVisible(true);
        }
     
        private JPanel getMiddlePanel() {
            middlePanel.add(btnOne);
            middlePanel.add(btnTwo);
            middlePanel.add(btnThree);
            middlePanel.add(btnFour);
            middlePanel.add(btnFive);
            middlePanel.add(btnSix);
            middlePanel.add(btnSeven);
            middlePanel.add(btnEight);
            middlePanel.add(btnNine);
            middlePanel.add(btnTen);
            middlePanel.add(btnEleven);
            middlePanel.add(btnTwelve);
            middlePanel.add(btnThirteen);
            middlePanel.add(btnFourteen);
            middlePanel.add(btnFiveteen);
            middlePanel.add(btnSixteen);
     
            return middlePanel;
        }
     
        // Creates JPanel to display in JFrame.
        // COMPLETE THIS METHOD TO DISPLAY YOUR OWN WIDGETS
        private JPanel getWorldView() {
            JPanel panel = new JPanel();
            panel.add(new JLabel("World View"));
            return panel;
        }
     
        public int getCurrentX() {
            for (int i = 0; i < locationPlayer[0].length; i++) {
                for (int z = 0; z < locationPlayer.length; z++) {
                    if (locationPlayer[z][i] == 1) {
                        return z;
                    }
                }
            }
     
            return 0;
        }
     
        public int getCurrentY() {
            for (int i = 0; i < locationPlayer[0].length; i++) {
                for (int z = 0; z < locationPlayer.length; z++) {
                    if (locationPlayer[z][i] == 1) {
                        return i;
                    }
                }
            }
            return 0;
        }
     
        public int getCurrentMonsterX() {
            for (int i = 0; i < monsterPlayer[0].length; i++) {
                for (int z = 0; z < monsterPlayer.length; z++) {
                    if (monsterPlayer[z][i] == 1) {
                        return z;
                    }
                }
            }
     
            return 0;
        }
     
        public int getCurrentMonsterY() {
            for (int i = 0; i < monsterPlayer[0].length; i++) {
                for (int z = 0; z < monsterPlayer.length; z++) {
                    if (monsterPlayer[z][i] == 1) {
                        return i;
                    }
                }
            }
            return 0;
        }
     
        public int getCurrentHealerX() {
            for (int i = 0; i < healerPlayer[0].length; i++) {
                for (int z = 0; z < healerPlayer.length; z++) {
                    if (healerPlayer[z][i] == 1) {
                        return z;
                    }
                }
            }
     
            return 0;
        }
     
        public int getCurrentHealerY() {
            for (int i = 0; i < healerPlayer[0].length; i++) {
                for (int z = 0; z < healerPlayer.length; z++) {
                    if (healerPlayer[z][i] == 1) {
                        return i;
                    }
                }
            }
            return 0;
        }
     
        public int getCurrentAnimalX() {
            for (int i = 0; i < animalPlayer[0].length; i++) {
                for (int z = 0; z < animalPlayer.length; z++) {
                    if (animalPlayer[z][i] == 1) {
                        return z;
                    }
                }
            }
     
            return 0;
        }
     
        public int getCurrentAnimalY() {
            for (int i = 0; i < animalPlayer[0].length; i++) {
                for (int z = 0; z < animalPlayer.length; z++) {
                    if (animalPlayer[z][i] == 1) {
                        return i;
                    }
                }
            }
            return 0;
        }
     
        public void movePlayer(int x, int y, int monsterX, int monsterY, int healerX, int healerY) {
            player.decrementHealth();
            monster.decrementHealth();
            rightArea.append("\n");
            rightArea.append("Players Health: " + player.getHealth() + "\n");
            rightArea.append("Monsters Health: " + monster.getHealth() + "\n");
            btnOne.setText("");
            btnTwo.setText("");
            btnThree.setText("");
            btnFour.setText("");
            btnFive.setText("");
            btnSix.setText("");
            btnSeven.setText("");
            btnEight.setText("");
            btnNine.setText("");
            btnTen.setText("");
            btnEleven.setText("");
            btnTwelve.setText("");
            btnThirteen.setText("");
            btnFourteen.setText("");
            btnFiveteen.setText("");
            btnSixteen.setText("");
     
            if ((x == 0) && (y == 0)) {
                btnOne.setText("P");
                btnOne.setForeground(Color.red);
            }
            if ((x == 0) && (y == 1)) {
                btnTwo.setText("P");
                btnTwo.setForeground(Color.red);
            }
            if ((x == 0) && (y == 2)) {
                btnThree.setText("P");
                btnThree.setForeground(Color.red);
            }
            if ((x == 0) && (y == 3)) {
                btnFour.setText("P");
                btnFour.setForeground(Color.red);
     
            }
            if ((x == 1) && (y == 0)) {
                btnFive.setText("P");
                btnFive.setForeground(Color.red);
            }
            if ((x == 1) && (y == 1)) {
                btnSix.setText("P");
                btnSix.setForeground(Color.red);
            }
            if ((x == 1) && (y == 2)) {
                btnSeven.setText("P");
                btnSeven.setForeground(Color.red);
            }
            if ((x == 1) && (y == 3)) {
                btnEight.setText("P");
                btnEight.setForeground(Color.red);
            }
            if ((x == 2) && (y == 0)) {
                btnNine.setText("P");
                btnNine.setForeground(Color.red);
            }
            if ((x == 2) && (y == 1)) {
                btnTen.setText("P");
                btnTen.setForeground(Color.red);
            }
            if ((x == 2) && (y == 2)) {
                btnEleven.setText("P");
                btnEleven.setForeground(Color.red);
            }
            if ((x == 2) && (y == 3)) {
                btnTwelve.setText("P");
                btnTwelve.setForeground(Color.red);
            }
            if ((x == 3) && (y == 0)) {
                btnThirteen.setText("P");
                btnThirteen.setForeground(Color.red);
            }
            if ((x == 3) && (y == 1)) {
                btnFourteen.setText("P");
                btnFourteen.setForeground(Color.red);
            }
            if ((x == 3) && (y == 2)) {
                btnFiveteen.setText("P");
                btnFiveteen.setForeground(Color.red);
            }
            if ((x == 3) && (y == 3)) {
                btnSixteen.setText("P");
                btnSixteen.setForeground(Color.red);
            }
     
            if ((monsterX == 0) && (monsterY == 0)) {
                btnOne.setText("M");
                btnOne.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 1)) {
                btnTwo.setText("M");
                btnTwo.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 2)) {
                btnThree.setText("M");
                btnThree.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 3)) {
                btnFour.setText("M");
                btnFour.setForeground(Color.red);
     
            }
            if ((monsterX == 1) && (monsterY == 0)) {
                btnFive.setText("M");
                btnFive.setForeground(Color.red);
            }
            if ((monsterX == 1) && (monsterY == 1)) {
                btnSix.setText("M");
                btnSix.setForeground(Color.red);
            }
            if ((monsterX == 1) && (monsterY == 2)) {
                btnSeven.setText("M");
                btnSeven.setForeground(Color.red);
            }
            if ((monsterX == 1) && (monsterY == 3)) {
                btnEight.setText("M");
                btnEight.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 0)) {
                btnNine.setText("M");
                btnNine.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 1)) {
                btnTen.setText("M");
                btnTen.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 2)) {
                btnEleven.setText("M");
                btnEleven.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 3)) {
                btnTwelve.setText("M");
                btnTwelve.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 0)) {
                btnThirteen.setText("M");
                btnThirteen.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 1)) {
                btnFourteen.setText("M");
                btnFourteen.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 2)) {
                btnFiveteen.setText("M");
                btnFiveteen.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 3)) {
                btnSixteen.setText("M");
                btnSixteen.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 0)) {
                btnOne.setText("M");
                btnOne.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 1)) {
                btnTwo.setText("M");
                btnTwo.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 2)) {
                btnThree.setText("M");
                btnThree.setForeground(Color.red);
            }
            if ((monsterX == 0) && (monsterY == 3)) {
                btnFour.setText("M");
                btnFour.setForeground(Color.red);
     
            }
            if ((monsterX == 1) && (monsterY == 0)) {
                btnFive.setText("M");
                btnFive.setForeground(Color.red);
            }
            if ((monsterX == 1) && (monsterY == 1)) {
                btnSix.setText("M");
                btnSix.setForeground(Color.red);
            }
            if ((monsterX == 1) && (monsterY == 2)) {
                btnSeven.setText("M");
                btnSeven.setForeground(Color.red);
            }
            if ((monsterX == 1) && (monsterY == 3)) {
                btnEight.setText("M");
                btnEight.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 0)) {
                btnNine.setText("M");
                btnNine.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 1)) {
                btnTen.setText("M");
                btnTen.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 2)) {
                btnEleven.setText("M");
                btnEleven.setForeground(Color.red);
            }
            if ((monsterX == 2) && (monsterY == 3)) {
                btnTwelve.setText("M");
                btnTwelve.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 0)) {
                btnThirteen.setText("M");
                btnThirteen.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 1)) {
                btnFourteen.setText("M");
                btnFourteen.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 2)) {
                btnFiveteen.setText("M");
                btnFiveteen.setForeground(Color.red);
            }
            if ((monsterX == 3) && (monsterY == 3)) {
                btnSixteen.setText("M");
                btnSixteen.setForeground(Color.red);
            }
     
            if ((healerX == 0) && (healerY == 0)) {
                btnOne.setText("H");
                btnOne.setForeground(Color.red);
            }
            if ((healerX == 0) && (healerY == 1)) {
                btnTwo.setText("H");
                btnTwo.setForeground(Color.red);
            }
            if ((healerX == 0) && (healerY == 2)) {
                btnThree.setText("H");
                btnThree.setForeground(Color.red);
            }
            if ((healerX == 0) && (healerY == 3)) {
                btnFour.setText("H");
                btnFour.setForeground(Color.red);
     
            }
            if ((healerX == 1) && (healerY == 0)) {
                btnFive.setText("H");
                btnFive.setForeground(Color.red);
            }
            if ((healerX == 1) && (healerY == 1)) {
                btnSix.setText("H");
                btnSix.setForeground(Color.red);
            }
            if ((healerX == 1) && (healerY == 2)) {
                btnSeven.setText("H");
                btnSeven.setForeground(Color.red);
            }
            if ((healerX == 1) && (healerY == 3)) {
                btnEight.setText("H");
                btnEight.setForeground(Color.red);
            }
            if ((healerX == 2) && (healerY == 0)) {
                btnNine.setText("H");
                btnNine.setForeground(Color.red);
            }
            if ((healerX == 2) && (healerY == 1)) {
                btnTen.setText("H");
                btnTen.setForeground(Color.red);
            }
            if ((healerX == 2) && (healerY == 2)) {
                btnEleven.setText("H");
                btnEleven.setForeground(Color.red);
            }
            if ((healerX == 2) && (healerY == 3)) {
                btnTwelve.setText("H");
                btnTwelve.setForeground(Color.red);
            }
            if ((healerX == 3) && (healerY == 0)) {
                btnThirteen.setText("H");
                btnThirteen.setForeground(Color.red);
            }
            if ((healerX == 3) && (healerY == 1)) {
                btnFourteen.setText("H");
                btnFourteen.setForeground(Color.red);
            }
            if ((healerX == 3) && (healerY == 2)) {
                btnFiveteen.setText("H");
                btnFiveteen.setForeground(Color.red);
            }
            if ((healerX == 3) && (healerY == 3)) {
                btnSixteen.setText("H");
                btnSixteen.setForeground(Color.red);
            }
     
        }
     
        public void updateArray(int x, int y) {
            for (int t = 0; t < locationPlayer[0].length; t++) {
                for (int i = 0; i < locationPlayer.length; i++) {
                    locationPlayer[i][t] = 0;
                }
            }
            locationPlayer[x][y] = 1;
        }
     
        public void updateMonsterArray(int x, int y) {
            for (int t = 0; t < monsterPlayer[0].length; t++) {
                for (int i = 0; i < monsterPlayer.length; i++) {
                    monsterPlayer[i][t] = 0;
                }
            }
            monsterPlayer[x][y] = 1;
        }
     
        public void updateHealerArray(int x, int y) {
            for (int t = 0; t < healerPlayer[0].length; t++) {
                for (int i = 0; i < healerPlayer.length; i++) {
                    healerPlayer[i][t] = 0;
                }
            }
            healerPlayer[x][y] = 1;
        }
     
        @Override
        public void actionPerformed(ActionEvent e) {
            String s = e.getActionCommand();
            System.out.println(getCurrentMonsterX());
            System.out.println(getCurrentMonsterX());
     
            if (s == "Right") {
                if ((locationPlayer[0][3] == 1) || (locationPlayer[1][3] == 1) || (locationPlayer[2][3] == 1) || (locationPlayer[3][3] == 1)) {
                    JOptionPane.showMessageDialog(this, "Invalid Move");
                } else {
                    int x = getCurrentX();
                    int y = getCurrentY();
                    y++;
     
                    int healerX = getCurrentHealerX();
                    int healerY = getCurrentHealerY();
     
                    int monsterX = getCurrentMonsterX();
                    int monsterY = getCurrentMonsterY();
                    if (monsterY < y) {
                        monsterY++;
                    } else if (monsterY > y) {
                        monsterY--;
                    } else if (monsterX < x) {
                        monsterX++;
                    } else if (monsterX > x) {
                        monsterX--;
                    }
                    movePlayer(x, y, monsterX, monsterY, healerX, healerY);
                    updateArray(x, y);
                    updateMonsterArray(monsterX, monsterY);
                    updateHealerArray(healerX, healerY);
     
                    //TODO: Need to ensure y is also within range
                    if ((x == monsterX + 1) || (x == monsterX - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    } else if ((y == monsterY + 1) || (y == monsterY - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    }
                }
            }
     
            if (s == "Down") {
                if ((locationPlayer[3][0] == 1) || (locationPlayer[3][1] == 1) || (locationPlayer[3][2] == 1) || (locationPlayer[3][3] == 1)) {
                    JOptionPane.showMessageDialog(this, "Invalid Move");
                } else {
                    int x = getCurrentX();
                    int y = getCurrentY();
                    x++;
     
                    int healerX = getCurrentHealerX();
                    int healerY = getCurrentHealerY();
     
                    int monsterX = getCurrentMonsterX();
                    int monsterY = getCurrentMonsterY();
                    if (monsterY < y) {
                        monsterY++;
                    } else if (monsterY > y) {
                        monsterY--;
                    } else if (monsterX < x) {
                        monsterX++;
                    } else if (monsterX > x) {
                        monsterX--;
                    }
                    movePlayer(x, y, monsterX, monsterY, healerX, healerY);
                    updateArray(x, y);
                    updateMonsterArray(monsterX, monsterY);
                    updateHealerArray(healerX, healerY);
     
                    if ((x == monsterX + 1) || (x == monsterX - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    } else if ((y == monsterY + 1) || (y == monsterY - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    }
                }
            }
     
            if (s == "Up") {
                if ((locationPlayer[0][0] == 1) || (locationPlayer[0][1] == 1) || (locationPlayer[0][2] == 1) || (locationPlayer[0][3] == 1)) {
                    JOptionPane.showMessageDialog(this, "Invalid Move");
                } else {
                    int x = getCurrentX();
                    int y = getCurrentY();
                    x--;
     
                    int healerX = getCurrentHealerX();
                    int healerY = getCurrentHealerY();
     
                    int monsterX = getCurrentMonsterX();
                    int monsterY = getCurrentMonsterY();
                    if (monsterY < y) {
                        monsterY++;
                    } else if (monsterY > y) {
                        monsterY--;
                    } else if (monsterX < x) {
                        monsterX++;
                    } else if (monsterX > x) {
                        monsterX--;
                    }
                    movePlayer(x, y, monsterX, monsterY, healerX, healerY);
                    updateArray(x, y);
                    updateMonsterArray(monsterX, monsterY);
                    updateHealerArray(healerX, healerY);
     
                    if ((x == monsterX + 1) || (x == monsterX - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    } else if ((y == monsterY + 1) || (y == monsterY - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    }
                }
            }
     
            if (s == "Left") {
                if ((locationPlayer[0][0] == 1) || (locationPlayer[1][0] == 1) || (locationPlayer[2][0] == 1) || (locationPlayer[3][0] == 1)) {
                    JOptionPane.showMessageDialog(this, "Invalid Move");
                } else {
                    int x = getCurrentX();
                    int y = getCurrentY();
                    y--;
     
                    int healerX = getCurrentHealerX();
                    int healerY = getCurrentHealerY();
     
                    int monsterX = getCurrentMonsterX();
                    int monsterY = getCurrentMonsterY();
                    if (monsterY < y) {
                        monsterY++;
                    } else if (monsterY > y) {
                        monsterY--;
                    } else if (monsterX < x) {
                        monsterX++;
                    } else if (monsterX > x) {
                        monsterX--;
                    }
     
                    movePlayer(x, y, monsterX, monsterY, healerX, healerY);
                    updateArray(x, y);
                    updateMonsterArray(monsterX, monsterY);
                    updateHealerArray(healerX, healerY);
     
                    if ((x == monsterX + 1) || (x == monsterX - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    } else if ((y == monsterY + 1) || (y == monsterY - 1)) {
                        player.setHealth(player.getHealth() - 10);
                        rightArea.append("\nAttack: " + player.getHealth());
                    }
                }
     
            }
     
     
     
        }
    }

    World
    package task1;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.StringTokenizer;
     
    /**
     * The World class holds a 2D array of patches. Methods are also provided to set
     * up the array from input data.
     * 
     * 
     */
    public class World {
    	/*
    	 * To create a World instance, provide the number of rows and columns for
    	 * the landscape. It is an error for the rows or columns to be 0 or less.
    	 */
    	public World(int rows, int columns) {
    		assert (rows > 0 && columns > 0);
    		_world = new Patch[rows][columns];
    	}
     
    	// Method to add a row of patches to the world.
    	// First argument is the row number.
    	// Second argument is a string, each character
    	// representing a patch name.
    	public void addFromLine(int row, String description)
    			throws DescriptionException {
    		if (description.length() != _world[0].length) {
    			throw new DescriptionException(description.length(),
    					_world[0].length);
    		}
    		// Break the string into token
    		for (int i = 0; i < description.length(); i++) {
    			try {
    				Patch patch = Patch.fromCharacter(description.charAt(i));
    				_world[row][i] = patch;
    			} catch (PatchException e) {
    				e.printStackTrace();
    			}
    		}
    		// For each character, create an appropriate patch instance.
    		// Place the patch in the world at the given row number,
    		// and a column number based on the position of the character
    		// in the description string.
    	}
     
    	// Convert the 2D array of patches to a string.
    	public String displayWorld() {
    		String output = "";
    		for (int i = 0; i < _world[0].length; i++) {
    			for (int j = 0; j < _world[i].length; j++) {
    				output += "Patch: " + _world[i][j].toString() + ", Cost: "
    						+ _world[i][j].getCost() + "\n";
    			}
     
    		}
     
    		return output;
    	}
     
    	public String toString() {
    		return "World: \n" + displayWorld();
    	}
     
    	// accessor to a square of the world.
    	// No error checking performed.
    	public Patch getPatch(int row, int column) {
    		return _world[row][column];
    	}
     
    	public void setPatch(int row, int col, char data) {
    		try {
    			_world[row][col] = Patch.fromCharacter(data);
    		} catch (PatchException e) {
    			e.printStackTrace();
    		}
    	}
     
    	/**
    	 * Method to read data from a file File needs to be as following layout: 2 2
    	 * -> Number of rows and columns fm -> First row will have fm dr -> Second
    	 * row will have dr
    	 * 
    	 * @param fileName
    	 * @return
    	 */
    	static World readFromFile(String fileName) {
    		try {
    			Scanner scanner = new Scanner(new File(fileName));
    			String rowsCol = scanner.nextLine();
    			int row = Integer.parseInt(rowsCol.substring(0, 1));
    			int col = Integer.parseInt(rowsCol.substring(2, 3));
    			World world = new World(row, col);
    			for (int i = 0; i < row; i++) {
    				String data = scanner.nextLine();
    				for (int y = 0; y < data.length(); y++) {
    					world.setPatch(i,y, data.charAt(y));
    				}
    			}
     
    			return world;
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    			return null;
    		} 
     
    	}
     
    	// array to hold the world definitions
    	private Patch[][] _world;
    }

    Player
    package task1;
     
    public class Player {
    	int health  = 1000;
    	boolean isAlive;
     
    	/**
    	 * Default Constructor
    	 */
    	public Player() {
     
    	}
     
    	/**
    	 * Gets Health
    	 * @return
    	 */
    	public int getHealth() {
    		return this.health;
    	}   
     
    	/**
    	 * Sets Health
    	 * @param health
    	 */
    	public void setHealth(int health) {
    		this.health = health;
    	}
     
    	/**
    	 * Gets if is alive
    	 * @return
    	 */
    	public boolean isAlive() {
    		return isAlive;
    	}
     
    	/**
    	 * Decrements health - if health hits 0 or lower then player dies
    	 */
    	public void decrementHealth() {
    		health --;
    		if (health<=0) {
    			isAlive = false;
    		}
    	}
    }

    Animal
     
    package task1;
     
     
    public class Animal {
     
       int health  = 100;
    	boolean isAlive;
     
    	/**
    	 * Default Constructor
    	 */
    	public Animal() {
     
    	}
     
    	/**
    	 * Gets Health
    	 * @return
    	 */
    	public int getHealth() {
    		return this.health;
    	}
     
    	/**
    	 * Sets Health
    	 * @param health
    	 */
    	public void setHealth(int health) {
    		this.health = health;
    	}
     
    	/**
    	 * Gets if is alive
    	 * @return
    	 */
    	public boolean isAlive() {
    		return isAlive;
    	}
     
    	/**
    	 * Decrements health - if health hits 0 or lower then player dies
    	 */
    	public void decrementHealth() {
    		health --;
    		if (health<=0) {
    			isAlive = false;
    		}
    	}
    }

    Monster
    package task1;
     
    public class Monster {
    	int health  = 10;
    	boolean isAlive;
     
    	/**
    	 * Default Constructor
    	 */
    	public Monster() {
     
    	}
     
    	/**
    	 * Gets Health
    	 * @return
    	 */
    	public int getHealth() {
    		return this.health;
    	}
     
    	/**
    	 * Sets Health
    	 * @param health
    	 */
    	public void setHealth(int health) {
    		this.health = health;
    	}
     
    	/**
    	 * Gets if is alive
    	 * @return
    	 */
    	public boolean isAlive() {
    		return isAlive;
    	}
     
    	/**
    	 * Decrements health - if health hits 0 or lower then monster dies
    	 */
    	public void decrementHealth() {
    		health --;
    		if (health<=0) {
    			isAlive = false;
    		}
    	}
    }

    Healer
     
    package task1;
     
    import java.util.HashMap;
    import java.util.Random;
     
    /**
     *
     * @author Administrator
     */
    public class Healer {
     
        int health = 10;
        boolean isAlive;
        private Random random = new Random();
     
     
        public Healer() {
     
        }
     
        public int getHealth() {
            return this.health;
        }
     
        public void setHealth(int health) {
            this.health = health;
        }
     
        public boolean isAlive() {
            return isAlive;
        }
     
        public void decrementHealth() {
            health--;
            if (health <= 0) {
                isAlive = false;
            }
        }
     
        public HashMap<Integer, Integer> randomMove(int x, int y) {
            //Map that stores the x and y co-ordinates
            HashMap<Integer, Integer> directionMap = new HashMap<Integer, Integer>();
     
            int direction = random.nextInt(4);
            int movements = random.nextInt(2) + 1;
            if (direction == 0) {//North
                y -= movements;
            }
            if (direction == 1) {//East
                x += movements;
            }
            if (direction == 2) {//South
                y += movements;
            }
            if (direction == 3) {//West
                x -= movements;
            }
            directionMap.put(x, y);
            return directionMap;
        }
    }

    Patch
    package task1;
     
    /**
     * The Patch class is a parent class of the different patch types which are
     * placed in the World object.
     * 
     * @author peter
     */
    public class Patch {
     
    	protected Patch(int cost, char letter) {
    		_cost = cost;
    		_letter = letter;
    	}
     
    	// Helper method for creating a patch type from
    	// a character.
    	// Needs extending to cope with other patch types.
    	static Patch fromCharacter(char c) throws PatchException {
    		switch (c) {
    		case 'g':
    		case 'G':
    			return new GrassPatch();
    		case 'f':
    		case 'F':
    			return new ForestPatch();
    		case 'm':
    		case 'M':
    			return new MountainPatch();
    		case 'd':
    		case 'D':
    			return new DesertPatch();
    		case 'r':
    		case 'R':
    			return new RiverPatch();
    		default:
    			throw new PatchException(c);
    		}
    	}
     
    	// accessor to the cost variable
    	public int getCost() {
    		return _cost;
    	}
     
    	// accessor to the letter variable
    	public int getLetter() {
    		return _letter;
    	}
     
    	// convert the patch to a string, using the _letter.
    	public String toString() {
    		return "" + _letter;
    	}
     
    	// _cost is the energy required for a character to move
    	// across this patch of land.
    	private int _cost;
     
    	// _letter is the letter to use when printing the patch
    	private char _letter;
    }

    River
    package task1;
     
    /**
     *
     */
    public class RiverPatch extends Patch {
        public RiverPatch () {
            super (2, 'R'); // provide cost for River to parent
        }
    }

    Mountain
    package task1;
     
    /**
     *
     */
    public class MountainPatch extends Patch {
        public MountainPatch () {
            super (10, 'M'); // provide cost for Mountain to parent
        }
    }

    Desert
    package task1;
     
    /**
     *
     */
    public class DesertPatch extends Patch {
        public DesertPatch () {
            super (5, 'D'); // provide cost for Desert to parent
        }
    }

    Forest
    package task1;
     
    /**
     *
     */
    public class ForestPatch extends Patch {
        public ForestPatch () {
            super (3, 'F'); // provide cost for Forest to parent
        }
    }

    Grass
    package task1;
     
    /**
     *
     * @author peter
     */
    public class GrassPatch extends Patch {
        public GrassPatch () {
            super (1, 'G'); // provide cost for Grass to parent
        }
    }

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: video game project (shorter methods required) HELP!!

    where did my post go??

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: video game project (shorter methods required) HELP!!

    Your post has been approved.

    please look at the design and be as critical as possible
    Critical about what? I still don't understand what the question is, and that's a lot of code and classes to ask us to compile and run. Does it compile? Are there exceptions? Does it work as you expect? Are you asking us to look at your design? If you wish to make the most of these forums, please phrase your problem as a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: video game project (shorter methods required) HELP!!

    Here are the exceptions. yes it compiles and runs. no errors



    --- Update ---

    Patch Exception
    package task1;
     
    /**
     *
     * @author peter
     */
    public class PatchException extends Exception {
        public PatchException (char c) {
            _char = c;
        }
     
        public String toString () {
            return "Invalid Patch character: " + _char;
        }
     
        private char _char;
    }

    DescriptionException
    package task1;
     
    /**
     * DescriptionException thrown when setting up a world instance
     * if the description does not match the required world size.
     *
     * @author peter
     */
    public class DescriptionException extends Exception {
        public DescriptionException (int descriptionLength, int worldColumns) {
            _descriptionLength = descriptionLength;
            _worldColumns = worldColumns;
        }
     
        public String toString () {
            return "Description length is " + _descriptionLength + 
                    " and not " + _worldColumns;
        }
     
        private int _descriptionLength;
        private int _worldColumns;
    }

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: video game project (shorter methods required) HELP!!


  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: video game project (shorter methods required) HELP!!

    Is a character class a good idea and allow player,monster,animal and healer to inherit? like what I did with the Patch (river,grass,etc.)
    What makes sense? Are there duplicate methods in each class (looks to me like the answer is yes)? Would it make sense that each of these classes be related? It will work either way, but often much easier to manage if you can re-use methods, classes, etc...in a way that makes sense to you.

    Is there a different way to do the JButtons in the movePlayer method in GameGui?
    When working with lots of different instance of the same class, arrays or Collections truly help when creating and accessing the classes. For instance, your GameGUI class has variables btnOne, btnTwo,...an array might be a better way to organize them, so that you can use loops to instantiate and/or access them

  10. The Following User Says Thank You to copeg For This Useful Post:

    chris96910 (April 1st, 2013)

  11. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: video game project (shorter methods required) HELP!!

    How can I prevent collisions of the characters? Currently the player "P" can move on top of the monster "M".

    I was told I should use collections as much as possible. (take a look at the HashMap in Healer).
    It should allow the healer to move random.

    Can you identify anywhere I can implement any more Collections?

    An Array for the buttons is a great idea thank you.
    I was a little afraid of trying to make a parent class after starting this way, but I was told to make shorter methods and avoid duplication.

Similar Threads

  1. Replies: 0
    Last Post: November 16th, 2012, 05:25 AM
  2. Video Game Character ArrayList Help?
    By MagicTricksKill in forum Object Oriented Programming
    Replies: 0
    Last Post: October 15th, 2012, 03:44 PM
  3. Software Project Final Year - Help required
    By aj4u in forum Paid Java Projects
    Replies: 1
    Last Post: April 11th, 2011, 08:20 PM
  4. Program compiled/ran as required - FAILED project WHY??????????
    By MISSAJ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 23rd, 2010, 03:35 PM
  5. Replies: 5
    Last Post: June 10th, 2010, 10:19 AM