Hi, I have some code for a program that I'm doing. Basically I'm trying to write method whereby the reset() method takes two parameters which are number of columns and number of rows. I'm doing this method whereby I have to to move all the contents of the Connect4Model constructor into the reset method. Once that's done all the constructor has to do is call the reset() method. This method should make the model reset when you press the reset button. What my difficulty is, is that when I move the Connect4Model constructor into the Connect4Model void reset() method it gives me errors which I don't understand. I was wondering if there's a possible solution to this. I've supplied the Connect4Model, Connect4View and Connect4Column code. I have to write the reset() method in the Connect4Model.

Connect4Model

public class Connect4Model
{
	Connect4Column [] columns; // an array of columns
	int NUM_COLUMNS; // how many columns we have in the game
	int NUM_ROWS;    // how many rows we have in the game
	int playerToGoNext = Connect4Column.RED_COUNTER; //can be red or yellow
	int autoplay;

	public Connect4Model(int numCols, int numRows)
	{
		NUM_COLUMNS = numCols;
		NUM_ROWS = numRows;

		columns = new Connect4Column[NUM_COLUMNS];
		for (int i=0; i < NUM_COLUMNS; i++) {
			columns[i] = new Connect4Column(NUM_ROWS);
		}
	}

	int getNumCols() // simply return the number of columns
	{
		return NUM_COLUMNS;
	}

	int getNumRows() // simply return the number of rows
	{
		return NUM_ROWS;
	}

	int getNextPlayer() // returns who gets to go next
	{
		return playerToGoNext;
	}
	void reset()
	{
		for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol ++)
		{
			for (int thisRow = 0; thisRow < NUM_ROWS; thisRow ++)
			{
				columns[thisCol].numCounters = 0;
				columns[thisCol].counters[thisRow] = 0;
			}
		}
	}
	int getAutoplay()
	{
		return autoplay;
	}
	boolean setAutoplay(int selectedOption)
	{
		if(selectedOption >= 1 && selectedOption <= 3) // valid
		{
			autoplay = selectedOption;
			return true;

		}
		else
			return false;
	}

	int getSuggestedMove(int player_colour)
	{
		java.util.Random r = new java.util.Random();
		return r.nextInt(NUM_COLUMNS);
	}

	boolean go(int thisColumn) // try to put a counter at this col
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return false;

		//System.out.println("thisCol is " + thisColumn);
		if(columns[thisColumn].addCounter(playerToGoNext)==true)
		{
			if(playerToGoNext==Connect4Column.YELLOW_COUNTER)
				playerToGoNext=Connect4Column.RED_COUNTER;
			else
				playerToGoNext=Connect4Column.YELLOW_COUNTER;
			return true;
		}
		return false;
	}

	int getCounter(int thisColumn, int thisRow) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getCounter(thisRow);
	}
	int getNumCounters(int thisColumn) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getNumCounters();
	}


}

