Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview You are to write a program that simulates playing the solitaire card ga

ID: 3831861 • Letter: O

Question

Overview

You are to write a program that simulates playing the solitaire card game Clock. We provide several sites at the end of this document that describe and demonstrate the rules; we suggest you familiarize yourself with the game first. Here’s how we want you to implement the simulation.

Part 1 - Required Classes

Create a Card class that can represent each of the 52 playing cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King … in each of the four suits: Spades, Hearts, Diamonds, and Clubs.
Update: These are the basic classes; you may create other classes also.

Implement the following methods:

constructors

getSuit()

getValue()

String toString(): to print, e.g., “10 of Hearts” or “Queen of Diamonds” or “Ace of Spades”
Update: We are thinking that printing cards more compactly -- e.g., “10H” or “QD” or “AS” -- i.e. a one (or two) character value, followed by a one-character suit -- will make both debugging and our grading easier.

Create a Deck class that represents a standard deck of 52 playing cards.

Implement the following methods:

constructors

void shuffle(): randomly interchanges cards in the deck to simulate shuffling

Card dealCard()

int cardsLeft()

String toString(): iterate through your deck array, printing all the cards, in a way that’s convenient to check what cards are there; we think it’ll be best to print all the cards on one line (or print them on four lines, if you want to get fancy); for cards that have been dealt, either don’t print them, or print them separately, labeled as “dealt”

Create a Pile class that contains no more than five cards… some face down, and some face up.

Implement the following methods:

constructors

void addCardFaceDown( Card card)

Card removeCard() - removes and returns the “top” face down card (null if there are none)

int getNumberOfFaceDown()

void addCardFaceUp( Card card)

int getNumberOfFaceUp()

String toString() - print the cards in the Pile; print the contents of a pile on one or two lines; label the portion of the pile that’s face up versus face down

Part 2 - The Clock Solitaire Game

Implement the Clock Solitaire Game simulation, with all the following capabilities. We strongly suggest you don’t ignore handling command line arguments, as this feature is simply to implement, and will be a great help in your debugging process, and in our grading. Name your primary class -- the one with the main() method -- ClockSolitaire.

Command-Line Arguments

Your program will accept two command line arguments. (These arguments are optional, and your program will use the “default” values if the argument(s) are not specified. And, implementing handling of command line arguments is not optional.)
Update: Ideally, you should handle the case where zero, one, or two command-line arguments are specified; use the default values if the argument isn’t provided.

Print level - one of the following three values:

verbose

normal (the default)

silent

Number of games to play (default: 1)

Setup Tasks

create thirteen Piles

these should be stored in an array of Piles (rather than thirteen discrete variables)

create and shuffle a Deck of Cards

deal out the deck to populate (initialize) the thirteen Piles with four face-down cards each

You may deal one card for each of the thirteen Piles, and then a second card, and so on…

or, you may deal four cards to fill the first Pile, four for the second Pile, and so on…

print the thirteen Piles -- the game board (this will mainly uses Pile.toString() ), if the print level is not “silent”; label each pile with its clock position; for simplicity, you can print one pile per line (you can get more creative, if you like)

print the “score”: the number of Piles with at least one face-down card, if the print level is not “silent”; note a game is won when the “score” is zero

this will be 13 at the beginning of each game

Playing the Game

remove (top, face-down) card from Pile 13, the Kings Pile

add it, face-up, to the (“bottom” of the) correct Pile

if the print level is “verbose”, print the game board -- the thirteen Piles -- as above

also, keep track of and print the “step number,” a counter that you increment each time you “remove-and-add” a card

remove (top, face-down) card from that same Pile

one needs to check that there are face-down cards remaining

if there are no face-down cards remaining, the game is over, if you’re on Pile 13

(if there are no face-down cards remaining, and you’re not on Pile 13, then there’s an error)

repeat this process by going to step 2

When Each Game is Done

print the thirteen Piles -- the game board (as above), if the print level is not “silent”

print the “score”: the number of Piles with at least one face-down card, if the print level is not “silent”

a score of “zero” is a “win”; note this is rare

increment a counter in a Scores array for the number of Piles with at least one face-down card

repeat, from the top (the Setup), according to how many games to play, as specified in the command-line arguments

When You’ve Completed Playing the Required Number of Games

print the number of games played

print the Scores array, along with percentages, as follows; this will be 14 lines, with both the number and percentage of games that resulted in each score
(Update: Actually, you only need to print 13 lines, indicating for 0, 1, 2, …, 12, the percentage of games that ended with that many face-down cards remaining; you’ll never end a game with 13 piles with face-down cards)

(print this for all print levels, including “silent”)

References: Rules of the Game

Here are some sites that describe the rules of Clock, and one that allows you to “play” it online:

https://www.thespruce.com/clock-solitaire-rules-412468

https://en.wikipedia.org/wiki/Clock_Patience

https://youtu.be/6AEJEf8L95g ; https://youtu.be/yUj320C9210

http://cardgameheaven.com/single-player-games/clock.html

http://www.novelgames.com/en/clocksolitaire/ (this site lets you play online)

Below is the code I wrote, im having trouble with this error:

java.lang.ArrayIndexOutOfBoundsException: 5
   at Pile.addCardFaceDown(Pile.java:30)
   at ClockSolitaire.main(ClockSolitaire.java:29)

what does it mean?

Code:

ClockSolitaire Class:

public class ClockSolitaire{


public static void main(String[] args)
{

//This creates variables, new deck and a new pile is created
Pile[] piles = new Pile[13];
Deck decks = new Deck();
int next = 0;
int score = 0;

//This just shuffles the deck
decks.shuffle();

//loop to add cards to the face down pile
for(int j = 0; j < piles.length - 1; j++)
{
for(int k = 0; k < 4; k++)
{
piles[j] = new Pile();
piles[j].addCardFaceDown(decks.dealCard());
}
}

//loop to print out all the piles
for(int j = 0; j < piles.length - 1; j++)
{
System.out.print(piles[j].toString());
}

//loop to print out the number of cards face down in each pile, and remove one card from each pile to
//and add it to the face up pile, whilst incrementing the number of times a card is removed and added
for(int j = piles.length -1 ; j < 0; j--)
{
System.out.print(piles[j].getNumberOfFaceDown());
piles[j].addCardFaceUp(piles[j].removeCard());
next++;
}

//loop to print the remaining piles
for(int j = 0; j < piles.length - 1; j++)
{
System.out.print(piles[j].toString());
}

//loop to score the game, which depends on the number of cards remaining face down in each pile
for(int j = 0; j < piles.length - 1; j++)
{
score += piles[j].getNumberOfFaceDown();
if(score == 0)
{
System.out.println("Your Score is: " + score + " You Win!");
}else
{
System.out.println("Your Score is: " + score + " You Lose!");
}
}
}
}

Pile class:

public class Pile
{

//create variables
private Card[] cards;
private Card[] down;
private Card[] up;

//This just initializes teh arrays
public Pile()
{
cards = new Card[5];
down = new Card[5];
up = new Card[5];
}

//This adds the face down cards together
public void addCardFaceDown(Card card)
{
//for loop to place card into the array for the face down pile
for(int i = down.length; i > 0; i--)
{
down[i] = card;
}
}

//This just takes the top card and removes it
public Card removeCard() {
//This just holds the value for the top card
Card card = new Card();

//give the top card the value of the top card in the pile
for(int j = down.length; j > 0; j--)
{
if(down[j].equals(""))
{
card = down[j];
}else
{
return null;
}
}

//returns the card on top
return card;
}

//This is just used to count the amount of face down cards
public int getNumberOfFaceDown()
{
//This just gets the card value
int numDown;
numDown = down.length;

//Returns the number of down cards
return numDown;
}

//method to add cards that face up
public void addCardFaceUp(Card card)
{
//loop to add the card face up
for(int j = up.length; j > 0; j++)
{
up[j] = card;
}
}

//This returns the amount of face up cards that are left
public int getNumberFaceUp()
{
//All of this is just returning the amount of face up cards that are remaining
int numUp;

numUp = up.length;

return numUp;
}

//This turns the card values into strings and counts the amount of up cards and the amount
//of cards down and then prints out how many are in each position
public String toString()
{
String print = "";

System.out.println("Cards Face Down");
  
for(int j = down.length; j > 1; j++)
{
print += down[j] + " ";
}

System.out.println("Cards Face Up");

for(int j = up.length; j > 0; j++)
{
print += up[j] + " ";
}

return print;
}
}

Deck class:

public class Deck
{

//This just creates variables to be used throughout
private Card[] cards;
private int remain;

//This is just the no args constructor
public Deck()
{
//This just initializes
remain = 52;
int cardValue = 1;
cards = new Card[52];

//This fills the deck with not repetition of cards
for(int j = 0; j < 52; j += 4)
{
cards[j] = new Card(cardValue, 'd');
cards[j + 1] = new Card(cardValue, 's');
cards[j + 2] = new Card(cardValue, 'c');
cards[j + 3] = new Card(cardValue, 'h');
cardValue++;
}
}

//This just shuffles the card
public void shuffle()
{
//This just shuffles the deck 500 times
for(int j = 0; j < 500; j++)
{
  
int num1, num2;
Card val;

//This initializes the variables and readies them to be randomized
num1 = (int)(Math.random() * 52);
num2 = (int)(Math.random() * 52);

//This part of the code does all of the shuffling
val = cards[num1];
cards[num1] = cards[num2];
cards[num2] = val;
}
}

//method to get the card on top of the deck
public Card dealCard()
{
//This holds a temporary ccard value hold and then
// takes 1 away from the remaining cards
Card hold = cards[remain-1];
remain--;

return hold;
}

//Returns the amount of remaining cards
public int cardsRemain()
{
return remain;
}

public String toString()
{
//This just adds a space into the object string
// and the rests prints out the cards
String object = "";

for(int j = 0; j < remain; j++)
{
object += " " + cards[j].toString();
}

return object;
}
}

Card class:

public class Card
{

//create variables to be used
private int value;
private char suit;

//This just initializes variables
public Card()
{
value = 0;
suit = 'a';
}

//This allows new values to be entered into the card method
public Card(int inVal, char inSuit)
{
value = inVal;
suit = inSuit;
}

//This method just gets the suit of the card
public String getSuit()
{
  
String name;

//This just switchs what suit is used
//It breaks once the suit is selected
switch(suit)
{
case 'h': name = "H";
break;
case 'd': name = "D";
break;
case 's': name = "S";
break;
case 'c': name = "C";
break;
default: return " ";
}

return name;
}

//This just returns the value of value
public int getValue()
{
return value;
}

//method to print turn the class into a string
public String toString()
{
//Will hold a value if the card is a face value
String card;

//Turns the face card values into characters
// Ex. Ace = A
switch(value)
{
case 1: card = "A";
break;
case 11: card = "J";
break;
case 12: card = "Q";
break;
case 13: card = "K";
break;
default: card = Integer.toString(value);
}

//Returns the shortened name of the card
// Ex. 10H = 10 of Hearts
return card + this.getSuit();
}
}

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Link
{
private Object valueField;
private Link   nextLink;

public Link (Object newValue, Link next)
{
    valueField = newValue;
    nextLink   = next;
}

// get Object part of link
public Object value()
{
    return valueField;
}

// get Link part of link
public Link next()
{
    return nextLink;
}
}

class LinkedList
{
private Link firstLink;

public LinkedList()
{
    firstLink = null;
}

// true if empty, false otherwise
public boolean empty()
{
    return firstLink == null;
}

// add an Object to our list
public void add (Object newValue)
{
    firstLink = new Link(newValue, firstLink);
}

// inspect front of list
public Object front()
{
    if (firstLink == null)
      return null;

    return firstLink.value();
}

// pop front of list
public Object pop()
{
    if (firstLink == null)
      return null;

    Object result = firstLink.value();
    firstLink = firstLink.next();
    return result;
}

// return an iterator for the list
public ListIterator iterator()
{
    return new ListIterator(firstLink);
}
}

