Basic concepts Game in Java "Playing Card" | Playing card game with face and suits
This is a source code of game "The Playing card game " in java. By this student can clear their concepts about gaming in java and also who a playing card game be developed. In this game I used classes, Objects, methods and others thing which is necessary for the gaming in the Java. It is consist of three class, you copy all the class and then paste in in your compiler, after changing the name of class and package you will be able to use it.
Class No:1
package PlayingCard;
Class No 2:
Class No:1
package PlayingCard;
public class Card {
// fields for class
String face; // card number ace, deuce....
String suit;// signs of card hearts diamond ....
// constructor for the class
public Card(String faceOfcard, String
suitOfcard)
{
face= faceOfcard;
suit = suitOfcard;
}
// on;y for get details of the class
public String toString()
{
return face + " of " + suit;// for get this kind of output "Ace of Heart"
}// toString ends here
}// class ends here
package PlayingCard;
import java.util.Random;
public class deckOfcard {
private Card[] deck; // declare a array of Card
data type (Card is a class which is a
self define data type)
private int currentCard;
private static final int NUMBER_OF_CARD=52;
// constant for number of cards in one deck
private static final Random rNo = new Random (); // for get the random number
// constructor of the class
public deckOfcard()
{
// array for named all the faces of the deck
String [] face = {"Ace","Deuce","Three","Four","Five","Six","Seven",
"Eight", "Nine", "Ten", "Jack","Queen","King"};
// array for named all the suit/ shapes of the deck
String [] suit = {"Heart",
"Diamond", "spade", "club"};
deck = new Card[NUMBER_OF_CARD];// so that only 52 card can
be save
currentCard=0;
for (int i = 0; i < deck.length; i++) {
deck[i] = new Card (face[i%13], suit [i/13]); // for getting the card
from all the suit of all the faces
}
}
// for shuffle the cards
public void shuffle()
{
currentCard=0;
for(int first=0; first<deck.length; first++)
{
int second= rNo.nextInt(NUMBER_OF_CARD);
// variable which have to generate a random
number
//shuffling process
//example:
// a=temp
//a==b
//b==temp
Card temp = deck[first];
deck[first] = deck[second];
deck[second] =temp;
}//
ending of for loop
}// shuffle function ends here
public Card dealCard()
{
if (currentCard< deck.length)
return deck[currentCard++];// for getting the card
from the deck and if card is more than 52 then it will null
else
return null;
}//card deal Card ends here
} // deckOfcard ends here
Class No:3
package PlayingCard;
public class Main {
public static void main(String[] args) {
// object for the class
deckOfcard deck = new deckOfcard ();
//call the shuffle
deck.shuffle();
//results of plyaer 1
System.out.println("Player 1 : ");
int sum1 = 0;
for(int i=1; i<=4; i++)
{
Card c = deck.dealCard();// card is got
sum1 = sum1 + cardNumber(c); // card number count
System.out.printf("\t=> %-19s(%d)\n", c,cardNumber(c)); // display card
}// for loop ends here
System.out.println("\n\tTotal Cards of player 1: (" + sum1+")"); // total number sum up
shown
System.out.println("\n\nPlayer 2 : ");
int sum2 = 0;
for(int i=1; i<=4; i++)
{
Card c = deck.dealCard();
sum2 = sum2 + cardNumber(c);
System.out.printf("\t=> %-19s(%d)\n", c,cardNumber(c));
}// for loop ends here
System.out.println("\n\tTotal Cards of player 1: (" + sum2+")");
//winning criteria
if(sum1 == sum2)
System.err.println("\n\t\tGame is Drawn");
else if(sum1 > sum2)
System.err.println("\n\t\tPlayer 1 wins");
else if (sum1 < sum2)
System.err.println("\n\t\tPlayer 2 wins");
}//main
functions ends here
//How to change String into integer (String--> int)
public static int cardNumber(Card card) {
if(card.face.equalsIgnoreCase("ace"))
return 1;
else if(card.face.equalsIgnoreCase("deuce"))
return 2;
else if(card.face.equalsIgnoreCase("three"))
return 3;
else if(card.face.equalsIgnoreCase("four"))
return 4;
else if(card.face.equalsIgnoreCase("five"))
return 5;
else if(card.face.equalsIgnoreCase("six"))
return 6;
else if(card.face.equalsIgnoreCase("seven"))
return 7;
else if(card.face.equalsIgnoreCase("eight"))
return 8;
else if(card.face.equalsIgnoreCase("nine"))
return 9;
else if(card.face.equalsIgnoreCase("ten"))
return 10;
else if(card.face.equalsIgnoreCase("jack"))
return 11;
else if(card.face.equalsIgnoreCase("queen"))
return 12;
else
return 13;
}
} // class ends here
Comments
Post a Comment