0

I am currently working on a text based rpg in java. Each time the player walks a certain number of steps, a random battle is supposed to occur. Each time I run the battle system, the program goes into a forever loop and the screen just freezes.

How can I fix this? Here is the code for the main class where all this is handled.

import javax.swing.*;
import java.awt.event.*;
public class Runner extends JFrame{

    String mat[][] = {{".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."},
                      {".",".",".",".",".",".",".",".",".","."}};

    boolean Burn;
    JPanel j = new JPanel();
    Player p = new Player(1,20,20,0,2,6,0,7);
    Enemy en = new goblin(1,8,8,0,1,3);
    private int steps = 0, randomBattle = (int)(Math.random()*(7-4))+4;

    public Runner()
    {
        System.out.println(""+p.getHp());
        JPanel j = new JPanel();
        JLabel gridDisplay = new JLabel();
        JButton north = new JButton("N");
        JButton south = new JButton("S");
        JButton east = new JButton("E");
        JButton west = new JButton("W");
        Grid grins = new Grid(mat,1,1,mat.length,mat[0].length);
        gridDisplay.setBounds(200,10,200,200);
        gridDisplay.setText(grins.toString());
        j.setLayout(null);
        north.setBounds(100,50,50,50);
        south.setBounds(100,105,50,50);
        east.setBounds(45,77,50,50);
        west.setBounds(155,77,50,50);
        j.add(north);
        j.add(south);
        j.add(east);
        j.add(west);
        j.add(gridDisplay);
        j.setVisible(true);
        j.setSize(800,600);
        add(j);

        north.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (randomBattle == steps)
                {
                    j.setVisible(false);
                    System.out.println("HP: "+p.getHp());
                    Battle(p, en);
                }
                grins.changeX(-1);
                gridDisplay.setText(grins.toString());
                steps++;
            }
        });

        south.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (randomBattle == steps)
                {
                    j.setVisible(false);
                    System.out.println("HP: "+p.getHp());
                    Battle(p, en);
                }
                grins.changeX(1);
                gridDisplay.setText(grins.toString());
                steps++;
            }
        });

        east.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (randomBattle == steps)
                {
                    j.setVisible(false);
                    System.out.println("HP: "+p.getHp());
                    Battle(p, en);
                }
                grins.changeY(-1);
                gridDisplay.setText(grins.toString());
                steps++;
            }
        });

        west.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (randomBattle == steps)
                {
                    j.setVisible(false);
                    System.out.println("HP: "+p.getHp());
                    Battle(p, en);
                }
                grins.changeY(1);
                gridDisplay.setText(grins.toString());
                steps++;
            }
        });
    }

    public void Battle(Player p, Enemy e)
    {
        JButton attack = new JButton("Attack");
        JLabel playerHp = new JLabel();
        JLabel enemyHp = new JLabel();
        JLabel battleStatus = new JLabel();
        boolean battle = true;

        setFocusable(true);
        setLayout(null);

        playerHp.setBounds(400,200,300,20);
        enemyHp.setBounds(400,20,300,20);
        attack.setBounds(350,300,100,20);

        playerHp.setText("HP :"+p.getHp());
        enemyHp.setText("HP :"+e.getHp());
        add(playerHp);
        add(enemyHp);
        add(attack);
        add(battleStatus);
        attack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                p.attack((Fighter)en);
                Burn = false;
            }
        });
        while(battle == true){
            if(Burn == false)
            {
                en.attack((Fighter)p);
                Burn = true;
            }
            if(p.getHp() <= 0)
            {
                battle = false;
            }
        }
    }

    public static void main(String args[])
    {
        Runner r = new Runner();
        r.setVisible(true);
    }
}
Dean J
  • 39,360
  • 16
  • 67
  • 93
Anonymous827364
  • 247
  • 1
  • 2
  • 7
  • 3
    Please improve your question: Narrow down the problem and tell us exactly where the endless loop occurs (use a debugger!). Format and indent the code properly, and follow Java naming conventions (method names never start with an uppercase letter). Additional hint: don't do long operations in the event dispatch thread. – RealSkeptic Jul 19 '15 at 18:20
  • 1
    You need to improve your question to have people able to help you. The problem isn't "help me build a turn based battle system", but "help me find this infinite loop"; ask more specific questions and you'll get better answers, and you'll get them more quickly. (I'm changing the title of this question now.) – Dean J Jul 19 '15 at 22:52

1 Answers1

0

I would recommend focusing on the loop in your battle function.

    while(battle == true){
        if(Burn == false)
        {
            en.attack((Fighter)p);
            Burn = true;
        }
        if(p.getHp() <= 0)
        {
            battle = false;
        }
    }

If this is the infinite loop, it appears as if the terminating condition of p.getHP() is less than or equal to 0 is never being met.

Kris Lange
  • 146
  • 5