We have prewritten code that we need to make into a game board in a Java GUI project.
I have created game boards before but I am VERY new to programming and don't want to revert to the way I did it last time.
Our teacher gave us some code for a few classes. The point of the project is to have a 6x6 game board of buttons. There is a button that reveals a fish if it's a fish and one that reveals an "x" if there is not a fish.
She used boolean [][] grid in the GoneFishingModel class but the GoneFishingView is the GUI class and I am not sure how to take a boolean grid from one class and make it into a game board in the GUI GoneFishingView class.
The code so far for each class is below.
**************GoneFishingModel*****************
import java.util.Random;
public class GoneFishingModel
{
public static int DIMENSION = 6;
private boolean[][] grid = new boolean[DIMENSION][DIMENSION];
private int triesRemaining = 30;
private int fishRemaining = 10;
public GoneFishingModel()
{
Random randomNumberGenerator = new Random();
for (int fishCounter = 0; fishCounter < fishRemaining; fishCounter++)
{
int x, y;
do
{
x = randomNumberGenerator.nextInt(DIMENSION);
y = randomNumberGenerator.nextInt(DIMENSION);
} while (grid[x][y]);
grid[x][y] = true;
}
}
public boolean fishAt(int row, int column)
{
boolean foundFish = grid[row][column];
triesRemaining--;
if (foundFish)
{
fishRemaining--;
}
return foundFish;
}
public int getTriesRemaining()
{
return triesRemaining;
}
public int getFishRemaining()
{
return fishRemaining;
}
public boolean fishWin()
{
return triesRemaining == 0 && fishRemaining > 0;
}
public boolean playerWins()
{
return fishRemaining == 0;
}
}
***********GoneFishingView********************
import javax.swing.JFrame;
public class GoneFishingView extends JFrame
{
public GoneFishingView(GoneFishingModel model)
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new GoneFishingView(new GoneFishingModel());
}
public void updateUI()
{
}
}
********FishingButton********
import javax.swing.JButton;
public class FishingButton extends JButton
{
public FishingButton(int row, int column)
{
}
}
********FishingButtonListener***********
public class FishingButtonListener
{
public FishingButtonListener(GoneFishingModel goneFishingModel, GoneFishingView goneFishingView,
FishingButton fishingButton)
{
}
}
What I have tried:
I tried declaring an array of Buttons IN the GoneFishingView class (as this is how I did my last GUI Treasure Hunt project) but I don't think that is what she wants us to do AND it is not a boolean array grid. I also added the classes EmptyButton (which holds the "x") and EmptyButtonListener. Do I need these two classes on top of the FishingButton Class?
Example of the code here:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class GoneFishingView extends JFrame
{
private JPanel panel;
private GoneFishingModel model;
private int width = 6;
private int height = 6;
private FishingButton [] buttons = new FishingButton[width*height];
private FishingButton fishingButton;
public GoneFishingView(GoneFishingModel model)
{
this.model = model;
setTitle("Gone Fishing");
setSize(800, 500);
setLayout(new BorderLayout());
JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(6, 6));
gridPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
add(gridPanel, BorderLayout.CENTER);
Random random = new Random();
int counter = 0;
while(counter < model.getFishRemaining())
{
int insertionIndex = random.nextInt(buttons.length);
if(buttons[insertionIndex] == null)
{
buttons[insertionIndex] = new FishingButton(model, this);
counter++;
}
}
for (int index = 0; index < buttons.length; index++)
{
if (buttons[index] == null)
{
buttons[index] = new EmptyButton(model, this);
}
}
for (int index = 0; index < buttons.length; index++)
{
gridPanel.add(buttons[index]);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
JPanel contents = new JPanel();
add(contents, BorderLayout.EAST);
contents.add(new JLabel("Tries"));
JSlider triesSlider = new JSlider (JSlider.VERTICAL, 0, 30,0);
triesSlider.setMajorTickSpacing(10);
triesSlider.setMinorTickSpacing(1);
triesSlider.setPaintTicks(true);
triesSlider.setPaintLabels(true);
contents.add(triesSlider);
triesSlider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e) {
triesSlider.setValue(model.getTriesRemaining());
}
});
contents.add(new JLabel("Fish"));
JSlider fishSlider = new JSlider (JSlider.VERTICAL, 0, 10,0);
fishSlider.setMajorTickSpacing(5);
fishSlider.setMinorTickSpacing(1);
fishSlider.setPaintTicks(true);
fishSlider.setPaintLabels(true);
contents.add(fishSlider);
fishSlider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
fishSlider.setValue(model.getFishRemaining());
}
});
}