-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayers.java
96 lines (81 loc) · 2.56 KB
/
Players.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//Player class for players
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Players implements Runnable {
private int ID; //player ID
private int totNumFound; //number of numbers found(matched) with moderator
private GameData gameData; //data of the game
private JPanel playerBoard; //for adding player panels
private JButton[] btnTicket; //buttons for players (cannot be pressed) just to cross out matched numbers
private final static int UB = 50;
private final static int winNUM = 3;
private final static int numBTN = 10;
private int[] ticket = new int[numBTN];
public Players(GameData gameData, int ID) {
this.ID = ID;
this.gameData = gameData;
this.totNumFound = 0;
// initialize player board
playerBoard = new JPanel();
playerBoard.setLayout(new GridLayout(1,10));
btnTicket = new JButton[numBTN];
//generate random numbers for players
Random rand = new Random();
for(int i=0; i<numBTN; i++) {
int p = 0;
while(p==0) {
p = rand.nextInt(UB+1);
}
ticket[i] = p;
}
//initialise player buttons
for(int i = 0; i < numBTN; i++) {
btnTicket[i] = new JButton(String.valueOf(ticket[i]));
btnTicket[i].setEnabled(false);
playerBoard.add(btnTicket[i]);
}
}
public void run() {
synchronized(gameData.lock[0]) {
//condition for game to continue
while(!gameData.gameCompleteFlag) {
//players wait until their chance number is announced
while(!gameData.numAnnouncedFlag || gameData.chanceFlag[ID]) {
try {
gameData.lock[0].wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//if game is not complete yet
if(!gameData.gameCompleteFlag) {
//increment totNumFound if there is a match
for(int i = 0; i < numBTN; i++) {
if(gameData.announcedNum == ticket[i]) {
this.totNumFound++;
//update player buttons
this.btnTicket[i].setBackground(Color.YELLOW);
ticket[i] = 0;
break;
}
}
//if 3 numbers are found, player wins
if(this.totNumFound == winNUM) {
// player set the success flag
gameData.successFlag[this.ID] = true;
}
// player sets its chance flag
gameData.chanceFlag[ID] = true;
//notify all others waiting for lock[0]
gameData.lock[0].notifyAll();
}
}
}
}
public JPanel getPlayerTicketPanel() {
return playerBoard;
}
}