O'Reilly logo

Mastering Ember.js by Mitchel Kelonye

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Game logic

Before we discuss the game component, it's a good idea to discuss the game logic:

  • The game is a 4 by 4 grid whose moves are made by sliding tiles using the keyboard arrow keys
  • The tiles merge if they are of equal magnitude
  • Each move generates a new random tile
  • The objective of the game is to slide these tiles until one of them merges to a value of 2048

The grid cells are represented by the App.Cell model located in models.js:

App.Cell = Em.Object.extend({ x: null, y: null, value: null, move: function(cell){ var value = this.get('value'); var nvalue = cell.get('value'); var score = nvalue+this.get('value'); cell.set('value', score); this.set('value', 0); if (value && nvalue) return score; return 0; }, isTile: function(){ return !!this.get('value'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required