Jump to content

Black Jack ;)

- - - - -

  • Please log in to reply
25 replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
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 :)
Posted Image

#2
ArtoStiloz

ArtoStiloz

    Programmer

  • Members
  • PipPipPipPip
  • 175 posts
Amazing :)
[SIGPIC][/SIGPIC]
Http://www.ArtoStiloz.Dk

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

ArtoStiloz said:

Amazing :)

Thanks :)
Posted Image

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Anyway, +rep for the tutorial.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Xav said:

Anyway, +rep for the tutorial.

:001_tt2: Thank you !
Posted Image

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Wilkommen.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Danke je viel, meine freund :D
Posted Image

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Du sprechst Deutsch? Wie?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Xav said:

Du sprechst Deutsch? Wie?

Nein doch kann mir sprechen Deutsch.
Posted Image

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
"Nein - ich kann kein Deutsch sprechen".
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Xav said:

"Nein - ich kann kein Deutsch sprechen".

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

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Fair enough. :) Nice tutorial.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users