Jul 24, 2014

Card , Deck , Hand Class In Poker Game Using Java

This Tutorial Is Focused On Initial Development Of  A Poker Game Using Java. Initially You Need Three Java Classes.Which Need To Store Card Details, Card Deck Details and Player Hand Details.

1. Card Class

This Class Contain The Information About A Card.Such As Card Index Which Vary From 1-52(Altogether Their Are 52 Playing Cards In The Pack), Card Suit Which Vary From 1-4(Their Are Four Suits In The Card Pack) and Card Value Which Vary From 2-14.

Also This Class Contain Few Methods To Get The Card Index , Card Suit and Card Value.This Class Should Have A Method To Create A New Card.In This Example I Have Used The Parameter Constructor To Create A New Card Objects. 


public class Card{

   private int CardIndex;//Vary From 0 to 51
   private int CardSuit;//Vary From 1 to 4 ( 1=Spades, 2=Hearts, 3=Clubs, 4=Diamonds)
   private int CardValue;//Vary From 2 to 14 ( 11=Jack, 12=Queen, 13=King, 14=Ace)

   public Card(){
   }

   public Card(int CardIndex,int CardSuit,int CardValue){
      this.CardIndex = CardIndex;
      this.CardSuit  = CardSuit;
      this.CardValue = CardValue;
   }

   public int getCardIndex(){
      return CardIndex;
   }

   public int getCardSuit(){
      return CardSuit;
   }

   public int getCardValue(){
      return CardValue;
   }

}

2.Deck Class

This Class Contains Set Of Card Objects.Initially This Class Contains All The 52 Card Objects.In A Game This Class Contains Current Card Objects Available In The Deck.To Store The Card Objects This Class Has An Array List Of Card Type.

Initially When The Game Is Starting, Deck Class Should Have All The 52 Card Objects. In This Class We Are Using Default Constructor To Create The Deck With 52 Card Objects.

Also This Class Should Have Few Methods To Shuffle The Deck , Get The Deck Size , Deal A Card From Deck and Add A Card To Deck.

import java.util.ArrayList;
import java.util.Collections;

public class Deck {

   private ArrayList<Card> deck = new ArrayList<Card>();//Cards Available In The Deck

       //Creating The Card Pack
   public Deck() { 
      int iIndex = 1;
      for (int iSuit = 1; iSuit < 5; iSuit++) { // 1=Spades, 2=Hearts, 3=Clubs, 4=Diamonds
         for(int iValue = 2; iValue < 15; iValue++ ) { //11=Jack, 12=Queen, 13=King, 14=Ace
            deck.add(new Card(iIndex,iSuit,iValue));
            iIndex++;
         }
      }
   }

   //Shuffles The Deck
   public void shuffle() {
      Collections.shuffle(deck);
   }

   //Get The Deck Size
   public int getDeckSize() {
      return deck.size();
   }

   //Deals(Return & Remove) A Card From The Top Of The Deck
   public Card dealCard() {
      return (Card)deck.remove(0);
   }

   public void addCard(Card c){
      deck.add(c);
   }

}

3.Hand Class

This Class Contains Set Of Card Objects Which Player Has.Initially This Class Has No Card Objects.In A Game This Class Contains Current Card Objects Available In The Player Hand.This Class Has An Array List Of Card Type To Store The Card Objects That Are Available In The Player Hand .

Also This Class Should Have Few Methods To Get The Hand Size ,  Add A Card To Hand, Get A Card From Hand and To Return The Hand Card List.

import java.util.ArrayList;

public class Hand {

   private ArrayList<Card> hand = new ArrayList<Card>();//Cards Available In The Hand

   public Hand(){
   }

   //Used To Add A Card To Hand
   public void addCard(Card c) {
      hand.add(c);
   }

   //Used To Remove A Card From Hand
   public Card removeCard(int iCardIndex) {
      Card x = new Card();
      for (Card c : hand) {
         if (c.getCardIndex() == iCardIndex) {
            x = c;
         }
      }
      hand.remove(x);
      return x;
   }

   //Used To Get The HandSize
   public int getHandSize() {
      return hand.size();
   }

   //Used To Get The CardList
   public ArrayList<Card> getHandCard() {
      return hand;
   }

}
   



JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...