-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
142 lines (119 loc) · 4.96 KB
/
Main.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
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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
if (args.length < 4) {
System.out.println("Usage: ./client server_hostname server_port " +
"opponent_level(1=dumb, 5, 7, 8) own_level(1=dumb, 5, 7, 8)");
return;
}
// variabile pentru conexiune
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
// variabile pentru joc;
Player ownPlayer = null, theOtherPlayer = null;
String endln = System.getProperty("line.separator");
try {
// realizez conexiunea la server
socket = new Socket(args[0], Integer.parseInt(args[1]));
// scriu in out pe socket
out = new DataOutputStream(socket.getOutputStream());
// citesc din in de pe socket
in = new DataInputStream(socket.getInputStream());
// trimit primul mesaj - dificulatea adversarului
byte[] message = new byte[1];
message[0] = Byte.parseByte(args[2]);
ServerConnection.sendMessage(message, out);
// primesc raspuns cu culoarea mea
message = ServerConnection.readMessage(in);
if (message[0] == 1) {
ownPlayer = new BlackPlayer();
theOtherPlayer = new WhitePlayer();
System.out.println("Sunt jucatorul Negru.");
} else if (message[0] == 0) {
ownPlayer = new WhitePlayer();
theOtherPlayer = new BlackPlayer();
System.out.println("Sunt jucatorul Alb.");
} else {
System.out.println("Eroare atribuire jucator.");
}
Board board = new Board(ownPlayer);
ExpectiMax exp = new ExpectiMax(theOtherPlayer);
int nMoves, from, span, dice1, dice2, round=0;
byte[] moveCommand;
while (true) {
message = ServerConnection.readMessage(in);
/* Se primeste un mesaj de la server care contine miscarile adversarului
si zarul propriu. Daca suntem primii la mutare, atunci nu primim
mutari din partea adversarului. Daca adversarul nu a putut sa mute,
atunci nu primim mutari din partea adversarului. */
round++;
nMoves = 0;
if (message[0] == 76){
System.out.println("Jocul a fost pierdut.");
break;
}
if (message[0] == 87){
System.out.println("Jocul a fost castigat!");
break;
}
System.out.println("");
// Se actualizeaza tabla cu mutarile adversarului.
while (message.length - nMoves*2 > 2){
from = message[nMoves*2];
if (from == 30) {
from = 0;
}
span = message[nMoves*2+1];
board.makeMove(from, span, theOtherPlayer);
nMoves++;
}
System.out.println("Runda " + round + endln);
System.out.println(board);
/* Daca jocul s-a terminat se iese din bucla */
if (board.gameEnded()){
break;
}
dice1 = message[nMoves*2];
dice2 = message[nMoves*2+1];
System.out.println("Zarurile aruncate: " + dice1 + " " + dice2);
/* Apelam functia cu zarurile sortate crescator pentru a include
* toate mutarile posibile */
if (dice1 < dice2) {
exp.doBestChoice(board, dice1, dice2);
}
else {
exp.doBestChoice(board, dice2, dice1);
}
moveCommand = exp.getBestChoice().getMoveCommand();
/* Compunerea mesajului ce urmeaza sa fie trimis catre
* server. */
byte[] response;
if (moveCommand[0] == 0){
response = new byte[0];
}
else {
response = new byte[moveCommand[0]*2];
for(int i = 0; i < response.length; i++){
response[i] = moveCommand[i+1];
if (response[i] == 0) {
response[i] = 30;
}
}
}
round++;
// Afisam noua tabla de joc.
System.out.println("Runda " + round + endln);
board = exp.getBestChoice();
System.out.println(board);
ServerConnection.sendMessage(response, out);
}
socket.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}