Skip to content

full featured match game with help from gleam & phoenix liveview

Notifications You must be signed in to change notification settings

toranb/elixir-gleam-match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Installation

To install on macOS

brew install gleam

Objectives

The entire project centers around a single Gleam source file. The game engine is driven from the elixir wrapper

defmodule Game.Engine do
  def flip(%__MODULE__{} = struct, flip_id) when is_binary(flip_id) do
    gleamify(struct, fn record ->
      :game.flip(record, flip_id)
    end)
  end

  def unflip(%__MODULE__{} = struct) do
    gleamify(struct, fn record ->
      :game.unflip(record)
    end)
  end

  def prepare_restart(%__MODULE__{} = struct) do
    gleamify(struct, fn record ->
      :game.prepare_restart(record)
    end)
  end
end

flip

flipp

This function is executed when the player clicks a playing card. Simply enumerate the cards and mark the one with the id as flipped using a boolean. If 2 cards have been flipped at this point attempt to match them by the id. When a match is found mark each card as paired and set the flipped for both back to false. Finally, if all the cards are paired declare the game over by marking the winner using a boolean value.

One edge case here is that if 2 cards are flipped but they do not match, you need to set the animating boolean to true. This will later instruct the engine to fire unflip.

unflip

unfliip

This function is executed after a 2nd card has flipped but failed to match. Simply enumerate the cards and mark the flipped attribute to false for any non paired card. You will also need to revert animating to false so the flip function works properly.

prepare_restart

prepareRestart

This function is executed after the player decides to play again. Simply enumerate the cards and mark all paired and flipped attributes to false.

Debugging Tips

To print something in the Gleam source code import the io module and use io.debug

import gleam/io

io.debug("Hello World!")

Learning Gleam

Because the language is so young today the best place to dive in is the getting started guide

License

Copyright © 2020 Toran Billups https://toranbillups.com

Licensed under the MIT License