Gridworld Tile Game

<< Two Player Starter Code | FinalProjectsTrailIndex | Worm Game >>

This tile game is adapted from an earlier example written by Cay Horstman when he first started designing Gridworld. It works a lot like a TV Game show called "Concentration." The game board contains black, or "covered" tiles (or Generic Actors as it is--you can make your own tile gif if you wish). The player's job is to uncover matching color pairs. Click on a tile to flip it. Then click on another. If both tiles have the same color, they both stay exposed. If not, they are both covered again. Try to match all pairs of all colors. The idea is to uncover all the matching colors in as few attempts as possible.

Tile.java

import info.gridworld.actor.Actor;
import java.awt.Color;

public class Tile extends Actor
{
	private Color color;
	private boolean up;

	public Tile(Color color){
		up = false;
		this.color = color;
	}

	public Color getColor(){
		if (up)
			return color;
		return Color.BLACK;
	}

	public void setColor(Color color){
		this.color = color;
	}

	public void flip(){
		up = !up;
	}
}

TileGame.java

import java.awt.Color;
import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class TileGame extends ActorWorld
{
    private Location up; 
    private int guessNumber;
    public TileGame(){
    		Color[] colors = { Color.RED, Color.BLUE, Color.GREEN, Color.CYAN, Color.PINK, Color.ORANGE, Color.GRAY, Color.MAGENTA, Color.WHITE, Color.YELLOW};
    		for (Color color : colors){
    			add(new Tile(color));
    			add(new Tile(color));
    		}
    		guessNumber=0;
    		System.setProperty("info.gridworld.gui.selection", "hide");
    		System.setProperty("info.gridworld.gui.frametitle", "Tile Game");
    		System.setProperty("info.gridworld.gui.tooltips", "hide");
    		setMessage("Click on the first tile");
    }

    public boolean locationClicked(Location loc){
	 Grid<Actor> gr = getGrid();
	 Tile t = (Tile) gr.get(loc);
	 if (t != null) 
	 {
		 t.flip(); 
		 if (up == null){
			 setMessage("Click on the second tile");
			 up = loc;
		 }else {
			 Tile first = (Tile) gr.get(up);
			 if (!first.getColor().equals(t.getColor())){
				 first.flip();
				 t.flip();
			 }
			 guessNumber++;
			 setMessage("Click on the first tile\n"+guessNumber+" guesses so far (10 is a perfect score)");
			 up = null;
		 } 
	 }
	 return true; 
    }

}

TileGameRunner.java

public class TileGameRunner 
{
	public static void main(String[] args)
	{
             TileGame game=new TileGame();
             game.show();
	}
}

Tweaking a world

You can tweak the behavior of your world as follows:

  1. Call setMessage to display different messages in the yellow area above the grid. (Scroll bars are added if needed.) This can happen at any time.
  2. Call addOccupantClass so that additional occupant classes are offered when a user clicks on an empty location. (By default, any classes that have ever been added to the world are displayed.)
  3. Call addGridClass to supply another grid class in the World -> Set grid menu, such as a torus grid that wraps around the edges. This is typically done in the main method of the runner program.
  4. Call step programmatically. You may want to do this in a program that tests student homework, so you don't have to press the Step button yourself.

Other worlds

You can use the GridWorld framework for games and other applications. In this case, you will want to install your own subclass of the info.gridworld.world.World class. There are three main extension points.

  1. Override the locationClicked method so that something happens when the user clicks on a location (or presses the Enter key). Your method should return true if you consume the event. If you return false, the GUI will carry out its default action, showing the constructor menu in an empty location or method menu in an occupied location.
  2. Override the keyPressed method so that something happens when the user hits a key. Your method should return true if you consume the key press. Don't consume standard keys such as the arrow keys or Enter. (Shifted arrows are ok to consume.)
  3. Override the step method so that something happens when the user presses the Step button. By default, nothing happens in the World class. (In the ActorWorld, the act method is invoked on all occupants.)

Secret Flags

  1. If you call System.setProperty("info.gridworld.gui.selection", "hide"); the selection square is hidden. This may be useful for special environments (such as a Quzzle game in which it doesn't make sense to select a fractional tile.)
  2. If you call System.setProperty("info.gridworld.gui.tooltips", "hide"); the tooltips are hidden.
  3. Call System.setProperty("info.gridworld.gui.frametitle", titleString); before showing the world to set a different title.

If you modify the framework to implement other special effects that shouldn't result in API clutter, consider using a similar “secret flag” mechanism.

Handy Links to the API

  1. World API
  2. ActorWorld API