Help with creating the card game war - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help with creating the card game war

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;iarmy2.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(" _____
");
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
Sign In or Register to comment.