class ListIterator
{
private Link currentLink;

public ListIterator (Link firstLink)
{
    currentLink = firstLink;
}

// true if we reached end of list, false otherwise
public boolean atEnd()
{
    return currentLink == null;
}

// move to next link
public void next()
{
    if (currentLink != null)
      currentLink = currentLink.next();
}

// return value of current link
public Object current()
{
    if (currentLink == null)
      return null;

    return currentLink.value();
}
}
class Card
{
// data fields for colors and suits
final static int width   = 50;
final static int height = 70;

final static int red     = 0;
final static int black   = 1;

final static int heart   = 0;
final static int spade   = 1;
final static int diamond = 2;
final static int club    = 3;

final static int ace     = 0;
final static int king    = 12;

private static String names[] = {"A", "2", "3", "4", "5", "6",
       "7", "8", "9", "10", "J", "Q", "K"};

// data fields
private boolean faceup;
private int rank;
private int suit;

// constructor
Card (int s, int r)
{
    suit = s;
    rank = r;
    faceup = false;
}

// get rank of card as an int in the interval [0, 12]
public int rank()
{
    return rank;
}

// get suit of card as an int in the interval [0, 3]
public int suit()
{
    return suit;
}

// true if card is face up, false otherwise
public boolean faceUp()
{
    return faceup;
}

// change value of faceup
public void flip()
{
    faceup = ! faceup;
}

// true if card is ace, false otherwise
public boolean isAce()
{
    return rank == ace;
}

// true if card is king, false otherwise
public boolean isKing()
{
    return rank == king;
}

// return color of card as an int in the range [0,1]
public int color()
{
    if (suit() == heart || suit() == diamond)
      return red;

    return black;
}

// draw the card
public void draw (Graphics g, int x, int y) {
    // clear rectangle, draw border
    g.clearRect(x, y, width, height);
    g.setColor(Color.black);
    g.drawRect(x, y, width, height);

    // draw body of card
    if (faceUp())
      {
if (color() == red)
   g.setColor(Color.red);
else
   g.setColor(Color.black);

g.drawString(names[rank()], x+3, y+15);

if (suit() == heart)
   {
     g.drawLine(x+25, y+30, x+35, y+20);
     g.drawLine(x+35, y+20, x+45, y+30);
     g.drawLine(x+45, y+30, x+25, y+60);
     g.drawLine(x+25, y+60, x+5, y+30);
     g.drawLine(x+5, y+30, x+15, y+20);
     g.drawLine(x+15, y+20, x+25, y+30);
   }
else if (suit() == spade)
   {
     g.drawLine(x+25, y+20, x+40, y+50);
     g.drawLine(x+40, y+50, x+10, y+50);
     g.drawLine(x+10, y+50, x+25, y+20);
     g.drawLine(x+23, y+45, x+20, y+60);
     g.drawLine(x+20, y+60, x+30, y+60);
     g.drawLine(x+30, y+60, x+27, y+45);
   }
else if (suit() == diamond)
   {
     g.drawLine(x+25, y+20, x+40, y+40);
     g.drawLine(x+40, y+40, x+25, y+60);
     g.drawLine(x+25, y+60, x+10, y+40);
     g.drawLine(x+10, y+40, x+25, y+20);
   }
else if (suit() == club)
   {
     g.drawOval(x+20, y+25, 10, 10);
     g.drawOval(x+25, y+35, 10, 10);
     g.drawOval(x+15, y+35, 10, 10);
     g.drawLine(x+23, y+45, x+20, y+55);
     g.drawLine(x+20, y+55, x+30, y+55);
     g.drawLine(x+30, y+55, x+27, y+45);
   }
      }
    else // face down
      {
g.setColor(Color.yellow);
g.drawLine(x+15, y+5, x+15, y+65);
g.drawLine(x+35, y+5, x+35, y+65);
g.drawLine(x+5, y+20, x+45, y+20);
g.drawLine(x+5, y+35, x+45, y+35);
g.drawLine(x+5, y+50, x+45, y+50);
      }
}
}
class CardPile
{
// coordinates of the card pile
protected int x;
protected int y;

// linked list of cards
protected LinkedList cardList;

CardPile (int xl, int yl)
{
    x = xl;
    y = yl;
    cardList = new LinkedList();
}
public final boolean empty()
{
    return cardList.empty();
}

// inspect card at the top of pile
public final Card top()
{
    return (Card)cardList.front();
}

// pop card at the top of pile
public final Card pop()
{
    return (Card)cardList.pop();
}

public boolean includes (int tx, int ty)
{
    return x <= tx && tx <= x + Card.width &&
           y <= ty && ty <= y + Card.height;
}
       
// to be overridden by descendants
public void select (int tx, int ty)
{
    // do nothing
}

// add a card to pile
public void addCard (Card aCard)
{
    cardList.add(aCard);
}

// draw pile
public void display (Graphics g)
{
    g.setColor(Color.black);

    if (cardList.empty())
      g.drawRect(x, y, Card.width, Card.height);
    else
      top().draw(g, x, y);
}

// to be overridden by descendants
public boolean canTake (Card aCard)
{
    return false;
}

// get number of cards in pile
public int getNoCards()
{
    int count = 0;
    ListIterator iterator = cardList.iterator();

    while (!iterator.atEnd())
      {
count++;
iterator.next();
      }

    return count;
}
}
class DeckPile extends CardPile
{
DeckPile (int x, int y)
{
    // first initialize parent
    super(x, y);

    // then create the new deck
    // first put them into a local pile
    CardPile pileOne = new CardPile(0, 0);
    CardPile pileTwo = new CardPile(0, 0);

    int count = 0;
    for (int i = 0; i < 4; i++)
      for (int j = 0; j <= 12; j++)
{
   pileOne.addCard(new Card(i, j));
   count++;
}

    // then pull them out randomly
    for (; count > 0; count--)
      {
int limit = ((int)(Math.random() * 1000)) % count;

// move down to a random location in pileOne
// while poping the cards into pileTwo
for (int i = 0; i < limit; i++)
   pileTwo.addCard(pileOne.pop());

// then add the card found there
// to our LinkedList object: cardList
addCard(pileOne.pop());

// now put back into pileOne the cards
// that we poped into pileTwo
while (!pileTwo.empty())
   pileOne.addCard(pileTwo.pop());
    }
}

public void select(int tx, int ty)
{
    // if deck becomes empty, refill from discard pile
    if (empty())
      {
while (!Solitaire.discardPile.empty())
   {
     addCard(Solitaire.discardPile.pop());
     top().flip();
   }
      }
    else
      Solitaire.discardPile.addCard(pop());
}
}
class DiscardPile extends CardPile
{
DiscardPile (int x, int y)
{
    super (x, y);
}

public void addCard (Card aCard)
{
    if (!aCard.faceUp())
      aCard.flip();

    super.addCard(aCard);
}

public void select (int tx, int ty)
{
    if (empty())
      return;
           
    Card topCard = top();

    // check the SuitPile's first
    for (int i = 0; i < Solitaire.no_suit_piles; i++)
      if (Solitaire.suitPile[i].canTake(topCard))
{
   Solitaire.suitPile[i].addCard(pop());
   return;
}

    // then check the TablePile's
    for (int i = 0; i < Solitaire.no_table_piles; i++)
      if (Solitaire.tableau[i].canTake(topCard))
{
   Solitaire.tableau[i].addCard(pop());
   return;
}
}
}

class SuitPile extends CardPile
{
SuitPile (int x, int y)
{
    super(x, y);
}

public boolean canTake (Card aCard)
{
    if (empty())
      return aCard.isAce();

    Card topCard = top();
    return (aCard.suit() == topCard.suit()) &&
           (aCard.rank() == 1 + topCard.rank());
}

public void select (int tx, int ty)  
{
    if (empty())
      return;

    Card topCard = top();

    // check the TablePile's
    for (int i = 0; i < Solitaire.no_table_piles; i++)
      if (Solitaire.tableau[i].canTake(topCard))
{
   Solitaire.tableau[i].addCard(pop());
   return;
}
}
}
class TablePile extends CardPile
{
final static int ydist = 25;

TablePile (int x, int y, int c)
{
    // initialize the parent class
    super(x, y);

    // then initialize our pile of cards
    for (int i = 0; i < c; i++)
      addCard(Solitaire.deckPile.pop());

    // flip topmost card face up
    top().flip();
}

public boolean canTake (Card aCard)
{
    if (empty())
      return aCard.isKing();

    Card topCard = top();

    // if our topmost card is face down
    // we can't accept another card
    if (!topCard.faceUp())
      return false;

    return (aCard.color() != topCard.color()) &&
           (aCard.rank() == topCard.rank() - 1);
}

public boolean includes (int tx, int ty)
{
    if (empty())
      return false;

    // don't test bottom of card
    return x <= tx && tx <= x + Card.width &&
           y <= ty;
}

public void select (int tx, int ty)
{
    if (empty())
      return;

    // if face down, then flip
    Card topCard = top();
    if (!topCard.faceUp())
      {
topCard.flip();
return;
      }

    // see if any suit pile can take card
    for (int i = 0; i < Solitaire.no_suit_piles; i++)
      if (Solitaire.suitPile[i].canTake(topCard))
{
   Solitaire.suitPile[i].addCard(pop());
   return;
}

    // try to create a build
    CardPile build = new CardPile(0, 0);

    // get the cards for the build from the suit pile
    while (!empty())
      {
// stop if we reached a card that is face down
if (!top().faceUp())
   break;

build.addCard(pop());
      }

    // We don't allow the user to play a King card
    // that is at the bottom of a table pile
    // to another table pile
    if (build.top().isKing() && empty())
      {
while (!build.empty())
   addCard(build.pop());
return;
      }   

    // if we have to play only one card
    if (build.top() == topCard)
      {
// put it back into the table pile
addCard(build.pop());

// we have already tried the suit piles
        // see if any other table pile can take card
        for (int i = 0; i < Solitaire.no_table_piles; i++)
          if (Solitaire.tableau[i].canTake(topCard))
            {
              Solitaire.tableau[i].addCard(pop());
              return;
            }
      }
    else // we got ourselves a build to play
      {
topCard = build.top();

// see if any other table pile can take this build
for (int i = 0; i < Solitaire.no_table_piles; i++)
   if (Solitaire.tableau[i].canTake(topCard))
     {
       while (!build.empty())
  Solitaire.tableau[i].addCard(build.pop());
      
       return;
     }

// can't play the build?
// then we must restore our pile
while (!build.empty())
   addCard(build.pop());
      }
}

private void stackDisplay(Graphics g)
{
    // holds y-coordinate of cards in pile
    int localy = y;

    LinkedList reverseCardList = new LinkedList();

    // get iterator for our list
    ListIterator iterator = cardList.iterator();

    // build reversed order list
    while (!iterator.atEnd())
      {
reverseCardList.add(iterator.current());
iterator.next();
      }

    // get iterator for reversed order list
    iterator = reverseCardList.iterator();

    // Go through the reversed order list
    // and draw each card in the list
    while (!iterator.atEnd())
      {
((Card)iterator.current()).draw(g, x, localy);
localy += ydist;
iterator.next();
      }
}

public void display (Graphics g)
{
    stackDisplay(g);
}
}

