AlecHsueh

AlecHsueh

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?

Showing Posts 1 to 6

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.

younes-alouani

younes-alouani

I think that you could dive straight into a project if Elixir was Object-Oriented (Python). The patterns used to design a game using functional programming are different. In my opinion start by studying the basics of Elixir (Syntax + Project organisation using Mix).
Reading this documentation will be a good starting point.
Good luck.

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 elixir_go | Hex

For a more complete example, there is this video

Eiji

Eiji

@AlecHsueh: I don’t exactly know about rules that you need to implement, but here is my idea:

Firstly declare your configuration with some raw data that you will process in game:

config :my_app, table_rules: %{
  omaha: %{}, # here you can describe rules for Omaha
  texas: %{} # here you can describe rules for Texas Hold'em
}
# Note: keep rules in format that you like

Secondly create a GenServer-based module:

defmodule MyApp.TableServer do
  use GenServer

  def card_played(player, card) do
    call_self({:card_played, player, card})
  end

  # rest public server API

  def init(opts) do
    table_name = opts[:table]
    rules = :my_app |> Application.fetch_env!(:table_rules) |> Map.fetch!(table_name)
    {:ok, %{rules: rules, table_name: table_name}}
  end

  def start_link(opts \\ [table: :texas]) do
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
  end

  defp call_self(request), do: GenServer.call(__MODULE__, request)

  def handle_call({:card_played, player, card}, _from, state = %{rules: rules}) do
    # your code based on table rules (case, cond, if, etc)
    {:reply, :ok, state}
  end

  # other handlers for calls (as in your public server API
end

Finally for each table you need to start a new server like:

alias MyApp.TableServer

TableServer.start_link(table: :config_table_name)

and call your server public API like:

alias MyApp.TableServer

player = %{} # …
card = %{} # …
TableServer.card_played(player, card)

Note: You can also use DynamicSupervisor for your table server processes.

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).

Eiji

Eiji

This comment:

your code based on table rules (…)

does not mean that you need to write login directly in this place.

I believe that this is easiest example for board games (as far as I remember table in each game have only one event like card played) at same time + time for players in their rounds to think about next move.

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews