Advertisement
Guest User

Shitty Blackjack

a guest
Jan 28th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 KB | None | 0 0
  1. package asdf;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Random;
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9.  
  10.  
  11. public class local {
  12.    
  13.     public static class Card {
  14.         //Fields
  15.         private int number; //Ace = 1, 2-10 = 2-10, Knight = 11, Queen = 12, King = 13
  16.         private int suit; //1 = Spades, 2 = Clubs, 3 = Diamonds, 4 = Hearts
  17.        
  18.         //Getters  and setters
  19.         public int getNumber() {
  20.             return number;
  21.         }
  22.         public void setNumber(int value) {
  23.             this.number = value;
  24.         }
  25.         public int getSuit() {
  26.             return suit;
  27.         }
  28.         public void setSuit(int suit) {
  29.             this.suit = suit;
  30.         }
  31.            
  32.         //Constructor
  33.         public Card(){
  34.            
  35.         }
  36.         public Card(int number, int suit){
  37.             this.setNumber(number);
  38.             this.setSuit(suit);
  39.         }
  40.        
  41.         @Override
  42.         public String toString(){
  43.             String fancyTitle = "";
  44.             switch(this.getNumber()){
  45.                 case 1:{
  46.                     fancyTitle += "Ace";
  47.                     break;}
  48.                 case 2:{
  49.                     fancyTitle += "Two";
  50.                     break;}
  51.                 case 3:{
  52.                     fancyTitle += "Three";
  53.                     break;}
  54.                 case 4:{
  55.                     fancyTitle += "Four";
  56.                     break;}
  57.                 case 5:{
  58.                     fancyTitle += "Five";
  59.                     break;}
  60.                 case 6:{
  61.                     fancyTitle += "Six";
  62.                     break;}
  63.                 case 7:{
  64.                     fancyTitle += "Seven";
  65.                     break;}
  66.                 case 8:{
  67.                     fancyTitle += "Eight";
  68.                     break;}
  69.                 case 9:{
  70.                     fancyTitle += "Nine";
  71.                     break;}
  72.                 case 10:{
  73.                     fancyTitle += "Ten";
  74.                     break;}
  75.                 case 11:{
  76.                     fancyTitle += "Knight";
  77.                     break;}
  78.                 case 12:{
  79.                     fancyTitle += "Queen";
  80.                     break;}
  81.                 case 13:{
  82.                     fancyTitle += "King";
  83.                     break;}
  84.                 default:{
  85.                     fancyTitle += "Error";
  86.                     break;
  87.                 }
  88.             }
  89.             fancyTitle += " of ";
  90.             switch(this.getSuit()){
  91.                 case 1:{
  92.                     fancyTitle += "Spades";
  93.                     break;}
  94.                 case 2:{
  95.                     fancyTitle += "Clubs";
  96.                     break;}
  97.                 case 3:{
  98.                     fancyTitle += "Diamonds";
  99.                     break;}
  100.                 case 4:{
  101.                     fancyTitle += "Hearts";
  102.                     break;}
  103.                 default:{
  104.                     fancyTitle += "programmer";
  105.                     break;}
  106.                 }
  107.             return fancyTitle;
  108.         }
  109.     }
  110.    
  111.        
  112.    
  113.     public static void main(String[] args) {
  114.        
  115.         System.out.println("Welcome to shitty Blackjack\n");
  116.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  117.         String input = "";
  118.         int round = 0;
  119.         do{
  120.             round++;
  121.             System.out.println("Round " +round +" starts");
  122.             Random rng = new Random();
  123.             Boolean twoDecks = rng.nextBoolean();
  124.            
  125.             ArrayList<Card> cards = new ArrayList<Card>();
  126.             //Setting up the deck(s)
  127.             if(twoDecks){
  128.                 for(int h = 1; h <= 2; h++){
  129.                     for(int j = 1; j<= 4; j++){
  130.                         for(int i = 1; i <= 13; i++){
  131.                             Card card = new Card(i,j);
  132.                             cards.add(card);
  133.                             //System.out.println(card.toString());
  134.                         }
  135.                     }
  136.                 }
  137.                 System.out.print("Two decks have been made");
  138.             }else{
  139.                     for(int j = 1; j<= 4; j++){
  140.                         for(int i = 1; i <= 13; i++){
  141.                             Card card = new Card(i,j);
  142.                             cards.add(card);
  143.                             //System.out.println(card.toString());
  144.                         }
  145.                     }
  146.                 System.out.print("One deck has been made");
  147.             }
  148.             Collections.shuffle(cards);
  149.             System.out.println(" and shuffled\n");
  150.    
  151.            
  152.            
  153.             ArrayList<Card> playerHand = new ArrayList<Card>();
  154.             ArrayList<Card> dealerHand = new ArrayList<Card>();
  155.            
  156.            
  157.             //Giving the player two random cards
  158.             System.out.println("Player is forced two cards");
  159.    
  160.             int choice = rng.nextInt(cards.size());
  161.             playerHand.add(cards.get(choice));
  162.             System.out.println("Player recives " +cards.get(choice).toString());       
  163.             cards.remove(choice);
  164.    
  165.             choice = rng.nextInt(cards.size());
  166.             playerHand.add(cards.get(choice));
  167.             System.out.println("Player recives " +cards.get(choice).toString());       
  168.             cards.remove(choice);
  169.            
  170.            
  171.            
  172.            
  173.            
  174.            
  175.             br = new BufferedReader(new InputStreamReader(System.in));
  176.             do{
  177.                 System.out.println("\nPlayerhand value is " +handValue(playerHand) +"\nEnter 'hit' or 'stop'");
  178.                 try{
  179.                     input = br.readLine();
  180.                     if(input.equals("hit")){
  181.                         choice = rng.nextInt(cards.size());
  182.                         playerHand.add(cards.get(choice));
  183.                         System.out.println("Player recives " +cards.get(choice).toString());       
  184.                         cards.remove(choice);
  185.                     }
  186.                 }catch(Exception e){
  187.                     System.out.println("Error\n" +e.getMessage());
  188.                 }
  189.                 if(playerHand.size() == 5){
  190.                     System.out.println("Player has five cards and is forced to stop");
  191.                     input = "stop";
  192.                 }
  193.             }while(!input.equals("stop"));
  194.            
  195.                    
  196.             System.out.println("\nPlayer has stopped picking cards\nValue is " +handValue(playerHand));
  197.             System.out.println("\nDealer draws cards");
  198.    
  199.            
  200.            
  201.             while(handValue(dealerHand) < 17){
  202.                 choice = rng.nextInt(cards.size());
  203.                 dealerHand.add(cards.get(choice));
  204.                 cards.remove(choice);
  205.             }
  206.             System.out.println("Dealerhand value is " +handValue(dealerHand) +"\n");
  207.            
  208.             if(handValue(playerHand) > handValue(dealerHand)){
  209.                 System.out.println("Player wins");
  210.             }
  211.             if(handValue(playerHand) < handValue(dealerHand)){
  212.                 System.out.println("Dealer wins");
  213.             }
  214.             if(handValue(playerHand) == handValue(dealerHand)){
  215.                 System.out.println("DRAW");
  216.             }
  217.             System.out.println("\nDo you want to play another round?\n'yes' to continue or anything else to stop");
  218.             try{
  219.                 input = br.readLine();
  220.             }catch(Exception e){
  221.                 System.out.println("Error\n" +e.getMessage());
  222.             }
  223.         }while(input.equals("yes"));       
  224.         try {
  225.             br.close();
  226.         } catch (Exception e) {
  227.             System.out.println("Error\n" +e.getMessage());
  228.         }
  229.         System.out.println("\nProgram END");
  230.     }//End of Main()
  231.  
  232.    
  233.     //Calculate hand score
  234.     //Card point values are: Ace = 1, 2-10 = 2-10, Knight, Queen and King = 10
  235.     public static int handValue(ArrayList<Card> hand){
  236.         int value = 0;
  237.         for(Card card : hand){
  238.             switch(card.getNumber()){
  239.                 case 1:{
  240.                     value += 1;
  241.                     break;}
  242.                 case 2:{
  243.                     value += 2;
  244.                     break;}
  245.                 case 3:{
  246.                     value += 3;
  247.                     break;}
  248.                 case 4:{
  249.                     value += 4;
  250.                     break;}
  251.                 case 5:{
  252.                     value += 5;
  253.                     break;}
  254.                 case 6:{
  255.                     value += 6;
  256.                     break;}
  257.                 case 7:{
  258.                     value += 7;
  259.                     break;}
  260.                 case 8:{
  261.                     value += 8;
  262.                     break;}
  263.                 case 9:{
  264.                     value += 9;
  265.                     break;}
  266.                 case 10:{
  267.                     value += 10;
  268.                     break;}
  269.                 case 11:{
  270.                     value += 10;
  271.                     break;}
  272.                 case 12:{
  273.                     value += 10;
  274.                     break;}
  275.                 case 13:{
  276.                     value += 10;
  277.                     break;}
  278.                 default:{
  279.                     value += 2989; //0xBAD in decimal
  280.                     break;
  281.                 }
  282.             }
  283.         }
  284.         return value;
  285.     }  
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement