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
Here we started by creating the cards type, lets proceed with creating the symbols for each card.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;} }
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.Code:public enum CardIcon { CLOVER('\u2664'), DIAMONDS('\u2666'), HEARTS('\u2665'),SPADES('\u2660'); public final char symbol; CardIcon(char c) {symbol = c;} }
So let's work on the cards now.
Binding every symbol and number to them. So we can create a deck.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; } }
So let's get on our deck
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).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); } }
Let's go on to player(rules 1/3)
So now we have created the game and it's rules.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; } }
Let's go on with the you(the player)
So our game is starting to close up. Right now we did the 2/3 of the rules.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."); } } }
Let's go on with the last part of rules 3/3.
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 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."); } }
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.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, 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![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks