Hi I am learning Java in school and I have been having problems making the card game war for an assignment. Here is the code, it has been divided into 4 classes, Player, Dealer, Card and Pile, and I need help completing two methods in the Player class. Thanks.
Player
public class Player {
private Pile playdeck;
private Pile winnings;
public Player(Pile pile)
{
playdeck=new Pile(52);
playdeck=pile;
winnings=new Pile(52);
}
//Removes a card from the player's play deck and returns it
//If this returns a null card, this player loses the game
public Card PlayCard()
{
Pile deck = null ;
Card card = deck.flip();
return card;
}
//Adds a pile of cards to the player's winnings
public void TakeWinnings(Pile current_winnings)
{
int size =current_winnings.getSize();
while (size >= 0 && size<52){
PlayCard();
if(size>current_winnings.getSize())
break;
}
}
}
Pile
import java.util.Random;
public class Pile {
private Card [] pile;
private int size;
public Pile(int s)
{
pile = new Card[s];
size=0;
}
public int getSize(){
return size;
}
//shifts all the cards up one spot, puts card in position 0 (top), and increments size
public void addCardToTop(Card card)
{
for(int i=size;i>0;i--)
{
pile[i]=pile[i-1];
}
pile[0]=card;
size++;
}
//puts card into position size (bottom) and increments size
public void addCardToBottom(Card card)
{
pile[size]=card;
size++;
}
//shifts all cards down one position overwriting position 0 (top) and decrements size
public void removeCardFromTop()
{
size--;
for(int i=0;i<size;i++)
{
pile[i]=pile[i+1];
}
}
//decrements size
public void removeCardFromBottom()
{
size--;
}
//returns the top card but does not remove it from the pile
public Card flip()
{
return pile[0];
}
public Card flipBottom()
{
return pile[size-1];
}
public void shuffle()
{
Random gen=new Random();
Card temp;
int x, y;
for(int i=0;i<1000;i++)
{
x=gen.nextInt(size);
y=gen.nextInt(size);
temp=pile[x];
pile[x]=pile[y];
pile[y]=temp;
}
}
public void printPile()
{
for(int i=0;i<size;i++)
{
//System.out.println(i);
pile[i].printCard();
}
}
}
Dealer
public class Dealer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pile deck= new Pile(52);
Pile p1=new Pile(52);
Pile p2=new Pile(52);
Pile current_battle=new Pile(52);
Player player1=new Player(p1);
Player player2=new Player(p2);
Card army1, army2;
int p1wins=0;
int p2wins=0;
int numWars=0;
int num_exchanges=0;
while(p1wins + p2wins <100)
{
deck = new Pile(52);
numWars=0;
num_exchanges=0;
for(int suit=1;suit<=4;suit++)
{
for(int value=1;value<=13;value++)
{
deck.addCardToBottom(new Card(value, suit));
}
}
deck.shuffle();
p1=new Pile(52);
p2=new Pile(52);
current_battle=new Pile(52);
for(int i=0;i<26;i++)
{
p1.addCardToBottom(deck.flipBottom());
deck.removeCardFromBottom();
p2.addCardToBottom(deck.flipBottom());
deck.removeCardFromBottom();
}
player1=new Player(p1);
player2=new Player(p2);
army1=player1.PlayCard();
army2=player2.PlayCard();
while(army1!=null && army2!=null)
{
/*
army1.printCard();
System.out.println("------ Versus ------");
army2.printCard();
*/
current_battle.addCardToBottom(army1);
current_battle.addCardToBottom(army2);
if(army1.GetValue()>army2.GetValue())
{
num_exchanges++;
player1.TakeWinnings(current_battle);
current_battle=new Pile(52);
}
else if(army1.GetValue()<army2.GetValue())
{
num_exchanges++;
player2.TakeWinnings(current_battle);
current_battle=new Pile(52);
}
else
{
numWars++;
for(int i=0;i<3;i++)
{
army1=player1.PlayCard();
army2=player2.PlayCard();
if(army1!=null && army2!=null)
{
current_battle.addCardToBottom(army1);
current_battle.addCardToBottom(army2);
}
}
}
army1=player1.PlayCard();
army2=player2.PlayCard();
}
//player1.printPlayer();
//player2.printPlayer();
if(army1==null && army2!=null)
{
System.out.println("--- 1WINS --- #" + (++p2wins));
System.out.println("There were "+num_exchanges+" exchanges and");
System.out.println(numWars + " Wars.");
}
else if (army2==null && army1!=null)
{
System.out.println("--- PLAYER 1 WINS --- #" + (++p1wins));
System.out.println("There were "+num_exchanges+" exchanges and");
System.out.println(numWars + " Wars.");
}
}
}
}
Card
public class Card {
private int value;
private int suit;
public Card()
{
value=0;
suit=0;
}
public Card(int v, int s)
{
value=v;
suit=s;
}
public int GetValue()
{
if(value==1)
{
return 14;
}
else
return value;
}
public int GetSuit()
{
return suit;
}
public void printCard()
{
if(this!=null)
{
System.out.print(" _____ \n");
System.out.print("| ");
if(value==1)
{
System.out.print("A ");
}
else if(value==11)
{
System.out.print("J ");
}
else if(value==12)
{
System.out.print("Q ");
}
else if(value==13)
{
System.out.print("K ");
}
else if(value==10)
{
System.out.print("10");
}
else
{
System.out.print(value + " ");
}
if(suit==1)
{
System.out.println("S |");
}
else if(suit==2)
{
System.out.println("H |");
}
else if(suit==3)
{
System.out.println("D |");
}
else
{
System.out.println("C |");
}
System.out.println(" ----- ");
}
else
{
System.out.println("----NULL CARD----");
}
}
}
Sorry if I did this wrong
Player
public class Player {
private Pile playdeck;
private Pile winnings;
public Player(Pile pile)
{
playdeck=new Pile(52);
playdeck=pile;
winnings=new Pile(52);
}
//Removes a card from the player's play deck and returns it
//If this returns a null card, this player loses the game
public Card PlayCard()
{
Pile deck = null ;
Card card = deck.flip();
return card;
}
//Adds a pile of cards to the player's winnings
public void TakeWinnings(Pile current_winnings)
{
int size =current_winnings.getSize();
while (size >= 0 && size<52){
PlayCard();
if(size>current_winnings.getSize())
break;
}
}
}
Pile
import java.util.Random;
public class Pile {
private Card [] pile;
private int size;
public Pile(int s)
{
pile = new Card[s];
size=0;
}
public int getSize(){
return size;
}
//shifts all the cards up one spot, puts card in position 0 (top), and increments size
public void addCardToTop(Card card)
{
for(int i=size;i>0;i--)
{
pile[i]=pile[i-1];
}
pile[0]=card;
size++;
}
//puts card into position size (bottom) and increments size
public void addCardToBottom(Card card)
{
pile[size]=card;
size++;
}
//shifts all cards down one position overwriting position 0 (top) and decrements size
public void removeCardFromTop()
{
size--;
for(int i=0;i<size;i++)
{
pile[i]=pile[i+1];
}
}
//decrements size
public void removeCardFromBottom()
{
size--;
}
//returns the top card but does not remove it from the pile
public Card flip()
{
return pile[0];
}
public Card flipBottom()
{
return pile[size-1];
}
public void shuffle()
{
Random gen=new Random();
Card temp;
int x, y;
for(int i=0;i<1000;i++)
{
x=gen.nextInt(size);
y=gen.nextInt(size);
temp=pile[x];
pile[x]=pile[y];
pile[y]=temp;
}
}
public void printPile()
{
for(int i=0;i<size;i++)
{
//System.out.println(i);
pile[i].printCard();
}
}
}
Dealer
public class Dealer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pile deck= new Pile(52);
Pile p1=new Pile(52);
Pile p2=new Pile(52);
Pile current_battle=new Pile(52);
Player player1=new Player(p1);
Player player2=new Player(p2);
Card army1, army2;
int p1wins=0;
int p2wins=0;
int numWars=0;
int num_exchanges=0;
while(p1wins + p2wins <100)
{
deck = new Pile(52);
numWars=0;
num_exchanges=0;
for(int suit=1;suit<=4;suit++)
{
for(int value=1;value<=13;value++)
{
deck.addCardToBottom(new Card(value, suit));
}
}
deck.shuffle();
p1=new Pile(52);
p2=new Pile(52);
current_battle=new Pile(52);
for(int i=0;i<26;i++)
{
p1.addCardToBottom(deck.flipBottom());
deck.removeCardFromBottom();
p2.addCardToBottom(deck.flipBottom());
deck.removeCardFromBottom();
}
player1=new Player(p1);
player2=new Player(p2);
army1=player1.PlayCard();
army2=player2.PlayCard();
while(army1!=null && army2!=null)
{
/*
army1.printCard();
System.out.println("------ Versus ------");
army2.printCard();
*/
current_battle.addCardToBottom(army1);
current_battle.addCardToBottom(army2);
if(army1.GetValue()>army2.GetValue())
{
num_exchanges++;
player1.TakeWinnings(current_battle);
current_battle=new Pile(52);
}
else if(army1.GetValue()<army2.GetValue())
{
num_exchanges++;
player2.TakeWinnings(current_battle);
current_battle=new Pile(52);
}
else
{
numWars++;
for(int i=0;i<3;i++)
{
army1=player1.PlayCard();
army2=player2.PlayCard();
if(army1!=null && army2!=null)
{
current_battle.addCardToBottom(army1);
current_battle.addCardToBottom(army2);
}
}
}
army1=player1.PlayCard();
army2=player2.PlayCard();
}
//player1.printPlayer();
//player2.printPlayer();
if(army1==null && army2!=null)
{
System.out.println("--- 1WINS --- #" + (++p2wins));
System.out.println("There were "+num_exchanges+" exchanges and");
System.out.println(numWars + " Wars.");
}
else if (army2==null && army1!=null)
{
System.out.println("--- PLAYER 1 WINS --- #" + (++p1wins));
System.out.println("There were "+num_exchanges+" exchanges and");
System.out.println(numWars + " Wars.");
}
}
}
}
Card
public class Card {
private int value;
private int suit;
public Card()
{
value=0;
suit=0;
}
public Card(int v, int s)
{
value=v;
suit=s;
}
public int GetValue()
{
if(value==1)
{
return 14;
}
else
return value;
}
public int GetSuit()
{
return suit;
}
public void printCard()
{
if(this!=null)
{
System.out.print(" _____ \n");
System.out.print("| ");
if(value==1)
{
System.out.print("A ");
}
else if(value==11)
{
System.out.print("J ");
}
else if(value==12)
{
System.out.print("Q ");
}
else if(value==13)
{
System.out.print("K ");
}
else if(value==10)
{
System.out.print("10");
}
else
{
System.out.print(value + " ");
}
if(suit==1)
{
System.out.println("S |");
}
else if(suit==2)
{
System.out.println("H |");
}
else if(suit==3)
{
System.out.println("D |");
}
else
{
System.out.println("C |");
}
System.out.println(" ----- ");
}
else
{
System.out.println("----NULL CARD----");
}
}
}
Sorry if I did this wrong