Connect4View

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class Connect4View extends JFrame
{
	DrawPanel canvas;
	Connect4Model model;
	ControlPanel myControlPanel, JRadioButton;


	public static void main(String[] args)
	{
		Connect4View w = new Connect4View();
		w.setVisible(true);
	}

	public Connect4View()
	{
		setTitle("Connect4 solution");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600,220);
		setLocation(300,300);

		int number_of_columns = 7;
		int number_of_rows = 9;

		model = new Connect4Model(number_of_columns,number_of_rows);
		canvas = new DrawPanel(number_of_columns,number_of_rows);
		myControlPanel = new ControlPanel();


		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);
		add(myControlPanel, BorderLayout.SOUTH);
 	}
	class ControlPanel extends JPanel implements ActionListener
	{
		JButton resetButton;
		JRadioButton twoPlayerButton;
		JRadioButton autoRedButton;
		JRadioButton autoYellowButton;
		JTextField numRows;
		JTextField numCols;

		public ControlPanel()
		{

			resetButton = new JButton("Reset Board");
			add(resetButton);
			resetButton.addActionListener(this);

			twoPlayerButton = new JRadioButton("Two Player Game");
			twoPlayerButton.addActionListener(this);
			add(twoPlayerButton);
			twoPlayerButton.setSelected(true);

			autoRedButton = new JRadioButton("Red Button");
			autoRedButton.addActionListener(this);
			add(autoRedButton);

			autoYellowButton = new JRadioButton("Yellow Button");
			autoYellowButton.addActionListener(this);
			add(autoYellowButton);

			ButtonGroup group = new ButtonGroup();
			group.add(twoPlayerButton);

			ButtonGroup group2 = new ButtonGroup();
			group.add(autoRedButton);

			ButtonGroup group3 = new ButtonGroup();
			group.add(autoYellowButton);

			numRows = new JTextField(4);
			add(numRows);
			numRows.addActionListener(this);
			numRows.setText("9");
			int newCol = Integer.parseInt(numRows.getText());


			numCols = new JTextField(4);
			add(numCols);
			numCols.addActionListener(this);
			numCols.setText("7");
			int newRow = Integer.parseInt(numCols.getText());
		}

		public void actionPerformed(ActionEvent event)
		{

			if(event.getSource()==twoPlayerButton)
			{
				System.out.println("Selected Two Player");
				model.setAutoplay(1);
				System.out.println("Two Player game");
			}
			else if(event.getSource()==autoRedButton)
			{
				System.out.println("Selected Red Button");
				model.setAutoplay(2);
				System.out.println("Computer Playing Yellow");

			}
			else if(event.getSource()==autoYellowButton)
			{
				System.out.println("Selected Yellow Button");
				model.setAutoplay(3);
				System.out.println("Computer Playing Red ");
				int thisCol;
				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			else if(event.getSource()==resetButton)
			{
				model.reset();
				System.out.println("Colum is " + numCols.getText());
				System.out.println("Rows is " + numRows.getText());
			}
			canvas.repaint();
		}
	}
	class DrawPanel extends JPanel implements MouseListener
	{
		public int numCols;
		public int numRows;

		public DrawPanel(int nc, int nr)
		{
			numCols = nc;
			numRows = nr;
			addMouseListener(this);
		}
		int getCol(int x)
		{
			return x*numCols/getWidth();
		}
		int getRow(int y)
		{
			return y*numRows/getHeight();
		}

		public void mouseReleased(MouseEvent event)
		{
		}
		public void mousePressed(MouseEvent event)
		{
		}
		public void mouseClicked(MouseEvent event)
		{
			int thisCol = getCol(event.getX());
			model.go(thisCol);

			if(model.autoplay == 1)
			{
				System.out.println("Two Player game");
			}
			else if(model.autoplay == 2)
			{

				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			else if(model.autoplay == 3)
			{
				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			//model.firstGo();
			repaint();

		}
		public void mouseEntered(MouseEvent event)
		{
		}
		public void mouseExited(MouseEvent event)
		{
		}
		Rectangle getRect(int thisCol, int thisRow)
		{
			// if input is out of range, return "null"
			if(thisCol <0 || thisRow < 0)
				return null;
			if(thisCol>=numCols || thisRow>=numRows)
				return null;

			// otherwise, make and return the Rectangle
			int w = getWidth()/numCols;
			int h = getHeight()/numRows;

			int x = thisCol*w;
			int y = thisRow*h;

			Rectangle myRect = new Rectangle(x,y,w,h);
			return myRect;
		}

		public void paint(Graphics g)
		{
			g.setColor(Color.gray);
			g.fillRect(0,0,getWidth(), getHeight());
			g.setColor(Color.black);


			Graphics2D g2 = (Graphics2D)g;
			// we'll use Graphics2D for it's "draw" method -
			// neater than the Graphics "drawRect" suppled
			// (which you could also use)

			for (int i = 0;i<numCols;i++)
				for(int j = 0;j<numRows;j++)
					g2.draw(getRect(i,j));

			for (int thisCol = 0; thisCol < model.getNumCols(); thisCol ++)
			{
				int num_of_counters = model.getNumCounters(thisCol);
				//System.out.println("col " + thisCol + " has " + num_of_counters);
				for (int counter=0; counter < num_of_counters; counter ++)
				{
					int colour = model.getCounter(thisCol, counter);
					if(colour==Connect4Column.RED_COUNTER)
						g2.setColor(Color.red);
					else if(colour==Connect4Column.YELLOW_COUNTER)
						g2.setColor(Color.yellow);

					Rectangle r = getRect(thisCol, numRows-counter-1);
					if (r != null)
						g2.fillOval(r.x, r.y, r.width, r.height);
				}
			}
		}
	}

 }

Connect4View

public class Connect4Column
{
	int [] counters; // array to store counters in this column
	int numCounters; // integer to say how many counters currently in this column
	int MAX_NUM_COUNTERS;  // height of the 'Connect 4' game

	static int YELLOW_COUNTER = 1;  // represents the 'yellow' counters
	static int RED_COUNTER = 2;  // represents the 'red' counters

	public Connect4Column(int maxCounters)
	{
		MAX_NUM_COUNTERS = maxCounters;
		counters = new int[MAX_NUM_COUNTERS];
		numCounters = 0;
	}

	public boolean addCounter(int thisCounter)
	{
		if (numCounters>=MAX_NUM_COUNTERS)
			return false;

		if (thisCounter == YELLOW_COUNTER
			 || thisCounter == RED_COUNTER)
		{
			counters[numCounters] = thisCounter;
			numCounters ++;
			return true;
		}
		else
			return false;
	}

	int getNumCounters()
	{
		return numCounters;
	}

	int getCounter(int thisRow)
	{
		if(thisRow<0 || thisRow >= MAX_NUM_COUNTERS)
			return 0;

		return counters[thisRow];

	}

}

ControlPanel, method actionPerformed, event "resetButton" (NEW GAME)
in place reset() method - create new instance of Connect4Model with current values from text-fields and force changes in DrawPanel class.

model = new Connect4Model(Integer.parseInt(numCols, Integer.parseInt(numRows.getText()));
                canvas.setModel(model);

write method setModel in class DrawPanel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.