The game ends and that player the winner task code this game
|
Laboratory Exercises |
---|
Figure 1 Typical Six Sided Dice
Exercise 1: Implementing the Die Class
Create a Die class with the following instance variable, constructor and methods:
public void throwDie(){}
public int getFaceValue(){}
|
Laboratory Exercises |
---|
• Initialises the instance variable faceValue to a random integer value (see paragraph
below) betweeen 1 and sides inclusive.
java.util.
The getFaceValue() method is an access method and simply returns the face value of the die.
Now create a tester class called DieTester which tests the constructor and all of the Die methods.
The test driver should simulate the behaviour of a die which is thrown thirty six times so you should use
Test 2: Test getFaceValue
Inspecting die face ...
Face value = 2increases our confidence that the class has been correctly implemented.
Laboratory Exercises |
---|
However, we begin by doing something relatively simple by developing the game described below
This is a single player game called Six or Lose. The die is placed on the table with some particular value showing on the die face. The player starts with a score of six. The player then throws the die. If the face is a six then the game is over and the player still has a score of six. On the other hand if the die face is not six then the player loses one point and throws again. This process continues until either the player has thrown a six or the player’s score has become equal to zero. The aim of the game is to throw a six before the player’s score reaches zero.
You’ve thrown a six!!! You win with a score of 1.
Notice that in this example of the game the player starts by throwing a one and so loses a point from their score. On the next turn after throwing the die and getting three the player loses one point from their score which now becomes four.
|
---|
You have not thrown a six. You lose.
How is this program to be written? Well the first thing to realise is that the code from the class Die can be re-used because this class provides all of the necessary methods for simulating the behaviour of the Die in this game. In fact, in order to write this program all that we need to do is to encode the relatively simple game logic in another class called DieGame1. To assist you in this task a pseudo-code solution is provided on the next page and below there a few tips relating to the implementation of this design.
Notice that to create a particular object of the class Die we need to use the new operator in conjunction with the constructor for that class. The other two statements are simply assigning specific values to an integer and boolean variable respectively.
There is not much to say about that part of the program which displays a start message and column headings; it is merely a matter of writing the appropriate strings and integer values using the standard println method.
Laboratory Exercises |
---|
create a Die object
set score to 6
set gameOver to falseprint start message
Laboratory Exercises |
---|
The effect of these two statements is to create and initialise two distinct objects belonging to the class Die. We express this by saying that die1 and die2 are instances of the same class. This means that we can perform all of the Die operations, thus far defined, on either of these objects.
Note also that each object has its own distinct Die face value: the face value of die1 is independent of the face value of the other Die. If this were not the case then the two Die would always show the same value which is not at all what we want.
Your score = 0
Notice that the player’s score is only displayed at the end of the game.
Laboratory Exercises |
---|
Starting Doubles ...
Your score = 3
Your task is to develop and encode the game logic in a new class called DieGame2. Some tips are
• use System.out.print if you don’t want to break the line display
Implementation Note and a Challenge
public class GameTester
{
}
}
Exercise 5. Building a Two-Player Game
After developing the Player class you should now be in a position to implement a two-player version of our simple game involving a pair of dice.
|
---|
{
public Player(int initialScore) {
score = initialScore;
}
|
---|
score to zero. In other words why didn’t we just write
public class Player
{
public Player()
{
score = 0;
}
game where the initial score is not equal to zero. By providing the constructor Player with a
parameter we can re-use this class to develop other kinds of dice game. For example, some dice
// Mutator method to increases player’s score
public void setScore(int points)
{
|
Laboratory Exercises |
---|
increment the player’s score. The same effect, of course, could have been obtained using the
statement below
for this class is provided below.
// Defines methods and attributes for Player class
} |
---|
public void setScore(int points)
{
score += points;
}// Returns value of players score
Laboratory Exercises |
---|
Pig is a well-known two-player, jeopardy style game, widely used in computing and mathematics courses to teach concepts of probability. The rules for the game of Pig are given below: Players
Best with two players but can work with more. (But note that the downtime between your turns grows longer with each additional player.)
Equipment
The game is played with a single six-sided die.Goal
Be the first player to reach 100 points.Game End
When a player reaches a total of one hundred or more points, the game ends and that player is the winner.Your task is to code this game. A simple way of doing it is for the program to alternately invite two human players to take their turn and to keep rolling until a one is thrown, or the player decides to stop rolling and pass the dice to the other player. A more complex and challenging approach might be to automate the game entirely and provide each virtual player with their own game playing algorithm.
Laboratory Exercises |
---|
Submission Notes and Marking Scheme
Use Blackboard to submit your source code files. The deadline for submission will be published on Blackboard. Late submissions will be penalised in line with School policy.
Students who submit work but have a poor understanding of what has been submitted may be heavilypenalised. When making a submission it is your responsibility to ensure that all code submitted via Blackboard is:
|
---|