bmitch

bmitch

New to Elixir - Mind reviewing my code?

I’m new to Elixir and just recently completed “Elixir for Programmers” by pragdave.

I thought creating tic tac toe in Elixir would be a good next step for me to continue my learning.

I’ve created the logic for the tic tac toe game and was wondering if anyone would mind reviewing it and providing me with feedback?

The meat of the code starts from here:

A couple of things I’d like to see if can be improved:

Thanks very much!

Most Liked

OvermindDL1

OvermindDL1

First thing first, always run your code through the elixir formatter. :slight_smile:

The way you are doing it is fine:

    game = Map.put(game, :board, new_board)
    |> Map.put(:whos_turn, rotate_player(who))
    Map.put(game, :game_state, update_game_state(game))

Though more efficiently would probably be (assuming you can write update_game_state to work on the old state):

  defp accept_move(game = %Game{}, who, index, true) do
    %{game |
      board: put_elem(game.board, index, who),
      whos_turn: rotate_player(who),
      game_state: update_game_state(game),
    }
  end

Use not in:

  def check_board({a, b, c, d, e, f, g, h, i}) when nil not in [a, b, c, d, e, f, g, h, u] do
    :draw
  end
  def check_board(no_nils), do: :in_progress

Otherwise there are a few places in code that could be simplified more but I’m a bit busy at moment… ^.^;

kokolegorille

kokolegorille

A really small note about aliasing… You can use MODULE when aliasing the module.

eg.

defmodule Tictactoe.Game do
  alias __MODULE__
end
bmitch

bmitch

Thanks very much @OvermindDL1 @yurko and @kokolegorille !! Great feedback.

yurko

yurko

I’d drop pattern matching and do something like this in the end

def check_board(board) do
  if Enum.count(board, &(&1 != nil)) == 0 do
    :draw
  else
    :in_progress
  end
end

upd it’d be wrong though since tuples are not enumerable in elixir, sorry for the confusion :smiley:

OvermindDL1

OvermindDL1

You can always Tuple.to_list/1 them, but the pattern matching way is probably a lot more efficient I’d wager, if that even matters. :slight_smile:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement