-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClobber.java~
1087 lines (942 loc) · 35.4 KB
/
Clobber.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.lang.reflect.*;
/**
* Clobber is a great game where bots shoot the heck out of each other.
*/
public class Clobber extends JFrame
{
/**************************************************************************************************/
/************************ Instance Data ***********************************************************/
/**************************************************************************************************/
private int _width;
private int _height;
private int min_x;
private int min_y;
private int max_x;
private int max_y;
private int delay=25;
private int bot_bul_thresh;
private int bot_bot_thresh;
private int shoot_frequency=20;
private int bot_step_size=2;
private int bullet_step_size=4;
private Graphics page;
private Vector<ClobberBotMan> bots;
private Vector<ClobberBotMan> deadbots;
private Vector<ClobberBullet> bullets;
private Vector<String> playerFileNames;
private Vector<String> playerDirNames;
private Vector<ArgHandler> argHandlers;
private Random rand = new Random();
private boolean tournament=false;
private int numTournaments=0;
private boolean display=true;
private int nextID=0;
private JPanel panel;
/**************************************************************************************************/
/************************ Public Constants ********************************************************/
/**************************************************************************************************/
public static final int EDGE_BUFFER=5;
public static final int BOT_EDGE_BUFFER=15;
public static final int BULLET_EDGE_BUFFER=5;
public static final int MAX_BOT_GIRTH=15;
public static final int BULLET_GIRTH=5;
public static final int MIN_START_DISTANCE=MAX_BOT_GIRTH*4;
/**************************************************************************************************/
/************************ Inner Class declarations ************************************************/
/**************************************************************************************************/
private abstract class ClobberObject
{
Point2D pos;
Point2D oldpos;
boolean deleteMe;
int clobberObjectID;
public void updatePosition(int i, int j)
{
if(i<min_x) i=min_x;
else if(i>=max_x) i=max_x;
if(j<min_y) j=min_y;
else if(j>=max_y) j=max_y;
oldpos.setLocation(pos);
pos.setLocation(i,j);
}
public double distance(ClobberObject o)
{
return pos.distance(o.pos);
}
public abstract boolean collision(ClobberObject o);
}
private class ClobberBotRes
{
int numKills;
int numSurvived;
int numDied;
int exceptionCount;
int points;
ClobberBot bot;
public String toString()
{
return bot + "\t" + points + "\t" + numDied + "\t" + numKills + "\t" + numSurvived + "\t" + exceptionCount + "\n";
}
public ClobberBotRes(ClobberBot bot)
{
numKills=0;
numDied=0;
exceptionCount=0;
points=0;
this.bot = bot;
}
}
private class ClobberBotMan extends ClobberObject
{
ClobberBot bot;
ClobberBotAction action;
int shotclock;
int numKills;
int numDied;
int exceptionCount;
ClobberBotRes res;
int bmin_x;
int bmax_x;
int bmin_y;
int bmax_y;
public ClobberBotMan(ClobberBot bot, double x, double y, ClobberBotRes res)
{
numKills=0;
numDied=0;
exceptionCount=0;
this.bot = bot;
this.res = res;
pos = new Point2D.Double(x,y);
oldpos = new Point2D.Double(x,y);
deleteMe=false;
clobberObjectID = nextID++;
bmin_x=min_x+BOT_EDGE_BUFFER;
bmax_x=max_x-BOT_EDGE_BUFFER;
bmin_y=min_y+BOT_EDGE_BUFFER;
bmax_y=max_y-BOT_EDGE_BUFFER;
}
/*public void updatePosition(int i, int j)
{
if(i<bmin_x) i=bmin_x;
else if(i>=bmax_x) i=bmax_x;
if(j<bmin_y) j=bmin_y;
else if(j>=bmax_y) j=bmax_y;
oldpos.setLocation(pos);
pos.setLocation(i,j);
}*/
public boolean collision(ClobberObject o)
{
if(o instanceof ClobberBotMan)
return (this.distance(o) < bot_bot_thresh);
if(o instanceof ClobberBullet)
return (this.distance(o) < bot_bul_thresh);
return false;
}
public String toString()
{
return bot + ": kills = " + numKills + ", deaths = " + numDied;
}
}
private class ClobberBullet extends ClobberObject
{
ClobberBotMan owner;
int xplus, yplus;
public ClobberBullet(ClobberBotMan owner, double x, double y, int xplus, int yplus)
{
this.owner = owner;
pos = new Point2D.Double(x,y);
oldpos = new Point2D.Double(x,y);
this.xplus = xplus;
this.yplus = yplus;
deleteMe=false;
clobberObjectID = nextID++;
}
public boolean collision(ClobberObject o)
{
if(o instanceof ClobberBullet)
return (this.distance(o) < bot_bul_thresh);
return false;
}
}
/**************************************************************************************************/
/************************ Argument Handler Classes ************************************************/
/**************************************************************************************************/
private class NoDisplayArg extends ArgHandler
{
public NoDisplayArg() { arg="-nodisplay"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
display=false;
return ++x;
}
else return x;
}
}
private class TournamentArg extends ArgHandler
{
public TournamentArg() { arg="-tournament"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
tournament=true;
display=false;
numTournaments = Integer.parseInt(args[++x]);
return ++x;
}
else return x;
}
public String toString() { return "[" + arg + " x]"; }
}
private class DelayArg extends ArgHandler
{
public DelayArg() { arg="-delay"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
delay=Integer.parseInt(args[++x]);
return ++x;
}
else return x;
}
public String toString() { return "[" + arg + " x]"; }
}
private class LoadPlayerArg extends ArgHandler
{
public LoadPlayerArg() { arg="-loadplayer"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
System.out.println("Matched -loadplayer");
playerFileNames.add(args[++x]);
return ++x;
}
else return x;
}
public String toString() { return "[" + arg + " player.class]"; }
}
private class LoadPlayersArg extends ArgHandler
{
public LoadPlayersArg() { arg="-loadplayers"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
playerDirNames.add(args[++x]);
return ++x;
}
else return x;
}
public String toString() { return "[" + arg + " list_name.txt]"; }
}
private class ShotFrequencyArg extends ArgHandler
{
public ShotFrequencyArg() { arg="-shotFrequency"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
shoot_frequency=Integer.parseInt(args[++x]);
return ++x;
}
else return x;
}
public String toString() { return "[" + arg + " x]"; }
}
private class BotStepSizeArg extends ArgHandler
{
public BotStepSizeArg() { arg="-botStepSize"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
bot_step_size=Integer.parseInt(args[++x]);
return ++x;
}
else return x;
}
public String toString() { return "[" + arg + " x]"; }
}
private class BulletStepSizeArg extends ArgHandler
{
public BulletStepSizeArg() { arg="-bulletStepSize"; }
public int handleArg(String[] args, int x) throws Exception
{
if(argMatch(args[x]))
{
bullet_step_size=Integer.parseInt(args[++x]);
return ++x;
}
else return x;
}
}
/**************************************************************************************************/
/************************ Getter methods **********************************************************/
/**************************************************************************************************/
/** returns the max x coordinate position */
public int getMaxX()
{
return max_x;
}
/** returns the max y coordinate position */
public int getMaxY()
{
return max_y;
}
/** returns the min x coordinate position */
public int getMinX()
{
return min_x;
}
/** returns the min y coordinate position */
public int getMinY()
{
return min_y;
}
/** returns the number of turns that must elapse between shots */
public int getShotFrequency()
{
return shoot_frequency;
}
/** returns the number number of steps in the x and/or y direction a bot may move per turn */
public int getBotStepSize()
{
return bot_step_size;
}
/** returns the number number of steps in the x and/or y direction a bullet moves per turn */
public int getBulletStepSize()
{
return bullet_step_size;
}
/**************************************************************************************************/
/************************ Constructors and game initalization code ********************************/
/**************************************************************************************************/
/** Initializes the argument handlers for parsing command line arguments */
private void initArgHandlers()
{
argHandlers = new Vector<ArgHandler>();
argHandlers.add(new NoDisplayArg());
argHandlers.add(new TournamentArg());
argHandlers.add(new DelayArg());
argHandlers.add(new ShotFrequencyArg());
argHandlers.add(new BotStepSizeArg());
argHandlers.add(new BulletStepSizeArg());
argHandlers.add(new LoadPlayerArg());
argHandlers.add(new LoadPlayersArg());
}
/** Main loop for parsing command line arguments */
private void getUserPrefs(String[] args)
{
int x=0;
int y=0;
try
{
while(x<args.length)
{
for(int z=0;z<argHandlers.size();z++)
{
y=argHandlers.get(z).handleArg(args,x);
if(x!=y) break;
}
if(x==y) usage();
x=y;
}
}
catch(Exception e)
{
usage();
}
}
private double getMinDistanceToBot(ClobberObject o)
{
double mindistance = 10000000000000.0;
for(int x=0;x<bots.size();x++)
{
double distance = bots.get(x).distance(o);
if(distance < mindistance) mindistance = distance;
}
return mindistance;
}
private void tallyResults()
{
for(int x=0;x<bots.size();x++)
{
bots.get(x).res.numKills += bots.get(x).numKills;
bots.get(x).res.points += (bots.get(x).numKills * 10);
bots.get(x).res.points += 4;
bots.get(x).res.numSurvived++;
}
for(int x=0;x<deadbots.size();x++)
{
deadbots.get(x).res.numKills += deadbots.get(x).numKills;
deadbots.get(x).res.points += (deadbots.get(x).numKills * 1);
deadbots.get(x).res.numDied++;
deadbots.get(x).res.exceptionCount += deadbots.get(x).exceptionCount;
}
}
private void addBotsToGame(Vector<ClobberBot> clobberbots)
{
for(int x=0;x<clobberbots.size();x++)
addBotToGame(clobberbots.get(x),null);
}
private void addBotToGame(ClobberBot bot, ClobberBotRes res)
{
int numtries=0;
ClobberBotMan botman;
double mindistance;
do
{
numtries++;
if(numtries > 1000) throw new RuntimeException("I Can't find a place to put all the bots");
botman = new ClobberBotMan(bot, rand.nextInt(_width), rand.nextInt(_height), res);
mindistance = getMinDistanceToBot(botman);
} while(mindistance < MIN_START_DISTANCE);
bots.add(botman);
}
/** Constructor called from main to create a game */
private Clobber(String[] args)
{
playerFileNames = new Vector<String>();
playerDirNames = new Vector<String>();
initArgHandlers();
getUserPrefs(args);
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.BLACK);
this.setContentPane(panel);
setBackground(Color.black);
this.show();
_width=(int)(this.getContentPane().getSize().getWidth());
_height=(int)(this.getContentPane().getSize().getHeight());
_width=600;
_height=600;
this.dispose();
min_x=EDGE_BUFFER;
min_y=EDGE_BUFFER;
max_x=_width-EDGE_BUFFER;
max_y=_height-EDGE_BUFFER;
bot_bul_thresh = (MAX_BOT_GIRTH + BULLET_GIRTH)/2;
bot_bot_thresh = (MAX_BOT_GIRTH);
bots = new Vector<ClobberBotMan>();
deadbots = new Vector<ClobberBotMan>();
bullets = new Vector<ClobberBullet>();
}
/**************************************************************************************************/
/************************ Game loop code **********************************************************/
/**************************************************************************************************/
/** Fills out WhatIKnow data structure with the current world state */
private WhatIKnow getWorldState(ClobberBotMan cbm)
{
BotPoint2D me = new BotPoint2D(cbm.pos.getX(), cbm.pos.getY(),cbm.clobberObjectID);
Vector<BulletPoint2D> bulpts= new Vector<BulletPoint2D>();
Vector<BulletPoint2D> mybulpts= new Vector<BulletPoint2D>();
for(int x=0;x<bullets.size();x++)
if(cbm != bullets.get(x).owner) bulpts.add(new BulletPoint2D(bullets.get(x).pos.getX(), bullets.get(x).pos.getY(), bullets.get(x).clobberObjectID, bullets.get(x).xplus, bullets.get(x).yplus));
else mybulpts.add(new BulletPoint2D(bullets.get(x).pos.getX(), bullets.get(x).pos.getY(), bullets.get(x).clobberObjectID, bullets.get(x).xplus, bullets.get(x).yplus));
Vector<BotPoint2D> botpts = new Vector<BotPoint2D>();
for(int x=0;x<bots.size();x++)
if(cbm != bots.get(x)) botpts.add(new BotPoint2D(bots.get(x).pos.getX(), bots.get(x).pos.getY(), bots.get(x).clobberObjectID));
return new WhatIKnow(me, bulpts, mybulpts, botpts);
}
/** Asks each bot what it wants to do*/
private void getBotActions()
{
for(int x=0;x<bots.size();x++)
{
ClobberBotMan cbm = bots.get(x);
try
{
cbm.action = cbm.bot.takeTurn(getWorldState(cbm));
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
cbm.action=null;
cbm.deleteMe=true;
cbm.numDied++;
cbm.exceptionCount++;
}
}
}
/** performs the set of actions the bots have decided upon. This may result in a bot moving or shooting */
private void performBotActions()
{
for(int x=0;x<bots.size();x++)
{
ClobberBotMan cbm = bots.get(x);
if(cbm.shotclock>0) cbm.shotclock--;
int i=(int)(cbm.pos.getX());
int j=(int)(cbm.pos.getY());
if(cbm.action==null) return;
int action = cbm.action.getAction();
if((action & ClobberBotAction.MOVE)>0)
{
if((action & ClobberBotAction.UP)>0) j -= bot_step_size;
if((action & ClobberBotAction.DOWN)>0) j += bot_step_size;
if((action & ClobberBotAction.LEFT)>0) i -= bot_step_size;
if((action & ClobberBotAction.RIGHT)>0) i += bot_step_size;
cbm.updatePosition(i,j);
}
else if(((action & ClobberBotAction.SHOOT)>0) && (cbm.shotclock<=0))
{
cbm.shotclock=shoot_frequency;
int xplus=0; int yplus=0;
if((action & ClobberBotAction.UP)>0) yplus -= bullet_step_size;
if((action & ClobberBotAction.DOWN)>0) yplus += bullet_step_size;
if((action & ClobberBotAction.LEFT)>0) xplus -= bullet_step_size;
if((action & ClobberBotAction.RIGHT)>0) xplus += bullet_step_size;
if(xplus!=0 || yplus!=0) bullets.add(new ClobberBullet(cbm, cbm.pos.getX(), cbm.pos.getY(), xplus, yplus));
}
}
}
/** updates the positions of all the bullets */
private void updateBulletPositions()
{
for(int x=bullets.size()-1;x>=0;x--)
{
ClobberBullet bul = bullets.get(x);
int i=(int)(bul.pos.getX()) + bul.xplus;
int j=(int)(bul.pos.getY()) + bul.yplus;
bul.updatePosition(i,j);
i=(int)(bul.pos.getX());
j=(int)(bul.pos.getY());
if((i<=min_x) || (i>=max_x) || (j<=min_y) || (j>=max_y)) bullets.remove(x);
}
}
/** Checks for collisions between bullets and bots */
private void checkBulletVSBotCollisions()
{
for(int x=bullets.size()-1;x>=0;x--)
{
ClobberBullet bul = bullets.get(x);
for(int y=bots.size()-1;y>=0;y--)
{
ClobberBotMan bot = bots.get(y);
if(bul.owner==bot) continue;
//if(bot.distance(bul) < bot_bul_thresh)
if(bot.collision(bul))
{
//System.out.println("Ouch!");
if(!bot.deleteMe) bot.numDied++;
bot.deleteMe=true;
bul.deleteMe=true;
bul.owner.numKills++; //@@@ Could potentially overcount kills
}
}
if(bul.deleteMe) bullets.remove(x);
}
}
/** Checks for collisions between bots */
private void checkBotVSBotCollisions()
{
for(int x=bots.size()-1;x>=0;x--)
{
ClobberBotMan bot1 = bots.get(x);
for(int y=x-1;y>=0;y--)
{
if(y==x) continue;
ClobberBotMan bot2 = bots.get(y);
//if(bot1.distance(bot2) < bot_bot_thresh)
if(bot1.collision(bot2))
{
if(!bot1.deleteMe) bot1.numDied++;
if(!bot2.deleteMe) bot2.numDied++;
//System.out.println("Ooof!");
bot1.deleteMe=true;
bot2.deleteMe=true;
bot1.numKills++; // @@@ Could overcount kills here
bot2.numKills++; // @@@ could overcount kills here
}
}
if(bot1.deleteMe) deadbots.add(bots.remove(x));
}
}
/** Updates the display. */
private void updateDisplay(Thread t)
{
if(!display) return;
try { t.sleep(delay); } catch(Exception e){}
page.clearRect(0,0,_width,_height);
for(int x=0;x<bots.size();x++)
{
ClobberBotMan cbm = bots.get(x);
cbm.bot.drawMe(page, cbm.pos);
}
for(int x=bullets.size()-1;x>=0;x--)
{
ClobberBullet bul = bullets.get(x);
int i=(int)(bul.pos.getX());
int j=(int)(bul.pos.getY());
int n=i-Clobber.BULLET_GIRTH/2 -1;
int m=j-Clobber.BULLET_GIRTH/2 -1;
page.setColor(Color.green);
page.fillOval(n,m,BULLET_GIRTH,BULLET_GIRTH);
}
try { t.wait(); } catch(Exception e){}
}
/** Prints the curr score of each bot to the screen */
public void printResults()
{
System.out.println("Living bots");
for(int x=0;x<bots.size();x++)
{
System.out.println(bots.get(x));
}
System.out.println("Dead bots");
for(int x=0;x<deadbots.size();x++)
{
System.out.println(deadbots.get(x));
}
}
/** Main game loop */
private void play()
{
int x=0;
int turnLimit=10000;
if(display)
{
this.show();
page = panel.getGraphics();
}
Thread t = new Thread();
t.start();
while(x<turnLimit)
{
x++;
getBotActions();
performBotActions();
updateBulletPositions();
if(display) updateDisplay(t);
checkBulletVSBotCollisions();
checkBotVSBotCollisions();
if(bots.size()<=1) break;
}
if(x==turnLimit)
{
//System.out.println("****************Turn Limit Reached **************************");
}
if(display)
{
this.dispose();
}
}
/** Prints out the syntax for running this program from the command line. */
public void usage()
{
System.out.print("Usage: java Clobber ");
for(int x=0;x<argHandlers.size();x++)
{
System.out.print(argHandlers.get(x));
}
System.out.println();
System.exit(-1);
}
class MyClassLoader extends ClassLoader {
Class cls;
public Class retClass () {
return cls;
}
public MyClassLoader (String classname) throws Exception
{
FileInputStream in = new FileInputStream (classname);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
buffer.write(ch);
}
byte[] rec = buffer.toByteArray();
cls = defineClass (classname.substring(0,classname.length()-6), rec, 0, rec.length);
}
}
private void loadHuman(Vector<ClobberBot> bots) throws Exception
{
bots.add(new GUIClobberBot(this));
}
private void loadPlayerFromFile(Vector<ClobberBot> bots, String fname) throws Exception
{
System.out.println("Loading player " + fname);
Class cls = new MyClassLoader(fname).retClass();
Constructor[] ct = cls.getConstructors();
Object[] oarr = new Object[1];
oarr[0]=this;
bots.add((ClobberBot)(ct[0].newInstance(this)));
//bots.add((ClobberBot)(ct[0].newInstance(oarr)));
}
private void loadPlayersInDir(Vector<ClobberBot> bots, String dirname) throws Exception
{
File dir = new File(dirname);
File[] contents = dir.listFiles();
for(int x=0;x<contents.length;x++)
{
String fname = contents[x].toString();
StringTokenizer st = new StringTokenizer(fname, "\\/");
st.nextToken();
playerFileNames.add(st.nextToken());
}
}
private void loadPlayersFromFile(Vector<ClobberBot> bots, String fname) throws Exception
{
BufferedReader br = new BufferedReader(new FileReader(fname));
String line;
while((line=br.readLine()) != null)
{
playerFileNames.add(line);
}
}
private Vector<ClobberBot> loadBots2() throws Exception
{
Vector<ClobberBot> bots = new Vector<ClobberBot>();
Iterator<String> it = playerDirNames.iterator();
while(it.hasNext())
{
loadPlayersFromFile(bots, it.next());
}
it = playerFileNames.iterator();
while(it.hasNext())
{
loadPlayerFromFile(bots, it.next());
}
return bots;
}
private Vector<ClobberBot> loadBots()
{
Vector<ClobberBot> bots = new Vector<ClobberBot>();
bots.add(new ClobberBot(null));
bots.add(new ClobberBot2(null));
bots.add(new ClobberBot3(this));
bots.add(new ClobberBot4(this));
//bots.add(new aalgawas(this ));
//bots.add(new chaney(this ));
//bots.add(new Dglen(this ));
//bots.add(new eatlakso(this ));
//bots.add(new eatlakso(this ));
//bots.add(new eatlakso(this ));
//bots.add(new eatlakso(this ));
//bots.add(new glyons(this ));
//bots.add(new jdohse(this ));
//bots.add(new jdohse(this ));
//bots.add(new jdohse(this ));
//bots.add(new jdohse(this ));
//bots.add(new jdohse(this ));
//bots.add(new Jrodrigu(this ));
//bots.add(new kybaker(this ));
//bots.add(new sobendor(this ));
//bots.add(new achild(this ));
//bots.add(new achild(this ));
//bots.add(new achild(this ));
//bots.add(new achild(this ));
//bots.add(new achild(this ));
return bots;
}
private class ClobberTourney
{
Vector<ClobberBotRes> b225;
Vector<ClobberBotRes> b125_1;
Vector<ClobberBotRes> b125_2;
Vector<ClobberBotRes> b125_3;
Vector<ClobberBotRes> brand;
Vector<ClobberBotRes> ball;
String[] args;
Clobber dummy;
private Vector<ClobberBotRes> load225Bots()
{
Vector<ClobberBotRes> clobberbots = new Vector<ClobberBotRes>();
clobberbots.add(new ClobberBotRes(new ClobberBot(null)));
clobberbots.add(new ClobberBotRes(new ClobberBot2(null)));
clobberbots.add(new ClobberBotRes(new ClobberBot3(null)));
clobberbots.add(new ClobberBotRes(new ClobberBot4(null)));
return clobberbots;
}
private Vector<ClobberBotRes> loadRandomBots()
{
Vector<ClobberBotRes> clobberbots = new Vector<ClobberBotRes>();
clobberbots.add(new ClobberBotRes(new ClobberBot(null)));
clobberbots.add(new ClobberBotRes(new ClobberBot2(null)));
clobberbots.add(new ClobberBotRes(new ClobberBot3(null)));
clobberbots.add(new ClobberBotRes(new ClobberBot4(null)));
return clobberbots;
}
private Vector<ClobberBotRes> loadSection1Bots()
{
Vector<ClobberBotRes> clobberbots = new Vector<ClobberBotRes>();
clobberbots.add(new ClobberBotRes(new p1(dummy)));
clobberbots.add(new ClobberBotRes(new p2(dummy)));
clobberbots.add(new ClobberBotRes(new p3(dummy)));
clobberbots.add(new ClobberBotRes(new p4(dummy)));
clobberbots.add(new ClobberBotRes(new p5(dummy)));
clobberbots.add(new ClobberBotRes(new p6(dummy)));
clobberbots.add(new ClobberBotRes(new p7(dummy)));
clobberbots.add(new ClobberBotRes(new p8(dummy)));
clobberbots.add(new ClobberBotRes(new p9(dummy)));
//clobberbots.add(new ClobberBotRes(new mweaver(dummy)));
//clobberbots.add(new ClobberBotRes(new crsteven(dummy)));
//clobberbots.add(new ClobberBotRes(new Dstone(dummy)));
//clobberbots.add(new ClobberBotRes(new epeterso(dummy)));
//clobberbots.add(new ClobberBotRes(new ClobberBotJA(dummy)));
//clobberbots.add(new ClobberBotRes(new jhodges(dummy)));
//clobberbots.add(new ClobberBotRes(new mgraybil(dummy)));
//clobberbots.add(new ClobberBotRes(new mlampe(dummy)));
//clobberbots.add(new ClobberBotRes(new agraham(dummy)));
return clobberbots;
}
private Vector<ClobberBotRes> loadSection2Bots()
{
Vector<ClobberBotRes> clobberbots = new Vector<ClobberBotRes>();
//clobberbots.add(new ClobberBotRes(new (dummy)));
return clobberbots;
}
private Vector<ClobberBotRes> loadSection3Bots()
{
Vector<ClobberBotRes> clobberbots = new Vector<ClobberBotRes>();
//clobberbots.add(new ClobberBotRes(new aalgawas(dummy)));
//clobberbots.add(new ClobberBotRes(new chaney(dummy)));
//clobberbots.add(new ClobberBotRes(new glyons(dummy)));
//clobberbots.add(new ClobberBotRes(new jdohse(dummy)));
//clobberbots.add(new ClobberBotRes(new Dglen(dummy)));
//clobberbots.add(new ClobberBotRes(new Jrodrigu(dummy)));
//clobberbots.add(new ClobberBotRes(new eatlakso(dummy)));
//clobberbots.add(new ClobberBotRes(new kybaker(dummy)));
//clobberbots.add(new ClobberBotRes(new sobendor(dummy)));
return clobberbots;
}
public String toString(Vector<ClobberBotRes> bots)
{
String str="BOT\tPoints\tnumDied\tnumKills\tnumSurvived\texcpetions\n";
for(int x=0;x<bots.size();x++)
str+=bots.get(x).toString();
return str;
}
public String toString()
{
String str="";
if(brand!=null) str += "Random bots\n" + toString(brand);
if(b225!=null) str += "225 bots\n" + toString(b225);
if(b125_1!=null) str += "125 section 1 bots\n" + toString(b125_1);
if(b125_2!=null) str += "125 section 2 bots\n" + toString(b125_2);
if(b125_3!=null) str += "125 section 3 bots\n" + toString(b125_3);
return str;
}
private ClobberBot constructInstance(ClobberBot bot) throws Exception
{
Class c = bot.getClass();
Class[] carr = {Clobber.class};
Constructor<ClobberBot> con = c.getConstructor(carr);
return con.newInstance(dummy);
}
public void runVSAllTourney(Vector<ClobberBotRes> bots, int numgames) throws Exception
{
for(int x=0;x<numgames;x++)
{
Clobber cb = new Clobber(args);
for(int y=0;y<bots.size();y++)
cb.addBotToGame(constructInstance(bots.get(y).bot), bots.get(y));
cb.play();
cb.tallyResults();
}
}
public void runPairwiseVSRandomTourney(Vector<ClobberBotRes> bots) throws Exception
{
Vector<ClobberBotRes> randBots = loadRandomBots();
for(int x=0;x<bots.size();x++)
{
for(int y=0;y<randBots.size();y++)
{
//System.out.println("Playing " + bots.get(x).bot + " vs " + bots.get(y).bot);
Clobber cb = new Clobber(args);
cb.addBotToGame(constructInstance(bots.get(x).bot), bots.get(x));
cb.addBotToGame(constructInstance(randBots.get(y).bot), randBots.get(y));
cb.play();
cb.tallyResults();
}
}
}
public void runPairwiseTourney(Vector<ClobberBotRes> bots) throws Exception
{
for(int x=0;x<bots.size();x++)
{
for(int y=x+1;y<bots.size();y++)
{
//System.out.println("Playing " + bots.get(x).bot + " vs " + bots.get(y).bot);
Clobber cb = new Clobber(args);
cb.addBotToGame(constructInstance(bots.get(x).bot), bots.get(x));
cb.addBotToGame(constructInstance(bots.get(y).bot), bots.get(y));