10 Replies - 11318 Views - Last Post: 03 April 2011 - 11:42 PM Rate Topic: -----

#1 jaeyuuji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 11

Fifteen - Gridworld

Posted 29 March 2011 - 12:43 PM

Description:

Quote

Here are the bugs that need to be fixed:

1. Diagonal moves are allowed (try it) but really shouldn’t be. A physical puzzle with sliding tiles wouldn’t allow this kind of move, so let’s fix the bug. [COMPLETED]

2. The letters are all placed on the board in the same starting positions each time. Instead, make each TextTile‘s initial Location random. Of course, be sure, Beware the 14-15 problem! Unsolvable puzzles are quite frustrating.

3. The puzzle doesn’t know when it’s complete. It should display “WINNING!” (by calling the method setMessage) once all the letters are in alphabetical order with the empty space in the bottom right corner. The message should also state how many moves the player took to complete the puzzle.

4. In addition to typing a letter, players want to be able to click on TextTile to move it to the empty Location. Implement this feature, but be careful to only allow legal moves!

For extra credit, create a single variable that allows you to change the size of the game (which is 4×4 right now) by changing its value and recompiling. Everything else about the game should work without changing any other code when you choose to run at 3×3 or 5×5.


import java.awt.Color;

public class TextTile
{
	private String letter;
	
	public TextTile(String letter)
	{
		this.letter = letter;
	}
	
	public String getText()
	{
		return letter;
	}
	
	public Color getColor()
	{
		return new Color(192, 64, 192);
	}
}


import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Location;
import info.gridworld.world.World;

import java.util.ArrayList;

public class TileGame
{
	public static void main(String[] args)
	{
		int grSize = 4;
		System.setProperty("info.gridworld.gui.selection", "hide");
		System.setProperty("info.gridworld.gui.tooltips", "hide");
		System.setProperty("info.gridworld.gui.frametitle", "TileGame!");
		
		TileWorld w = new TileWorld(new BoundedGrid<TextTile>(grSize, grSize));
		
		// there must be a better way...
		if (grSize == 2)
		{
			w.add(w.getRandomEmptyLocation(), new TextTile("A"));
			w.add(w.getRandomEmptyLocation(), new TextTile("B"));
			w.add(w.getRandomEmptyLocation(), new TextTile("C"));
		}
		else if (grSize == 3) //A thru H
		{
			w.add(w.getRandomEmptyLocation(), new TextTile("A"));
			w.add(w.getRandomEmptyLocation(), new TextTile("B"));
			w.add(w.getRandomEmptyLocation(), new TextTile("C"));
			w.add(w.getRandomEmptyLocation(), new TextTile("D"));
			w.add(w.getRandomEmptyLocation(), new TextTile("E"));
			w.add(w.getRandomEmptyLocation(), new TextTile("F"));
			w.add(w.getRandomEmptyLocation(), new TextTile("G"));
			w.add(w.getRandomEmptyLocation(), new TextTile("H"));
		}
		else if(grSize == 4) //A thru O
		{
			w.add(w.getRandomEmptyLocation(), new TextTile("A"));
			w.add(w.getRandomEmptyLocation(), new TextTile("B"));
			w.add(w.getRandomEmptyLocation(), new TextTile("C"));
			w.add(w.getRandomEmptyLocation(), new TextTile("D"));
			w.add(w.getRandomEmptyLocation(), new TextTile("E"));
			w.add(w.getRandomEmptyLocation(), new TextTile("F"));
			w.add(w.getRandomEmptyLocation(), new TextTile("G"));
			w.add(w.getRandomEmptyLocation(), new TextTile("H"));
			w.add(w.getRandomEmptyLocation(), new TextTile("I"));
			w.add(w.getRandomEmptyLocation(), new TextTile("J"));
			w.add(w.getRandomEmptyLocation(), new TextTile("K"));
			w.add(w.getRandomEmptyLocation(), new TextTile("L"));
			w.add(w.getRandomEmptyLocation(), new TextTile("M"));
			w.add(w.getRandomEmptyLocation(), new TextTile("N"));
			w.add(w.getRandomEmptyLocation(), new TextTile("O"));
		}
		else //A thru X
		{
			w.add(w.getRandomEmptyLocation(), new TextTile("A"));
			w.add(w.getRandomEmptyLocation(), new TextTile("B"));
			w.add(w.getRandomEmptyLocation(), new TextTile("C"));
			w.add(w.getRandomEmptyLocation(), new TextTile("D"));
			w.add(w.getRandomEmptyLocation(), new TextTile("E"));
			w.add(w.getRandomEmptyLocation(), new TextTile("F"));
			w.add(w.getRandomEmptyLocation(), new TextTile("G"));
			w.add(w.getRandomEmptyLocation(), new TextTile("H"));
			w.add(w.getRandomEmptyLocation(), new TextTile("I"));
			w.add(w.getRandomEmptyLocation(), new TextTile("J"));
			w.add(w.getRandomEmptyLocation(), new TextTile("K"));
			w.add(w.getRandomEmptyLocation(), new TextTile("L"));
			w.add(w.getRandomEmptyLocation(), new TextTile("M"));
			w.add(w.getRandomEmptyLocation(), new TextTile("N"));
			w.add(w.getRandomEmptyLocation(), new TextTile("O"));
			w.add(w.getRandomEmptyLocation(), new TextTile("P"));
			w.add(w.getRandomEmptyLocation(), new TextTile("Q"));
			w.add(w.getRandomEmptyLocation(), new TextTile("R"));
			w.add(w.getRandomEmptyLocation(), new TextTile("S"));
			w.add(w.getRandomEmptyLocation(), new TextTile("T"));
			w.add(w.getRandomEmptyLocation(), new TextTile("U"));
			w.add(w.getRandomEmptyLocation(), new TextTile("V"));
			w.add(w.getRandomEmptyLocation(), new TextTile("W"));
			w.add(w.getRandomEmptyLocation(), new TextTile("X"));
		}
		
		
		w.setMessage("It's TileGame Time!");
		
		w.show();
	}
}


