1 import java.util.Random;
2
10 public class BattleAI
11 {
12
19 public static Attack recommendMonsterAttack(Monster attacker, Monster opponent)
20 {
21
25 Monster myMonster = attacker;
26 Monster mangyBastard = opponent;
27
28 if(isWeakAgainst(myMonster.getType(),mangyBastard.getType()))
30 return myMonster.getAttack(myMonster.getAttacks().length-1);
31
32 if(isWeakAgainst(mangyBastard.getType(),myMonster.getType()))
34 return myMonster.getAttack(0);
35
36
42
43 int attack;
44 Random random = new Random();
45 random.setSeed(System.currentTimeMillis());
46 attack = random.nextInt(myMonster.getMaxLevel());
47 if(attack <= (myMonster.getLevel()-1))
48 return myMonster.getAttack(myMonster.getNumberOfAttacks()-1);
49 attack = random.nextInt(myMonster.getNumberOfAttacks()-1);
50 return myMonster.getAttack(attack);
51 }
52
53
65 private static Item itemLookUp(int itemType, int itemSubType, String[] items, People itemContainer)
66 {
67 for(int i = 0;i < items.length;i++)
68 {
69 if(items[i] != null)
70 {
71 Item item = itemContainer.getItem(items[i]);
72 if(item.getBrand() == itemType)
73 {
74 if(item.getSubBrand() == itemSubType)
75 return item;
76 }
77 }
78 }
79 return null;
80 }
81
94 public static Attack recommendPlayerAttack(People attacker, Player opponent) throws ItemChangeException, MonsterChangeException
95 {
96 Monster opp = opponent.getMonster(opponent.getActiveMonster());
97 Monster att = attacker.getMonster(attacker.getActiveMonster());
98
99
100
104 String[] items = attacker.getItems();
105
106 PotionItem potion = null;
107
108 if((100 * (att.getDefense()/att.getMaxDefense())) < 20)
109 {
110 potion = (PotionItem)itemLookUp(Item.POTION, PotionItem.DEFUP, attacker.getItems(), attacker);
111 if(potion != null)
112 throw new ItemChangeException(potion);
113 }
114
115 if((100 * (att.getAttack()/att.getMaxAttack())) < 20)
116 {
117 potion = (PotionItem)itemLookUp(Item.POTION, PotionItem.ATTUP, attacker.getItems(), attacker);
118 if(potion != null)
119 throw new ItemChangeException(potion);
120 }
121
122 if((100 * (att.getHP()/att.getMaxHP())) < 20)
123 {
124 potion = (PotionItem)itemLookUp(Item.POTION, PotionItem.HPUPFULL, attacker.getItems(), attacker);
125 if(potion != null)
126 throw new ItemChangeException(potion);
127
128 potion = (PotionItem)itemLookUp(Item.POTION, PotionItem.HPUPHALF, attacker.getItems(), attacker);
129 if(potion != null)
130 throw new ItemChangeException(potion);
131
132 potion = (PotionItem)itemLookUp(Item.POTION, PotionItem.HPUPTHIRD, attacker.getItems(), attacker);
133 if(potion != null)
134 throw new ItemChangeException(potion);
135 }
136
137 if( isWeakAgainst(opp.getKind(), att.getKind()) )
138 {
139 for(int i = 0; i < attacker.getNumberOfMonsters(); i++)
140 {
141 if( !isWeakAgainst(opp.getKind(), attacker.getMonster(i).getKind()) )
142 throw new MonsterChangeException(attacker.getActiveMonster(), i);
143 }
144 }
145 return recommendMonsterAttack(att, opp);
146 }
147
148
155 private static boolean isWeakAgainst(int plt, int opt) {
157 switch(plt)
158 {
159 case Monster.GAS:
160 {
161 if(opt == Monster.ELECTRIC || opt == Monster.EARTH)
162 return true;
163 return false;
164 }
165 case Monster.FIRE:
166 {
167 if(opt == Monster.GAS || opt == Monster.ELECTRIC)
168 return true;
169 return false;
170 }
171 case Monster.WATER:
172 {
173 if(opt == Monster.GAS || opt == Monster.FIRE)
174 return true;
175 return false;
176 }
177 case Monster.EARTH:
178 {
179 if(opt == Monster.WATER || opt == Monster.FIRE)
180 return true;
181 return false;
182 }
183 case Monster.ELECTRIC:
184 {
185 if(opt == Monster.EARTH || opt == Monster.WATER)
186 return true;
187 return false;
188 }
189 }
190 return false;
191 }
192
193 }