Goal

The goal of this project is to apply the concepts of Inheritance. The abstract class will help us implement classes (Card and XOPiece) that can be used interchangeably! To begin, create a project2 folder. Within this folder, also create partA, partB and partC folders. Make sure to store all files in the correct subfolder.

PART A. -- CREATING A DECK OF CARDS

Create a Card class that represents a playing card with a face value and a suit. Modify the skeleton file below.

public class Card extends Token
{

/** These are the unicode characters to display the typical suits of a playing card */
private static final char spades = '\u2660';
private static final char hearts = '\u2661';
private static final char diamonds = '\u2662';
private static final char clubs = '\u2663';

public static enum Suit {SPADES, HEARTS, DIAMONDS, CLUBS}
public static enum Face {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}

private Suit mySuit;
private Face myFace;

/**
* Constructor that creates an empty Card
* Instance data should be initialized to null
*/
public Card()
{
/* FIX ME */
}

/**
* Constructor that inializes all
* all instance data to proper values.
*/
public Card(Suit s, Face f)
{
/* FIX ME */
}

/* ADD OTHER METHODS YOU NEED */
}

Notice that the Card class is a subclass of Token class. Notice that Token is an abstract class that is provided below.

public abstract class Token
{
protected String tokenValue;

/* Have the derived class implement this method
* to display the value of the object
* using a 3 character-long String.
* i.e. what you want shown as output on screen
*/
public abstract String toString();

public boolean match(Token other)
{
return this.tokenValue.equals(other.tokenValue);
}
}

Then create a Deck class that represents a Deck of Cards.

import java.util.*;

/**
* A Deck class represents a complete deck of Cards
*/
public class Deck
{
protected ArrayList deck;

/** Constructor creates a group of 52 cards using the Card's
* enumerated types of face and suits
*/
public Deck()
{
/* FIX ME */
}

/** Constructor creates a group of 13 cards
* that all belong to one suit
*/
public Deck(Card.Suit s)
{
/* FIX ME */
}

/**
* toString method:
* @overrides toString method to display all 52 cards
* Print out the Deck of Cards
* 13 cards per line please.
*/
public String toString()
{
/* FIX ME */
return null;
}

/**
* getCard method:
* Get a card from the Deck at a specific location
*/
public Card getCard(int index)
{
/* FIX ME */
return null;
}

/**
* shuffle method:
* Randomizes the order of the stored cards
* One easy way to shuffle is to loop through the
* cards and randomly swap each card with another one.
*/
public void shuffle()
{
/* FIX ME */
}

/**
* deal method:
* removes the top Card from the deck
*/
public Card deal()
{
/* FIX ME */
return null;
}

/**
* getCardCount
* determines how many cards are left on the deck.
*/
public int getCardCount()
{
/* FIX ME */
return 0;
}
}

Now make sure that all the code so far works with the driver program, Dealer.

/** Driver program that creates a complete Deck
* of 52 cards, shuffles the cards and deals one
* card at a time until the deck is empty
*/
public class Dealer
{
public static void main(String[] args)
{
Deck d = new Deck();
System.out.println(d);

d.shuffle();
System.out.println(d);

Card c;
int count = 0;
while (d.getCardCount() > 0)
{
c = d.deal();
System.out.println("#" + (count+1)+" Dealt: " + c);
count++;
}
}
}

Here is a sample output: see image.

NOTE:

  • The output continues until all cards have been dealt
  • Notice how the TEN card of any suit is printed out with a 't'.
  • At this point, no need to worry about the inherited tokenValue for the Card class. You may set it to the face or suit value.

PART B -- TICTACTOE

We now want to create the TicTacToe game! To do this, we need 4 classes - TicTacToePlayer, XOPiece, SquareBoard and TicTacToeGame.

The XOPiece class will again be a subclass of the Token class. This class is responsible for being either an X or an O on the grid/board of the game. Create two constructors, which both use the inherited tokenValue variable to hold what you want printed to screen.

public class XOPiece extends Token
{
/** enumerated type that dictates the two values of
* tic tac toe
*/
public static enum XO {X, O}

/** Constructor to hold an empty piece
* that is neither X or O. An empty
* piece should just display 3 spaces
*/
public XOPiece()
{
/*FIX ME*/
}

/** Constructor that creates a piece for
* the TicTacToe board -- either an X or O
* See sample output.
*/
public XOPiece(XO piece)
{
/*FIX ME*/
}

public String toString()
{
return tokenValue;
}
}

Now create the SquareBoard class. A board is an n x n structure that contains Tokens.

public class SquareBoard {
private int size;
private Token[][] board;

/**
* Constructor that creates an n x n board of
* Tokens and each Token is set to an initial Token
*/
public SquareBoard(int size, Token e)
{
/* FIX ME */
}

/**
* String representation of the board
* showing the contents of each space
*/
public String toString()
{
/* FIX ME */
return null;
}

/**
* Method that prints out the board with
* the coordinates of each slot
* to show the users how to identify each slot
*/
public void printBoardCoordinates()
{
for (int i=0; i {
for (int j=0; j {
System.out.print("["+i+","+j+"] ");
}
System.out.println();
}
System.out.println();
}

/**
* Sets a Token on the board with coordinates (i,j)
*/
public boolean setPiece(Token t, int i, int j)
{
/* FIX ME */
return true;
}

/**
* Gets a Token that is on the board with coordinates (i,j)
*/
public Token getPiece(int i, int j)
{
/* FIX ME */
return null;
}
}

Create a TicTacToePlayer class by creating a subclass of the Player class. Here is the complete Player class.

/**
* Class that represents a Player in a game
*/
public class Player {
private String name;
private int wins;
private int games;

public Player(String name) {
this.name = name;
wins = 0;
games = 0;

}

/**
* Returns the number of times the Player won.
*/
public int getWins() {
return this.wins;
}

/**
* Setter method for the Player's name
*/
public void setName(String name) {
this.name = name;
}

/**
* Getter method for the Player's name
*/
public String getName() {
return name;
}

/**
* Updates the player's total number of games played
*/
public void lost() {
games++;
}

/**
* Updates the player's number of wins
*/
public void won() {
System.out.println(this.name + " wins!");
wins++;
games++;
}

/**
* String representation of a player
*/
public String toString() {
return "\n" + name + ": " +
"wins: " + wins + "\n\tout of : " + games + "games\n";
}
}

In your new TicTacToePlayer class, add a new instance data -- an XOPiece. This way, each TicTacToePlayer will be assigned either an XOPiece that represents an X, while the other Player will be assigned an XOPiece that is O. Don't forget the getter and setter method for this new instance data.

Now you ready to implement the TicTacToeGame by putting all the different parts together and build the logic of the game! Below is a skeleton class.

  • Welcome and explain the game to the 2 users who will share the same terminal to play.
  • Ask each player for their name.
  • Assign an XOPiece to each TicTacToePlayer.
  • Display the board with coordinates for the players to understand how to identify each spot on the board.
  • While keeping track of whose turn it is, ask the current player for coordinates on the board to put down their piece.
  • Implement a method called checkWin to check if anybody has won ( i.e. the Tokens in a row, column or diagonal all match). Make sure that the inherited match method (of the Token class) is invoked in checkWin when comparing elements on the board. Try to make the logic as generic as possible by using loops in your code. (Do not hard code specific elements from your 2d array -- ex. board[0][0].match(board[1][1]). Instead, use code like board[[i][j].match(board[k][l] ).
public class TicTacToe
{

public static void main(String[] args)
{

SquareBoard board = new SquareBoard(3, new XOPiece());
board.printBoardCoordinates();
System.out.println(board);

}

}



/* Here is the output for printBoardCoordinates
* and an empty board
* followed by a sample outcome of a finished game
*/

Figure: see image.

PART C -- THE MASH UP

Now let's create something that uses the classes from Part A and B ... by creating Mashup.java! This game is similar to TicTacToe but with a few key differences.

  • The board is 4x4 and not 3x3 in size.
  • The Mashup player owns a group of cards that all have the same suit (instead of a XOPiece). You decide which suit per player. Shuffle these cards.
  • The Mashup player puts down one card on the board at each turn.
  • If a Mashup Player completes a row, column or diagonal of 4 cards ( all of the same suit ) from their deck, they win!

This means that you're Mashup code will be very similar to your TicTacToe class! Yay! However, you make a MashupPlayer class to replace the TicTacToePlayer class. And you will have to check a bigger board instead of the 3x3. (Hmmm ... It's like the game Connect 4 now actually.) For the checkWin method to work like before, make sure the tokenValue of your Card class is set appropriately for the match method to work. We are matching Card suits and not Card faces.

See a sample result of a Mashup Game, where the player with the spades cards wins!

Figure: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.