-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice.java
59 lines (50 loc) · 1.49 KB
/
Dice.java
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
import java.util.Random;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Dice extends JButton{
static int number = 0;
static int turn = 0;
static ArrayList<Player> playerList;
static JLabel info;
Dice(ArrayList<Player> pl, JLabel i){
initGUI();
playerList = pl;
info = i;
class Trykk implements ActionListener{
int number = 0;
Random ran = new Random();
Dice dice;
JLabel info;
Trykk(Dice dice, JLabel i){
this.dice = dice;
info = i;
}
@Override
public void actionPerformed(ActionEvent e){
Dice.number = ran.nextInt(6) + 1;
dice.setText(Integer.toString(Dice.number));
Dice.movePlayer(playerList.get(turn));
if(turn < playerList.size()-1){
turn++;
}
else if (playerList.size() != 1) turn--;
info.setText(playerList.get(turn).getName() + "'s move: ");
}
}
this.addActionListener(new Trykk(this, info));
}
public static void movePlayer(Player p){
p.move(number);
}
public void initGUI(){
setPreferredSize(new Dimension(50,50));
setOpaque(true);
setFont(new Font("Arial", Font.PLAIN, 18));
setText("");
}
public void gameOver(){
this.setEnabled(false);
}
}