3 Replies - 412 Views - Last Post: 03 February 2013 - 04:39 AM Rate Topic: -----

#1 MyDream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-February 13

java random number in 7*7 array without repeat

Posted 03 February 2013 - 01:51 AM

Hello! I want to generate a java random number in 7x7 array without repeat?
Please help!!!
Below are my codes, can't work, please help!!

import java.applet.*;
import java.awt.*;

public class randomGen extends Applet implements Runnable {
	protected int model[][];
	protected Button view[][];

	protected void createModel() {
		model = new int[7][7];

		
		for (int row = 0; row < 7; row++)
			for (int col = 0; col < 7; col++)
				model[row][col] = 0;

		
		model[0][0] = 1;

		model[1][0] = 3;

		model[2][1] = 2;
		
		model[3][4] = 6;

		model[4][6] = 7;
		
		model[5][1] = 5;
		
		model[6][6] = 4;
		
	}

	protected void createView() {
		setLayout(new GridLayout(7, 7));

		view = new Button[7][7];

	
		for (int row = 0; row < 7; row++)
			for (int col = 0; col < 7; col++) {
				view[row][col] = new Button();
				add(view[row][col]);
			}
	}

	protected void updateView() {
		for (int row = 0; row < 7; row++)
			for (int col = 0; col < 7; col++)
				if (model[row][col] != 0)
					view[row][col].setLabel(String.valueOf(model[row][col]));
				else
					view[row][col].setLabel("");
	}

	public void init() {
		createModel();
		createView();
		updateView();
	}

	protected boolean checkRow(int row, int num) {
		for (int col = 0; col < 7; col++)
			if (model[row][col] == num)
				return false;

		return true;
	}

	protected boolean checkCol(int col, int num) {
		for (int row = 0; row < 7; row++)
			if (model[row][col] == num)
				return false;

		return true;
	}

	protected boolean checkBox(int row, int col, int num) {
		row = (row / 3) * 3;
		col = (col / 3) * 3;

		for (int r = 0; r < 3; r++)
			for (int c = 0; c < 3; c++)
				if (model[row + r][col + c] == num)
					return false;

		return true;
	}

	public void start() {
		(new Thread(this)).start();
	}

	public void run() {
		try {

			Thread.sleep(1000);

			solve(0, 0);
		} catch (Exception e) {
		}
	}

	public void solve(int row, int col) throws Exception {
		
		if (row > 6)
			throw new Exception("Solution found");

		if (model[row][col] != 0)
			next(row, col);
		else {
			for (int num = 1; num < 8; num++) {
				if (checkRow(row, num) && checkCol(col, num)
						&& checkBox(row, col, num)) {
					model[row][col] = num;
					updateView();

					Thread.sleep(1000);

					next(row, col);
				}
			}

			model[row][col] = 0;
			updateView();
		}
	}

	public void next(int row, int col) throws Exception {
		if (col < 6)
			solve(row, col + 1);
		else
			solve(row + 1, 0);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: java random number in 7*7 array without repeat

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: java random number in 7*7 array without repeat

Posted 03 February 2013 - 02:00 AM

What are the parameters of the 49 numbers, 1 - 49? Something else?
Was This Post Helpful? 0
  • +
  • -

#3 MyDream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-February 13

Re: java random number in 7*7 array without repeat

Posted 03 February 2013 - 02:16 AM

The parameters for the 7x7 array are only to use 1-7 to generate, like the below:

1234567
3745216
2457631
5162743
7316452
4673125
6521374
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: java random number in 7*7 array without repeat

Posted 03 February 2013 - 04:39 AM

So that's not random. It looks like you're trying to generate a 7x7 Sudoku-like array where each of the rows and columns contain the values 1 through 7 with no repeats. Right? I suspect that was the assignment, so someone can't just give you the answer. I'll look at your code to see if there are any obvious mistakes or possible hints to help you.

Results: No comments in your code, no clue how to run it or what it's supposed to be doing while it is running, so too hard. If you want help, you need to be clearer about what you're trying to do and where you need help.

This post has been edited by GregBrannon: 03 February 2013 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1