diff --git a/README.md b/README.md index 6b71b95..d9e3943 100644 --- a/README.md +++ b/README.md @@ -480,13 +480,13 @@ Next, let's update our GridLayer to pick 8 pairs of tiles that can be matched (4 First, let's pick 8 random tile types: - 8.times.map { Tile::TYPES.sample } + Tile::TYPES.sample(8) -In Ruby 1.9+, `Array#sample` returns a random element from the array. We want to do it 8 times and collect them into a new array, so we use `times.map`. +In Ruby 1.9+, `Array#sample` returns a random element from the array. Called with an integer argument, it returns an array containing that many randomly selected elements, without repeats. Once we've done that, we want to create pairs of tiles: - types = 8.times.map { Tile::TYPES.sample } * 2 + types = Tile::TYPES.sample(8) * 2 In Ruby, if you multiply an Array by a number `n`, it creates a new array with each element appearing `n` times. So: @@ -495,7 +495,7 @@ In Ruby, if you multiply an Array by a number `n`, it creates a new array with e In our case, we'll end up with something like: - > types = 8.times.map { Tile::TYPES.sample } * 2 + > Tile::TYPES.sample(8) * 2 => ["star", "girl_pink", "star", "gem_blue", "star", "boy", "key", "girl_cat", "star", "girl_pink", "star", "gem_blue", @@ -503,11 +503,17 @@ In our case, we'll end up with something like: (Notice that I grouped them in fours. These correspond directly to the tiles in our game!) +Finally, we don't want the tiles to arranged in order, that would be boring (and make the game very easy)! So, let's things up with Array#shuffle!: + + types = Tile::TYPES.sample(8) * 2 + type.shuffle! + Let's make sure we pass a type to each Tile as we construct it. We can use `Array#shift` to make it easy on ourselves: # grid_layer.rb def load_tiles - types = 8.times.map { Tile::TYPES.sample } * 2 + types = Tile::TYPES.sample(8) * 2 + types.shuffle! @tiles = 4.times.map do |row| 4.times.map do |column| @@ -748,6 +754,15 @@ The only real trick here is that `Callback.with` expects a block, so we need `ha @active_tiles = [] end +Another issue with you may have noticed is that strange things happen if you tap the same tile twice, rather than two different tiles. Let's nip that in the bud: + + # grid_layer.rb + if tile = tile_to_flip(touch.location) + break if @active_tiles.include?(tile) + tile.flip + # … + end + Take a deep breath, run `rake` again, and play your game. If everything went well, matched tiles should stay matched, unmatched tiles should get flipped back over, and you should be limited to two selections at a time -- just like you would expect! ## #Winning diff --git a/app/grid_layer.rb b/app/grid_layer.rb index 01add89..18a44a0 100644 --- a/app/grid_layer.rb +++ b/app/grid_layer.rb @@ -25,6 +25,7 @@ def handle_touches touch = touches.any_object if tile = tile_to_flip(touch.location) + break if @active_tiles.include?(tile) tile.flip @active_tiles << tile if @active_tiles.size < 2 @@ -54,7 +55,8 @@ def handle_active_tiles end def load_tiles - types = 8.times.map { Tile::TYPES.sample } * 2 + types = Tile::TYPES.sample(8) * 2 + types.shuffle! @tiles = 4.times.map do |row| 4.times.map do |column|