+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Black Jack ;)

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Black Jack ;)

    Okay, today I will show what I have been working on for some days on. Had to learn enums and abstract classes(VERY ANNOYING!!!). So I will do my best to explain and of course share my code

    So let's get going !

    Our cardtypes

    Code:
    public enum CardType {
    
    	TWO("2"),THREE("3"),FOUR("4"),FIVE("5"),SIX("6"),
            SEVEN("7"),EIGHT("8"),NINE("9"),TEN("10"),
    	JACK("J"),QUEEN("Q"),KING("K"),ACE("A");
    	
    	public final int valor = ordinal() + 2;
    	public final String symbol;
    	
    	CardType(String sym) {symbol = sym;}
    }
    Here we started by creating the cards type, lets proceed with creating the symbols for each card.

    Code:
    public enum CardIcon {
    	CLOVER('\u2664'), DIAMONDS('\u2666'),
            HEARTS('\u2665'),SPADES('\u2660');
    	
    	public final char symbol;
    	
    	CardIcon(char c) {symbol = c;}
    }
    Well since I didn't know how to implement icons I made in photoshop. It gave me only one alternative and that was to use ASCII codes.
    So let's work on the cards now.

    Code:
    public class Card {
    
    	private CardIcon ci;
    	private CardType ct;
    	
    	public Card(CardIcon icon, CardType type) {
    		ci = icon;
    		ct = type;
    	}
    	public CardIcon icon() {
    		return ci;
    	}
    	public CardType type() {
    		return ct;
    	}
    	public String toString() {
    		return ci.toString() +" "+ ct.toString();
    	}
    	public String toSymbol() {
    		return ci.symbol + ct.symbol;
    	}
    }
    Binding every symbol and number to them. So we can create a deck.
    So let's get on our deck

    Code:
    import java.util.*;
    
    public class Deck {
    	private List<Card> theDeck = new LinkedList<Card>();
    
    	public void onTop(Card c) {
    		theDeck.add(0,c);
    	}
    	public Card takeOffTop() {
    		Card c = theDeck.get(0);
    		theDeck.remove(0);
    		return c;
    	}
    	public int numberDeck() {
    		return theDeck.size();
    	}
    	public void throwCard() {
    		theDeck.clear();
    	}
    	public Card watch(int nr) {
    		return theDeck.get(nr);
    	}
    	public Card takeOff(int nr) {
    		Card c = theDeck.get(nr);
    		theDeck.remove(nr);
    		return c;
    	}
    	public int search(CardIcon ci, CardType ct) {
    		int i = 0;
    		for(Card c : theDeck)
    			if(c.icon()==ci && k.type()==ct)
    				return i;
    			else
    				i++;
    		return -1;
    	}
    	public void newGame() {
    		theDeck.clear();
    		for(CardIcon ci : CardIcon.values())
    			for(CardType ct : CardType.values())
    			theDeck.add(new Card(ci,ct));
    	}
    	public void shuffle() {
    		Collections.shuffle(theDeck);
    	}
    }
    Here are we linking every card and value to each other. So we can remove, select another card or look on. Last code part is to always shuffle the decks for each player(you against the computer).

    Let's go on to player(rules 1/3)

    Code:
    public abstract class Player{
    
    	protected Deck game;
    	protected Deck hand = new Deck();
    	protected int p;
    	protected String symbols;
    	
    	public Player(Deck c) {
    		game = c;
    	}
    	public abstract void play();
    	
    	public void newGame() {
    		hand.throwCard();
    		p = 0;
    		symbols = "";
    	}
    	public Card newCard() {
    		Card c = game.takeOffTop();
    		hand.onTop(c);
    		symbols += c.toSymbol()+" ";
    		int aces = 0;
    		p = 0;
    		for(int i = 0; i<hand.numberDeck(); i++) {
    			CardType s = hand.watch(i).type();
    			p += s.valor;
    			if(s == CardType.ACE)
    				aces++;
    		}
    		for(int j = 1; j<=aces && p>=21; j++)
    			p -= 13;
    		return c;
    	}
    	public int point() {
    		return p;
    	}
    }
    So now we have created the game and it's rules.

    Let's go on with the you(the player)

    Code:
    import static javax.swing.JOptionPane.*;
    
    public class Human  extends Player {
    	
    	public Human(Deck c) {
    		super(c);
    	}
    	public void play() {
    		newGame();
    		while(p< 21) {
    			newCard();
    			if(p< 21) {
    				int answer = showConfirmDialog(null,
    						"You got : "+symbols+"\nand have "+p+" points."+
    						"\nDo you wan another card? ");
    			
    			if(answer == 1)
    				break;
    			else if(answer == 2)
    				System.exit(0);
    		}
    		else
    			showMessageDialog(null,
    					"You got : "+symbols+
    					"\nand have "+p+" points.");
    		}
    	}
    }
    So our game is starting to close up. Right now we did the 2/3 of the rules.
    Let's go on with the last part of rules 3/3.

    Code:
    import static javax.swing.JOptionPane.*;
    
    public class Computer extends Player {
    	private Player theplayer;
    	
    	public Computer(Deck c, Player the) {
    		super(c);
    		theplayer = the;
    	}
    	public void play() {
    		newGame();
    		while(p< 21 && p< theplayer.point())
    			newCard();
    			showMessageDialog(null, "The computer got: "+symbols+
    				"\nand has "+p+" points.");
    	}
    }
    Now we have made the rules and the game itself. HOWEVER, this isn't the game itself it's the rules,cards&icons + simple interface for the game. Now for the game.

    Code:
    import static javax.swing.JOptionPane.*;
    
    public class BlackJack {
    	public static void main(String[] arg) {
    		Deck thegame = new Deck();
    		Human you = new Human(thegame);
    		Computer it = new Computer(thegame,you);
    		
    		while(true) {
    			thegame.newGame();
    			thegame.shuffle();
    			you.play();
    			String s = "";
    			if(you.point() > 21)
    				s = "You lost!";
    			else if (you.point() == 21)
    				s = "You won!";
    			else {
    			it.play();
    			if(it.point() <= 21 && it.point() >= you.point())
    				s = "You lost!";
    			else
    				s = "You won!";
    			}
    			int answer = showConfirmDialog(null,s+"\nNew game?");
    			if (answer !=0)
    				break;
    		}
    	}
    }
    Okay, now that's the whole game. First of all we created two enums(enums are thing done to count,this is so far I understand which is !=100% accurate answer). Then we started to create the field of the game, the game(rule), then the player(rule) and computer(rule) finally we created the game itself.
    Okay, so now I can't explain everything 100% due to it's very new for me enums and abstract classes. I wish I could better English and to express what every part does. Anyways enjoy my little thing

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    ArtoStiloz's Avatar
    ArtoStiloz is offline Programmer
    Join Date
    Sep 2008
    Location
    Denmark
    Posts
    173
    Rep Power
    14

    Re: Black Jack ;)

    Amazing

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Black Jack ;)

    Quote Originally Posted by ArtoStiloz View Post
    Amazing
    Thanks

  5. #4
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Black Jack ;)

    Quote Originally Posted by tom2000 View Post
    Java EE is defined by its specification. As with other Java Community Process specifications, Java EE is also considered informally to be a standard since providers must agree to certain conformance requirements in order to declare their products as Java EE compliant; albeit with no ISO or ECMA standard.

    Overcoming Fear of

    Flying
    snakeskin boots
    What are you talking about?

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Black Jack ;)

    Ignore him, he's just spamming.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Black Jack ;)

    Quote Originally Posted by Xav View Post
    Ignore him, he's just spamming.
    Okay Xav

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Black Jack ;)

    Notice how he is now banned!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Black Jack ;)

    Quote Originally Posted by Xav View Post
    Notice how he is now banned!
    Funny

  10. #9
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Black Jack ;)

    Anyway, +rep for the tutorial.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  11. #10
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Black Jack ;)

    Quote Originally Posted by Xav View Post
    Anyway, +rep for the tutorial.
    :001_tt2: Thank you !

+ Reply to Thread
Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PNG PHP: Black to transparent??
    By bbqroast in forum PHP Development
    Replies: 1
    Last Post: 07-07-2011, 03:58 AM
  2. Black hat SEO?
    By relapse in forum Search Engine Optimization
    Replies: 4
    Last Post: 01-24-2010, 08:02 PM
  3. C++ problem?Black Jack
    By Coder Zombie in forum C and C++
    Replies: 1
    Last Post: 01-09-2009, 08:47 PM
  4. Black book?
    By Epiphany in forum Visual Basic Programming
    Replies: 9
    Last Post: 10-30-2008, 10:31 PM
  5. Black Hat SEO
    By Kaabi in forum Search Engine Optimization
    Replies: 9
    Last Post: 04-23-2007, 02:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts