Menu

(Solved) : 0 Introduction Perditio Tempus Pronounced Per Dish Ee Oh Tem Pus Solitaire Game Played Dec Q28381672 . . .

0. Introduction.

Perditio Tempus (pronounced per-DISH-ee-ohTEM-pus) is a solitaire game played with a deck of cards. Itsname means ‘‘a waste of time’’ in Latin. In this project, you willwrite a Java program that plays Perditio Tempus. Despite the name,this program may still be interesting because it demonstrates somethings about how stacks work. Also, if you have a program thatplays Perditio Tempus, then you need not waste time playing ityourself.

1. Theory.

A standard deck has 52 cards. Each card hasone of four suits, called spade, club, heart, anddiamond, but suits are not used in Perditio Tempus. Eachcard also has one of thirteen ranks, called ace, two,three, four, five, six, seven, eight, nine, ten, jack, queen,and king. These ranks correspond to the numbers 1 through13, respectively.
      Perditio Tempus is played in thefollowing way. You shuffle the deck, and the deal the cardsface-down into a row of thirteen piles, with four cards ineach pile. The row of piles is called the tableau(pronounced tabLOW). The piles are numbered from1 to 13, so that each pile corresponds to a card rank. Play nowproceeds according to the following rules.

Set the variable p to 1.

If pile number p is empty, then go to step 4.

Turn over the card from the top of pile number p. Resetp to the number given by the card’s rank. Throw the cardaway. Go to step 2.

If all piles are empty, then you have won. If some pile is notempty, then you have lost.

You can use arrays and stacks to play Perditio Tempus. Theshuffled deck of cards, before it has been dealt into the tableau,can be represented as an array stack. The tableau itself can berepresented as an array of piles. Each pile in the array can berepresented as a linked stack.

2. Example.

Here is an example game of Perditio Tempus, shown step by step.This game was played automatically by a version of the program youare asked to write for this assignment. It assumes that the deckhas been shuffled and dealt to form the tableau.

Got five from pile 1.  
Got queen from pile 5.  
Got six from pile 12.  
Got two from pile 6.  
Got two from pile 2.  
Got six from pile 2.  
Got six from pile 6.  
Got nine from pile 6.  
Got four from pile 9.  
Got six from pile 4.  
Got three from pile 6.  
Got nine from pile 3.  
Got seven from pile 9.  
Got ten from pile 7.  
Got jack from pile 10.  
Got five from pile 11.  
Got king from pile 5.  
Got ace from pile 13.  
Got queen from pile 1.  
Got eight from pile 12.  
Got seven from pile 8.  
Got three from pile 7.  
Got nine from pile 3.  
Got two from pile 9.  
Got king from pile 2.  
Got ten from pile 13.  
Got jack from pile 10.  
Got four from pile 11.  
Got three from pile 4.  
Got queen from pile 3.  
Got jack from pile 12.  
Got king from pile 11.  
Got jack from pile 13.  
Got four from pile 11.  
Got five from pile 4.  
Got three from pile 5.  
Got two from pile 3.  
Got seven from pile 2.  
Got four from pile 7.  
Got eight from pile 4.  
Got eight from pile 8.  
Got nine from pile 8.  
Got king from pile 9.  
Got queen from pile 13.  
Got ace from pile 12.  
Got ten from pile 1.  
Got eight from pile 10.  
Got seven from pile 8.  
Got ten from pile 7.  
Got five from pile 10.  
Got ace from pile 5.  
Got ace from pile 1.  
You won!

The game was won this time. However, here is another game ofPerditio Tempus, played by the same program, which was lost. Thisis because pile 1 became empty, but there were still cards left inother piles.

Got jack from pile 1.  
Got jack from pile 11.  
Got three from pile 11.  
Got six from pile 3.  
Got two from pile 6.  
Got ace from pile 2.  
Got nine from pile 1.  
Got king from pile 9.  
Got two from pile 13.  
Got king from pile 2.  
Got ace from pile 13.  
Got three from pile 1.  
Got four from pile 3.  
Got nine from pile 4.  
Got four from pile 9.  
Got four from pile 4.  
Got king from pile 4.  
Got eight from pile 13.  
Got three from pile 8.  
Got ten from pile 3.  
Got queen from pile 10.  
Got jack from pile 12.  
Got four from pile 11.  
Got five from pile 4.  
Got seven from pile 5.  
Got two from pile 7.  
Got queen from pile 2.  
Got ace from pile 12.  
Got five from pile 1.  
Got six from pile 5.  
Got king from pile 6.  
Got seven from pile 13.  
Got queen from pile 7.  
Got ten from pile 12.  
Got nine from pile 10.  
Got six from pile 9.  
Got five from pile 6.  
Got eight from pile 5.  
Got ace from pile 8.  
Pile 1 is empty. You lost!

Your program is very unlikely to produce output exactly likeeither of these example games. It will depend on how Java choosesrandom numbers to shuffle the deck.

3. Implementation.

Your program will involve Java classes called Random, Card,Deck, Pile, Tableau, and Perditio. Here are brief descriptions ofall these classes.

The class Random. The Java class libraryprovides a class called Random that implements a random numbergenerator. You will use it to help shuffle the deck. To use Random,you must put the following line at the start of your program.

import java.util.Random;

Note that java and util are in lower case, Random begins with anupper case letter, and the line ends with a semicolon. The importstatement obtains something from the Java library. The library isdivided into packages and subpackages. Thepackage java contains things that are generally useful when writingJava programs. Its subpackage util contains various utilities, oneof which is Random.
      You need only Random’s constructorand its method nextInt. The constructor call new Random() returns anew instance of the class Random. If r is an instance of Random,then r.nextInt()returns a randomly generated int. It may benegative, and it may have a large absolute value.
      For this program, you will needsmall positive random int’s. You can get these by using the Javaexpression Math.abs(r.nextInt()) % m, where m returns a positiveint greater than 0. The expression will return a random int greaterthan equal to 0, but less than m.

The class Card. Each instance of the class Cardrepresents a playing card. Source code for this class is availableon Moodle. To make your program easier to grade, you must use thissource code; you must not modify or extend Card in any way. Here isa short description of Card’s public methods, and what they do.

public Card(int rank)

Constructor. Make a new instance of Card with a given rank,which must be between 1 and 13.

public int getRank()

Return the rank of this Card.

public String toString()

Return a String that describes this Card. It must be used onlyfor printing. For example, it might return “ace”, “two”, “queen”,”king”, etc.

Note that Card’s are immutable objects, so no part of them canbe changed after they are created by their constructor.

The class Deck. An instance of the class Deckrepresents a deck of Card’s. You must write Deck yourself. It musthave the following methods, and they must work as describedhere.

public Deck()

(5 points.) Constructor. Make an array containing 52 differentCard’s. You must use one or more loops: you will receive no pointsif you just write 52 assignment statements. The order of Card’swithin the array does not matter.

public Card deal()

(5 points.) Return the next Card from the array made by theconstructor. You need not pick a Card at random, because the Card’sin the array will already be shuffled into random order by the timeyou call deal. Throw an IllegalStateException if no Card’s remainto be dealt from the array. This method must work in O(1)time.

public void shuffle()

(10 points.) Shuffle the deck of Card’s that is represented bythe array you made in the constructor. The easiest way is theDurstenfeld-Fisher-Yates algorithm, named after its inventors. Itexchanges randomly chosen pairs of array elements, and works inO(n) time for an array of size n. Youmust use the following pseudocode for this algorithm.

Do steps 2 and 3 for integer values of i, starting fromthe length of the array minus 1, and ending with 1.

Let j be a random integer between 0 and i,inclusive.

Exchange the array elements at indexes i andj.

If shuffle is called after any Card’s have been dealt (by themethod deal), then it must throw an IllegalStateException. You arenot allowed to shuffle the deck after dealing has started.

To implement Deck, you can use ideas from the array stack classthat was discussed in the lectures.

The class Pile. Each instance of the class Pilerepresents a pile of Card’s. You must write Pile yourself. It musthave the following nested classes and methods, which must work asdescribed here.

private class Layer

(5 points.) An instance of this class represents a layer in thePile. It must have two slots. One must be called card, and it mustpoint to an instance of the class Card. The other must be callednext, and it must point to the next Layer in this Pile. The classLayer must also have a constructor that takes two arguments, calledcard and next, which determine the values of the two slots.

public Pile()

(2 points.) Constructor. Initialize a new empty Pile ofCard’s.

public void add(Card card)

(3 points.) Add card on top of this Pile.

public Card turn()

(3 points.) Turn over a Card from the top of this Pile. Returnthe Card. Throw an IllegalStateException if there are no cards inthe Pile.

public boolean isEmpty()

(2 points.) Test if this Pile has no Card’s left in it.

To implement Pile, you can use ideas from the linked stack classthat was discussed in the lectures.

The class Tableau. Each instance of the classTableau represents thirteen Pile’s of Card’s, as describedpreviously. You must write Tableau yourself. It must have thefollowing methods, which must work as described here.

public Tableau()

(5 points.) Constructor. Make an array with thirteen empty Pilesin it (see below). Also make a new instance of Deck. Shuffle theDeck. Deal four cards from the shuffled Deck into each Pile.

private boolean hasWon()

(5 points.) Test if all thirteen Pile’s are empty.

public void play()

(10 points.) Play a game of Perditio Tempus. Each time you get aCard from a Pile, write:

Got c from pile p.

where c is the name of the Card and p is thenumber of the Pile. At the end of the game, if the program won,then write:

You won!

If the program lost, then write:

Pile p is empty. You lost!

where p is the number of the empty Pile that you triedto get a card from. See the examples for details.

In Java, array indexes are numbered starting from 0, but inPerditio Tempus, piles are numbered starting from 1. The easiestway to handle this is to make an array with indexes 0 through 13,and then never use index 0.

The class Perditio. (0 points.) This class mustcontain only the main method. It starts the program running. Youmust write Perditio yourself.

———————————Card.java———————————-

// CARD. A playing card. It’s immutable.final class Card{// RANK NAME. Printable names of card ranks. We don’t use index 0. private static final String[] rankName = { “”, // 0 “ace”, // 1 “two”, // 2 “three”, // 3 “four”, // 4 “five”, // 5 “six”, // 6 “seven”, // 7 “eight”, // 8 “nine”, // 9 “ten”, // 10 “jack”, // 11 “queen”, // 12 “king” // 13 }; private int rank; // Card rank, between 1 and 13.// CARD. Constructor. Make a new CARD with a given RANK. public Card(int rank) { if (1 <= rank && rank <= 13) { this.rank = rank; } else { throw new IllegalArgumentException(“Illegal rank.”); } }// GET RANK. Return the RANK of this CARD. public int getRank() { return rank; }// TO STRING. Return a STRING that describes this CARD, for printing only. public String toString() { return rankName[rank]; }}

Expert Answer


Answer to 0 Introduction Perditio Tempus Pronounced Per Dish Ee Oh Tem Pus Solitaire Game Played Dec Q28381672 . . .

OR