Results 1 to 5 of 5
  1. #1
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Finding hardest hole in mini-golf game

    So What I need to have is to find the hardest hole in a minigolf game, and that is the highest number. Now the problem is, the program starts counting for the hardest hole from the end (hole 18), but I need to get it to do it from (hole 1). Scoreboard contains the method findHardestHole. Other files are also included. 2nd file is panel. 3rd is driver.

    Java Code:
    	//Name______________________________ Date_____________
       import javax.swing.*;
       import java.awt.*;
       public class ScoreCard09 extends JPanel
       {
          private JTextField[] input;
          public ScoreCard09()
          {
             setLayout(new GridLayout(2, 18));
          
             for(int x = 1; x <= 18; x++)
             {
                add(new JLabel("" + x, SwingConstants.CENTER));
             }
          
             input = new JTextField[18];
             for(int x = 0; x < input.length; x++)
             {
                input[x] = new JTextField();
                add(input[x]);
             }
          }
          public void randomize()
          {
             for (int i = 0; i < input.length; i++)
                input[i].setText("" + (int)(Math.random() * 4 + 1));
          }
          public int findTotal()
          {
             int total = 0;
             for (int i = 0; i < input.length; i++)
                total += Integer.parseInt(input[i].getText());
             return total;   
          }
          public int findAces()
          {
             int count = 0;
             for (int i = 0; i < input.length; i++)
                if (Integer.parseInt(input[i].getText()) == 1)
                   count++;
             return count;
          }
          public int findHardestHole()
          {
             int hardest = 1;
             int strokes = Integer.parseInt(input[0].getText());
             for (int i = 0; i < input.length; i++)
                if (Integer.parseInt(input[i].getText()) > strokes)
                   hardest = i+1;
             return hardest;
          
          }
       }
    PANEL:

    Java Code:
       //Torbert, e-mail: mr@torbert.com, website: www.mr.torbert.com
    	//version 7.14.2003
    
       import javax.swing.*;
       import java.awt.*;
       import java.awt.event.*;
       public class Panel09 extends JPanel
       {
          ScoreCard09 scorecard;
          JLabel output;
          public Panel09()
          {
             setLayout(new BorderLayout());
          
             output = new JLabel("------", SwingConstants.CENTER);
             add(output, BorderLayout.NORTH);
          
             scorecard = new ScoreCard09();
             add(scorecard, BorderLayout.CENTER);
          
             JPanel panel = new JPanel();
             panel.setLayout(new FlowLayout());
             addButton(panel, "Randomize", new Listener1());
             addButton(panel, "Total Score", new Listener2());
             addButton(panel, "Holes in One", new Listener3());
             addButton(panel, "Hardest Hole", new Listener4());
             add(panel, BorderLayout.SOUTH);
          }
          private void addButton(JPanel p, String s, ActionListener a)
          {
             JButton b = new JButton(s);
             b.addActionListener(a);
             p.add(b);
          }
          private class Listener1 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                scorecard.randomize();
                output.setText("------");
             }
          }
          private class Listener2 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                output.setText("Total Score: " + scorecard.findTotal());
             }
          }
          private class Listener3 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                output.setText("Holes in One: " + scorecard.findAces());
             }
          }
          private class Listener4 implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                output.setText("Hardest Hole: " + scorecard.findHardestHole());
             }
          }
       }
    DRIVER:

    Java Code:
       import javax.swing.JFrame;
       public class Driver09
       {
          public static void main(String[] args)
          {
             JFrame frame = new JFrame("Unit4, Lab09: Miniature Golf");
             frame.setSize(500, 150);
             frame.setLocation(150, 100);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setContentPane(new Panel09());
             frame.setVisible(true);
          }
       }

  2. #2
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Finding hardest hole in mini-golf game

    Bump

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Finding hardest hole in mini-golf game

    What is wrong?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Finding hardest hole in mini-golf game

    Quote Originally Posted by JosAH View Post
    What is wrong?

    kind regards,

    Jos
    The problem is, the program starts counting from the last hole (hole 18), but I need it to count from the first hole to look for the hardest hole.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Finding hardest hole in mini-golf game

    Hi Wizard0860,

    There is a problem with your implementation of the findHardestHole() method.
    At the moment 'strokes' is set to the value of the first element in 'input'. Within the loop the value of each element is compared to 'strokes' but 'strokes' never changes so you will always be comparing the value of the current element with the first.

    Regards.

Similar Threads

  1. Trouble with a mini game ( using KeyEvent)
    By PuppetJacks in forum New To Java
    Replies: 3
    Last Post: 02-27-2011, 02:58 PM
  2. Java Mini-Game!
    By Ehkx in forum New To Java
    Replies: 3
    Last Post: 01-11-2011, 10:38 PM
  3. The 3 Word Story [Mini Game]
    By Eku in forum Forum Lobby
    Replies: 90
    Last Post: 11-16-2008, 09:01 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
  •