Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

Card Base Classes

This code is a set of base classes for building card games. I have included a Deck object and a Hand object. I have also included a small console app to show the basics of what you can do with it. Once a card from the deck is dealt into a hand, it won't be dealt again. This could could easily be modified for different functionality


//Build-Hands
import java.io.*;
import java.lang.*;
import java.util.Random;

// Copyright Benjamin S. Roberts 2001. Ben Roberts retains all rights
// to this code. It may be freely distributed or modified as long as
// this copyright and contact information remain in the code, and you
// are not some corporate entity hawking my code to make money off it.
// Also, this code may not be used in any "pay to play" source code
// or game ventures. Basically, as long as you don't earn money from
// my work here, I'm fine with you using it.
// I may be reached at:
// sucrerey@hotmail.com
// available for contract work, weddings, and birthday parties.


public class buildHands
{
public static void main(String args[])
{
Hand hand1;
String[] cards1, cards2,cards3;
int x;

hand1 = new Hand(5);
cards1 = hand1.returnHand();
hand1 = null; // the Deck() in hand1 is not recreated so there is now reshuffle
hand1 = new Hand(5); // creating a different hand from the same Deck() object
cards2 = hand1.returnHand();
hand1 = null;
hand1 = new Hand(5);
cards3 = hand1.returnHand();

System.out.println("System Cards:");
for(x=0;x System.out.print(cards1[x]+ ", ");

System.out.println("");
System.out.println("User Cards:");
for(x=0;x System.out.print(cards2[x]+", ");

System.out.println("");
System.out.println("Second Player Cards:");
for(x=0;x System.out.print(cards3[x]+", ");
}
}




//Deck
import java.util.Random;
import java.lang.*;

// Copyright Benjamin S. Roberts 2001. Ben Roberts retains all rights
// to this code. It may be freely distributed or modified as long as
// this copyright and contact information remain in the code, and you
// are not some corporate entity hawking my code to make money off it.
// Also, this code may not be used in any "pay to play" source code
// or game ventures. Basically, as long as you don't earn money from
// my work here, I'm fine with you using it.
// I may be reached at:
// sucrerey@hotmail.com
// available for contract work, weddings, and birthday parties.

public class Deck extends Object
{
private String[][] theDeck;
private boolean[][] inDeck;
private int suit, face;
private String shape, value;
Deck()
{
theDeck = new String[4][13];
inDeck = new boolean[4][13];
for(suit=0;suit<4;suit++)
{
// this switch names the suit, you could change
// it to be the first part of an image file name.
switch(suit)
{
case 0:
shape = "Clubs";
break;
case 1:
shape = "Diamonds";
break;
case 2:
shape = "Hearts";
break;
case 3:
shape = "Spades";
break;
default:
break;

}
for(face=0;face<13;face++)
{
// this switch names the face. it could be changed
// to name the last part of an image file name.
switch(face)
{
case 0:
value = "Ace";
break;
case 1:
value = "Deuce";
break;
case 2:
value = "Three";
break;
case 3:
value = "Four";
break;
case 4:
value = "Five";
break;
case 5:
value = "Six";
break;
case 6:
value = "Seven";
break;
case 7:
value = "Eight";
break;
case 8:
value = "Nine";
break;
case 9:
value = "Ten";
break;
case 10:
value = "Jack";
break;
case 11:
value = "Queen";
break;
case 12:
value = "King";
break;
default:
System.out.println("bad face loop");
}

// the following line create the string based on the previous
// switches. this could be modified to create an image name
// and extension.
theDeck[suit][face] = "The " + value + " of " + shape;
inDeck[suit][face] = true;
}
}
}
public String returnCard(int x, int y)
{
if(inDeck[x][y]==false)
{
return "dealt";
}
else
{
inDeck[x][y] = false;
return theDeck[x][y];
}
}
}



//Hand
import java.util.Random;
import java.io.*;

// Copyright Benjamin S. Roberts 2001. Ben Roberts retains all rights
// to this code. It may be freely distributed or modified as long as
// this copyright and contact information remain in the code, and you
// are not some corporate entity hawking my code to make money off it.
// Also, this code may not be used in any "pay to play" source code
// or game ventures. Basically, as long as you don't earn money from
// my work here, I'm fine with you using it.
// I may be reached at:
// sucrerey@hotmail.com
// available for contract work, weddings, and birthday parties.


public class Hand extends Object
{
private Deck HandDeck;
private String[] hand;

Hand(int numCards)
{
HandDeck = new Deck();
int a = 0;
int b = 0;
hand = new String[numCards];

for(int count=0;count {
seeker:
for(;;)
{
a=(int)Math.abs(4*Math.random());
b=(int)Math.abs(13*Math.random());
hand[count] = HandDeck.returnCard(a,b);
if(hand[count].equals("dealt"))
continue seeker;
else
break;
}
}
}
public String[] returnHand()
{
return hand;
}
}



No comments: