-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
199 lines (160 loc) · 4.51 KB
/
Player.java
File metadata and controls
199 lines (160 loc) · 4.51 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
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;
public class Player {
protected String name;
protected int port;
protected InetAddress address;
protected PrintWriter out;
protected HashMap<String, Integer> gameScores;
protected Socket socket;
protected GameHubGUI gui;
Tetris game = null;
protected boolean exit = false;
int score;
public Player(String name) {
}
public Player(String name, PrintWriter out) {
this.name = name;
this.out = out;
gameScores = new HashMap<>();
gameScores.put("TETRIS", 0);
gameScores.put("SNAKE", 0);
}
public boolean donePlaying() {
return exit;
}
public Socket getSocket() {
return socket;
}
public String getPlayerName() {
return this.name;
}
public void setPlayerName(String name) {
this.name = name;
}
public void setPort(int port) {
this.port = port;
}
public int getPort() {
return this.port;
}
public void setAddress(InetAddress address) {
this.address = address;
}
public InetAddress getAddress() {
return this.address;
}
public PrintWriter getWriter() {
return this.out;
}
public void setWriter(PrintWriter out) {
this.out = out;
}
public void addScore(int score, String game) {
if (gameScores.containsKey(game)) {
int prevscore = gameScores.get(game);
if (score > prevscore) {
gameScores.put(game, score);
}
}
}
public int getScore(String game) {
int score = 0;
if (gameScores.containsKey(game)) {
score = gameScores.get(game);
}
return score;
}
public void addGame(String game) {
if (!gameScores.containsKey(game)) {
gameScores.put(game, 0);
}
}
public void addHub(GameHubGUI gui) {
this.gui = gui;
}
public void runSingleGame() {
game = new Tetris(this);
game = Tetris.getInstance(this);
game.setAlwaysOnTop(true);
game.setVisible(true);
game.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
score = game.findScore();
addScore(score, "TETRIS");
// gameScores.put("TETRIS", score);
// out.println("TETRIS:" + name + ":" + score);
out.println("REPORT:" + name + ":" + score);
System.out.println("REPORT:" + name + ":" + score);
}
});
game.addComponentListener(new ComponentListener() {
@Override
public void componentHidden(ComponentEvent e) {
score = game.findScore();
addScore(score, "TETRIS");
// gameScores.put("TETRIS", score);
// out.println("TETRIS:" + name + ":" + score);
out.println("REPORT:" + name + ":" + score);
System.out.println("REPORT:" + name + ":" + score);
}
public void componentMoved(ComponentEvent e) {
// displayMessage(e.getComponent().getClass().getName() + " ---
// Moved");
}
public void componentResized(ComponentEvent e) {
// displayMessage(e.getComponent().getClass().getName() + " ---
// Resized ");
}
public void componentShown(ComponentEvent e) {
// displayMessage(e.getComponent().getClass().getName() + " ---
// Shown");
}
});
}
public void runTetris() {
game = new Tetris(this);
game = Tetris.getInstance(this);
game.setAlwaysOnTop(true);
game.setVisible(true);
game.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
score = game.findScore();
// gameScores.put("TETRIS", score);
addScore(score, "TETRIS");
out.println("TETRIS:" + name + ":" + score);
out.println("GETSCORE" + name);
}
});
game.addComponentListener(new ComponentListener() {
@Override
public void componentHidden(ComponentEvent e) {
score = game.findScore();
addScore(score, "TETRIS");
// gameScores.put("TETRIS", score);
out.println("TETRIS:" + name + ":" + score);
out.println("GETSCORE" + name);
}
public void componentMoved(ComponentEvent e) {
// displayMessage(e.getComponent().getClass().getName() + " ---
// Moved");
}
public void componentResized(ComponentEvent e) {
// displayMessage(e.getComponent().getClass().getName() + " ---
// Resized ");
}
public void componentShown(ComponentEvent e) {
// displayMessage(e.getComponent().getClass().getName() + " ---
// Shown");
}
});
}
}