LAB 12 - INTERFACES



PROBLEM STATEMENT: In this lab you are suppose to implement the TWO following interfaces in a class that plays a simple dice game. One of the interfaces, Rollable relates to any system that uses dice, and the other, Game relates to game play and score monitoring.

Your job is to define a class LuckySeven which implements both the interfaces as detailed below.

The Lucky Seven Game:

Lucky Seven is a gambling game popular at IIT Kanpur fetes. As part of your LuckySeven class, you need to build a Rollable die. And then you need to build the functionality for playing the following game:

You start with an initial amount (say Rs 100). At each turn, you bet a certain amount and you roll two dies (or the same die twice) and if the sum of both the rolls turns out to be seven, you win seven times your bet - otherwise you lose the amount of your bet. The game ends when you "quit" or are bankrupt.

The class LuckySeven must have a set of variables relating to the game such as "balance". There must be a "main" function which will start the game. Either main or some other routine should ask the user for his name, call the "play" routine, and after each round, give the user the option of quitting or continuing. At any point during the game, the results of the last K scores in a history array which contains a record with the amount betted, result, and balance. (e.g. K= 10, or the number of actual plays, whichever is less).



The Interfaces:


public interface Rollable
{
	public void roll();
	// For example, this can be used to roll a die.  
	// Bailey's Elements of Java contains this example of a Die class 
	// that implements this interface.  [Note: this is for your
	// idea only - the code itself cannot be directly used since some
	// graphical primitives are specific to the package "elements"
	// used in that book.]


	public int value();
	// For example, this can be used to return the value on the
	// top face of the die. 
}

public interface Game
{
        int HISTORYLENGTH;   // by default interface variables are public static final

	void play();	     // all interface methods are public by default
	// invoked at each play.  In repeated games, this would
	// set some static variables (such as balance money) etc. 

	void quit();
	// quits the program; in repeated games, 
	// may be offered as a choice after each round. 

	void setPlayerName(String name);
	// asked once at the outset, or in some games, only if you
	// make it to the "high scores" table. 
	// the implementing method must have an instance variable
	//	which is set. 

	void showHistory();
	// show the results from the last HISTORYLENGTH rounds in the
	// current session (e.g. HISTORYLENGTH=10). 

}

NOTE: To help you in graphically drawing the face of a die, we have defined a method that uses the Canvas.java class that we have been using in class. This routine - void drawDie(int facevalue)) is part of DisplayDie.java . You may wish to use this routine in your LuckySeven.java class, extend it to show two dies at once, or altogether use your own graphics code.