Class is now in session. kinda!

First off, thanks a lot for your easy to follow along Beginner Javascript Guide (3rd Edition). It helped me understand the language a bit better. I have a question about classes. I’m attempting to write a blackjack game as my first project.

I’ve created two classes, TableRules and Table. I want to tell the table what the TableRules are. Would the best way to do this be to create a new object and pass in the tableRules as a parameter like so?

let table = new Table(new TableRules());

I see this works when I console.log(table.rules), but I’m not sure why it works or if it’s the best (and only) way to go about this.

class TableRules {
  constructor() {

      // Number of spots at the table
      this.numSpots = 6;

      // Number of decks in the shoe
      this.numDecks = 6;

      // Minimum bet default $10
      this.minBet = 10;

      // Maximum bet default $1000
      this.maxBet = 1000;

      // Blackjack pays 3:2
      this.blackjackPays = 1.5;

      // Whether the dealer stands on soft 17
      this.dealerStandsOnSoft17 = true;

      // Whether double is allowed after split
      this.doubleAfterSplit = true;

      // Whether  is allowed
      this.surrender = false;

      // Whether insurance is allowed
      this.insurance = true;
  }
}



class Table {

  constructor(rules) {

      this.rules = rules;
      this.initSpots();
      this.initShoe();
  }
1 Like

Hi @MrProNovice - welcome to the forums :slight_smile:

Do you anticipate the property values in your TableRules class ever changing? Or will they always be these values?

Thanks for the reply.

I do anticipate it changing but not on a each table if that makes sense.

Before I picked up your book, my original HTML had a drop down to select the number of decks that my js used.

let numberSelectedDecksStr = document.getElementById(“decks”).value;
let numberSelector = parseInt(numberSelectedDecksStr, 10);

I then ran a loop to fill the deck array with the cards before shuffling. In the real world, the drop down is not desired since my goal is to have multiple people on the table such as myself and my evil brother.

Putting aside the auth stuff that I’m not even getting into right now, my app is like a room. The room has a bunch of tables. Each table has a set of rules. You select the table you want to play on, and click join to sit down.

Somehow, someway, I need to be able to have a bunch of tables with different rules to deal with minimum bets, # of decks, and surrender. My original thought, and probably remedial, was to create multiple tablerules.js such as 1deck.js, 6deck.js, 8deck.js and do something with that but honestly it makes no sense and it most definitely wrong.

A better way would probably be to create separate table objects and pass in the rules somehow but I’m not sure what to do.

Given what you just mentioned, going with a class-based solution may be unnecessary. You need classes when creating many objects of the same type. That doesn’t seem to be the case here.

Using an object may be the most direct way:

let variationOne = {
  numSpots: 6,
  numDecks: 6,
  minBet: 10,
  maxBet: 1000,
  blackjackPays: 1.5,
  dealerStandsOnSoft17: true,
  doubleAfterSplit: true,
  surrender: false,
  insurance: true,
};

You can create duplicate objects for each variation where the values would be different :slight_smile:

Thanks! I was plugging along until someone got me stuck on classes a couple months ago.

I just picked up your data structure and algorithm book yesterday. Looking forward to starting it in a few days.

1 Like

Classes and Objects in JavaScript are more similar than not (which isn’t the case in other languages!). If you are curious, this article goes into more detail:

Regarding the Algorithms book, that’s awesome! Hope you like it. If you have any questions, you know where to post them :slight_smile: