ku95 0 Light Poster

I have this program where I need to get the suggested next move (a column index) and also call the 'go' method using this column as the parameter. This is what I have come up with so far.

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) // 2.5
	{
		java.util.Random r = new java.util.Random();
		for (int x = 0; x <= NUM_ROWS; x++)
		{
			go(r.nextInt(NUM_COLUMNS));
		}
		return player_colour;
	}

	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(500,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;

		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);
		}

		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 Red");
			}
			else if(event.getSource()==autoYellowButton)
			{
				System.out.println("Selected Yellow Button");
				model.setAutoplay(3);
				System.out.println("Computer Playing Yellow ");
			}
			else if(event.getSource()==resetButton)
			{
				model.reset();
			}

			//System.out.println("event is " + event.getActionCommand());
			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)
			{
				try { Thread.sleep(1000);} catch (Exception e){};
			}
			if(model.autoplay == 2)
			{
				try { Thread.sleep(1000);} catch (Exception e){};
			}
			if(model.autoplay == 3)
			{
				try { Thread.sleep(1000);} catch (Exception e){};
			}

			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);
				}
			}
		}
	}

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