Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
	{
		// TODO

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}

	public static void main(String[] args)
	{
		new GoneFishingView(new GoneFishingModel());
	}

	public void updateUI()
	{
		// TODO
	}
}


********FishingButton********
import javax.swing.JButton;

public class FishingButton extends JButton
{

	public FishingButton(int row, int column)
	{
		// TODO
	}

}


********FishingButtonListener***********
public class FishingButtonListener
{

	public FishingButtonListener(GoneFishingModel goneFishingModel, GoneFishingView goneFishingView,
			FishingButton fishingButton)
	{
		// TODO
        	}

}


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
{
	//Declare a new panel to hold contents
    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)
	{
		// TODO
		this.model = model;
		setTitle("Gone Fishing");
		setSize(800, 500);
		setLayout(new BorderLayout());
		
		//Create a new panel to hold grid
        JPanel gridPanel = new JPanel();
        
        
        //Set the panel to a grid layout, 6 by 6
        gridPanel.setLayout(new GridLayout(6, 6));
        //Set border of grid panel
        gridPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        //add panel and set to center
        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 the current index is null
            if (buttons[index] == null)
            {
                //add an EmptyButton() 
                buttons[index] = new EmptyButton(model, this);
            }
        }
        // Loop to add all of the contents of the buttonGrid into the gamePanel
        for (int index = 0; index < buttons.length; index++)
        {
            //add buttons to panel
            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"));
		//create a new slider with a 0-30 range and set initial to 0
        JSlider triesSlider = new JSlider (JSlider.VERTICAL, 0, 30,0);
        //set the major ticks to every 10
        triesSlider.setMajorTickSpacing(10);
        //set the minor ticks to every 1
        triesSlider.setMinorTickSpacing(1);
        //paint the ticks
        triesSlider.setPaintTicks(true);
        //make the label visible
        triesSlider.setPaintLabels(true);
        contents.add(triesSlider);
        triesSlider.addChangeListener(new ChangeListener() 
        {
            public void stateChanged(ChangeEvent e) { 
                //set value of slider to current speed although you have to click on slider to update which is dumb
                triesSlider.setValue(model.getTriesRemaining());
                // when manually changing the slider via mouse, set textfield text accordingly, but this won't work unless wecomment out above but go ahead and try it
                //speedTextField.setText(triesSlider.getValue() + "");
                
            }
        });
        contents.add(new JLabel("Fish"));
		//create a new slider with a 0-10 range and set initial to 0
        JSlider fishSlider = new JSlider (JSlider.VERTICAL, 0, 10,0);
        //set the major ticks to every 10
        fishSlider.setMajorTickSpacing(5);
        //set the minor ticks to every 5
        fishSlider.setMinorTickSpacing(1);
        //paint the ticks
        fishSlider.setPaintTicks(true);
        //make the label visible
        fishSlider.setPaintLabels(true);
        contents.add(fishSlider);
        fishSlider.addChangeListener(new ChangeListener() 
        {
            public void stateChanged(ChangeEvent e) 
            { 
                //set value of slider to current speed although you have to click on slider to update which is dumb
                fishSlider.setValue(model.getFishRemaining());
            	
                
            }
        });
	}
Posted
Updated 4-May-21 22:03pm
v3

1 solution

You just need to add the code that iterates through the array and adds the buttons to your game board. Something like:
Java
for (int i = 0; i < DIMENSION; ++i) {
    for (int j = 0; j < DIMENSION; ++j) {
            // add the button at gameboard location [i][j]
        }
    }
}

Then when a button is pressed you can use its location to find the boolean item and decide what result to display.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900