Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

Black Jack ;)


  • Please log in to reply
25 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 24 September 2008 - 01:07 PM

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

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.

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.

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

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)

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)

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.

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.

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 :)
  • 6

#2 ArtoStiloz

ArtoStiloz

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 104 posts

Posted 27 September 2008 - 01:40 AM

Amazing :)
  • 0
[SIGPIC][/SIGPIC]
Http://www.ArtoStiloz.Dk

#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 27 September 2008 - 06:15 AM

Amazing :)


Thanks :)
  • 0

#4 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 05 October 2008 - 06:58 AM

Anyway, +rep for the tutorial.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 05 October 2008 - 01:20 PM

Anyway, +rep for the tutorial.


:001_tt2: Thank you !
  • 0

#6 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 06 October 2008 - 12:24 PM

Wilkommen.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 06 October 2008 - 09:21 PM

Danke je viel, meine freund :D
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 07 October 2008 - 11:48 AM

Du sprechst Deutsch? Wie?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 07 October 2008 - 10:08 PM

Du sprechst Deutsch? Wie?


Nein doch kann mir sprechen Deutsch.
  • 0

#10 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 08 October 2008 - 11:54 AM

"Nein - ich kann kein Deutsch sprechen".
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 09 October 2008 - 07:01 AM

"Nein - ich kann kein Deutsch sprechen".


:P, Well I learnt it ages ago so have never used it as often as english :D
  • 0

#12 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 09 October 2008 - 10:00 AM

Fair enough. :) Nice tutorial.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?