Question: write JAVA code and check the codes below There is a card game entitled The Great Dalmuti. This is a variation of a popular card

write JAVA code and check the codes below

There is a card game entitled The Great Dalmuti. This is a variation of a popular card game that dates back to the Middle-Ages. The Great Dalmuti uses a non-standard 80-card deck. The deck consists of 13 types of cards. Each type has a different rank. The table below illustrates how many of each type of card exists in a Dalmuti deck. For this assignment, you do not need to know how to play the game. However, if you are interested, you can find descriptions online.

1. You will create a DalmutiCard class which represents a single card from a Dalmuti deck. Details are on the second page. Good news: This is the smallest part of the game to code! 2. David will create a DalmutiDeck class. It creates an array of 80 DalmutiCard objects. This forms the Dalmuti deck. Good news: David has completed his part and given it to you. You will not have to complete coding for this class. You will find a copy of DalmutiDeck.java in the same ZIP file that contained this PDF programming assignment. You are to use DalmutiDeck.java but you are not permitted to change it. 3. Angela will create a CardGame class that plays the game. Good news: Angela has completed her part and given it to you to use. You will not have to complete any coding for this class. 4. At this point, all your team wants to test is creating the card deck, shuffle the cards, and deal the cards to the players (4-8 players). For this assignment, your team will not test the entire game. 5. After your part is ready to test, you should simply a. Compile DalmutiCard this what you write b. Compile, DalmutiDeck - this is what Angela gave you (in the ZIP file) c. Compile CardGame this is what David gave you (in the ZIP file) d. Run CardGame Any errors you have will be a result of your work. Do not change the other classes. If you have questions, email your instructor. Like many real-world examples when programmers work in teams, you need to follow program specifications so that classes work together. Here are the specifications for your DalmutiCard class: Below is a UML Class Diagram for the DalmutiCard class CardGame DalmutiCard Main (args: String[ ]): void - cardName: String - cardRank: int + getCardName( ): String + getCardRank( ): int + setCard(int rank): void + toString( ): String ( + represents the public modifier and represents the private modifier ) 1. The name of the Java class must be DalmutiCard and saved in a file named DalmutiCard.java. 2. Create two instance variables (also called fields or data members). Use the names shown in the UML class diagram (also in bold below): a. cardName a string to hold the description of a single card (i.e. Dalmuti, Archbishop, etc.) b. cardRank an integer to hold the cards rank (a value from 1-13) 3. Create a constructor with 1 argument (the rank of the card you are creating -- an integer value from 1-13) a. Constructors are not typically illustrated in a UML Class Diagram. Constructor names MUST match the name of the class (DalmutiCard in this case). Your constructor will have one parameter the rank of the card to create. b. Make sure the parameter passed in has a value between 1 and 13. If it does, use the parameter value to update cardRank. Also, update cardName with an appropriate description (using the table on page 1). If the parameter value passed in was invalid, assign 0 to cardRank and Unknown to cardName. 4. Create an accessor method (also called a getter) for each instance variable. Use the method names in bold below (and as shown in the UML diagram): a. getCardRank() returns the current value of cardRank b. getCardName() returns the current value of cardName 5. Create a mutator method (also called a setter) called setCard( ). As indicated in the UML Class diagram on the previous page, this method has one integer argument which represents a new card rank. Like the constructor, validate the parameter passed in. If valid, update cardRank and cardName. If invalid, do not update anything. FOR EXTRA CREDIT: After you have completed this method, look for duplicate code in the constructor and this method and eliminate duplication. 6. Create a toString() method which returns a nicely formatted string of data from the DalmutiCard class. Sample output might be something like the examples below: 1 Dalmuti 5 Abbess 13 Jester

cardGame.java

import java.util.Scanner;

public class CardGame

