Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -495,19 +495,25 @@ 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",
"star", "boy", "key", "girl_cat"]

(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|
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/grid_layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down