jaredlyon / JareBot

A revisited JS project
GNU General Public License v3.0
5 stars 1 forks source link

Blackjack Module Fails to Read Pushes #1

Closed jaredlyon closed 4 years ago

jaredlyon commented 5 years ago

image

The player hit a blackjack, which should have been an auto-win in its own right (currently on the blackjack.js kanban); regardless, the bot failed to push on an equal hand.

jaredlyon commented 4 years ago

Issue lies in the calculateWinner function since the function is a boolean and the player can only win or lose:

  calculateWinner(player_hand, cpu_hand, cards) {
    let player_total = this.calculateTotal(player_hand);
    while (
      this.calculateTotal(cpu_hand) <= player_total &&
      this.calculateTotal(cpu_hand) < 21
    ) {
      cpu_hand.push(this.drawCard(cards));
    }
    let cpu_total = this.calculateTotal(cpu_hand);
    if (cpu_total > 21 || cpu_total <= player_total) {
      if (
        cpu_total == player_total &&
        player_hand.length > cpu_hand.length
      ) {
        return false;
      }
      return true;
    } else {
      return false;
    }
  },
jaredlyon commented 4 years ago

Added int returns to the function in order to represent the push scenario: image

Rough edits:

  calculateWinner(player_hand, cpu_hand, cards) {
    //dealer draw - attempt to beat player
    let player_total = this.calculateTotal(player_hand);
    while (this.calculateTotal(cpu_hand) < player_total && this.calculateTotal(cpu_hand) < 21) {
      cpu_hand.push(this.drawCard(cards));
    }
    let cpu_total = this.calculateTotal(cpu_hand);

    /**
     * 0 dealer bust
     * 1 dealer win
     * 2 player bust
     * 3 player win
     * 4 push
     */
    if (cpu_total > 21) {
      return 0;
    } else if (player_total > 21) {
      return 2;
    } else {
      if (cpu_total > player_total) {
        return 1;
      } else if (cpu_total < player_total) {
        return 3;
      } else if (cpu_total == player_total) {
        return 4;
      }
    }
  },