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.
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!
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

New Topic/Question
Reply


MultiQuote








|