Gridworld Dice Games

<< Hunt the Wumpus | FinalProjectsTrailIndex | Space Invader Game >>

  1. Attach:DiceGames.zip

Dice.java

The Dice class is the generic Dice Object which will turn red if clicked.

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

public class Dice extends Actor
{
 private Color color;
 private boolean locked;
 private boolean hold;
 private int value;
 public Dice()
 {
    locked = false;
    hold = false;
    this.color = Color.WHITE;
    value=0;
 }

 public Color getColor()
 {
      return color;
 }

 public void setColor(Color color)
 {
      this.color = color;
 }
 public int getValue()
 {
      return value;
 }

 public void setValue(int value)
 {
      this.value = value;
 }

 public void lock()
 {
        locked = true;
        this.color=Color.RED;
 }
 public void select()
 {
        if (!locked) hold = !hold;
        if (hold || locked) this.color=Color.RED;
        else this.color=Color.WHITE;
 }
 public boolean isLocked()
 {
     return locked;
 }
 public boolean isHeld()
 {
     return hold;
 }
}

Dice1.java

There are 6 (Dice1...Dice6) for the six possible face of the dice. Gridworld needs to have the gif image match the class name to show the gif image in the grid.

public class Dice1 extends Dice
{
   public Dice1(){
       super();
       setValue(1);
    }
}

DiceGame.java

Unlike the bugs and critters, the real control of the game is here in the DiceGame class witch is a subclass of World. Clicking will hold or "freeze" the dice, and pressing the step button will randomly select a new value for the remaining dice. If all are held, it will roll all six.

import info.gridworld.world.*;
import info.gridworld.grid.*;
public class DiceGame extends World<Dice>
{
    private Grid gr;
    public DiceGame(){
        gr=new BoundedGrid(1,6);
        this.setGrid(gr);
        System.setProperty("info.gridworld.gui.selection", "hide");
    	System.setProperty("info.gridworld.gui.frametitle", "Dice Game");
    	System.setProperty("info.gridworld.gui.tooltips", "hide");
    	setMessage("Click Step to Roll\nClick Dice to hold");
    	for (int i=0; i<6; i++)
    	   add(new Location(0,i),getDice(toss()));

    }
    public boolean locationClicked(Location loc){

        Dice d=(Dice)gr.get(loc);
        d.select();
        return true; //consume the mouse click
    }
    public Dice getDice(int value){
        if (value==1) return new Dice1();
        if (value==2) return new Dice2();
        if (value==3) return new Dice3();
        if (value==4) return new Dice4();
        if (value==5) return new Dice5();
        if (value==6) return new Dice6();
        return null;
    }
    public int toss(){
        return (int)(5*Math.random())+1;
    }

    public void step(){
        boolean noNewDice=true;
        for (int i=0; i<6; i++){
            Location loc = new Location(0,i);
            Dice d=(Dice)gr.get(loc);
            if (d.isHeld() ){
                d.lock();
            } else {
                remove(loc);
                add(loc,getDice(toss()));
                noNewDice=false;
            }
        }
        if (noNewDice)
            for (int i = 0 ; i< 6; i++){
               Location loc = new Location(0,i);
               remove(loc);
                add(loc,getDice(toss()));
            }
    }
}

DiceGameRunner.java

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