Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asdf;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Random;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class local {
- public static class Card {
- //Fields
- private int number; //Ace = 1, 2-10 = 2-10, Knight = 11, Queen = 12, King = 13
- private int suit; //1 = Spades, 2 = Clubs, 3 = Diamonds, 4 = Hearts
- //Getters and setters
- public int getNumber() {
- return number;
- }
- public void setNumber(int value) {
- this.number = value;
- }
- public int getSuit() {
- return suit;
- }
- public void setSuit(int suit) {
- this.suit = suit;
- }
- //Constructor
- public Card(){
- }
- public Card(int number, int suit){
- this.setNumber(number);
- this.setSuit(suit);
- }
- @Override
- public String toString(){
- String fancyTitle = "";
- switch(this.getNumber()){
- case 1:{
- fancyTitle += "Ace";
- break;}
- case 2:{
- fancyTitle += "Two";
- break;}
- case 3:{
- fancyTitle += "Three";
- break;}
- case 4:{
- fancyTitle += "Four";
- break;}
- case 5:{
- fancyTitle += "Five";
- break;}
- case 6:{
- fancyTitle += "Six";
- break;}
- case 7:{
- fancyTitle += "Seven";
- break;}
- case 8:{
- fancyTitle += "Eight";
- break;}
- case 9:{
- fancyTitle += "Nine";
- break;}
- case 10:{
- fancyTitle += "Ten";
- break;}
- case 11:{
- fancyTitle += "Knight";
- break;}
- case 12:{
- fancyTitle += "Queen";
- break;}
- case 13:{
- fancyTitle += "King";
- break;}
- default:{
- fancyTitle += "Error";
- break;
- }
- }
- fancyTitle += " of ";
- switch(this.getSuit()){
- case 1:{
- fancyTitle += "Spades";
- break;}
- case 2:{
- fancyTitle += "Clubs";
- break;}
- case 3:{
- fancyTitle += "Diamonds";
- break;}
- case 4:{
- fancyTitle += "Hearts";
- break;}
- default:{
- fancyTitle += "programmer";
- break;}
- }
- return fancyTitle;
- }
- }
- public static void main(String[] args) {
- System.out.println("Welcome to shitty Blackjack\n");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String input = "";
- int round = 0;
- do{
- round++;
- System.out.println("Round " +round +" starts");
- Random rng = new Random();
- Boolean twoDecks = rng.nextBoolean();
- ArrayList<Card> cards = new ArrayList<Card>();
- //Setting up the deck(s)
- if(twoDecks){
- for(int h = 1; h <= 2; h++){
- for(int j = 1; j<= 4; j++){
- for(int i = 1; i <= 13; i++){
- Card card = new Card(i,j);
- cards.add(card);
- //System.out.println(card.toString());
- }
- }
- }
- System.out.print("Two decks have been made");
- }else{
- for(int j = 1; j<= 4; j++){
- for(int i = 1; i <= 13; i++){
- Card card = new Card(i,j);
- cards.add(card);
- //System.out.println(card.toString());
- }
- }
- System.out.print("One deck has been made");
- }
- Collections.shuffle(cards);
- System.out.println(" and shuffled\n");
- ArrayList<Card> playerHand = new ArrayList<Card>();
- ArrayList<Card> dealerHand = new ArrayList<Card>();
- //Giving the player two random cards
- System.out.println("Player is forced two cards");
- int choice = rng.nextInt(cards.size());
- playerHand.add(cards.get(choice));
- System.out.println("Player recives " +cards.get(choice).toString());
- cards.remove(choice);
- choice = rng.nextInt(cards.size());
- playerHand.add(cards.get(choice));
- System.out.println("Player recives " +cards.get(choice).toString());
- cards.remove(choice);
- br = new BufferedReader(new InputStreamReader(System.in));
- do{
- System.out.println("\nPlayerhand value is " +handValue(playerHand) +"\nEnter 'hit' or 'stop'");
- try{
- input = br.readLine();
- if(input.equals("hit")){
- choice = rng.nextInt(cards.size());
- playerHand.add(cards.get(choice));
- System.out.println("Player recives " +cards.get(choice).toString());
- cards.remove(choice);
- }
- }catch(Exception e){
- System.out.println("Error\n" +e.getMessage());
- }
- if(playerHand.size() == 5){
- System.out.println("Player has five cards and is forced to stop");
- input = "stop";
- }
- }while(!input.equals("stop"));
- System.out.println("\nPlayer has stopped picking cards\nValue is " +handValue(playerHand));
- System.out.println("\nDealer draws cards");
- while(handValue(dealerHand) < 17){
- choice = rng.nextInt(cards.size());
- dealerHand.add(cards.get(choice));
- cards.remove(choice);
- }
- System.out.println("Dealerhand value is " +handValue(dealerHand) +"\n");
- if(handValue(playerHand) > handValue(dealerHand)){
- System.out.println("Player wins");
- }
- if(handValue(playerHand) < handValue(dealerHand)){
- System.out.println("Dealer wins");
- }
- if(handValue(playerHand) == handValue(dealerHand)){
- System.out.println("DRAW");
- }
- System.out.println("\nDo you want to play another round?\n'yes' to continue or anything else to stop");
- try{
- input = br.readLine();
- }catch(Exception e){
- System.out.println("Error\n" +e.getMessage());
- }
- }while(input.equals("yes"));
- try {
- br.close();
- } catch (Exception e) {
- System.out.println("Error\n" +e.getMessage());
- }
- System.out.println("\nProgram END");
- }//End of Main()
- //Calculate hand score
- //Card point values are: Ace = 1, 2-10 = 2-10, Knight, Queen and King = 10
- public static int handValue(ArrayList<Card> hand){
- int value = 0;
- for(Card card : hand){
- switch(card.getNumber()){
- case 1:{
- value += 1;
- break;}
- case 2:{
- value += 2;
- break;}
- case 3:{
- value += 3;
- break;}
- case 4:{
- value += 4;
- break;}
- case 5:{
- value += 5;
- break;}
- case 6:{
- value += 6;
- break;}
- case 7:{
- value += 7;
- break;}
- case 8:{
- value += 8;
- break;}
- case 9:{
- value += 9;
- break;}
- case 10:{
- value += 10;
- break;}
- case 11:{
- value += 10;
- break;}
- case 12:{
- value += 10;
- break;}
- case 13:{
- value += 10;
- break;}
- default:{
- value += 2989; //0xBAD in decimal
- break;
- }
- }
- }
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement