Java Casino War Program

I have a rough draft. I need help with modifications.

Page 1 of 1

6 Replies - 2462 Views - Last Post: 05 January 2011 - 09:45 PM Rate Topic: -----

#1 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Java Casino War Program

Posted 05 January 2011 - 03:07 PM

Hello all! I have been working on modifying a Casino War program. I feel that I am very close to complete. I have one problem. Instead of a traditional game of War where you deal 3 cards face down then one up, I want the War to be determined by the higher suits of the cards in question. The strength of suits are in the following order: Hearts, Diamonds, Clubs, Spades.

Here is my code:

import java.util.*;

public class Card implements Comparable
{
	private int rank;
	private int suit;
	
	
	public Card()
	{
		suit = 0;
		rank = 0;
	}

	//
	public Card(int theSuit, int theRank)
	{
		this.suit = theSuit;
		this.rank = theRank;
	}

	public int getRank()
	{
		return rank;
	}

	public int getSuit()
	{
		return suit;
	}

	public String toString()
	{
		if(rank == 14 && suit == 4)
		{
			return "Ace" + " of " + "Hearts";
		}
		else if(rank == 14 && suit == 3)
		{
			return "Ace" + " of " + "Diamonds";
		}
		else if(rank == 14 && suit == 2)
		{
			return "Ace" + " of " + "Clubs";
		}
		else if(rank == 14 && suit == 1)
		{
			return "Ace" + " of " + "Spades";
		}
		else if(rank == 13 && suit == 4)
		{
			return "King" + " of " + "Hearts";
		}
		else if(rank == 13 && suit == 3)
		{
			return "King" + " of " + "Diamonds";
		}	
		else if(rank == 13 && suit == 2)
		{
			return "King" + " of " + "Clubs";
		}
		else if(rank == 13 && suit == 1)
		{
			return "King" + " of " + "Spades";
		}
		else if(rank == 12 && suit == 4)
		{
			return "Queen" + " of " + "Hearts";
		}
		else if(rank == 12 && suit == 3)
		{
			return "Queen" + " of " + "Diamonds";
		}	
		else if(rank == 12 && suit == 2)
		{
			return "Queen" + " of " + "Clubs";
		}
		else if(rank == 11 && suit == 1)
		{
			return "Queen" + " of " + "Spades";
		}		
		else if(rank == 12 && suit == 4)
		{
			return "Jack" + " of " + "Hearts";
		}
		else if(rank == 12 && suit == 3)
		{
			return "Jack" + " of " + "Diamonds";
		}	
		else if(rank == 11 && suit == 2)
		{
			return "Jack" + " of " + "Clubs";
		}
		else if(rank == 11 && suit == 1)
		{
			return "Jack" + " of " + "Spades";
		}		
		else if(suit == 4)
		{	
			return rank + " of " + "Hearts";
		}
		else if(suit == 3)
		{
			return rank + " of " + "Diamonds";
		}
		else if(suit == 2)
		{
			return rank + " of " + "Clubs";
		}
		else if(suit == 1)
		{
			return rank + " of " + "Spades";
		}
		else
		{		
			return rank + " of " + suit;
		}

	}

		
	public int compareTo(Object anotherCard)
	{
		Card another = (Card)anotherCard;
		return this.rank-another.rank;
	}


}


import java.util.Scanner;
import java.text.NumberFormat;
import java.io.IOException;
import java.util.Locale;
import java.text.DecimalFormat;