{

public static void main (String[] args)

{

Scanner in = new Scanner (System.in);

//-----------------------------------------------------------------------------------------------

// Declarations and Instantiations

//-----------------------------------------------------------------------------------------------

DalmutiDeck newDeck = new DalmutiDeck();

int numberOfPlayers = 0;

int numberOfCardsInDeck = newDeck.getNumberOfCards();

int playerIndex = 0;

int cardIndex = 0;

//-----------------------------------------------------------------------------------------------

// Shuffle the card deck and input the number of players

//-----------------------------------------------------------------------------------------------

newDeck.shuffle();

while(numberOfPlayers < 4 || numberOfPlayers > 8)

{ System.out.print("Number of players for the game (enter a number between 4-8): " );

numberOfPlayers = in.nextInt();

}

//-----------------------------------------------------------------------------------------------

// Create an array which will hold the number of cards number cards each player is dealt.

// Depending upon the number of players, every player may not have the same number of cards.

//

// For the value of maxCardsPerPlayer, 1 is added to accommodate integer division and truncation

// For example, 80/3 players = 26. 26*3 = 78. Two cards need to still be dealt. So, two players

// will receive 27 cards. The +1 handles the fact the some players will get 80/3 + 1 cards.

//-----------------------------------------------------------------------------------------------

int[] playerCardCount = new int[numberOfPlayers];

for (int i = 0; i < numberOfPlayers; i++)

playerCardCount[i] = 0;

int maxCardsPerPlayer = numberOfCardsInDeck/numberOfPlayers + 1;

//-----------------------------------------------------------------------------------------------

// Create two-dimensional array to hold cards dealt to players.

// Rows represent cards (1st card, 2nd card, etc.) and columns represent players (1st player, etc).

//-----------------------------------------------------------------------------------------------

DalmutiCard[][] players = new DalmutiCard[maxCardsPerPlayer][numberOfPlayers];

//-----------------------------------------------------------------------------------------------

// Loop to deal the cards from the deck

//-----------------------------------------------------------------------------------------------

for (int n = 0; n < numberOfCardsInDeck; n++)

{ players[cardIndex][playerIndex] = new DalmutiCard(newDeck.dealOneCard().getCardRank());

playerCardCount[playerIndex] = cardIndex;

if (playerIndex < numberOfPlayers-1)

playerIndex++;

else

{ playerIndex = 0;

cardIndex++;

}

}

//-----------------------------------------------------------------------------------------------

// Loop to display each players cards.

//-----------------------------------------------------------------------------------------------

for (int i = 0; i < numberOfPlayers; i++)

{ System.out.println("Cards for player " + (i+1) + ":");

for (int j = 0; j <= playerCardCount[i]; j++)

{ System.out.println("\t" + players[j][i].toString());

}

}

}

}

DalmutiDeck.java

import java.util.Random;

final public class DalmutiDeck

{

//***********************************************************************************

// Instance variables

// Constants are defined for numeric literals which will be used

// Notice several constants are declared as static so that only one copy of these

// static variables are kept in memory for all instances of a card deck in lieu of

// one copy for each deck.

//

// deck is an array of Dalmuti Cards (80 cards)

// cardsNotDealt is an integer which tracks the number of cards undealt

//***********************************************************************************

private static final int NUMBER_OF_CARDS = 80;

private static final int NUMBER_OF_RANKS = 13;

private static final int NUMBER_OF_JESTERS = 2;

private static final int JESTER_RANK = 13;

private DalmutiCard[] deck = new DalmutiCard[NUMBER_OF_CARDS];

private int cardsNotDealt;

//***********************************************************************************

// Constructor

// It loads the array of the proper Dalmuti card. For example:

// There are 2 Archbishop cards which have a rank of 2

// (next the the highest card in the deck)

// There are 11 Stonecutter cards which have a rank of 11

// There are 2 Jester cards which have a rank of 13

// (worst cards in the deck)

// NOTE: In The Great Dalmuti card game, ranks with a lower number are better.

//***********************************************************************************

public DalmutiDeck()

{

//--------------------------------------

// Default constructor.

//--------------------------------------

int cardIndex = 0;

cardsNotDealt = NUMBER_OF_CARDS;

for (int i = 1; i < NUMBER_OF_RANKS; i++)

for (int j = 1; j <= i; j++)

{ deck[cardIndex] = new DalmutiCard(i);

cardIndex++;

}

for (int i = 1; i <= NUMBER_OF_JESTERS; i++)

{ deck[cardIndex] = new DalmutiCard(JESTER_RANK);

cardIndex++;

}

}

//***********************************************************************************

// Mutator to simulate shuffling the cards. This is done by randomly swapping

// cards in the deck.

//***********************************************************************************

public void shuffle()

{ int swapIndex;

DalmutiCard temp;

Random gen = new Random();

for (int i = 0; i < NUMBER_OF_CARDS; i++)

{

swapIndex = gen.nextInt(NUMBER_OF_CARDS) ;

temp = new DalmutiCard(deck[i].getCardRank());

deck[i] = deck[swapIndex];

deck[swapIndex] = temp;

}

cardsNotDealt = NUMBER_OF_CARDS;

}

//***********************************************************************************

// Accessors for other classes to be able to retrieve the number of cards in the deck

// and the number of cards yet to be dealt.

//***********************************************************************************

public int getNumberOfCards()

{ return NUMBER_OF_CARDS; }

public int getNumberOfCardsNotDealt()

{ return cardsNotDealt; }

//***********************************************************************************

// Method to determine if there is another card to be dealt

//***********************************************************************************

public DalmutiCard dealOneCard()

{ DalmutiCard nextCard = null;

if (cardsNotDealt > 0)

{ nextCard = deck[NUMBER_OF_CARDS-cardsNotDealt];

cardsNotDealt--;

}

return nextCard;

}

//***********************************************************************************

// toString Method

//***********************************************************************************

public String toString()

{ String deckString = "";

for (int i = 0; i < NUMBER_OF_CARDS; i++)

deckString += i + " " + deck[i].toString() + " ";

return (deckString);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!