Work in progress...
Some "Skeleton code" for a card game. Deck class (arraylist) that consists of 52 "card" objects. A shuffle function is included, which will randomly swap each card in the deck. The basic functionality is done; IE - the playGame() function will deal out 5 cards to the player (human) and computer, show the player his/her hand. I am leaning towards creating a poker game function first; this will parse the user and computers hand, giving a value to each (ie: pair < royal flush), depending on how good their hand is. The code is broken into 3 files, each file is seperated by "----". A future implementation will introduce a GUI and some basic 2D images.
package cardgames; import java.util.ArrayList; import java.util.Random; /** * * @author Branden */ public class deck { //private int size; ArrayList<card> cardDeck;// = new ArrayList<card>(); public deck() { // this.size = size; cardDeck = new ArrayList<card>(); //this.cardDeck = cardDeck; card ace1 = new card("club","A",1); card ace2 = new card("diamond","A",1); card ace3 = new card("heart","A",1); card ace4 = new card("spade","A",1); card king1 = new card("club","K",10);//face values are all 10, except Ace card king2 = new card("diamond","K",10); card king3 = new card("heart","K",10); card king4 = new card("spade","K",10); card queen1 = new card("club","Q",10); card queen2 = new card("diamond","Q",10); card queen3 = new card("heart","Q",10); card queen4 = new card("spade","Q",10); card jack1 = new card("diamond", "J", 10); card jack2 = new card("club","J",10); card jack3 = new card("spade","J",10); card jack4 = new card("heart","J",10); // // card two1 = new card("club",'2',2); // card two2 = new card("diamond",'2',2); // // card seven1 = new card("heart", '7', 7); // card seven2 = new card("diamond", '7', 7); // card seven3 = new card("spade", '7', 7); // card seven4 = new card("club", '7', 7); card tempCardC; card tempCardD; card tempCardS; card tempCardH; //this is must faster than manually entering these cards for (int i=2;i<=10;i++)//start at two, because Ace takes care of 1 value { tempCardC = new card("club",""+i,i);//type case i into str tempCardD = new card("diamond",""+i,i);//type case i into astr tempCardS = new card("spade",""+i,i);//type case i into str tempCardH = new card("club",""+i,i);//type case i into str //System.out.println(tempCardC.getCardVal()); cardDeck.add(tempCardC); cardDeck.add(tempCardD); cardDeck.add(tempCardS); cardDeck.add(tempCardH); //add all the different number cards } cardDeck.add(ace1); cardDeck.add(ace2); cardDeck.add(ace3); cardDeck.add(ace4); cardDeck.add(king1); cardDeck.add(king2); cardDeck.add(king3); cardDeck.add(king4); cardDeck.add(queen1); cardDeck.add(queen2); cardDeck.add(queen3); cardDeck.add(queen4); cardDeck.add(jack1); cardDeck.add(jack2); cardDeck.add(jack3); cardDeck.add(jack4); //cardDeck.add(seven1); //cardDeck.add(seven2); //cardDeck.add(seven3); //cardDeck.add(seven4); } public void pokerGame(){ // // Random drawCard = new Random(); // int randomNumber = drawCard.nextInt(cardDeck.size() - 1) + 1; // //code for PLAYER //deck is pre-shuffled* ArrayList<card> playerHand = new ArrayList<card>();//player's hand for(int i=1;i<=5;i++)//give the player 5 cards from the top of the shuffled deck { playerHand.add(cardDeck.get(i));//add the card to the hand cardDeck.remove(i);//remove the card from the deck so that it cannot be picked again (for the computer's sake } //display the user's hand for (int i=0;i<5;i++){ System.out.println("Card: "+playerHand.get(i).getCardVal()+" * "+playerHand.get(i).getSuit()+"\n");//player hand print } //code for COMPUTER ArrayList<card> compHand = new ArrayList<card>(); for(int i=1;i<=5;i++)//give the comp 5 cards from the top of the shuffled deck { compHand.add(cardDeck.get(i));//add the card to the hand cardDeck.remove(i);//remove the card from the deck so that it cannot be picked again (for the computer's sake } } // public boolean checkPair(){ // for(int i=) // // return false; // } @Override return "deck{" + "size=" + "cardDeck=" + cardDeck + '}'; } public void shuffleDeck()//do this so that we can retain the entire deck instead of removing used cards. { for (int i=0;i<cardDeck.size();i++) { int randomNumber = rand.nextInt(cardDeck.size() - 1) + 1; //So : go through each item of the deck, in incrementing order, and replace the i'th position with a random position in the deck card tempStore = cardDeck.get(i); card tempStore2 = cardDeck.get(randomNumber); cardDeck.remove(randomNumber);//remove the item at the random number generated position, and replace it with the item in the i position cardDeck.add(randomNumber, tempStore); cardDeck.remove(i);//remove the item at cardDeck i position, and replace it with the item retrieved from the random number generator cardDeck.add(i, tempStore2); } } // @Override // public String toString() { // return "deck{" + "size=" + size + '}'; // } } ------------------------------------------------------- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cardgames; /** * * @author Branden */ //a card will have a suit..a value ( ie: K,Q,J,A,1,2,3,4,etc), and a value (1,2,3....11) public class card { private String suit; private String cardVal; private int worth; this.suit = suit; this.cardVal = cardVal; this.worth = worth; } return cardVal; } this.cardVal = cardVal; } return suit; } this.suit = suit; } public int getWorth() { return worth; } public void setWorth(int worth) { this.worth = worth; } @Override return "card{" + "suit=" + suit + "cardVal=" + cardVal + "worth=" + worth + '}'; } } ------------------------------ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cardgames; import java.util.ArrayList; import java.util.Scanner; /** * * @author Branden */ public class Main { /** * @param args the command line arguments */ // TODO code application logic here //*** need seperate func that will create a traditional 52 card deck...shuffling works fine //for shuffling, we should stick w/ doing a shuffle method bc > drawing a random card will not reorder the deck, a user could simply call on that position again to manipulate it(the card) //once I decide on an interaction, I need to figure out whether the card should be removed from the deck after pulled, or should it be replaced (and reshuffled) // card ace1 = new card("club",'A',1); // card ace2 = new card("diamond",'A',1); // card ace3 = new card("heart",'A',1); // card ace4 = new card("spade",'A',1); // card jack1 = new card("diamond", 'J', 10); // card jack2 = new card("club",'J',10); // card jack3 = new card("spade",'J',10); // card jack4 = new card("heart",'J',10); // card seven1 = new card("heart", '7', 7); // card seven2 = new card("diamond", '7', 7); // card seven3 = new card("spade", '7', 7); // card seven4 = new card("club", '7', 7); //ArrayList<card> cards = new ArrayList<card>(); // // //cards.add(ace1); //cards.add(ace2); //cards.add(ace3); //cards.add(ace4); //cards.add(jack1); //cards.add(jack2); //cards.add(jack3); //cards.add(jack4); //cards.add(seven1); //cards.add(seven2); //cards.add(seven3); //cards.add(seven4); deck playDeck = new deck();//new implemention...the deck is created by simply calling deck()..it is preset to 52 cards. the arraylist is initialized and filled within the constructor //for a card game,there shouldnt be any need for a dynamic deck size. // System.out.println(playDeck.toString()); playDeck.shuffleDeck(); // System.out.println(playDeck.toString()); //initial game setup boolean wantToPlay=true; playDeck.pokerGame(); //do you want to continue playing? while (wantToPlay==true) { if (decision2.equals("n")) { wantToPlay = false; } else if (decision2.equals("y")) { playDeck = new deck(); playDeck.shuffleDeck(); wantToPlay = true; playDeck.pokerGame(); } else } //*while* user wants to play (wantToPlay == true) //shuffle deck //game logic method(s)here (include prompts and stuff) //prompt user if they want to play again..wantToPlay = true/false <<re-enter or exit the loop //if prompts are not made..then the game will go crazy } //**note for next testing ... fix the worth of face cards..its not right }
Comments
Subscribe to comments
You need to login to post a comment.

Any suggestions on how I could rework this so that it deals to ai and player from their own individual decks?
Any suggestions on how I could rework this so that it deals to ai and player from their own individual decks?