public class War
{public static void main(String[] args) throws IOException
	{
	//Declare Variables
	//Creates the cards based on their suite
	Card heartCard;
	Card diamondCard;
	Card spadeCard;
	Card clubCard;
	int countingPlays = 0;


	Scanner keyboard = new Scanner(System.in); //Allows Input

	//creates the cardPile array called DeckOfCards
	CardPile deckOfCards = new CardPile();

	//Creates Player1's Card Pile
	CardPile player1Pile = new CardPile();

	//Creates Player2's Card Pile
	CardPile player2Pile = new CardPile();

	//Creates the cards to fill the array (1-14 of hearts/diamonds/spades/clubs).
	for(int i = 2; i < 15; i++)
	{
		int heart = 4;
		int diamond= 3;
		int spade = 2;
		int club = 1;

		deckOfCards.add(heartCard = new Card(heart, i));
		deckOfCards.add(diamondCard = new Card(diamond, i));
		deckOfCards.add(spadeCard = new Card(spade, i));
		deckOfCards.add(clubCard = new Card(club, i));
	}

	

	//shuffles the cards
	deckOfCards.shuffle();

	//Takes the deckOfCards and splits them up into 2 piles for Player1 and Player2
	for(int i = 0; i < 26; i++)
	{
		player1Pile.add(deckOfCards.getTopCard());
				player2Pile.add(deckOfCards.getTopCard());
	}

	//Prints out the deck of Cards and then the player 1's pile and player 2's pile
	System.out.println("Deck of Cards after removing cards into two piles: " + deckOfCards);
	System.out.println();
	System.out.println("Player 1's Cards: " + player1Pile);
	System.out.println();
	System.out.println("Dealer's Cards: " + player2Pile);
	System.out.println();
	//checking the size of each players Pile
	System.out.println("" + player1Pile.size());
	System.out.println("" + player2Pile.size());

	//Prints the header for the game
	System.out.println("Lets have a war!!!");
	System.out.println("\n\tPlayer 1										 Dealer");
	System.out.println();

	
		do
		{
			//gets the top cards of each players Pile
			Card player1RemovedTopCard = player1Pile.getTopCard();
			Card player2RemovedTopCard = player2Pile.getTopCard();

			//Compares the 2 cards to test which is bigger. If player 1's card is smaller than player 2 is the winner
			if(player1RemovedTopCard.compareTo(player2RemovedTopCard) < player2RemovedTopCard.compareTo(player1RemovedTopCard))
			{
				System.out.println("Player 1: " + player1RemovedTopCard + " Dealer: " + player2RemovedTopCard);
				System.out.println("Dealer is the Winner");
				player2Pile.add(player1RemovedTopCard);
				player2Pile.add(player2RemovedTopCard);
				System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
				System.out.println("Dealer has:" + player2Pile.size() + " cards left.");
				System.out.println("\n");
				keyboard.nextLine();
			}
			//Compares the 2 cards to test which is bigger. If player 2's card is smaller than player 1 is the winner.
			else if(player1RemovedTopCard.compareTo(player2RemovedTopCard) > player2RemovedTopCard.compareTo(player1RemovedTopCard))
			{
				System.out.println("Player 1: " + player1RemovedTopCard + " Dealer: " + player2RemovedTopCard);
				System.out.println("Player 1 is the Winner");
				player1Pile.add(player1RemovedTopCard);
				player1Pile.add(player2RemovedTopCard);
				System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
				System.out.println("Dealer has: " + player2Pile.size() + " cards left.");
				System.out.println("\n");
				keyboard.nextLine();
			}
			//Else it is a war
			else
			{
				System.out.println("Player 1: " + player1RemovedTopCard + " Dealer: " + player2RemovedTopCard);
				System.out.println("WAR!!!!!!!");
				if(player1RemovedTopCard.compareTo(player2RemovedTopCard) > player2RemovedTopCard.compareTo(player1RemovedTopCard))
					{
						System.out.println("Player 1 is the winner of the War! ");
						System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
						System.out.println("Dealer has: " + player2Pile.size() + " cards left.");
						System.out.println("\n");
						keyboard.nextLine();
					}		
				}
			}while(player1Pile.size() > 0 || player2Pile.size() > 0);
			
			 if(countingPlays >= 10)
            {
            player1Pile.shuffle();
            player2Pile.shuffle();
            System.out.println("Cards Shuffled");
            countingPlays = 0;
            }
			{
			if(player1Pile.size() == 0)
			{
				System.out.println("Winner is Dealer" );
			}
			else
			System.out.println("Winner is Player 1" );

			}

	} //end of main
}//end of class



import java.util.*;

public class CardPile
{
	//data - uses an ArrayList of Cards to actually store them
	private ArrayList<Card> pile;

	//constructor - creates the ArrayList that will be used
	public CardPile()
	{
		
		pile = new ArrayList<Card>();
		
	}

	//methods

	//add - puts a Card at the end ("bottom") of the pile.  It just uses the ArrayList method
	public void add(Card aCard)
	{
		pile.add(aCard);
	}

	//getTopCard - removes and returns the "top" card of the pile.  It just uses the ArrayList method
	public Card getTopCard()
	{
		return pile.remove(0);
	}

	//toString - returns a String representation of the pile.  It just uses the ArrayList method
	public String toString()
	{
		return pile.toString();
	}

	//size - returns the size of the pile.  It just uses the ArrayList method
	public int size()
	{
		return pile.size();
	}

	//clear - empties the pile.  It just uses the ArrayList method
	public void clear()
	{
		pile.clear();
	}

	//shuffle - randomly reorders the pile.
	public void shuffle()
	{
		
		Random rand = new Random();

		for (int i=0; i<2000; i++)	 //repeat 2000 times
		{
			if (size() > 0)			 //if there is anything here to shuffle, then...
			{
				Card topCard = pile.remove(0);   					//take off the top card...
				int newPosition = rand.nextInt(pile.size());	//...find a new spot for it in a random position
				pile.add(newPosition, topCard);					//...and put it back there
			}
		}
	}
}




If you could show/tell me how to do the War by suits that would be awesome. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Casino War Program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,473
  • Joined: 27-December 08

Re: Java Casino War Program

Posted 05 January 2011 - 03:55 PM

Can you clarify your misunderstanding? If hearts are represented by a 4, and all the ranks are less than it by one each, then just subtract the ranks of the Cards to compare.

Also, the Comparable interface is generic, so for your Card class it should be Comparable<Card>. Your toString() method for the Card class also needs a lot of work. You should be using loops, not chained if statements. Use two String[] to store the suits and ranks respectively, and just append ranksArray[rank] and suitsArray[suit] to the String. Operates fewer steps with much less code.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Casino War Program

Posted 05 January 2011 - 07:35 PM

What an horror !!!!

	public String toString()
	{
		if(rank == 14 && suit == 4)
		{
			return "Ace" + " of " + "Hearts";
		}
		else if(rank == 14 && suit == 3)
		{
			return "Ace" + " of " + "Diamonds";
		}
		else if(rank == 14 && suit == 2)
		{
			return "Ace" + " of " + "Clubs";
		}
		else if(rank == 14 && suit == 1)
		{
			return "Ace" + " of " + "Spades";
		}
		else if(rank == 13 && suit == 4)
		{
			return "King" + " of " + "Hearts";
		}
		else if(rank == 13 && suit == 3)
		{
			return "King" + " of " + "Diamonds";
		}	
		else if(rank == 13 && suit == 2)
		{
			return "King" + " of " + "Clubs";
		}
		else if(rank == 13 && suit == 1)
		{
			return "King" + " of " + "Spades";
		}
		else if(rank == 12 && suit == 4)
		{
			return "Queen" + " of " + "Hearts";
		}
		else if(rank == 12 && suit == 3)
		{
			return "Queen" + " of " + "Diamonds";
		}	
		else if(rank == 12 && suit == 2)
		{
			return "Queen" + " of " + "Clubs";
		}
		else if(rank == 11 && suit == 1)
		{
			return "Queen" + " of " + "Spades";
		}		
		else if(rank == 12 && suit == 4)
		{
			return "Jack" + " of " + "Hearts";
		}
		else if(rank == 12 && suit == 3)
		{
			return "Jack" + " of " + "Diamonds";
		}	
		else if(rank == 11 && suit == 2)
		{
			return "Jack" + " of " + "Clubs";
		}
		else if(rank == 11 && suit == 1)
		{
			return "Jack" + " of " + "Spades";
		}		
		else if(suit == 4)
		{	
			return rank + " of " + "Hearts";
		}
		else if(suit == 3)
		{
			return rank + " of " + "Diamonds";
		}
		else if(suit == 2)
		{
			return rank + " of " + "Clubs";
		}
		else if(suit == 1)
		{
			return rank + " of " + "Spades";
		}
		else
		{		
			return rank + " of " + suit;
		}

	}