public class Solitaire extends Applet
{
final static int no_card_piles = 13;
final static int no_suit_piles = 4;
final static int no_table_piles = 7;

final static int topMargin      = 40;
final static int leftMargin     = 5;
final static int distTable      = 5;
final static int distSuit       = 10;

static DeckPile deckPile;
static DiscardPile discardPile;
static TablePile tableau[];
static SuitPile suitPile[];
static CardPile allPiles[];

protected void initialize()
{
    // first allocate the arrays
    allPiles = new CardPile[no_card_piles];
    suitPile = new SuitPile[no_suit_piles];
    tableau = new TablePile[no_table_piles];

    // then fill them in
    int xDeck = leftMargin + (no_table_piles - 1) * (Card.width + distTable);
    allPiles[0] = deckPile = new DeckPile(xDeck, topMargin);
    allPiles[1] = discardPile = new DiscardPile(xDeck - Card.width - distSuit,
      topMargin);

    for (int i = 0; i < no_suit_piles; i++)
      allPiles[2+i] = suitPile[i] =
new SuitPile(leftMargin + (Card.width + distSuit) * i, topMargin);

    for (int i = 0; i < no_table_piles; i++)
      allPiles[6+i] = tableau[i] =
new TablePile(leftMargin + (Card.width + distTable) * i,
        Card.height + distTable + topMargin, i+1);

    showStatus("Welcome to Solitaire!");
}

protected boolean gameEnded()
{
    if (!deckPile.empty())
      return false;

    if (!discardPile.empty())
      return false;

    for (int i = 0; i < no_table_piles; i++)
      if (!tableau[i].empty())
return false;

    // if we reached this point then all piles are empty
    // notify the user in the status bar
    showStatus("Congratulations! You have won this game.");

    return true;
}

public void init()
{
    // Create a new game button
    Button b = new Button("New game");

    // Define, instantiate, and register a listener to handle button presses
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
{ // start a new game
   initialize();
   repaint();
}
    });

    // Add the button to the applet
    add(b);

    // Define, instantiate and register a MouseListener object.
    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e)
{
   if (gameEnded())
     return;
  
   showStatus("Neat click!");

   int x = e.getX();
   int y = e.getY();

   for (int i = 0; i < no_card_piles; i++)
     if (allPiles[i].includes(x, y))
       {
  allPiles[i].select(x, y);
  repaint();
       }
}
    });

    initialize();
}

public void paint(Graphics g)
{
    for (int i = 0; i < no_card_piles; i++)
      allPiles[i].display(g);
}
}