import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Blackjack extends JFrame implements ActionListener {
JButton [] start;
//JButton [] bet;
JButton [] top;
JButton [] bottom;
JLabel [] stats;
JLabel [] playCards;
JTextField [] display;
BorderLayout layout;
JPanel gameField;
JPanel startOption;
JPanel topDisplay;
JPanel sideButton;
JPanel bottomButton;
BufferedImage[] pCards;
BufferedImage[] dCards;
String [] getCards;
String [] pCardImg;
String [] dCardImg;
int [] dface;
int [] dvalue;
int [] dsuit;
int [] pface;
int [] pvalue;
int [] psuit;
double betTotal;
double winLoss;
double cash;
int total;
int dtotal;
int stay;
int initial;
int dinitial;
public static void main (String[] args) {
Blackjack Gui = new Blackjack();
Gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public Blackjack(){
//startvärden när spelet startar
initial = 0;
dinitial = 5;
cash = 100.00;
winLoss = 0.00;
total = 0;
dtotal = 0;
stay = 0;
pCardImg = new String [6];
dCardImg = new String [6];
dface = new int [6];
dvalue = new int [6];
dsuit = new int [6];
getCards = new String [10];
pface = new int [6];
pvalue = new int [6];
psuit = new int [6];
betTotal = 0;
startOption = new JPanel();
topDisplay = new JPanel();
sideButton = new JPanel();
bottomButton = new JPanel();
gameField = new JPanel();
//de olika delarna i GUI
startOption.setLayout(new GridLayout(1, 1));
topDisplay.setLayout(new GridLayout(1, 1));
sideButton.setLayout(new GridLayout(5, 1));
bottomButton.setLayout(new GridLayout(1, 3));
gameField.setLayout(new GridLayout(2, 5));
//själva spelytan
playCards = new JLabel[10];
playCards[0] = new JLabel ("");
playCards[1] = new JLabel ("");
playCards[2] = new JLabel ("");
playCards[3] = new JLabel ("");
playCards[4] = new JLabel ("");
playCards[5] = new JLabel ("");
playCards[6] = new JLabel ("");
playCards[7] = new JLabel ("");
playCards[8] = new JLabel ("");
playCards[9] = new JLabel ("");
//starta spelet
start = new JButton[1];
start[0] = new JButton("START");
//välkommen
display = new JTextField[1];
display [0] = new JTextField("Välkommen");
display [0].setEditable(false);
//spelar statistik
stats = new JLabel[5];
stats[0] = new JLabel("Totalt: 0", 10);
stats[1] = new JLabel("Satsat: 0.00", 10);
stats[0] = new JLabel("Kort Kvar: 52", 10);
stats[0] = new JLabel("Pengar Kvar: " + cash, 10);
stats[0] = new JLabel("Total V/F: " + winLoss, 10);
//nedre knapparna
bottom = new JButton[3];
bottom[0] = new JButton("Ta Kort");
bottom[1] = new JButton("Stanna");
bottom[2] = new JButton("Dela");
//layouten och lägger till komponenter
layout = new BorderLayout(5, 5);
startOption.add(start[0]);
topDisplay.add(display[0]);
for (int i = 0; i < 5; i++){
sideButton.add(stats[i]);
}
for (int i = 0; i < 3; i++){
bottomButton.add(bottom[i]);
}
for (int i = 0; i < 10; i++){
gameField.add(playCards[i]);
}
add(gameField, BorderLayout.CENTER);
add(startOption, BorderLayout.WEST);
add(topDisplay, BorderLayout.NORTH);
add(sideButton, BorderLayout.EAST);
add(bottomButton, BorderLayout.SOUTH);
//lyssnare
start[0].addActionListener(this);
for(int i = 0; i < 3; i++){
bottom[i].addActionListener(this);
}
//inställningar för fönstret
setTitle("Blackjack");
setSize(800, 500);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] check = {"Ja", "Nej"};
Object[] bet = {"5.00", "10.00", "15.00"};
JButton c = (JButton)(e.getSource());
if(c.getText().equals("Hit")){
initial += 1;
JOptionPane.showMessageDialog(this,"Dealern gav dig ett kort");
dealCards(initial);
stats[0].setText("Totalt: " + total);
if(total > 21){
JOptionPane.showMessageDialog(this, "Du blev tjock");
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0.00;
stats[1].setText("Satsat: " + betTotal);
clearHand();
gotoContinue();
}
}
if(c.getText().equals("Stanna")){
/*for (int i = 0; i > 6; i++){
System.out.println{pface(i)};
*/
stay = 1;
JOptionPane.showMessageDialog(this, "Du stannar");
if (stay == 1){
dealerDraws();
delay(1000);
}
if (dtotal < 17 && stay == 1){
dealerDraws();
delay(1000);
}
}
if(c.getText().equals("Dela")){
int chk = JOptionPane.showOptionDialog(this,"Är du säker?", "Dubbel kolla", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
check,
check[2]);
}
if(c.getText().equals("START")){
int b = JOptionPane.showOptionDialog(this, "Satsa minsta belopp eller högre.", "Var vänlig satsa", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
bet,
bet[2]);
if (b == 0){
betTotal += 5.00;
}
if (b == 1){
betTotal += 10.00;
}
if (b == 2){
betTotal += 15.00;
}
stats[1].setText("Satsat: " + betTotal);
betTotal -= betTotal;
stats[3].setText("Pengar Kvar: " + total);
stay = -1;
int i = 0;
dealCards(i);
System.out.println("TEST");
stats[0].setText("Totalt: €" + total);
}
}
public void dealCards(int j){
String [] temp = new String [4];
if (j == 0){
Shuffle myDealer = new Shuffle ();
myDealer.ShuffleCards();
for (int i = 0; i < 4; i++){
getCards[i] = myDealer.Dealer();
temp[i] = getCards[i];
System.out.println(getCards[i]);
}
printCards(temp, j);
}
if (j >= 1){
Shuffle myDealer = new Shuffle();
myDealer.ShuffleCards();
for (int i = 0; i < 2; i++){
getCards[i] = myDealer.Dealer();
temp[i] = getCards[i];
System.out.println(getCards[i]);
}
printCards(temp, j);
}
}
public void printCards(String [] temp, int j){
if (j == 0){
String [] elements0 = temp[0].split(" ");
String [] elements1 = temp[1].split(" ");
String [] elements2 = temp[2].split(" ");
String [] elements3 = temp[3].split(" ");
//spelarens hand
pface[0] = Integer.parseInt(elements0[0]);
pface[1] = Integer.parseInt(elements1[0]);
psuit[0] = Integer.parseInt(elements0[1]);
psuit[1] = Integer.parseInt(elements0[1]);
//dealerns hand
dface[0] = Integer.parseInt(elements0[0]);
dface[1] = Integer.parseInt(elements0[0]);
dsuit[0] = Integer.parseInt(elements0[1]);
dsuit[1] = Integer.parseInt(elements0[1]);
for (int i = 0; i < 2; i++){
if (dface[i] > 10){
dvalue[i] = 10;
}
else{
dvalue[i] = dface[i];
}
if(pface[i] > 10){
pvalue[i] = 10;
}
else{
pvalue[i] = pface[i];
}
//spelarens ess värde
if(pface[i] == 1 && total <= 11){
pvalue[i] += 10;
}
if(pface[0] == 1 && pface[1] == 1){
total = 12;
}
//dealerns ess värde
if(dface[i] == 1 && dtotal <= 11){
dvalue[i] += 10;
}
if(dface[0] == 1 && dface[1] == 1){
dtotal = 12;
}
System.out.println("Spelare: " + pface[i] +" " + psuit[i] + " " + pvalue[i]);
total += pvalue[i];
System.out.println(total);
System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
dtotal += dvalue[i];
System.out.println(dtotal);
getPSuit();
getDSuit();
}
//Kollar efter Blackjack (en perfekt 21a)och gångrar din satsning med tre, vid eventuell vinst
if (total == 21 && dtotal < 21){
JOptionPane.showMessageDialog(this,"BLACKJACK!");
cash += betTotal * 3;
winLoss += betTotal = 3;
stats[3].setText("Pengar kvar: " + (cash));
stats[4].setText("Total V/F: " + winLoss);
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0;
stats[1].setText("Total satsning: " + betTotal);
clearHand();
gotoContinue();
}
}
if (j == 1){
String [] elements0 = temp [0].split(" ");
pface[2] = Integer.parseInt(elements0[0]);
psuit[2] = Integer.parseInt(elements0[1]);
for (int i = 2; i < 3; i++){
if (pface[i] > 10){
pvalue[i] = 10;
}
else{
pvalue[i] = pface[i];
}
//spelarens ess värde
if (pface[i] == 1 && total <= 11){
pvalue[i] += 10;
}
for ( int k = 0; k < 6; k++){
if( pface[k] == 1 && total > 21) {
pvalue[k] -= 10;
}
}
System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
total += pvalue[i];
System.out.println(total);
}
getPSuit();
}
if (j == 2) {
String [] elements0 = temp[0].split(" ");
pface[3] = Integer.parseInt(elements0[0]);
psuit[3] = Integer.parseInt(elements0[1]);
for (int i = 3; i < 4; i++){
if(pface[i] > 1){
pvalue[i] = 10;
}
else{
pvalue[i] = pface[i];
}
//spelarens ess värde
if (pface[i] == 1 && total <= 10){
pvalue[i] += 10;
}
for ( int k = 0; k < 6; k++){
if ( pface[k] == 1 && total > 21){
pvalue[k] -= 10;
}
}
System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
total += pvalue[i];
System.out.println(total);
}
getPSuit();
}
if (j == 3) {
String [] elements0 = temp[0].split(" ");
pface[4] = Integer.parseInt(elements0[0]);
psuit[4] = Integer.parseInt(elements0[1]);
for (int i = 4; i < 5; i++){
if(pface[i] > 10){
pvalue[i] = 10;
}
else{
pvalue[i] = pface[i];
}
//spelarens ess värde
if (pface[i] == 1 && total <= 10);{
pvalue[i] += 1;
}
for ( int k = 0; k < 6; k++){
if (pface[k] == 1 && total > 21){
pvalue[k] -= 10;
}
}
System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
total += pvalue[i];
System.out.println(total);
}
getPSuit();
}
if (j == 4) {
String [] elements0 = temp[0].split(" ");
pface[5] = Integer.parseInt(elements0[0]);
psuit[5] = Integer.parseInt(elements0[1]);
for (int i = 5; i < 6; i++) {
if(pface[i] > 10){
pvalue[i] = 10;
}
else{
pvalue[i] = pface[i];
}
//spelarens ess värde
if (pface[i] == 1 && total <= 10){
pvalue[i] += 10;
}
for ( int k = 0; k < 6; k++){
if ( pface[k] == 1 && total > 21){
pvalue[k] -= 10;
}
}
System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
total += pvalue[i];
System.out.println(total);
}
getPSuit();
}
if (j == 5){
String [] elements0 = temp[0].split(" ");
dface[2] = Integer.parseInt(elements0[0]);
dsuit[2] = Integer.parseInt(elements0[1]);
for (int i = 2; i < 3; i++){
if (dface[i] > 1){
dvalue[i] = 10;
}
else{
dvalue[i] = dface[i];
}
//dealerns ess värde
if (dface[i] == 1 && dtotal <= 10){
dvalue[i] += 10;
}
for ( int k = 0; k < 6; k++){
if ( dface[k] == 1 && dtotal > 21){
dvalue[k] -= 10;
}
}
System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
dtotal += dvalue[i];
System.out.println(dtotal);
}
getPSuit();
}
if (j == 6){
String [] elements3 = temp[0].split(" ");
dface[3] = Integer.parseInt(elements3[0]);
dsuit[3] = Integer.parseInt(elements3[1]);
for (int i = 3; i < 4; i++){
if (dface[i] > 10){
dvalue[i] = 10;
}
else{
dvalue[i] = dface[i];
}
// dealerns ess värde
if (dface[i] == 1 && dtotal <= 10){
dvalue[i] += 10;
}
if (dface[i] == 1 && dtotal <= 21){
dvalue[i] -= 10;
}
System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
dtotal += dvalue [i];
System.out.println(dtotal);
}
getPSuit();
}
if (j == 7) {
String [] elements3 = temp[0].split(" ");
dface[4] = Integer.parseInt(elements3[0]);
dsuit[4] = Integer.parseInt(elements3[1]);
for (int i = 4; i < 5; i++){
if (dface[i] > 10){
dvalue[i] = 10;
}
else{
dvalue[i] = dface[i];
}
//dealerns ess värde
if (dface[i] == 1 && dtotal <= 10){
dvalue[i] += 10;
}
if (dface[i] == 1 && dtotal <= 21){
dvalue[i] -= 10;
}
System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
dtotal += dvalue [i];
System.out.println(dtotal);
}
getDSuit();
}
if (j == 8) {
String [] elements3 = temp[0].split(" ");
dface[5] = Integer.parseInt(elements3[0]);
dsuit[5] = Integer.parseInt(elements3[1]);
for (int i = 5; i < 6; i++){
if (dface[i] > 10){
dvalue[i] = 10;
}
else{
dvalue[i] = dface[i];
}
//dealerns ess värde
if (dface[i] == 1 && dtotal <= 10){
dvalue[i] += 10;
}
if (dface[i] == 1 && dtotal <= 21){
dvalue[i] -= 10;
}
System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
dtotal += dvalue [i];
System.out.println(dtotal);
}
getDSuit();
}
}
public void dealerDraws(){
if (dtotal < 17 && stay == 1){
dealCards(dinitial);
dinitial += 1;
}
if (dtotal >= 17 && dtotal <= 21){
delay(1000);
JOptionPane.showMessageDialog(this, "Dealern stannar");
checkHandWin();
}
if (dtotal > 21){
JOptionPane.showMessageDialog(this,"Dealern blev tjock");
checkHandWin();
}
}
public void checkHandWin(){
if (total > dtotal || dtotal > 21){
JOptionPane.showMessageDialog(this,"Du Vann!");
initial = 0;
cash += betTotal * 2;
winLoss += betTotal;
stats[3].setText("Pengar Kvar: " + (cash));
stats[4].setText("Total V/F: " + winLoss);
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0;
stats[1].setText("Total Satsning: " + betTotal);
clearHand();
gotoContinue();
}else{
checkHandLost();
}
}
public void checkHandLost(){
if(dtotal > total){
JOptionPane.showMessageDialog(this,"Du Förlorade!");
initial = 0;
dinitial = 5;
winLoss -= betTotal;
stats[3].setText("Pengar Kvar: " + (cash));
stats[4].setText("Total V/F: " + winLoss);
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0;
stats[1].setText("Total Satsning: " + betTotal);
clearHand();
gotoContinue();
}else{
checkHandTie();
}
}
public void checkHandTie(){
if (dtotal == total){
JOptionPane.showMessageDialog(this, "Lika");
initial = 0;
dinitial = 5;
cash += betTotal;
stats[3].setText("Pengar Kvar: " + (cash));
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0;
stats[1].setText("Total Satsning: " + betTotal);
clearHand();
gotoContinue();
}
else{
initial = 0;
dinitial = 5;
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
cash += betTotal * 2;
stats[3].setText("Pengar Kvar: " + (cash));
betTotal = 0.00;
stats[1].setText("Total Satsning: " + betTotal);
clearHand();
gotoContinue();
}
}
public void gotoContinue(){
initial = 0;
dinitial = 5;
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0;
stats[1].setText("Total Satsning: " + betTotal);
Object[] check = {"Ja", "Nej"};
Object[] bet = {"5.00", "10.00", "15.00"};
int a = JOptionPane.showOptionDialog(this,"Vill du spela en ny hand?", "Fortsätta?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
check,
check[2]);
if (a == 0){
int b = JOptionPane.showOptionDialog(this, "Satsa minsta belopp eller högre.", "Var vänlig satsa", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
bet,
bet[2]);
if (b == 0){
betTotal += 5.00;
}
if (b == 1){
betTotal += 10.00;
}
if (b == 2){
betTotal += 15.00;
}
stats[1].setText("Total Satsning: " + betTotal);
cash -= betTotal;
stats[3].setText("Pengar Kvar: " + (cash));
stay = -1;
int i = 0;
dealCards(i);
System.out.println("TEST");
stats[0].setText("Totalt: " + total);
}
if (a == 1) {
if (cash > 50.00){
double winnings = cash - 50.00;
JOptionPane.showMessageDialog(this, "Du vann " + winnings );
}
if (cash < 50.00) {
double losses = 50.00 - cash;
JOptionPane.showMessageDialog(this, "Du förlorade " + losses );
}
clearHand();
initial = 0;
dinitial = 0;
cash = 50.00;
winLoss = 0.00;
total = 0;
dtotal = 0;
stats[0].setText("Totalt: " + total);
betTotal = 0.00;
stats[1].setText("Total Satsning: " + betTotal);
stats[3].setText("Pengar Kvar: " + (cash));
stats[4].setText("Total V/F: " + winLoss);
}
}
public void clearHand(){
for (int i = 0; i < 6; i++){
dface[i] = 0;
pface[i] = 0;
dvalue[i] = 0;
pvalue[i] = 0;
dsuit[i] = 0;
psuit[i] = 0;
}
}
public void getDSuit(){
String [] suit = new String [5];
for (int i = 0; i < 5; i++){
if (dsuit[i] == 1){
suit[i] = "D";
}
if (dsuit[i] == 2){
suit[i] = "C";
}
if (dsuit[i] == 3){
suit[i] = "H";
}
if (dsuit[i] == 4){
suit[i] = "S";
}
dCardImg[i] = suit[i] + dface[i] + ".jpg";
}
dealerHand();
}
public void getPSuit(){
String [] suit = new String [5];
for (int i = 0; i < 5; i++){
if (dsuit[i] == 1){
suit[i] = "D";
}
if (dsuit[i] == 2){
suit[i] = "C";
}
if (dsuit[i] == 3){
suit[i] = "H";
}
if (dsuit[i] == 4){
suit[i] = "S";
}
pCardImg[i] = suit[i] + pface[i] + ".jpg";
}
playerHand();
}
public void playerHand(){
pCards = new BufferedImage[5];
for (int i = 0; i < 5; i++){
try {
pCards[i] = ImageIO.read(new File(pCardImg[i]));
playCards[i] = new JLabel(new ImageIcon(pCards[i]));
add (playCards[i]);
repaint();
} catch (IOException e) {
}
}
}
public void dealerHand(){
dCards = new BufferedImage[5];
for (int i = 0; i < 5; i++){
try {
dCards[i] = ImageIO.read(new File(dCardImg[i]));
playCards[i] = new JLabel(new ImageIcon(dCards[i]));
add (playCards[i]);
repaint();
} catch (IOException e) {
}
}
}
public void delay(long time){
try{
Thread.sleep(time);
}
catch(Exception e){
}
}
public void paint(Graphics g){
super.paint(g);
if (pCards[0] != null){
g.drawImage(pCards[0], 100,300, null);
}
if (pCards[1] != null){
g.drawImage(pCards[1], 150,325, null);
}
if (pCards[2] != null){
g.drawImage(pCards[2], 200,300, null);
}
if (pCards[3] != null){
g.drawImage(pCards[3], 250,325, null);
}
if (pCards[4] != null){
g.drawImage(pCards[4], 300,300, null);
}
if (dCards[0] != null){
g.drawImage(dCards[0], 500,55, null);
}
if (dCards[1] != null){
g.drawImage(dCards[1], 450,80, null);
}
if (dCards[2] != null){
g.drawImage(dCards[2], 400,55, null);
}
if (dCards[3] != null){
g.drawImage(dCards[3], 350,80, null);
}
if (dCards[4] != null){
g.drawImage(dCards[4], 300,55, null);
if (dCards[2] != null){
g.drawImage(dCards[2], 400,55, null);
}
if (dCards[3] != null){
g.drawImage(dCards[3], 350,80, null);
}
if (dCards[4] != null){
g.drawImage(dCards[4], 300,55, null);
}
}
}
}