To install on macOS
brew install gleam
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
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
.
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.
This function is executed after the player decides to play again. Simply enumerate the cards and mark all paired
and flipped
attributes to false.
To print something in the Gleam source code import the io module and use io.debug
import gleam/io
io.debug("Hello World!")
Because the language is so young today the best place to dive in is the getting started guide
Copyright © 2020 Toran Billups https://toranbillups.com
Licensed under the MIT License