From a2dfd2480da5f4e71de06bdae750f950b6152cd7 Mon Sep 17 00:00:00 2001 From: Adam Gardner Date: Tue, 7 Jan 2014 17:02:59 -1000 Subject: [PATCH 1/3] Make sure each tile type is chosen only once. --- README.md | 10 +++++----- app/grid_layer.rb | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6b71b95..82025d1 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", @@ -507,7 +507,7 @@ Let's make sure we pass a type to each Tile as we construct it. We can use `Arra # grid_layer.rb def load_tiles - types = 8.times.map { Tile::TYPES.sample } * 2 + types = Tile::TYPES.sample(8) * 2 @tiles = 4.times.map do |row| 4.times.map do |column| diff --git a/app/grid_layer.rb b/app/grid_layer.rb index 01add89..e614cc4 100644 --- a/app/grid_layer.rb +++ b/app/grid_layer.rb @@ -54,7 +54,7 @@ def handle_active_tiles end def load_tiles - types = 8.times.map { Tile::TYPES.sample } * 2 + types = Tile::TYPES.sample(8) * 2 @tiles = 4.times.map do |row| 4.times.map do |column| From 99d4b516556a8de91972cd1217d4d33b4ec9bbd1 Mon Sep 17 00:00:00 2001 From: Adam Gardner Date: Tue, 7 Jan 2014 17:13:26 -1000 Subject: [PATCH 2/3] Shuffle tiles to make game more interesting. --- README.md | 6 ++++++ app/grid_layer.rb | 1 + 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 82025d1..be0725b 100644 --- a/README.md +++ b/README.md @@ -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 = Tile::TYPES.sample(8) * 2 + types.shuffle! @tiles = 4.times.map do |row| 4.times.map do |column| diff --git a/app/grid_layer.rb b/app/grid_layer.rb index e614cc4..cd5a225 100644 --- a/app/grid_layer.rb +++ b/app/grid_layer.rb @@ -55,6 +55,7 @@ def handle_active_tiles def load_tiles types = Tile::TYPES.sample(8) * 2 + types.shuffle! @tiles = 4.times.map do |row| 4.times.map do |column| From df546e283cd3ade870a7bf631f290551f22a536f Mon Sep 17 00:00:00 2001 From: Adam Gardner Date: Tue, 7 Jan 2014 17:21:23 -1000 Subject: [PATCH 3/3] Correctly handle tapping an already active tile. --- README.md | 9 +++++++++ app/grid_layer.rb | 1 + 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index be0725b..d9e3943 100644 --- a/README.md +++ b/README.md @@ -754,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 cd5a225..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