3 Replies - 250 Views - Last Post: 16 November 2011 - 05:47 AM Rate Topic: -----

#1 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Random generated puzzel and no number may be the same

Posted 16 November 2011 - 02:16 AM

Hi,

I have a friend that's in university and he asked me I could help him a bit, so I did that.

He has an assigment where you need to make a puzzel and make output like this:
|--------------------------------|
| 6 | 3 | 3 | 9 |
| 0 | 4 | 10 | 5 |
| 8 | 10 | 1 | 11 |
| 5 | 13 | 2 | 0 |
|--------------------------------|
But the problem is that some number's are the same. and that's not allowed. (2 time's 10, 2 time's 3)

And I don't now how i should fix that.

Here i generate the bord:
package puzzel.slidepuzzle;

import java.util.Arrays;
import java.util.Random;
/*
 * 
 * Calculation:
 * List all numbers on the board lexically, i.e., left to right and top to bottom.
 For each number n in this list, count the number of inversions: the numbers that precede n
in the list and that are larger than n.
 If the total number of inversions, added over all numbers in the list, is even then the puzzle
is solvable. If it's odd, the puzzle can't be solved.
 *  int i = Arraylist[huide] -1;
 *  
 *  Formule: 
 * 
 * 
 */
public class ConfigGenerator {
		private Random randomizer= new Random();
		final int rowWidth  = 4;
		final int rowHeight = 4;
		 private int result[][];
		
		
		public int[][] getRandomConfig(){

			int result[][] = new int[rowWidth][rowHeight];
			 for (int row = 0; row < result.length; row++) {
				              
				 
				             for (int col = 0; col < result[row].length; col++) {
				 
				            
				            	 result[row][col]  = randomizer.nextInt(15);
				            
				            	  
				            	 
				            	 
				            	 	
				             }
				             
				             	
				         }
			 	
			  for(int i = 0; i < result.length; i++) {
				  
				   
				  
				              for(int j = 0; j < result[i].length; j++) {
				  
				                       
				  
				                  System.out.print(" |  " + result[i][j] + "\t" + " ");
				                  
				                  //System.out.println();
				                  
				             }
				              System.out.print(" | ");
				              
				              	
				              System.out.println();
				              
				         
				   
				  			
				  			
				          }


   return result;
   
			
			
		}
		
		public boolean isSolvable(int[][] config) {
	
				
		
		
			
			
			
			
			return false;
			
		}
}



In the board class I call this class and run getRandomConfig()

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Random generated puzzel and no number may be the same

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Random generated puzzel and no number may be the same

Posted 16 November 2011 - 03:12 AM

you can maintain a hashmap or any structure with O(1) access time. Each time you generate a number, check if its in the hashmap, if it is, then generate another one, if its not then set the number to the current cell and save the number in the hashmap.
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random generated puzzel and no number may be the same

Posted 16 November 2011 - 05:00 AM

It is just generating Lotonumbers from 0 to 15

Fill an ArrayList with number 0 to 15
Randomly remove numbers from that ArrayList

http://www.dreaminco...snippet2992.htm

ArrayList<Integer> al = new ArrayList<Integer>();
for(int i = 0; i < 16; ++i)
  al.add(i);

Random ran = new Random();
int[][] result = new int[4][4];
for(int i = 0; i < 4; i++) {
    for(int j = 0; j < 4; j++) {
      int index = ran.nextInt(al.size());
      result[i][j] = al.remove(index);
    }
}


Was This Post Helpful? 2
  • +
  • -

#4 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Random generated puzzel and no number may be the same

Posted 16 November 2011 - 05:47 AM

View Postpbl, on 16 November 2011 - 01:00 PM, said:

It is just generating Lotonumbers from 0 to 15

Fill an ArrayList with number 0 to 15
Randomly remove numbers from that ArrayList


I would advise the first reply, using a hashmap, the above solution isn't exactly dynamic in scale. Although you could make it so, don't copy paste.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1