# Thread: Finding hardest hole in mini-golf game

1. Member
Join Date
Nov 2012
Posts
29
Rep Power
0

## 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. Member
Join Date
Nov 2012
Posts
29
Rep Power
0

Bump

3. ## Re: Finding hardest hole in mini-golf game

What is wrong?

kind regards,

Jos

4. Member
Join Date
Nov 2012
Posts
29
Rep Power
0

## Re: Finding hardest hole in mini-golf game

Originally Posted by JosAH
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. Senior Member
Join Date
Oct 2010
Posts
317
Rep Power
4

## 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.

#### Posting Permissions

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