Java Beginners

Moderators: zibadian
Number of threads: 1285
Number of posts: 2739

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with creating the card game war Posted by cgrant1995 on 11 Oct 2012 at 10:22 AM
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
Thread Tree
cgrant1995 Help with creating the card game war on 11 Oct 2012 at 10:22 AM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.