Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help on war card game project

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help on war card game project

    Im having a brain block and I have no clue what to do next. Im not asking for someone to write code for me, im asking for someone to give me an idea or suggestion so I know what to write next. Im pretty confused. Below are my classes related to the card game war. The rules of war are below.

    1) Each player is dealt 26 cards to unplayed pile.
    2) Repeat steps 3 through 5 until one or both unplayed piles become empty
    3)each player plays the topmost card from his unplayed pile by placing it face up on his war pile.
    4. If cards have same rank, repeat step 2.
    5.Otherwise, move both war piles to the winning pile of the player who has the card of a higher rank at the top of war pile.
    6.The player with the largest winning pile wins.
    There are 3 piles unplayed pile ,war pile, winning pile

    Deck
    import java.util.*;
    public class Deck {
     
    	public static final int MAX_SIZE = 52;
    	private static ArrayList<Card> cards;
    	public Deck(){
    		reset();
     
    	}
    	private void reset() {
    		cards = new ArrayList<Card>();
    		addSuit(Suit.spade);
    		addSuit(Suit.heart);
    		addSuit(Suit.diamond);
    		addSuit(Suit.club);
     
    	}	
     
    	private void addSuit(Suit suit){
    		for (int i = 1; i <= 13; i++)
    			cards.add(new Card(suit, i));
    	}
    	public boolean isEmpty(){
    		return cards.isEmpty();
     
    	}
     
    	//methods
     
    	//add - puts a Card at the end ("bottom") of the pile.  It just uses the ArrayList method
    	public void add(Card aCard)
    	{
    		cards.add(aCard);
    	}
    	public void clear()
    	{
    		cards.clear();
    	}
     
    //getTopCard - removes and returns the "top" card of the pile.  It just uses the ArrayList method
    public static Card getTopCard()
    {
    	return cards.remove(0);
    }
    	public int size(){
    		return cards.size();
    	}
    	public Card deal(){
    		if( isEmpty())
    			return null;
    		else 
    			return cards.remove(cards.size() -1);
     
     
    		}
    	public Card[] deal(int number){
    		if (number > cards.size())
    			return null;
    			else{
    				Card[] hand = new Card[number];
    				for(int i = 0; i < hand.length; i++)
    					hand[i] = deal();
    					return hand;
    			}
     
    		}
     
    	public void shuffle(){
    	if (cards.size() < MAX_SIZE)
    		return;
    	Random gen = new Random();
    	Card[] array = new Card[MAX_SIZE];
    	while (cards.size() > 0){
    		Card card = cards.remove(cards.size() - 1);
    		int i = gen.nextInt(MAX_SIZE);
    		while (array[i] != null)
    			i = gen.nextInt(MAX_SIZE);
    			array[i] = card;
     
    	}
    	for (Card card : array)
    		cards.add(card);
    	}
    	public String toString(){
    		String result = "";
    		for(Card card : cards)
    			result += card + "\n";
    		return result;
     
     
    	}
     
    }

    Card
    public class Card implements Comparable{
    	private Suit suit;
    	private int rank;
    	private boolean faceUp;
     
    	public Card(Suit suit, int rank){
    		this.suit = suit;
    		this.rank = rank;
    		faceUp = false;
     
    	}
     
    	public Card(int i, char rank2) {
    		// TODO Auto-generated constructor stub
    	}
     
    	public boolean equals(Object other){
    		if ( this == other)
    			return true;
    		else if (! (other instanceof Card ))
    			return false;
    		else{
    			Card otherCard = (Card)other;
    			return rank == otherCard.rank;
    		}
    	}
    	public int compareTo(Object other){
    		if (! (other instanceof Card))
    			throw new IllegalArgumentException("Parameter must be a card ");
    		Card otherCard = (Card)other;
    		return rank - otherCard.rank;
     
     
     
    	}
     
    	public int getRank(){
    		return rank;
     
    	}
    	public Suit getSuit(){
    		return suit;
    	}
    	public boolean isFaceUp(){
    		return faceUp;
    	}
     
    	public boolean isRed(){
    		return suit == Suit.heart || suit == Suit.diamond;
     
    	}
    	public void turn(){
    		faceUp = ! faceUp;
    	}
    	public String toString(){
    		return rankToString() + " of " + suit;
    	}
    	private String rankToString(){
    		if ( rank == 1)
    			return "Ace";
    			else if (rank == 11)
    				return "Jack";
    			else if (rank == 12)
    				return "queen";
    			else if (rank == 13)
    				return "king";
    			else
    				return "" + rank;
    	}}

    Suit
    public class Suit implements Comparable{
    	static public final Suit spade = new Suit(4, "spades");
    	static public final Suit heart = new Suit(3, "hearts");
    	static public final Suit diamond = new Suit(2, "diamonds");
    	static public final Suit club = new Suit(1, "clubs");
     
    	private int order;
    	private String name;
    	private Suit(int ord, String nm){
    		name = nm;
    		order = ord;
    	}
    	public int compareTo(Object other){
    		if(! (other instanceof Suit))
    			throw new IllegalArgumentException("Parameter must be a Suit");
    		Suit otherSuit = (Suit)other;
    		return order - otherSuit.order;
    	}
    public String toString(){
    return name;
    }
     
    }

    And heres what i have so far

    import java.util.*;
    public class War{
     
    		public static void main	(String args[]){
    			{
     
    			int halfDeck;
     
    			Scanner scanner = new Scanner (System.in);
     
    			Deck unplayedpile = new Deck();
    			Deck deck1 = new Deck();
    			Deck deck2 = new Deck();
     
    		unplayedpile.shuffle();
     
    		unplayedpile.add(Deck.getTopCard());
     
    		Card card1 = unplayedpile.getTopCard();
    	    Card card2 = unplayedpile.getTopCard();
     
     
     
    		System.out.println(card1);
    		System.out.println(card2);
     
    		}}


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,537
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: help on war card game project

    Do you a specific question?

    Quote Originally Posted by sc0field1 View Post
    so I know what to write next.
    If this is your question in disguise then my answer is:

    - a best selling novel
    - a cheque to me for $500
    - a letter apologising to your mother
    - your name in the snow

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: help on war card game project

    What is your problem exactly?

    You need to break the problem down for us so we can take things one step at a time.

    What part are you stuck on and are there any errors etc?!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Credit card validator
    By SOK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 03:11 AM
  2. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  3. Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2010, 09:37 AM
  4. my programs- Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2010, 07:06 PM
  5. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 09:16 AM