|
| 1 | +/** |
| 2 | + * @file PlayBestCardAction.js file |
| 3 | + * @author Steven on 2020-06-10 |
| 4 | + */ |
| 5 | + |
| 6 | +import ActionHandler from '@/classes/ai/ActionHandler' |
| 7 | +import helpers from '@/classes/ai/aiHelpers' |
| 8 | + |
| 9 | +/** |
| 10 | + * Attempts to play cards from an AI players hand based on a given |
| 11 | + * piority. Each card has a reasonable but simple strategy for attempting |
| 12 | + * to play it. |
| 13 | + * |
| 14 | + * If a card type is not included in the priority it will not be played |
| 15 | + * even if it is in the hand. This make it possible to make more complicated |
| 16 | + * ActionHandlers that can be added to the AiHandler if desired. |
| 17 | + */ |
| 18 | +export default class PlayBestCardAction extends ActionHandler { |
| 19 | + /** |
| 20 | + * Creates a new PlayBestCardAction class |
| 21 | + * @constructor PlayBestCardAction |
| 22 | + * @param player The player that this handler is for. |
| 23 | + * @param playOrder A list of cards type in the order that they should be |
| 24 | + * considered for play. Types not in the order will never be played. |
| 25 | + */ |
| 26 | + constructor (player, playOrder) { |
| 27 | + super(player) |
| 28 | + this.playOrder = this.createOrder(playOrder) |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Creates an object that maps card types to priorities to be used when |
| 33 | + * sorting the players hand. |
| 34 | + */ |
| 35 | + createOrder (playOrder) { |
| 36 | + let cardOrder = {} |
| 37 | + for (let i = 0; i < playOrder.length; i++) { |
| 38 | + cardOrder[playOrder[i]] = i |
| 39 | + } |
| 40 | + return cardOrder |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns an move object for playing the higest priority card in the |
| 45 | + * players hand or undefined if none of them can be played. |
| 46 | + * |
| 47 | + * A mini chain of responsibility for cards that uses internal functions for |
| 48 | + * each card type for now. |
| 49 | + */ |
| 50 | + handle (hand, players, stacks, scores) { |
| 51 | + let cards = this.sortHand(hand) |
| 52 | + for (let card of cards) { |
| 53 | + let type = card.type.toLowerCase() |
| 54 | + |
| 55 | + // Finding the correct method for this card type |
| 56 | + if (card.type in this.playOrder) { |
| 57 | + let move |
| 58 | + if (card.isSafety()) { |
| 59 | + move = this.playSafety(card) |
| 60 | + } else if (card.isAttack()) { |
| 61 | + move = this.playAttack(card, players, scores) |
| 62 | + } else if (type in this) { |
| 63 | + move = this[type](card, {hand, players, stacks, scores}) |
| 64 | + } |
| 65 | + if (move) { return move } |
| 66 | + } |
| 67 | + } |
| 68 | + return undefined |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns a sorted list of the cards in a players hand. |
| 73 | + * Sorts by lowest order value and then by highest card value. |
| 74 | + * Cards types that are not in the playOrder will move to back. |
| 75 | + */ |
| 76 | + sortHand (hand) { |
| 77 | + return hand.cards.sort((a, b) => { |
| 78 | + if (!(a.type in this.playOrder)) { return 1 } |
| 79 | + else if (!(b.type in this.playOrder)) { return -1 } |
| 80 | + |
| 81 | + if (a.type === b.type) { |
| 82 | + return b.value - a.value |
| 83 | + } |
| 84 | + return this.playOrder[a.type] - this.playOrder[b.type] |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Make a move for an instruction card. |
| 90 | + * It is currently always possible to start a new instruction if a player |
| 91 | + * has an instruction card. |
| 92 | + * @param card The card to attempt to play. |
| 93 | + * @param state an object with all the state needed to make a decision |
| 94 | + * @return a move object for starting a new stack with the given card. |
| 95 | + */ |
| 96 | + instruction (card, state) { // eslint-disable-line no-unused-vars |
| 97 | + return { |
| 98 | + playType: 'startNewStack', |
| 99 | + card: card, |
| 100 | + player: this.player, |
| 101 | + target: this.player |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Make a move for adding a repeat card to the largest stack that |
| 107 | + * is available. |
| 108 | + * Should prioritize group stacks over normal stacks (even if lower in value?) |
| 109 | + * @param card The card to attempt to play. |
| 110 | + * @param state an object with all the state needed to make a decision |
| 111 | + * @return a move object for adding a repeat to a stack, or undefined if |
| 112 | + * no stack can be played on. |
| 113 | + */ |
| 114 | + repeat (card, state) { |
| 115 | + // get the player owned stack with the largest score |
| 116 | + let stack = state.stacks.filter((s) => { |
| 117 | + return s.playerId === this.player.id && s.willAccept(card) |
| 118 | + }).sort((a, b) => { |
| 119 | + return b.getScore() - a.getScore() |
| 120 | + }).shift() |
| 121 | + |
| 122 | + if (stack) { |
| 123 | + return { |
| 124 | + playType: 'playCardOnStack', |
| 125 | + card: card, |
| 126 | + player: this.player, |
| 127 | + target: stack |
| 128 | + } |
| 129 | + } |
| 130 | + return undefined |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Make a move for adding a variable card to the best stack available. |
| 135 | + * Prioritizes unmatched Rx cards, then stack with lowest variable in it. |
| 136 | + * @param card The card to attempt to play. |
| 137 | + * @param state an object with all the state needed to make a decision |
| 138 | + * @return a move object for adding a variable to a stack, or undefined if |
| 139 | + * no stack can be played on. |
| 140 | + */ |
| 141 | + variable (card, state) { |
| 142 | + let stacks = state.stacks.filter((s) => { |
| 143 | + return s.playerId === this.player.id && s.willAccept(card) |
| 144 | + }) |
| 145 | + let stack = stacks.sort(helpers.varStackCompare).shift() |
| 146 | + |
| 147 | + if (stack) { |
| 148 | + return { |
| 149 | + playType: 'playCardOnStack', |
| 150 | + card: card, |
| 151 | + player: this.player, |
| 152 | + target: stack |
| 153 | + } |
| 154 | + } |
| 155 | + return undefined |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Hack another players stack under specific conditions. |
| 160 | + * Will not hack single card stacks as this is a waste. Picks the biggest |
| 161 | + * stack available that meets the criteria. |
| 162 | + * @param card The card to attempt to play. |
| 163 | + * @param state an object with all the state needed to make a decision |
| 164 | + * @return a move object for hacking a stack, or undefined if |
| 165 | + * no stack can be hacked. |
| 166 | + */ |
| 167 | + hack (card, state) { |
| 168 | + let stack = state.stacks.filter((s) => { |
| 169 | + return s.playerId !== this.player.id && s.cards.length > 1 && s.isHackable() |
| 170 | + }).sort((a, b) => { |
| 171 | + return b.getScore() - a.getScore() |
| 172 | + }).shift() |
| 173 | + |
| 174 | + if (stack) { |
| 175 | + return { |
| 176 | + playType: 'hackStack', |
| 177 | + card: card, |
| 178 | + player: this.player, |
| 179 | + target: stack |
| 180 | + } |
| 181 | + } |
| 182 | + return undefined |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Play a safety card on oneself if not already protected. |
| 187 | + * @param card The card to attempt to play. |
| 188 | + * @param state an object with all the state needed to make a decision |
| 189 | + * @return a move object for playing a safety, or undefined |
| 190 | + * if the player is already protected. |
| 191 | + */ |
| 192 | + playSafety (card) { |
| 193 | + if (!this.player.helpedBy(card.type)) { |
| 194 | + return { |
| 195 | + playType: 'playSpecialCard', |
| 196 | + card: card, |
| 197 | + player: this.player, |
| 198 | + target: this.player |
| 199 | + } |
| 200 | + } |
| 201 | + return undefined |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Play an attack card on the opponent with the highest score that |
| 206 | + * is not already attacked by or protected from the card. |
| 207 | + * @param card The card to attempt to play. |
| 208 | + * @param state an object with all the state needed to make a decision |
| 209 | + * @return a move object for playing an attack, or undefined |
| 210 | + * no target can be found. |
| 211 | + */ |
| 212 | + playAttack (card, players, scores) { |
| 213 | + let target = players.filter((p) => { |
| 214 | + return p.id !== this.player.id && !p.hurtBy(card.type) && !p.isProtectedFrom(card.type) |
| 215 | + }).sort((a, b) => { |
| 216 | + return scores[b.id].score - scores[a.id].score |
| 217 | + }).shift() |
| 218 | + |
| 219 | + if (target) { |
| 220 | + return { |
| 221 | + playType: 'playSpecialCard', |
| 222 | + card: card, |
| 223 | + player: this.player, |
| 224 | + target: target |
| 225 | + } |
| 226 | + } |
| 227 | + return undefined |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Find the grouping of stacks that uses the most stacks if one exists. |
| 232 | + * @param card The card to attempt to play. |
| 233 | + * @param state an object with all the state needed to make a decision |
| 234 | + * @return a move object for grouping some stacks, or undefined if |
| 235 | + * no group can be found. |
| 236 | + */ |
| 237 | + group (card, state) { |
| 238 | + let groupable = state.stacks.filter((s) => { |
| 239 | + // don't group single group cards with the same value as card |
| 240 | + return s.playerId === this.player.id && s.getScore() <= card.value |
| 241 | + }) |
| 242 | + if (groupable.length == 0) { return undefined } |
| 243 | + |
| 244 | + let stacks = helpers.groupStacks(card.value, groupable) |
| 245 | + if (stacks.size > 0) { |
| 246 | + return { |
| 247 | + playType: 'groupStacks', |
| 248 | + card: card, |
| 249 | + player: this.player, |
| 250 | + target: stacks |
| 251 | + } |
| 252 | + } |
| 253 | + return undefined |
| 254 | + } |
| 255 | +} |
| 256 | + |
0 commit comments