Untitled

 avatar
unknown
plain_text
2 years ago
15 kB
5
Indexable
import java.util.Scanner;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Blackjack extends JPanel {
	
	// Declarations
	public static ArrayList<Cards> cards = new ArrayList<>();
	public static ArrayList<Cards> playerCards = new ArrayList<>();
	public static ArrayList<Cards> dealerCards = new ArrayList<>();
	public static ArrayList<ImageIcon> cardIcons = new ArrayList<>();
	
	// Using two sums for the case where Ace is both 1 and 11.
	public static int playerS1 = 0;
	public static int playerS2 = 0;
	public static int dealerS1 = 0;
	public static int dealerS2 = 0;
	
	public static int balance = 1000;
	
	public static int winner; // 0 = player, 1 = dealer, 2 = tie
	
	public static int dealerMaxSum = 0;
	
	public static void main(String[] args) {
		
		// Declarations
		Random rng = new Random();
		
		int numberOfDecks = 3;
				
		Scanner s = new Scanner(System.in);
		
		JFrame frame = new JFrame("Blackjack");
		frame.setSize(1200, 800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLayout(null);
		
		Icon hitIcon = new ImageIcon(Blackjack.class.getResource("/HitButton.PNG"));
		Icon standIcon = new ImageIcon(Blackjack.class.getResource("/StandButton.PNG"));


		JButton hit = new JButton();
		JButton stand = new JButton();
		hit.setIcon(hitIcon);
		stand.setIcon(standIcon);

		hit.setBounds(1000, 400, hitIcon.getIconWidth(), hitIcon.getIconHeight());
		stand.setBounds(1000, 200, standIcon.getIconWidth(), standIcon.getIconHeight());
		
		hit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (playerS1 < 21 || playerS2 < 21 && playerS1 != 21 && playerS2 != 21) {
					hit(frame);
					playerSum();
					printGame();
					}
				}
			});
		
		stand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dealerMaxSum = Math.max(dealerS1, dealerS2);
				while (dealerMaxSum <= 16) {
					dealerHit(frame);
					dealerSum();
					dealerMaxSum = Math.max(dealerS1, dealerS2);
					printGame();
					
				}
				printWinner();
				
			}
			
		});



		JLabel backgroundLabel = new JLabel(new ImageIcon(Blackjack.class.getResource("background.png")));
		backgroundLabel.setSize(frame.getSize());
		frame.setContentPane(backgroundLabel);
		
		ImageIcon icon;
		
		frame.add(hit);


		frame.add(stand);
		
		// Fill the deck with cards
        String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

        for (String rank : ranks) {
            for (String suit : suits) {
            	String iconAddress = rank + suit;
                Blackjack instance = new Blackjack();
            	icon = new ImageIcon (new ImageIcon(instance.getClass().getResource(iconAddress + ".png")).getImage().getScaledInstance(1200, 800,  java.awt.Image.SCALE_SMOOTH));
                Cards card = new Cards(suit, rank, numberOfDecks, icon);
                cards.add(card);
            }
        }
       
        // Game loop
        while(true) {
        	        	
        	// Resetting array lists at the start of a new game
        	playerCards.clear();
        	dealerCards.clear();
        	cards.clear();
        	for (String rank : ranks) {
                for (String suit : suits) {
                	Blackjack instance = new Blackjack();
                	String iconAddress = rank + suit;
                	icon = new ImageIcon (new ImageIcon(instance.getClass().getResource(iconAddress + ".png")).getImage().getScaledInstance(1200, 800,  java.awt.Image.SCALE_SMOOTH));
                    Cards card = new Cards(suit, rank, numberOfDecks, icon);
                    cards.add(card);
                }
        	}
        	
        	System.out.println("Current balance: " + balance);
            System.out.print("Enter bet ammount: ");
            		
    		int bet = Integer.parseInt(s.nextLine()); 	//	THIS IS COMMENTED FOR TESTS - IT'S PART OF THE BALANCE WORKINGS
           
            System.out.println("---------------------------------");
    		
        	clearCards(frame);
            
    		// Initial deal
            hit(frame);
            dealerHit(frame);
            hit(frame);
            
            // Calculate sum of player and dealer cards
            playerSum();
            dealerSum();
            
            // Print out current state of cards
            printGame(); 
            
            displayPlayerCards(frame);
            displayDealerCards(frame);
            
            while(true) {
            	
                String input2 = s.nextLine();
                
                if(input2.equals("hit")) {
                	if(playerS1 < 21 || playerS2 < 21 && playerS1 != 21 && playerS2 != 21) {
                		hit(frame);
                		playerSum();
                        printGame(); 
                	}
                	else {
                		break;
                	}
                }
                else if(input2.equals("stand")) {
                	dealerMaxSum = Math.max(dealerS1, dealerS2);
                	while(dealerMaxSum <= 16) {
                		dealerHit(frame);
                		dealerSum();
                    	dealerMaxSum = Math.max(dealerS1, dealerS2);
                        printGame(); 
                	}
                	break;
                }
                
                displayDealerCards(frame);
               
            }
            printWinner();
            
            if(winner == 0) {
            	balance += bet;					
            }
            else if(winner == 1) {
            	balance -= bet;
            }
            
        }
        
        // Prints number of cards of each kind - FOR DEBUGGING
        /* 
        for(Cards card : cards) {
        	System.out.println(card.numberOfCards + " x " + card.rank + " of " + card.suit);
        }
        */
    }
	
	public static void clearCards(JFrame frame) {
	    Component[] components = frame.getContentPane().getComponents();
	    
	    for (Component component : components) {
	        if (component instanceof JLabel) {
	            frame.remove(component);
	        }
	    }
	    
	    frame.revalidate();
	    frame.repaint();
	}
	
	public static void displayDealerCards(JFrame frame) {
	    int x = 450; // Starting X position for the card icons
	    int y = 100; // Y position for the card icons
	    int cardWidth = 70; // Width for the card icons
	    int cardHeight = 100; // Height for the card icons

	    for (Cards card : dealerCards) {
	        Image resizedImage = card.icon.getImage().getScaledInstance(cardWidth, cardHeight, Image.SCALE_SMOOTH);
	        ImageIcon resizedIcon = new ImageIcon(resizedImage);
	        JLabel cardLabel = new JLabel(resizedIcon);
	        cardLabel.setBounds(x, y, cardWidth, cardHeight);
	        
	        // Set the Z-order of the cardLabel to the topmost layer (highest Z-order)
	        frame.add(cardLabel, Integer.valueOf(0));

	        // Increment X position to display the next card icon
	        x += 80; 
	        
	        // Ensure the cardLabel is brought to the front after setting Z-order
	        cardLabel.repaint();
	    }

	    frame.revalidate();
	    frame.repaint();
	}

	public static void displayPlayerCards(JFrame frame) {
	    int x = 450; // Starting X position for the card icons
	    int y = 400; // Y position for the card icons
	    int cardWidth = 70; // Width for the card icons
	    int cardHeight = 100; // Height for the card icons

	    for (Cards card : playerCards) {
	        Image resizedImage = card.icon.getImage().getScaledInstance(cardWidth, cardHeight, Image.SCALE_SMOOTH);
	        ImageIcon resizedIcon = new ImageIcon(resizedImage);
	        JLabel cardLabel = new JLabel(resizedIcon);
	        cardLabel.setBounds(x, y, cardWidth, cardHeight);
	        
	        // Set the Z-order of the cardLabel to the topmost layer (highest Z-order)
	        frame.add(cardLabel, Integer.valueOf(0));

	        // Increment X position to display the next card icon
	        x += 80; 
	        
	        // Ensure the cardLabel is brought to the front after setting Z-order
	        cardLabel.repaint();
	    }

	    frame.revalidate();
	    frame.repaint();
	}
	
	public static void printWinner() {
        if(playerS2 > 21) {
     	   if(playerS1 > 21) {
     		   System.out.println("You lost!\n");
     		   winner = 1;
     	   }
     	   else if(playerS1 <= 21 && playerS1 < dealerMaxSum) {
     		   System.out.println("You lost!\n");
     		   winner = 1;
     	   }
     	   else if(playerS1 <= 21 && playerS1 > dealerMaxSum) {
     		   System.out.println("You won!\n");
     		   winner = 0;
     	   }
     	   else if(playerS1 <= 21 && playerS1 == dealerMaxSum) { 
     		   System.out.println("Tie!\n");
     		   winner = 2;
     	   }
        }
        else if(playerS2 == 21) {
     	   if(playerS2 > dealerMaxSum || dealerMaxSum > 21) {
     		   System.out.println("You won!\n");
     		   winner = 0;
     	   }
     	   else if(dealerMaxSum == 21) {
     		   System.out.println("Tie!\n");
     		   winner = 2;
     	   }
        }
        else if(playerS2 < 21) {
     	   if(dealerMaxSum > 21) {
     		   System.out.println("You won!\n");
     		   winner = 0;
     	   }
     	   else if(playerS2 > dealerMaxSum) {
     		   System.out.println("You won!\n");
     		   winner = 0;
     	   }
     	   else if(playerS2 < dealerMaxSum) {
     		   System.out.println("You lost!\n");
     		   winner = 1;
     	   }
     	   else if(playerS2 == dealerMaxSum) {
     		   System.out.println("Tie!\n");
     		   winner = 2;
     	   }
        }
         
	}
	// Hit method
	public static void hit(JFrame frame) {
		
		Random rng = new Random();
		Boolean cardFound = false;

		while(!cardFound) {
			int randomIndex = rng.nextInt(52);
			if(cards.get(randomIndex).numberOfCards > 0) {
				
				Cards card = cards.get(randomIndex);
				
				card.useCard();
				playerCards.add(card);
				
				
				cardFound = true;
			}
		}
		
		displayPlayerCards(frame);
		playerSum();
		
	}
	
	// Dealer Hit method
	public static void dealerHit(JFrame frame) {
		
		Random rng = new Random();
		Boolean cardFound = false;

		while(!cardFound) {
			int randomIndex = rng.nextInt(52);
			if(cards.get(randomIndex).numberOfCards > 0) {
				
				Cards card = cards.get(randomIndex);
				
				card.useCard();
				dealerCards.add(card);
				
				cardFound = true;
			}
		}
		displayDealerCards(frame);
	}
	
	// Player Cards Sum method
	public static void playerSum() {
		
		playerS1 = 0;
		playerS2 = 0;
		
		for( Cards card : playerCards ) {
			switch(card.rank) {
				case "2":
					playerS1 += 2;
					playerS2 += 2;
					break;
				case "3":
					playerS1 += 3;
					playerS2 += 3;
					break;
				case "4":
					playerS1 += 4;
					playerS2 += 4;
					break;
				case "5":
					playerS1 += 5;
					playerS2 += 5;
					break;
				case "6":
					playerS1 += 6;
					playerS2 += 6;
					break;
				case "7":
					playerS1 += 7;
					playerS2 += 7;
					break;
				case "8":
					playerS1 += 8;
					playerS2 += 8;
					break;
				case "9":
					playerS1 += 9;
					playerS2 += 9;
					break;
				case "10":
					playerS1 += 10;
					playerS2 += 10;
					break;
				case "J":
					playerS1 += 10;
					playerS2 += 10;
					break;
				case "Q":
					playerS1 += 10;
					playerS2 += 10;
					break;
				case "K":
					playerS1 += 10;
					playerS2 += 10;
					break;
				case "A":
					playerS1 += 1;
					playerS2 += 11;
					break;
			}
		}
	}

	// Dealer Cards Sum method
	public static void dealerSum() {
		
		dealerS1 = 0;
		dealerS2 = 0;
		
		for( Cards card : dealerCards ) {
			switch(card.rank) {
				case "2":
					dealerS1 += 2;
					dealerS2 += 2;
					break;
				case "3":
					dealerS1 += 3;
					dealerS2 += 3;
					break;
				case "4":
					dealerS1 += 4;
					dealerS2 += 4;
					break;
				case "5":
					dealerS1 += 5;
					dealerS2 += 5;
					break;
				case "6":
					dealerS1 += 6;
					dealerS2 += 6;
					break;
				case "7":
					dealerS1 += 7;
					dealerS2 += 7;
					break;
				case "8":
					dealerS1 += 8;
					dealerS2 += 8;
					break;
				case "9":
					dealerS1 += 9;
					dealerS2 += 9;
					break;
				case "10":
					dealerS1 += 10;
					dealerS2 += 10;
					break;
				case "J":
					dealerS1 += 10;
					dealerS2 += 10;
					break;
				case "Q":
					dealerS1 += 10;
					dealerS2 += 10;
					break;
				case "K":
					dealerS1 += 10;
					dealerS2 += 10;
					break;
				case "A":
					dealerS1 += 1;
					dealerS2 += 11;
					break;
			}
		}
		
	}

	// Prints Player and Dealer Cards method
	public static void printGame() {
		        
		// Prints player sum
		if(playerS1 != playerS2) {
			if(playerS2 < 21) {
				System.out.println("Player cards sum: " + playerS1 + " / " + playerS2);
			}
			else if (playerS2 == 21) {
				System.out.println("Player cards sum: " + playerS2);
			}
			else {
				System.out.println("Player cards sum: " + playerS1);
			}
		}
		else {
			System.out.println("Player cards sum: " + playerS2);
		}
		
        System.out.println();
    	
        System.out.println("Player cards:");

        for( Cards card : playerCards ) {
        	System.out.println(card.rank + " of " + card.suit);
        }
       
        System.out.println();
        
        // Prints dealer sum
        if(dealerS1 != dealerS2) {
			if(dealerS2 < 21) {
				System.out.println("Dealer cards sum: " + dealerS1 + " / " + dealerS2);
			}
			else if (dealerS2 == 21){
				System.out.println("Dealer cards sum: " + dealerS2);
			}
			else {
				System.out.println("Dealer cards sum: " + dealerS1);
			}
		}
		else {
			System.out.println("Dealer cards sum: " + dealerS1);
		}
        
        System.out.println();
        
        System.out.println("Dealer cards:");
        
        for( Cards card : dealerCards ) {
        	System.out.println(card.rank + " of " + card.suit);
        }
        
        System.out.println();
	}}
Editor is loading...
Leave a Comment