-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
219 lines (194 loc) · 5.87 KB
/
Copy pathPlayer.java
File metadata and controls
219 lines (194 loc) · 5.87 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* this class encapsulates all properties of a player-
* its type(human,ai, lan)
* its name
* its color
* time it has (through the temporizador object)
* its label in the gui
* message to be displayed for it in various occasions in the gui
*
* by default represents the human player
* and its derived classes represent AI,savedgame etc
*/
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
abstract class Player extends JComponent//for easy use in gui events
{
Color team;
String name;
Temporizador time;
nameLabel lab;
boolean isActivatedTurn=false;
boolean renamable=true;
volatile StringBuffer revival=new StringBuffer();
boolean hasCheck=false;
public Player(Color c, String nam)
{
team=c;
name=nam;
time=new Temporizador(this);
lab=new nameLabel("",JLabel.CENTER,this);
//lab.updateName();
lab.setToolTipText(getTeamName());
lab.setFont(CONSTANTS.windowfont);
game.onGameEnd(new Task()
{
public void perform()
{
lab.deHighlightName();
}
});
game.onGameStart(new Task()
{
public void perform()
{
lab.updateName();
}
});
lab.setOpaque(false);
lab.setFocusable(false);
}
/***
* this method will be used by filereader and ai to move gotis
*/
public void actualAction(Point toMove,Point moverA)//coordinates in terms of board not gui
{
goti g=game.chessBoard[toMove.x][toMove.y];
//System.out.println(toMove+" "+moverA);
toMove=gui.guidelegate(toMove,true);
Environment.log("goti selected for motion is"+g);
MouseEvent cli=new MouseEvent(this,0,9999,0,toMove.x+5,toMove.y+5,1,true);
game.getChessBoard().mousePressed(cli);
try
{
Thread.sleep(180);
}
catch (Exception ie)
{
ie.printStackTrace();
}
double k=0;
moverA=gui.guidelegate(moverA,true);
double kf=-10;
do
{
k=Math.pow(2,kf);
g.guiloc.x=(int) ((moverA.x*k+toMove.x)/(k+1));
g.guiloc.y=(int) ((moverA.y*k+toMove.y)/(k+1));
kf+=0.2;
gui.hold.repaint();
try
{
if(kf>7)
Thread.sleep(1);
else
Thread.sleep(9);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}while(kf<50);
MouseEvent rel=new MouseEvent(this,0,9999,0,moverA.x+5,moverA.y+5,1,true);
try
{
Thread.sleep(20);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
game.getChessBoard().mouseReleased(rel);
}
public Temporizador getTime()
{
return time;
}
public String getName()
{
return name;
}
public String getStringColor()
{
return "("+team.getRed()+","+team.getGreen()+","+team.getBlue()+")";
}
public Color getColor()
{
return team;
}
public nameLabel getGUIRepresentation()
{
return lab;
}
abstract public String getRevivalCandidate();
public void setName(String s)
{
Environment.log(toString()+" ka name changed to "+s);
name=s;
lab.updateName();
}
abstract public void playMove();
abstract public void endMove();
/**
* will be overriden by
* AI class to end stockfish process
* SavedGameReader class to close file resources
*/
abstract public void destroy();
/**
* except Human, all return themselves
* as human's mouse event is caused by the game panel itself
* while others manually trigger them, and pass themselves in the constructor of MouseEvent
*/
abstract public JComponent getMouseEventCauser();
@Override
public String toString()
{
return getColor()+" "+getName()+" "+getClass().getSimpleName();
}
public void performActions(boolean checktrue,ArrayList<Runnable> tasklist)
{
MessageAnimator exec=null;
Runnable aud=null;
exec=new MessageAnimator(game.getOpponentOf(this).getName()+" won!😎",false);
aud=(new Runnable()
{
public void run()
{
audio.play("win");
}
});
tasklist.add(exec);
tasklist.add(aud);
tasklist.add(new Runnable()
{
public void run()
{
if(checktrue)
gui.checkstat.setText(getName()+" has got a checkmate! ");//arrange for pronoun here too
else
gui.checkstat.setText(getName()+" has got a stalemate! ");//arrange for pronoun here too
game.setGameEnd();
}
});
}
public void setActivatedTurn()
{
if(isActivatedTurn)
return;
isActivatedTurn=true;
lab.highlightName();
game.getOpponentOf(this).lab.deHighlightName();
game.getOpponentOf(this).isActivatedTurn=false;
//System.out.println(this.toString()+" got activated");
}
public String getTeamName()//stored in the saved game file
{
if(team.equals(goti.colWhit))
return "white";
else
return "black";
}
}