AlecHsueh

AlecHsueh

How to implement different game rules?

I’m learning Elixir by myself for fun now. I have basic Python knowledge before. I think the best way to learn a new programming language is to do some real projects. I’m try to create a web based poker game. The basic unit of the game is the table. Different tables have different game rules. e.g. Texas Hold’em table, Omaha table. etc. But as a functional programming language, Elixir has no class no object no inheritance. My question is how to implement a structure that can associate with different game rules?

Most Liked

kelvinst

kelvinst

Just use different modules for different games. Maybe also create a basic module with @callbacks and use it as a @behaviour on the implementation modules, to define a common interface for them, and ensure they have the functions the base game module calls.

kokolegorille

kokolegorille

If You like Poker with Elixir, there is an old post about it here https://tokafish.com/playing-poker-with-elixir-part-1/ You might need to adapt for newer version, but the post is nice.

Other games I often saw, Tic Tac Toe, Tetris…

I wrote a little game engine for go here https://hex.pm/packages/elixir_go

For a more complete example, there is this video

axelson

axelson

Scenic Core Team

I would take care not to link the game implementation (e.g. :card_played) to a GenServer. You can implement all of the logic with pure modules and functions.

To Spawn, or not to Spawn? by Saša Jurić gives a good overview about when and how to reach for processes and even talks about an implementation of a card game to boot (Blackjack).

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

We're in Beta

About us Mission Statement