import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.world.World;

import java.util.ArrayList;

public class TileWorld extends World<TextTile>
{
	private int count = 0;
	
	public TileWorld(Grid gr)
	{
		super(gr);
	}
	
	public boolean locationclicked(Location loc)
    {
    	Grid<TextTile> gr = getGrid();
    	
    	ArrayList<Location> locs = gr.getOccupiedLocations();
    	
    	for(Location x : locs) //for each letter in a location
    	{
    		//if( gr.get(x).getText() ) //get the letter and if it equals description
    		{
    			//creates array of all empty adjecent locations
    			ArrayList<Location> dest = gr.getEmptyAdjacentLocations(x);
    			
    			if (dest.size() > 0)
    	
    			{
    				if(dest.get(0).getDirectionToward(x) == 0 || dest.get(0).getDirectionToward(x) == 90 || dest.get(0).getDirectionToward(x) == 180 || dest.get(0).getDirectionToward(x) == 270)
    				{
    					gr.put(dest.get(0), gr.remove(x));
    				}
    			}
    		}
    	}
    	count++;
        setMessage("Number of steps:" + count);
        return true;
    }
    
	public boolean keyPressed(String description, Location loc)
    {
    	//description contains the letter that was typed
    	setMessage("Letter: " + description + "\nNumber of steps: " + count);
    	
    	//a text tile grid was created
    	Grid<TextTile> gr = getGrid();
    	
    	//array of occupied locations was created
    	ArrayList<Location> locs = gr.getOccupiedLocations();
    	
    	for(Location x : locs) //for each letter in a location
    	{
    		if( gr.get(x).getText().equals(description) ) //get the letter and if it equals description
    		{
    			//creates array of all empty adjecent locations
    			ArrayList<Location> dest = gr.getEmptyAdjacentLocations(x);
    			
    			if (dest.size() > 0)
    			{
    				if(dest.get(0).getDirectionToward(x) == 0 || dest.get(0).getDirectionToward(x) == 90 || dest.get(0).getDirectionToward(x) == 180 || dest.get(0).getDirectionToward(x) == 270)
    				{
    					gr.put(dest.get(0), gr.remove(x));
    				}
    			}
    		}
    	}
    	count++;
    	return true;
    }
}


