66import Model .PreProcess ;
77import Model .card .Card ;
88
9+ import Model .card .hermione .Hermione ;
910import Model .card .hermione .Hero ;
1011import Model .card .hermione .Minion ;
1112import Model .card .spell .Spell ;
1213import Model .card .spell .Target ;
1314import exeption .*;
1415
1516
17+ import java .util .ArrayList ;
1618import java .util .Random ;
1719
1820public class AI extends Account {
1921 int level ;
20- int move ; //0: insert card , 1: move Hero , 2: attack with hero , 3: move minions , 4: attack with minions
22+ int move ; //0: insert card , 1&3: select hero, 2: move Hero ,
23+ // 4: attack with hero ,and then : minions : select-move-select-attack
2124 Map map ;
2225 Player enemy ;
2326
@@ -43,24 +46,79 @@ public AI(int level) throws FullDeckException, DeckAlreadyHasThisCardException,
4346 }
4447
4548 public String play () {
49+ move ++ ;
4650 String command ;
4751 map = Game .battle .getMap ();
4852 enemy = Game .battle .getEnemyPlayer ();
49- Random randTypeCard = new Random ();
50- int randCard = randTypeCard .nextInt (2 );
51- if (randCard == 0 ) {
52- command = insertMinion ();
53- } else {
54- command = insertSpell ();
53+ switch (move ){
54+ case 0 :
55+ Random randTypeCard = new Random ();
56+ int randCard = randTypeCard .nextInt (2 );
57+ if (randCard == 0 ) {
58+ command = insertMinion ();
59+ } else {
60+ command = insertSpell ();
61+ }
62+ if (command != null && !command .isEmpty ()) return command ;
63+ else move ++ ;
64+ case 1 :
65+ case 3 :
66+ command = "Select " + collection .getMainDeck ().getHero ().getCardID ();
67+ return command ;
68+ case 2 :
69+ Hero hero = collection .getMainDeck ().getHero ();
70+ for (int i = 2 ; i >0 ; i --){
71+ Cell [] cells = map .getCellsInDistance (hero .getLocation () , i ) ;
72+ for (Cell cell : cells ){
73+ if (cell .getCardOnCell () == null ) {
74+ command = "Move to (" + cell .getX () + ", " + cell .getY ()+")" ;
75+ return command ;
76+ }
77+ }
78+ }
79+ move ++ ;
80+
81+ case 4 :
82+ hero = collection .getMainDeck ().getHero () ;
83+ command = attack (hero );
84+ if (command != null ) return command ;
85+ else move ++;
86+ default :
87+ if (move > player .getMinionsInGame ().size () + 5 ) {
88+ move = -1 ;
89+ return "End turn" ;
90+ }
91+ if (move % 2 == 1 ){
92+ command = "Select " + player .getMinionsInGame ().get (move -5 ) ;
93+ return command ;
94+ }
95+ else {
96+ Minion card = player .getMinionsInGame ().get (move -6 ) ;
97+ command = attack (card ) ;
98+ return command ;
99+ }
55100 }
56- if (command != null && !command .isEmpty ()) return command ;
57-
58- Hero hero = collection .getMainDeck ().getHero ();
59-
60-
61- return "End turn" ;
62101 }
63102
103+ private String attack (Hermione card ) {
104+ String command ;
105+ if (card .canAttackThisCard (enemy .getDeck ().getHero ())){
106+ command = "Attack " + enemy .getDeck ().getHero ();
107+ return command ;
108+ }
109+ Random rand = new Random ();
110+ ArrayList <Minion > target = new ArrayList <>(enemy .getMinionsInGame ()) ;
111+ int counter = 0 ;
112+ for (int i = rand .nextInt (target .size ()) ; true ; i = rand .nextInt (target .size ())){
113+ counter ++ ;
114+ if (card .canAttackThisCard (target .get (i ))){
115+ command = "Attack " + target .get (i );
116+ return command ;
117+ }
118+ if (counter > 30 ) break ;
119+ }
120+ return null ;
121+ }
64122
65123 private String insertMinion () {
66124 String command = "" ;
@@ -146,7 +204,7 @@ private Deck getDeck(int level) throws FullDeckException, DeckAlreadyHasThisCard
146204 DeckAlreadyHasThisItemException e ) {
147205 throw e ;
148206 }
149-
207+ break ;
150208 case 2 :
151209 try {
152210 deck .addCardToDeck (PreProcess .getHeroes ().get (4 ));
@@ -178,7 +236,7 @@ private Deck getDeck(int level) throws FullDeckException, DeckAlreadyHasThisCard
178236 DeckAlreadyHasThisItemException e ) {
179237 throw e ;
180238 }
181-
239+ break ;
182240 case 3 :
183241 try {
184242 deck .addCardToDeck (PreProcess .getHeroes ().get (6 ));
0 commit comments