Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Puzzle Game. Need some help

  1. #1
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Puzzle Game. Need some help

    hi

    So i'm doing this project for school but i'm quite literally at a standstill. Let me just briefly tell you what the puzzle is supposed to do, because honestly its really weird.

    Its based off of that old game "Drive Ya' Nuts!" (which is a very appropriate name). Basically you have 7 hexagons, with each side being numbered 1 through 6 in any order. You have to assemble it so that each side is touching the side of another hex, with the condition being that the sides of the two hexagons must be of an identical number. Confused? I understand. Here is an image:

    http://www.samstoybox.com/toypics/DriveYaNuts2.jpg

    Notice the layout, and how the numbers of all the adjacent hexes are matching.

    The algorithm is my problem; I cannot for love of all that is sacred, wrap my head around how im going to implement it. The way the program is implemented is that im using a circular linked list to represent each hex (a requirement); this way, i can rotate it very easily. Here is what is working in my program:

    -The ability to make hexes
    -print the hexes
    -rotate the hexes

    I know i have to use some sort of recursive method, because i need to be able to backtrack if my sequence of hexes are incorrect and start over. Also each hex can only be used once, so i need a way to somehow know which hexes have already been used. I tried adding all the hexes into an arraylist before passing them through a method; i dont know if thats helpful or not...

    package project3;
     
    import java.util.ArrayList;
    import java.util.Iterator;
     
    //circular linked list
    class Hex
    {
    	Node head;
     
     
    	Hex()
    	{
    		head = null;		
    	}
     
    	public void insert(int n)	
    	{
    		Node temp = new Node(n);
    		Node current = head;
     
    		//if the list is empty, then the new node equal to head
    		if(head == null)
    		{
    			head = temp;
    			head.next = head;
    			System.out.println("hit");
    		}
    		else
    		{	
    			//Starting from head, move through the list until you get to head again
    			while(current.getNext() != head) 
    			{
    				current = current.getNext();
    			}
    			//set the last node's pointer to the new node
    			current.setNext(temp);
    			//set the pointer of the last node back to the first node
    			temp.next = head; 
    		}
    	}
     
    	public Hex rotate() 
    	{
            Hex rotated = new Hex();        
    		Node current = head;
     
            do 
            {
            	current = current.getNext();
            	rotated.insert(current.getElement());
     
            } while(current != head);
     
            return rotated;
        }
     
    //Compare
    	public boolean compareCenter(Hex h1, Hex h2)
    	{
    		Node current1 = h1.head;
    		Node current2 = h2.head;
    		Boolean match = false;
     
    		while(match == false)
    		{
    			if(current1.getElement() == current2.getElement())
    			{
    				match = true;				
    			}
    			else
    			{
    				h2 = h2.rotate();
    			}
    		}
     
    		return true;		
     
    	}
     
    	/*public boolean compareSides(Hex h1, Hex h2)
    	{
     
    	}*/
     
    //Board
    	public void Board(ArrayList<Hex> hexList, int[] use, int[] order)
    	{
     
    	}
     
    //Assemble
    	public static void Assemble(ArrayList<Hex> hexList) //int[] use, int[]pos)
    	{
    		Iterator<Hex> itr = hexList.iterator();
     
    		while(itr.hasNext())
    		{
    			System.out.println(itr.next());
    		}
    	}
     
     
     
     
    //node class
    	class Node
    	{
    		int element;
    		Node next;
     
    		Node()
    		{
     
    		}
     
    		Node(int element)
    		{
    			this.element = element;
    			next = null;
    		}
     
    		Node(int element, Node next)
    		{
    			this.element = element;
    			this.next = next;
    		}
     
    		public int getElement() 
    		{
    			return element;
    		}
     
    		public void setElement(int element)
    		{
    			this.element = element;
    		}
     
    		public Node getNext()
    		{
    			return next;
    		}
     
    		public void setNext(Node next) 
    		{
    			this.next = next;
    		}		
    	}
     
    	public String toString()
    	{
    		Node current = head;
    		String output = "";
    		for(int i = 0; i < 6; i++) 
    		{
    			output += "[" + current.getElement() + "]";
    			current = current.getNext();
    		}
    		return output;	
    	}
     
    }
     
    //MAIN
     
    public class Project3 {
    	public static void main(String[] args) 
    	{
    		Hex h1 = new Hex();	
    		h1.insert(1); h1.insert(6); h1.insert(4); h1.insert(2); h1.insert(5); h1.insert(3);	
     
    		Hex h2 = new Hex();		
    		h2.insert(1); h2.insert(6); h2.insert(5); h2.insert(4); h2.insert(3); h2.insert(2);
     
    		Hex h3 = new Hex();		
    		h3.insert(1); h3.insert(4); h3.insert(6); h3.insert(2);	h3.insert(3); h3.insert(5);
     
    		Hex h4 = new Hex();		
    		h4.insert(1); h4.insert(6);	h4.insert(5); h4.insert(3);	h4.insert(2); h4.insert(4);
     
    		Hex h5 = new Hex();		
    		h5.insert(1); h5.insert(4);	h5.insert(3); h5.insert(6);	h5.insert(5); h5.insert(2);
     
    		Hex h6 = new Hex();		
    		h6.insert(1); h6.insert(2);	h6.insert(3); h6.insert(4);	h6.insert(6); h6.insert(6);
     
    		Hex h7 = new Hex();		
    		h7.insert(1); h7.insert(6);	h7.insert(2); h7.insert(4);	h7.insert(5); h7.insert(3);
     
    		ArrayList<Hex> hexList = new ArrayList<Hex>();		
    		hexList.add(h1);
    		hexList.add(h2);
    		hexList.add(h3);
    		hexList.add(h4);
    		hexList.add(h5);
    		hexList.add(h6);
    		hexList.add(h7);
     
    		//System.out.println(hexList);	
     
                   Hex.Assemble(hexList);					
     
    		int[] useArray = new int[6];
    		int[] posArray = new int[6];
     
    		for(int i = 0; i < 6; i++)
    		{
    			useArray[i] = 0;
    			posArray[i] = 0;
    		}
     
    		/*for(int i = 0; i < 6; i++)
    		{
    			System.out.print(useArray[i]);
    			System.out.print(posArray[i]);
    		}*/
     
     
    		//System.out.println(h1);
     
    	/*	h1 = h1.rotate();
     
    		System.out.println(h1);
     
    		h1 = h1.rotate();
     
    		System.out.println(h1);*/
     
    	}
     
    }

    Honestly, i would never ask such an broad and seemingly general question, but frankly im desperate at this point. I've spent this whole day just trying to make methods that will perform the operation but none of them have taken me far. I'm not asking for someone to program this for me, but i'm sure there are people out there with more experience who can at least guide me or even through out some ideas for me to try. (my face looks exactly like my icon right now)

    Anyone? Thanks in advance
    Last edited by clydefrog; March 12th, 2012 at 01:19 PM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Puzzle Game. Need some help

    @clydefrog: Please provide the SSCCE
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Puzzle Game. Need some help

    that is an SSCCE i believe.

    What do want me to change?

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Puzzle Game. Need some help

    I want you to provide SSCCE. Well let me make it simple for you.
    1. When you run your program, what does it output?
    2. If there are errors/exceptions, paste here the full trace.
    3. Why is the difference of the resulting output and your expected output?
    4. Where exactly you think you are stuck?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  5. #5
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Puzzle Game. Need some help

    Oh.

    1. It will output all the sides of the hexes; there are 7 hexes. it prints them in a row. example: [1][6][4][2][5][3] this is one hex

    2. No errors

    3 and 4. well, i'm stuck on how i should implement a recursive formula to solve the puzzle. The explaination of the puzzle is given above.

    ALSO: i edit my code up top, and added an extra line. it should output correctly now

    thank you

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Puzzle Game. Need some help

    Well, i have checked the code and it's output. It is showing something like this.
    hit
    hit
    hit
    hit
    hit
    hit
    hit
    [1][6][4][2][5][3]
    [1][6][5][4][3][2]
    [1][4][6][2][3][5]
    [1][6][5][3][2][4]
    [1][4][3][6][5][2]
    [1][2][3][4][6][6]
    [1][6][2][4][5][3]
    Which one is the central hexagon, which one is the first one and so on?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Puzzle Game. Need some help

    1. Insert in all the 7 linked lists from 1 to 6. (Ascending Order) Don't hardcode the numbers. This is the actual puzzle, not the hardcoded one.
    2. I will recommend you to use Maps instead of linked list.
    3. Give the pairs like (T,1),(B,2),(TL,3),(TR,4),(BL,5),(BR,6) where T=top, B=Bottom, R=Right, L=Left
    4. Now, place all the six round hexagons and start rotating them one by one, until they get mapped.
    5. Now, place the central hexagon and get all the directions which are facing inwards and map them to the central hexagon.

    I know this is the difficult task to do but it requires you to think more than i've just said.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  8. The Following User Says Thank You to Mr.777 For This Useful Post:

    clydefrog (March 12th, 2012)

  9. #8
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Puzzle Game. Need some help

    interesting suggestion, thank you.

    The way my professor wants it, to be able to enter your OWN hexagons; thats not a problem. I'm just hard coding it for now, to see if i can get it to work. But this opens up the problem that the order you place them in initially, might not work and so it will require me to back track and alter the order. There is a possibility that i will get, no solution.

    I remember my professor tossing around a few hints that recursion is the way to go about it. unfortunately linked lists are also a requirement

    Also, is there a way i can get methods to access variables outside of their scope, without having to pass in 7 linked lists every single time?

  10. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Puzzle Game. Need some help

    Also, is there a way i can get methods to access variables outside of their scope, without having to pass in 7 linked lists every single time?
    What do you mean to say?
    I remember my professor tossing around a few hints that recursion is the way to go about it. unfortunately linked lists are also a requirement
    Remember, recursion is not the only solution.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  11. #10
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Puzzle Game. Need some help

    Quote Originally Posted by Mr.777 View Post
    What do you mean to say?
    like for example i call a method A and pass in my 7 linkedlists. Then within that method, i call another method B. Is it possible to access variables defined in method A from method B without having to pass in the variables?

    Very simple example:

    public void MethodA()
    {
         int[] anArray = new int[5];
         int index = 5;
         boolean bool = MethodB(index);
    }
    public boolean MethodB( int index)
    {
         if(anArray[i] == index)
         {
              //do something, return boolean
         }
    }

    In the above example, is there a way i can access anArray from methodB without passing it through? Something like that...

  12. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Puzzle Game. Need some help

    In the provided code, NO. But;
    int[] anArray = new Array[2];
    void methodA(){
    anArray[0] = 5;
    }
    void methodB(){
    anArray[1]=6;
    }
    void show(){
    System.out.println(anArray[0]);
    System.out.println(anArray[1]);
    }
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. [SOLVED] Method help, one thing then another - puzzle
    By Scotty in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 01:52 PM
  2. Sliding puzzle Restart button(same exact game)
    By carterb32 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2011, 04:29 AM
  3. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM
  4. JavaScript pic puzzle
    By fr334a11 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2011, 08:29 AM
  5. [SOLVED] Problem in implementation of Fifteen Puzzle with 2D Arrays
    By bruint in forum Collections and Generics
    Replies: 8
    Last Post: May 3rd, 2009, 10:37 PM