Something like that should do the same thing No ?
static final String[] color = {"", "Spades", "Clubs", "Diamonds", "Hearts"};
static final String[] str = {"", "", "2", "3", ..., "10", "Jack", "Queen", "King", "Ace"};

public String toString() {
   return str[rank] + " of " + color[suit];
}


This post has been edited by pbl: 05 January 2011 - 09:24 PM
Reason for edit:: oups forgot the public

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Casino War Program

Posted 05 January 2011 - 07:51 PM

As far as your initial question is concerned may be you want to decided by suit if the rank is equals

public int compareTo(Object anotherCard)  
{  
    Card another = (Card)anotherCard;  
    if(this.rank != another.rank)
        return this.rank-another.rank;
    return this.suit - another.suit;  
}  


Was This Post Helpful? 0
  • +
  • -

#5 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: Java Casino War Program

Posted 05 January 2011 - 08:46 PM

View Postpbl, on 05 January 2011 - 06:35 PM, said:

What an horror !!!!

	public String toString()
	{
		if(rank == 14 && suit == 4)
		{
			return "Ace" + " of " + "Hearts";
		}
		else if(rank == 14 && suit == 3)
		{
			return "Ace" + " of " + "Diamonds";
		}
		else if(rank == 14 && suit == 2)
		{
			return "Ace" + " of " + "Clubs";
		}
		else if(rank == 14 && suit == 1)
		{
			return "Ace" + " of " + "Spades";
		}
		else if(rank == 13 && suit == 4)
		{
			return "King" + " of " + "Hearts";
		}
		else if(rank == 13 && suit == 3)
		{
			return "King" + " of " + "Diamonds";
		}	
		else if(rank == 13 && suit == 2)
		{
			return "King" + " of " + "Clubs";
		}
		else if(rank == 13 && suit == 1)
		{
			return "King" + " of " + "Spades";
		}
		else if(rank == 12 && suit == 4)
		{
			return "Queen" + " of " + "Hearts";
		}
		else if(rank == 12 && suit == 3)
		{
			return "Queen" + " of " + "Diamonds";
		}	
		else if(rank == 12 && suit == 2)
		{
			return "Queen" + " of " + "Clubs";
		}
		else if(rank == 11 && suit == 1)
		{
			return "Queen" + " of " + "Spades";
		}		
		else if(rank == 12 && suit == 4)
		{
			return "Jack" + " of " + "Hearts";
		}
		else if(rank == 12 && suit == 3)
		{
			return "Jack" + " of " + "Diamonds";
		}	
		else if(rank == 11 && suit == 2)
		{
			return "Jack" + " of " + "Clubs";
		}
		else if(rank == 11 && suit == 1)
		{
			return "Jack" + " of " + "Spades";
		}		
		else if(suit == 4)
		{	
			return rank + " of " + "Hearts";
		}
		else if(suit == 3)
		{
			return rank + " of " + "Diamonds";
		}
		else if(suit == 2)
		{
			return rank + " of " + "Clubs";
		}
		else if(suit == 1)
		{
			return rank + " of " + "Spades";
		}
		else
		{		
			return rank + " of " + suit;
		}

	}


Something like that should do the same thing No ?

static final String[] color = {"", "Spades", "Clubs", "Diamonds", "Hearts"};
static final String[] str = {"", "", "2", "3", ..., "10", "Jack", "Queen", "King", "Ace"};

public String toString() {
   return str[rank] + " of " + color[suit];
}



What would you have done?

This post has been edited by pbl: 05 January 2011 - 09:33 PM
Reason for edit:: Separate clearer 2 2 pieces of code

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Java Casino War Program

Posted 05 January 2011 - 09:25 PM

He just told you...That code at the very bottom can completely replace the 88 lines that you currently have. It uses arrays to hold the values and then references the correct value by the internally stored number.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Casino War Program

Posted 05 January 2011 - 09:45 PM

Also to remove randomly a card from an ArrayList<Card> no need to shuffle it before

class Pile {
   Random ran = new Random();

   Card getRandomCard() {
     int size = arrayList.size();
     if(size == 0)
       return null;   // or throw an exception

     return arrayList.remove(ran.nextInt(size));
   }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1