-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoobPlayerAgent.java
326 lines (265 loc) · 8.97 KB
/
NoobPlayerAgent.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package book;
import jade.core.Agent;
import jade.core.AID;
import jade.core.behaviours.*;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class NoobPlayerAgent extends Agent{
private static final long serialVersionUID = 1L;
public ArrayList<String> cardTypes= new ArrayList<String>();
public ArrayList<String> cardNumbers= new ArrayList<String>();
public ArrayList<Card> cards= new ArrayList<>();
public int wallet=100;
private String finalAction="";
public int actualBet=0;
public ArrayList<Card> tableCards= new ArrayList<>();
public ArrayList<Card> ownCards= new ArrayList<>();
public void createCards()
{
cardTypes.add("clubs");
cardTypes.add("diamonds");
cardTypes.add("hearts");
cardTypes.add("spades");
//Adding numerations and combinations
for (int i=2;i<=10;i++)
{
cardNumbers.add(Integer.toString(i));
}
cardNumbers.add("A");
cardNumbers.add("J");
cardNumbers.add("K");
cardNumbers.add("Q");
int index=0;
//Putting all cards to array
for(int i=0;i< cardTypes.size();i++)
{
for (int j=0;j<cardNumbers.size();j++)
{
Card actualCard= new Card(cardTypes.get(i), cardNumbers.get(j),index);
index+=1;
cards.add(actualCard);
}
}
}
@Override protected void setup()
{
//Template of agent service
DFAgentDescription description = new DFAgentDescription();
description.setName(getAID());
description.addLanguages("en");
//Service description template
ServiceDescription service= new ServiceDescription();
service.setType("player");
service.setName("noob");
//Creating cards
createCards();
description.addServices(service);
try{
DFService.register(this,description);
} catch (FIPAException e)
{
e.printStackTrace();
}
addBehaviour(new NoobBehaviour());
}
@Override
protected void takeDown()
{
try
{
DFService.deregister(this);
}
catch (FIPAException fe)
{
fe.printStackTrace();
}
System.out.println("El agente "+getAID().getName()+" ya no ofrece sus servicios.");
}
class NoobBehaviour extends SimpleBehaviour{
@Override public void action(){
System.out.println("Noob is ready to take turn");
ACLMessage msg= blockingReceive();
System.out.println("Noob received msg");
if(msg!=null)
{
System.out.println(msg.getContent());
splitMessage(msg.getContent());
System.out.println("My cards are");
System.out.println(ownCards);
System.out.println("My highest rank is: ");
int rank= getRankHand();
String r="";
System.out.println(rank);
ACLMessage response= new ACLMessage(ACLMessage.CONFIRM);
response.addReceiver(msg.getSender());
response.setLanguage("en");
//Chose behaviour
if (finalAction=="fold")
{
r+="fold|0";
}
else if (actualBet==0)
{
r+="bet|";
r+= Integer.toString(rank);
}
else{
if (rank>25)
{
r+="check|";
r+=Integer.toString(rank);
}
else
{
r+="fold|0";
finalAction="fold";
}
}
response.setContent(r);
send(response);
}
}
@Override public boolean done()
{
return false;
}
}
public int getRankHand()
{
int highestHand=0;
for(int i=0;i<tableCards.size();i++)
{
for (int j=0;j<tableCards.size();j++)
{
for(int k=0;k<tableCards.size();k++)
{
if (i!=j && j!=k && i!=k)
{
//Creating comparisons
ArrayList<Card> actualHand= new ArrayList<>();
actualHand.add(ownCards.get(0));
actualHand.add(ownCards.get(1));
actualHand.add(tableCards.get(i));
actualHand.add(tableCards.get(j));
actualHand.add(tableCards.get(k));
int actualRank= rankHand(actualHand);
if(actualRank>highestHand)
{
highestHand=actualRank;
}
}
}
}
}
return highestHand;
}
public int rankHand(ArrayList<Card> hand)
{
int rank=0;
//Looking for royal flush and Straight
ArrayList<Card> royalFlush= new ArrayList<>();
String[] flush={"A","K","Q","J","10"};
//Getting hand rank
Map<String,Integer> cardValues= new HashMap<>();
cardValues.put("A",1);
cardValues.put("2",2);
cardValues.put("3",3);
cardValues.put("4",4);
cardValues.put("5",5);
cardValues.put("6",6);
cardValues.put("7",7);
cardValues.put("8",8);
cardValues.put("9",9);
cardValues.put("10",10);
cardValues.put("J",11);
cardValues.put("Q",12);
cardValues.put("K",13);
for(Card c : hand)
{
rank+= cardValues.get(c.getCardNumber());
}
//First royal flush
/*boolean isFlush=true;
if(allSameSuit(hand))
{
for (int i=0;i<hand.size();i++)
{
for(int j=0;j<flush.length;j++)
{
boolean contains = Arrays.stream(flush).anyMatch(hand.get(i).getCardNumber()::equals);
if(!contains)
{
isFlush= false;
break;
}
}
}
if(isFlush) return 10;
//Checking for straights
boolean isStraight=true;
String[] straight={"2","3","4","5","6","7","8","9","10","A","J","K","Q"};
for(int i=0;i<7;i++)
{
ArrayList<String> extractedStraight= new ArrayList<>();
String extractedHand="";
for (int j=0;j<5;j++)
{
extractedStraight.add(straight[i+j]);
}
for (int j=0;j<hand.size();j++)
{
if(!extractedStraight.contains(hand.get(j).getCardNumber()))
{
isStraight=false;
break;
}
}
}
if(isStraight) return 9;
//Checking for four of a kind
}
*/
return rank;
}
public boolean allSameSuit(ArrayList<Card> hand)
{
String suit= hand.get(0).getCardType();
for(int i=0;i<hand.size();i++)
{
if(!hand.get(i).getCardType().equalsIgnoreCase(suit))
{
return false;
}
}
return true;
}
public void splitMessage(String msg)
{
//This message will set the table cards, own cards and bet
String[] splitMsg= msg.split("\\|");
//There will be 3 split parts, bet-tableCards-playerCards
actualBet= Integer.parseInt(splitMsg[0]);
//Now we resplit the tableCards
String[] tableCardsStr= splitMsg[1].split(",");
tableCards.clear();
ownCards.clear();
for (int i=0;i<tableCardsStr.length;i++)
{
tableCards.add(cards.get(Integer.parseInt(tableCardsStr[i])));
}
String[] ownCardsStr= splitMsg[2].split(",");
for (int i=0;i<ownCardsStr.length;i++)
{
ownCards.add(cards.get(Integer.parseInt(ownCardsStr[i])));
}
System.out.println(ownCards);
}
}