All of this is being done in JCreator.

So I have completed part one, part of part two, part of part three, and part of part four.
My issues that I have not been able to solve are
  • In part two, I was able to randomize the Tiles but I cannot prevent the puzzle to randomize itself so that it becomes unsolvable.
  • In part three, I created a variable that stores the number of steps that have been take (count) but I have not been able to create anything to make sure that all of the Tiles are in alphabetical order. I was thinking of using a 2D Array but I am not very familiar on how to use a 2D Array. I also am not sure what class should I type up such the code.
  • In part four, I am able to click on my grid and the tiles move to an empty Location but it moves in an odd manner and I am unsure how to fix it.


I just need hints on what I should do, and have what I did wrong in part four pointed out.
Thank you for taking the time to read this and also if you help me.

This post has been edited by jaeyuuji: 29 March 2011 - 12:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Fifteen - Gridworld

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Fifteen - Gridworld

Posted 29 March 2011 - 03:29 PM

Quote

In part three, I created a variable that stores the number of steps that have been take (count) but I have not been able to create anything to make sure that all of the Tiles are in alphabetical order. I was thinking of using a 2D Array but I am not very familiar on how to use a 2D Array. I also am not sure what class should I type up such the code.

The Grid interface has a method to get the occupied locations (getOccupiedLocations(), I believe). You can traverse the returned List and check for ordering.

Quote

In part four, I am able to click on my grid and the tiles move to an empty Location but it moves in an odd manner and I am unsure how to fix it.

Odd how so?
Was This Post Helpful? 1
  • +
  • -

#3 jaeyuuji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 11

Re: Fifteen - Gridworld

Posted 29 March 2011 - 11:41 PM

I will attempt part three again, but I'm not familiar with the term traverse.

Here is what happened in part four:

No Click:
Posted Image

One Click:
Posted Image

This post has been edited by jaeyuuji: 29 March 2011 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Fifteen - Gridworld

Posted 30 March 2011 - 03:07 PM

Traverse means to iterate through, or use a loop.

As for the tile movements, the loop iterates through the letters row-by-row. So by the time you hit A, the only place it can move is down. Then T has an opening to move only left. Now that T has moved left, that leaves room to move M and L up when the loop gets to them. I guess the better question is, how do you want your tiles to be moved on a click?
Was This Post Helpful? 1
  • +
  • -

#5 jaeyuuji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 11

Re: Fifteen - Gridworld

Posted 30 March 2011 - 07:08 PM

I want the specific tile to move to an empty location but when I click anywhere in the grid, all the tiles move.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Fifteen - Gridworld

Posted 30 March 2011 - 07:10 PM

You can break out of your loop after you move a tile. Note that if statement determines if a Tile is moved or not.
Was This Post Helpful? 0
  • +
  • -

#7 jaeyuuji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 11

Re: Fifteen - Gridworld

Posted 31 March 2011 - 12:21 AM

Is it possible to create a NEW Array List that holds both a location and an object?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Fifteen - Gridworld

Posted 31 March 2011 - 04:09 AM

Only if you design a class that encapsulates both location and Object. Then you can store an ArrayList of that class.
Was This Post Helpful? 1
  • +
  • -

#9 jaeyuuji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 11

Re: Fifteen - Gridworld

Posted 02 April 2011 - 11:40 PM

getOccupiedLocations only gets the location, so what can I use to get what object is in the location?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Fifteen - Gridworld

Posted 03 April 2011 - 09:03 PM

See the Grid get() method.
Was This Post Helpful? 0
  • +
  • -

#11 jaeyuuji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-March 11

Re: Fifteen - Gridworld

Posted 03 April 2011 - 11:42 PM

Alright thanks! You can close this now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1