/concentration-js

An old card game written for the browser.

Primary LanguageJavaScript

Concentration.js

playing cards

An old card game written for the browser. Running at simonchaffetz.com/concentration-js

Rules

[Wikipedia official][wikipedia] [wikipedia]: http://en.wikipedia.org/wiki/Concentration_(game)#Rules

Setup

The game logic starts in the file concentration.js.

This app uses gulp to concatenate JavaScript & CSS files. With npm installed, run

  sudo npm install -g gulp

Then from the project root, run

  npm install
  gulp watch

gulp watch updates the concatenated JavaScript file whenever you make a change to one of the source files.

The Good Stuff

AI

The computer AI uses this neat JavaScript LRU Cache to track up to 20 cards. It subscribes to a 'showing' event triggered whenever a card is shown to keep it decoupled from the game logic. See it in action here.

Promises

A lot of the UI logic is asynchronous. The game waits for the player to pick a card, then waits for them to pick another, and then waits for them to click a third time to confirm the end of their turn. In traditional JavaScript, this would be callback hell.

Concentration.js uses Q's promises to keep the code sane.