• 0

[JAVA] Random Numbers


Question

I am making a checkerboard and I have everything working but the assigning of the checkers. I want to limit the red checkers to 5 on the board and the white to 8 on the board. I know how to use the Random class but how can I limit it to only assign 5 red and 8 white?

My code:

Driver:

import javax.swing.*;
import java.awt.*;

public class CheckerDriver
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame ("Checker Board");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

		frame.getContentPane().add (new CheckerBoardPanel());
		frame.pack();
		frame.setVisible (true);		
	}
}

Panel:

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class CheckerBoardPanel extends JPanel
{
	private final int SQUARE = 50; // the heigth and width of checker
	private int[][] table = new int[8][8]; // the checkerboard
	private int row, col; // the rows and columns of board

	//--------------------------------------------------
	// Sets up the board and assigns the checker locations 
	//--------------------------------------------------
	public CheckerBoardPanel()
	{	
		Random generator = new Random();	

		//-------------------------------
		// assigns the checker locations
		// 0 = blank, 1 = red, 2 = white
		//-------------------------------
		for (row=0; row < table.length; row++)
		{
			for (col=0; col < table[row].length; col++)
			{
				if (row % 2 == 0) // check for even row
				{
					if (col % 2 == 0) //checks for even column
					{
						table[row][col] = generator.nextInt(3);						
					}
					else
					{
						table[row][col] = 0;
					}
				}
				else
				{
					if (col % 2 == 0)
					{
						table[row][col] = 0;
					}
					else
					{
						table[row][col] = generator.nextInt(3);						
					}
				}
			}
		}				

		//----------------------------------------
		// prints checker locations for debugging
		//----------------------------------------
		for (int row = 0; row < table.length; row++)
		{
			for (int col = 0; col < table[row].length; col++)
				System.out.print (table[row][col] + "\t");
			System.out.println();
		}

		setPreferredSize(new Dimension (400, 600));
		setBackground(Color.black);
	}

	//------------------------
	// draws the checkerboard
	//------------------------
	public void paintComponent (Graphics page)
	{
		super.paintComponent(page);

		int x = 0; // x-coord for first the squares then the checkers
		int y = 0; // y-coord for first the squares then the checkers
		int rowCount = 0; // counts the rows on board

		page.setColor(Color.white); // square color

		for(int countY = 0; countY <= 400; countY++)
		{
			if (rowCount % 2 == 0) // if row is even do this
			{
				x = 50;				
				for	(int countX = 0; countX <= 400; countX++) // draws the rows
				{					
					page.fillRect (x, y, SQUARE, SQUARE);
					x = x + 100;
				}
			}
			else // if row is odd do this
			{
				for	(int countX = 0; countX <= 400; countX++) // draws the rows
				{
					page.fillRect (x, y, SQUARE, SQUARE);
					x = x + 100;
				}
			}
			x = 0;
			y = y + 50;
			rowCount = rowCount + 1;
		}

		page.setColor(Color.yellow); // display area color
		page.fillRect(0, 400, 400, 200); // display area

		x = 0; // x-coord for checker
		y = 0; // y-coord for checker

		//---------------------------------
		// draws the checkers on the board
		//---------------------------------
		for (row=0; row < table.length; row++)
		{
			if (row % 2 == 0) //checks even row
			{
				x = 0;
			}
			else
			{
				x = 50;
			}
			for (col=0; col < table[row].length; col++)
			{
				if (row % 2 == 0) //checks even row
				{
					if (col % 2 == 0) //checks even column
					{						
						if (table[row][col] == 1)
						{
							page.setColor(Color.red);
							page.fillOval(x, y, 50, 50);
						}
						else if (table[row][col] == 2)
						{
							page.setColor(Color.white);
							page.fillOval(x, y, 50, 50);
						}
						x = x + 100;
					}
				}
				else
				{
					if (col % 2 != 0) //checks odd column
					{
						if (table[row][col] == 1)
						{
							page.setColor(Color.red);
							page.fillOval(x, y, 50, 50);
						}
						else if (table[row][col] == 2)
						{
							page.setColor(Color.white);
							page.fillOval(x, y, 50, 50);
						}
						x = x + 100;
					}										
				}
			}
			y = y + 50;
		}													
	}
}

This does everything but limiting the checker numbers.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

A common solution is to use a 'bag' which only has 5 red and 8 white in it, and is passed a your random variable which it scales to the number of chequers left in the bag to pick one, returns it and removes it from the bag.

Link to comment
Share on other sites

  • 0

Ok I got it. I used a arraylist like this:

public CheckerBoardPanel()
	{	
		int index = 0;
		int indexCounter = 32;

		Random generator = new Random();

		ArrayList <Integer> list = new ArrayList <Integer>();

		for (int red = 0; red < 5; red++)
		{
			list.add(1);
		}

		for (int white = 0; white < 8; white++)
		{
			list.add(2);
		}

		for (int blank = 0; blank < 19; blank++)
		{
			list.add(0);
		}			

		//System.out.println(list);

		//-------------------------------
		// assigns the checker locations
		// 0 = blank, 1 = red, 2 = white
		//-------------------------------
		for (row=0; row < table.length; row++)
		{
			for (col=0; col < table[row].length; col++)
			{
				if (row % 2 == 0) // check for even row
				{
					if (col % 2 == 0) //checks for even column
					{
						index = generator.nextInt(indexCounter);
						table[row][col] = list.remove(index);
						indexCounter = indexCounter - 1;												
					}
					else
					{
						table[row][col] = 0;
					}
				}
				else
				{
					if (col % 2 == 0)
					{
						table[row][col] = 0;
					}
					else
					{
						index = generator.nextInt(indexCounter);
						table[row][col] = list.remove(index);
						indexCounter = indexCounter - 1;						
					}
				}
			}
		}				

		//----------------------------------------
		// prints checker locations for debugging
		//----------------------------------------
		for (int row = 0; row < table.length; row++)
		{
			for (int col = 0; col < table[row].length; col++)
				System.out.print (table[row][col] + "\t");
			System.out.println();
		}

		setPreferredSize(new Dimension (400, 600));
		setBackground(Color.black);
	}

thanks for the help.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.