Computer Science 182 Data Structures and Program Design
Programming Project #3 – Link List Card Games
One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them.
Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you write will not just display all the cards on the screen, it will not reveal the cards until dealt. And please, 52-Pickup is not a valid game for this project
Students that are not strong Java programmers please do not get too ambitious, it is enough to deal Blackjack (dealer and player), hit and stand. Some basic scoring of the game is required, but keep it simple.
The cards you see on the screen will be part of a Linked List. When you deal a hand of cards, you must create your own Linked List of cards and move the dealt card(s) from the deck's linked list to the hands linked list.
Make sure you pay attention to the following guidelines for this project:
current card = list.first
while (current card is NOT null)
display current card on the screen
current card = current card.next
Here are some hints that make this project easier:
Here is a zip file with the cards of the deck:
images.zip (Links to an external site.)
You will need to download and unzip this file into the same directory as your project files in order to load card images and display them.
Here is the code you should start with.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Project3 extends JFrame implements ActionListener {
private static int winxpos=0,winypos=0; // place window here
private JButton shuffleButton,exitButton, newButton;
private CardList theDeck = null;
private JPanel northPanel;
private MyPanel centerPanel;
private static JFrame myFrame = null;
//////////// MAIN ////////////////////////
public static void main(String[] args) {
Project3 tpo = new Project3();
}
//////////// CONSTRUCTOR /////////////////////
public Project3 ()
{
myFrame = this; // need a static variable reference to a JFrame object
northPanel = new JPanel();
northPanel.setBackground(Color.white);
shuffleButton = new JButton("Shuffle");
northPanel.add(shuffleButton);
shuffleButton.addActionListener(this);
newButton = new JButton("New Deck");
northPanel.add(newButton);
newButton.addActionListener(this);
exitButton = new JButton("Exit");
northPanel.add(exitButton);
exitButton.addActionListener(this);
getContentPane().add("North",northPanel);
centerPanel = new MyPanel();
getContentPane().add("Center",centerPanel);
theDeck = new CardList(52);
setSize(800,700);
setLocation(winxpos,winypos);
setVisible(true);
}
//////////// BUTTON CLICKS ///////////////////////////
public void actionPerformed(ActionEvent e) {
if (e.getSource()== exitButton) {
dispose(); System.exit(0);
}
if (e.getSource()== shuffleButton) {
theDeck.shuffle();
repaint();
}
if (e.getSource()== newButton) {
theDeck = new CardList(52);
repaint();
}
}
// This routine will load an image into memory
//
public static Image load_picture(String fname)
{
// Create a MediaTracker to inform us when the image has
// been completely loaded.
Image image;
MediaTracker tracker = new MediaTracker(myFrame);
// getImage() returns immediately. The image is not
// actually loaded until it is first used. We use a
// MediaTracker to make sure the image is loaded
// before we try to display it.
image = myFrame.getToolkit().getImage(fname);
// Add the image to the MediaTracker so that we can wait
// for it.
tracker.addImage(image, 0);
try { tracker.waitForID(0); }
catch ( InterruptedException e) { System.err.println(e); }
if (tracker.isErrorID(0)) { image=null;}
return image;
}
// -------------- end of load_picture ---------------------------
class MyPanel extends JPanel {
//////////// PAINT ////////////////////////////////
public void paintComponent (Graphics g) {
//
int xpos = 25, ypos = 5;
if (theDeck == null) return;
Card current = theDeck.getFirstCard();
while (current!=null) {
Image tempimage = current.getCardImage();
g.drawImage(tempimage, xpos, ypos, this);
// note: tempimage member variable must be set BEFORE paint is called
xpos += 80;
if (xpos > 700) {
xpos = 25; ypos += 105;
}
current = current.getNextCard();
} //while
}
}
} // End Of class Project3
/*****************************************************************
Class Link, the base class for a link list of playing cards
May be placed in a file named Link.java
******************************************************************/
class Link {
protected Link next;
public Link getNext() { return next; }
public void setNext(Link newnext) { next = newnext; }
} // end class Link
/*****************************************************************
Class Card, the derived class each card is one object of type Card
May be placed in a file named Card.java
******************************************************************/
class Card extends Link {
private Image cardimage;
public Card (int cardnum) {
cardimage = Project3.load_picture("images/gbCard" + cardnum + ".gif");
// code ASSUMES there is an images sub-dir in your project folder
if (cardimage == null) {
System.out.println("Error - image failed to load: images/gbCard" + cardnum + ".gif");
System.exit(-1);
}
}
public Card getNextCard() {
return (Card)next;
}
public Image getCardImage() {
return cardimage;
}
} //end class Card
/*****************************************************************
Class CardList, A Linked list of playing cards
May be placed in a file named CardList.java
Note : This class can be used to create a 'hand' of cards
Just Create another CardList object, and delete cards from
'theDeck' and insert the cards into the new CardList object
******************************************************************/
class CardList {
private Card firstcard = null;
private int numcards=0;
public CardList(int num) {
numcards = num; //set numcards in the deck
for (int i = 0; i < num; i++) { // load the cards
Card temp = new Card(i);
if (firstcard != null) {
temp.setNext(firstcard);
}
firstcard = temp;
}
}
public Card getFirstCard() {
return firstcard;
}
public Card deleteCard(int cardnum) {
Card target, targetprevious;
if (cardnum > numcards)
return null; // not enough cards to delete that one
else
numcards--;
target = firstcard;
targetprevious = null;
while (cardnum-- > 0) {
targetprevious = target;
target = target.getNextCard();
if (target == null) return null; // error, card not found
}
if (targetprevious != null)
targetprevious.setNext(target.getNextCard());
else
firstcard = target.getNextCard();
return target;
}
public void insertCard(Card target) {
numcards++;
if (firstcard != null)
target.setNext(firstcard);
else
target.setNext(null);
firstcard = target;
}
public void shuffle() {
for ( int i = 0; i < 300; i++) {
int rand = (int)(Math.random() * 100) % numcards;
Card temp = deleteCard(rand);
if (temp != null) insertCard(temp);
} // end for loop
} // end shuffle
} // end class CardList
Here is the image of a card back. You can download it using a right click on the image:
Here are 52 card images. You can download them using a right click on the image or by clicking here:
You can download the 52 card images using this link: https://coc.instructure.com/courses/15862/files/1933674/download?wrap=1
The code for Blackjack is given below :
/*
This program lets the user play Blackjack. The computer
acts as the dealer. The user has a stake of $100, and
makes a bet on each game. The user can leave at any time,
or will be kicked out when he loses all the money.
House rules: The dealer hits on a total of 16 or less
and stands on a total of 17 or more. Dealer wins ties.
A new deck of cards is used for each game.
*/
public class Blackjack {
public static void main(String[] args) {
int money; // Amount of money the user has.
int bet; // Amount user bets on a game.
boolean userWins; // Did the user win the game?
TextIO.putln("Welcome to the game of blackjack.");
TextIO.putln();
money = 100; // User starts with $100.
while (true) {
TextIO.putln("You have " + money + " dollars.");
do {
TextIO.putln("How many dollars do you want to bet? (Enter 0 to end.)");
TextIO.put("? ");
bet = TextIO.getlnInt();
if (bet < 0 || bet > money)
TextIO.putln("Your answer must be between 0 and " + money + '.');
} while (bet < 0 || bet > money);
if (bet == 0)
break;
userWins = playBlackjack();
if (userWins)
money = money + bet;
else
money = money - bet;
TextIO.putln();
if (money == 0) {
TextIO.putln("Looks like you've are out of money!");
break;
}
}
TextIO.putln();
TextIO.putln("You leave with $" + money + '.');
} // end main()
static boolean playBlackjack() {
// Let the user play one game of Blackjack.
// Return true if the user wins, false if the user loses.
Deck deck; // A deck of cards. A new deck for each game.
BlackjackHand dealerHand; // The dealer's hand.
BlackjackHand userHand; // The user's hand.
deck = new Deck();
dealerHand = new BlackjackHand();
userHand = new BlackjackHand();
/* Shuffle the deck, then deal two cards to each player. */
deck.shuffle();
dealerHand.addCard( deck.dealCard() );
dealerHand.addCard( deck.dealCard() );
userHand.addCard( deck.dealCard() );
userHand.addCard( deck.dealCard() );
TextIO.putln();
TextIO.putln();
/* Check if one of the players has Blackjack (two cards totaling to 21).
The player with Blackjack wins the game. Dealer wins ties.
*/
if (dealerHand.getBlackjackValue() == 21) {
TextIO.putln("Dealer has the " + dealerHand.getCard(0)
+ " and the " + dealerHand.getCard(1) + ".");
TextIO.putln("User has the " + userHand.getCard(0)
+ " and the " + userHand.getCard(1) + ".");
TextIO.putln();
TextIO.putln("Dealer has Blackjack. Dealer wins.");
return false;
}
if (userHand.getBlackjackValue() == 21) {
TextIO.putln("Dealer has the " + dealerHand.getCard(0)
+ " and the " + dealerHand.getCard(1) + ".");
TextIO.putln("User has the " + userHand.getCard(0)
+ " and the " + userHand.getCard(1) + ".");
TextIO.putln();
TextIO.putln("You have Blackjack. You win.");
return true;
}
/* If neither player has Blackjack, play the game. First the user
gets a chance to draw cards (i.e., to "Hit"). The while loop ends
when the user chooses to "Stand". If the user goes over 21,
the user loses immediately.
*/
while (true) {
/* Display user's cards, and let user decide to Hit or Stand. */
TextIO.putln();
TextIO.putln();
TextIO.putln("Your cards are:");
for ( int i = 0; i < userHand.getCardCount(); i++ )
TextIO.putln(" " + userHand.getCard(i));
TextIO.putln("Your total is " + userHand.getBlackjackValue());
TextIO.putln();
TextIO.putln("Dealer is showing the " + dealerHand.getCard(0));
TextIO.putln();
TextIO.put("Hit (H) or Stand (S)? ");
char userAction; // User's response, 'H' or 'S'.
do {
userAction = Character.toUpperCase( TextIO.getlnChar() );
if (userAction != 'H' && userAction != 'S')
TextIO.put("Please respond H or S: ");
} while (userAction != 'H' && userAction != 'S');
/* If the user Hits, the user gets a card. If the user Stands,
the loop ends (and it's the dealer's turn to draw cards).
*/
if ( userAction == 'S' ) {
// Loop ends; user is done taking cards.
break;
}
else { // userAction is 'H'. Give the user a card.
// If the user goes over 21, the user loses.
Card newCard = deck.dealCard();
userHand.addCard(newCard);
TextIO.putln();
TextIO.putln("User hits.");
TextIO.putln("Your card is the " + newCard);
TextIO.putln("Your total is now " + userHand.getBlackjackValue());
if (userHand.getBlackjackValue() > 21) {
TextIO.putln();
TextIO.putln("You busted by going over 21. You lose.");
TextIO.putln("Dealer's other card was the "
+ dealerHand.getCard(1));
return false;
}
}
} // end while loop
/* If we get to this point, the user has Stood with 21 or less. Now, it's
the dealer's chance to draw. Dealer draws cards until the dealer's
total is > 16. If dealer goes over 21, the dealer loses.
*/
TextIO.putln();
TextIO.putln("User stands.");
TextIO.putln("Dealer's cards are");
TextIO.putln(" " + dealerHand.getCard(0));
TextIO.putln(" " + dealerHand.getCard(1));
while (dealerHand.getBlackjackValue() <= 16) {
Card newCard = deck.dealCard();
TextIO.putln("Dealer hits and gets the " + newCard);
dealerHand.addCard(newCard);
if (dealerHand.getBlackjackValue() > 21) {
TextIO.putln();
TextIO.putln("Dealer busted by going over 21. You win.");
return true;
}
}
TextIO.putln("Dealer's total is " + dealerHand.getBlackjackValue());
/* If we get to this point, both players have 21 or less. We
can determine the winner by comparing the values of their hands. */
TextIO.putln();
if (dealerHand.getBlackjackValue() == userHand.getBlackjackValue()) {
TextIO.putln("Dealer wins on a tie. You lose.");
return false;
}
else if (dealerHand.getBlackjackValue() > userHand.getBlackjackValue()) {
TextIO.putln("Dealer wins, " + dealerHand.getBlackjackValue()
+ " points to " + userHand.getBlackjackValue() + ".");
return false;
}
else {
TextIO.putln("You win, " + userHand.getBlackjackValue()
+ " points to " + dealerHand.getBlackjackValue() + ".");
return true;
}
} // end playBlackjack()
} // end class Blackjack


Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games...
Java BlackJack Game:
Help with 1-4 using the provided code below
Source code for Project3.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Project3 extends JFrame implements ActionListener
{
private static int winxpos = 0, winypos = 0; // place window
here
private JButton exitButton, hitButton, stayButton, dealButton,
newGameButton;
private CardList theDeck = null;
private JPanel northPanel;
private MyPanel centerPanel;
private static JFrame myFrame = null;
private CardList playerHand = new CardList(0);
private CardList dealerHand = new CardList(0);...
Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card { private final String face; // face of card ("Ace", "Deuce", ...) private final String suit; // suit of card ("Hearts", "Diamonds", ...) // two-argument constructor initializes card's face and suit public...
NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...
HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...
HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...
JAVAFX ONLY PROGRAM!!!!!
SORTING WITH NESTED CLASSES AND LAMBDA
EXPRESSIONS.
DIRECTIONS ARE BELOW:
DIRECTIONS:
The main point of the exercise is to demonstrate your
ability to use various types of nested classes. Of course, sorting
is important as well, but you don’t really need to do much more
than create the class that does the comparison. In general, I like
giving you some latitude in how you design and implement your
projects. However, for this assignment, each piece is very...
(c++please,thank you!!)You'll implement is the Card class, which represents a single playing card: class Card { private: int rank; // Should be in the range 0-12. int suit; // Should be in the range 0-3. public: // constructors, destructor, accessors, and mutators }; You'll need to implement the needed constructors, destructors, accessor functions, and mutator methods. To do this, think carefully about what operations (if any) you’ll need to perform on an individual card and what information you’ll need to...
Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler { /** * The number of consecutive shuffle steps to be performed in each call * to each sorting...
In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...
CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As needed Recursion OBJECTIVES Understand linked lists and sorting concepts and the syntax specific to implementing those concepts in Java. PROJECT The final objective of this project is to create a multi-player card game, but you will create your classes in the order given, as specified. (Analogy: you must have a solid foundation before you can build a house). In order to allow creative freedom,...