-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack.java
More file actions
67 lines (63 loc) · 2.26 KB
/
Copy pathBlackjack.java
File metadata and controls
67 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Blackjack
{
private int dealerCards = (int)((Math.random())*21);
private int playerCards = (int)((Math.random())*21);
private JLabel score1 = new JLabel("The dealer has "+dealerCards);
private JLabel score2 = new JLabel("You have "+playerCards);
private JFrame window = new JFrame("Game");
public Blackjack()
{
JLabel dealer = new JLabel("Would you like to hit or stay?");
JButton hit = new JButton(new AbstractAction("Hit"){
@Override
public void actionPerformed(ActionEvent e){
playerCards += (int)((Math.random())*15);
score2.setText("You have "+playerCards);
if(dealerCards != 21){
dealerCards += (int)((Math.random())*10);
score1.setText("The dealer has "+dealerCards);
}
if(dealerCards > 21){
System.out.println("The dealer went bust, you won!");
System.exit(0);
}
if(playerCards > 21){
System.out.println("You went bust! Game Over.");
System.exit(0);
}
}
});
JButton stay = new JButton(new AbstractAction("Stay"){
@Override
public void actionPerformed(ActionEvent e){
if(dealerCards != 21){
dealerCards += (int)((Math.random())*14);
score1.setText("The dealer has "+dealerCards);
}
if(dealerCards > 21){
System.out.println("The dealer went bust, you won!");
System.exit(0);
}
}
});
JPanel board = new JPanel();
board.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
board.setLayout(new GridLayout(0, 1));
board.add(dealer);
board.add(score1);
board.add(score2);
board.add(hit);
board.add(stay);
window.add(board, BorderLayout.CENTER);
window.setSize(500, 500);
window.setTitle("Blackjack");
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setVisible(true);
}
public static void main(String[] args){
new Blackjack();
}
}