MrThoe

Blackjack 3

Jan 17th, 2023
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. public class MyProgram
  2. {
  3.     public static Deck deck = new Deck();
  4.    
  5.     public static void main(String[] args)
  6.     {
  7.         //printDeck();
  8.         deck.shuffle2();
  9.         //DEAL 2 CARDS
  10.         Card c = new Card();
  11.        
  12.         int points = 0;
  13.         int count = 0;  //START AT THE TOP OF DECK
  14.         System.out.println(deck.cards[0]);
  15.         System.out.println(deck.cards[1]);
  16.         points += deck.cards[0].getPoint();
  17.         points += deck.cards[1].getPoint();
  18.         System.out.println(points);
  19.     }
  20.    
  21.     public static void printDeck(){
  22.         for(int i = 0;  i < 52; i++){
  23.             System.out.println(deck.cards[i]);
  24.         }
  25.     }
  26. }
  27.  
  28.  
  29. public class Deck {
  30.  
  31.     Card[] cards = new Card[52];
  32.    
  33.     public Deck(){
  34.         int count = 0;
  35.         for(int i = 0; i < 4; i++){
  36.             for(int j = 0; j < 13; j++){
  37.                 cards[count] = new Card(i, j);
  38.                 count++;
  39.             }
  40.         }
  41.     }
  42.    
  43.     //Shuffle by going through deck and swapping
  44.     public void shuffle2(){
  45.        for(int i = 0; i < 52; i++){
  46.            int c = (int)(Math.random()*52);
  47.            Card a = cards[c];
  48.            cards[c] = cards[i];
  49.            cards[i] = a;
  50.        }  
  51.        
  52.     }
  53.  }
  54.  
  55. public class Card {
  56.     public static String values = "23456789TJQKA";
  57.     public static String suits = "CDHS";
  58.     private char suit;
  59.     private char val;
  60.     private int point;
  61.    
  62.     public Card(){
  63.         int index = randomInt(0,13);
  64.         val = values.charAt(index);
  65.         index = randomInt(0,4);
  66.         suit = suits.charAt(index);
  67.         if(index < 8){
  68.             point = index + 2;
  69.         } else if (index == 12) {   //ACE
  70.             point = 1;
  71.         } else {
  72.             point = 10;     //FACES & 10
  73.         }
  74.        
  75.     }
  76.    
  77.     public int getPoint(){
  78.         return point;
  79.     }
  80.    
  81.     public Card(int s, int v){
  82.         val = values.charAt(v);
  83.         suit = suits.charAt(s);
  84.         if(v < 8){
  85.             point = v + 2;
  86.         } else if (v == 12) {   //ACE
  87.             point = 1;
  88.         } else {
  89.             point = 10;     //FACES & 10
  90.         }
  91.     }
  92.    
  93.    
  94.     public String toString(){
  95.         return "" + val + suit;
  96.     }
  97.    
  98.     public int randomInt(int lower, int higher){
  99.         return (int)(Math.random()*(higher-lower))+lower;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment