Overview This assignment will give you more experience working with and using cl
ID: 3873855 • Letter: O
Question
Overview This assignment will give you more experience working with and using classes. It will also give you more practice with methods and arrays. You are part of a small video game development team that is trying to program the card game WAR. You just know this is going to be the next "killer app" that will sell at least 1,000,000 copies, but you still need to program several of its components. One of these components is the Deck class. You have been given a complete class called Card, but you still need to finish writing the Deck class.Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
import java.util.Random;
public class Deck {
private Card[] deck;
private int top;
public Deck()
{
deck = new Card[52];//deck with 52 cards....
top = 0;//first element is top in the deck
int i=0;
while(i<52)
{
if(i<13)
{
deck[i]=new Card((i%13)+1,'D');//generating diamond suit
}
else if(i<26)
{
deck[i]=new Card((i%13)+1,'S');//generating spade suit
}
else if(i<39)
{
deck[i]=new Card((i%13)+1,'H');////generating heart suit
}
else
{
deck[i]=new Card((i%13)+1,'C');//generating clubs suit
}
i++;
}
}
public void Shuffle()
{
Random r = new Random();
int n;
int i=0;
while(i<52)//shuffling randomly...52 cards....
{
n =r.nextInt()%52;//genrating random position to swap
//swapping
swap(i,n);
i++;
}
}
public Card draw()
{
//returning top card in deck
return deck[top];
}
public boolean isEmpty()
{
if(deck.length==0)return true;//if empty
else return false;//if not empty
}
public void swap(int i,int n)
{
//swapping
Card k = deck[i];
deck[i]=deck[n];
deck[n]=k;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.