sbuttgereit

sbuttgereit

Small, beginner project idea that might give a little Elixir outreach, too

I was browsing the orange site yesterday and, as happens from time to time, the Jeff Atwood GitHub repo for “BASIC Computer Games” came up as a topic:

“Basic Computer Games (GitHub)”

In this repo, they take the classic 1973 book “101 BASIC Computer Games”, or “BASIC Computer Games” as it was later called, and rewrite those games in different computer languages. Given the relative simplicity of these games, the accessibility of BASIC as a language to define the requirements, and the challenge offered by Jeff Atwood in this repo… it struck me as a fairly interesting and unique way for someone starting with Elixir to get a problem to solve with just a touch more meat than a “hello world” or an overly prescriptive, hand holding tutorial. Also I noticed that nobody had yet contributed any Elixir, Erlang, or Other BEAM language (OK, I didn’t search exhaustively, but I didn’t see any where I looked) versions to the repo; seemed like a low-effort way to get a little more Elixir exposure in the broader world.

Finally, a note about the book, “BASIC Computer Games (Book)”. As I mentioned, it originally published in 1973 and it’s just a collection of 101 games written in the BASIC computer language as collected by David H. Ahl, specifically the flavor of BASIC that shipped with the old DEC minicomputers… think PDP-11. Later editions targeted microcomputer BASICs. The second version of the book was the first computer book to sell over one million copies. For me personally, the book was a substantial part of my introduction to programming… entering the games into my VIC-20 & C-64 and having to convert from the BASIC of the book to the BASIC of my computers… without the internet for help… built in me an understanding of the basics of computing, troubleshooting, and using documentation which I use to this day. I will often continue to use programs from this book as early learning projects when starting a new computer language.

Anyway… just a thought… adopt or ignore at your leisure!

Most Liked

codeanpeace

codeanpeace

Thanks for sharing – took a quick crack at porting the first game Acey Ducey.

Acey Ducey

This is a simulation of the Acey Ducey card game.

In the game, the dealer (the computer) deals two cards face up. You have an option to bet or not to bet depending on whether or not you feel the next card dealt will have a value between the first two.

Your initial money is set to $100; you may want to alter this value if you want to start with more or less than $100. The game keeps going on until you lose all your money or interrupt the program.

The original program author was Bill Palmby of Prairie View, Illinois.

defmodule Game do
  @deck for suit <- [:spades, :hearts, :clubs, :diamonds], value <- 1..13, do: {suit, value}

  defstruct funds: 100, deck: []

  def play(), do: play(%__MODULE__{}) # for convenience

  def play(%__MODULE__{funds: funds}) when funds <= 0, do: IO.puts("~~~ game over ~~~")
  def play(%__MODULE__{deck: deck} = game) when length(deck) < 3, do: play(%{game | deck: Enum.shuffle(@deck)})
  def play(%__MODULE__{deck: deck, funds: funds} = game) do
    IO.gets("<hit enter>\n")

    [first_card, second_card, third_card | remaining_deck] = deck

    IO.puts("~~~ new round ~~~")
    IO.puts("first card: #{format(first_card)}")
    IO.puts("second card: #{format(second_card)}\n")
    IO.puts("funds: $#{funds}")

    bet = prompt_to_place_bet(funds)
    new_funds = if win?(first_card, second_card, third_card), do: funds + bet, else: funds - bet

    IO.puts("\nthird card: #{format(third_card)}")
    IO.puts("funds: $#{funds} => $#{new_funds}")
    IO.puts("~~~ end round ~~~\n")

    play(%{game | deck: remaining_deck, funds: new_funds})
  end

  # re-prompt if invalid integer and/or out of bounds
  defp prompt_to_place_bet(funds) do
    input = IO.gets("place your bet: $")
    case Integer.parse(input) do
      {bet, _} when bet in 0..funds -> bet
      _ -> prompt_to_place_bet(funds)
    end
  end

  # for a stricter win condition (non-inclusive)
  defp win?({_, first}, {_, second}, {_, third}) when third > first and third < second, do: true
  # for a looser win condition (inclusive)
  #defp win?({_, first}, {_, second}, {_, third}) when third in first..second, do: true
  defp win?(_, _, _), do: false

  defp format({suit, value}) do
    case value do
      1 -> "ace of #{suit}"
      11 -> "prince of #{suit}"
      12 -> "queen of #{suit}"
      13 -> "king of #{suit}"
      value -> "#{value} of #{suit}"
    end
  end
end

Game.play() # equivalent to Game.play(%Game{funds: 100, deck: 100})
codeanpeace

codeanpeace

For transparency and posterity’s sake, there’s a bug in win?/3. (╯°□°)╯︵ ┻━┻

The version above depends on the unfortunate and incorrect assumption that the first card’s value is less than that of the second card. The version below uses Enum.sort/1 to not rely on that assumption being true.

  # for a stricter win condition (non-inclusive)
  defp win?({_, first}, {_, second}, {_, third}) do
    [floor, ceiling] = Enum.sort([first, second])
    (floor < third) && (third < ceiling)
  end
  # for a looser win condition (inclusive)
  #defp win?({_, first}, {_, second}, {_, third}) do
    #[_, middle, _] = Enum.sort([first, second, third])
    #middle == third
  #end

Where Next?

Popular in Challenges Top

adamu
I said I was on a break, but I took a sneak peak and it looked fun so… Part 1 completes in half a millisecond with a single pass of the ...
New
LostKobrakai
This topic is about Day 9 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bismark
Took me a minute to remember my binary math :smile: :grimacing:… import Bitwise __DIR__ |&gt; Path.join("puzzle.txt") |&gt; File.stream...
New
sasajuric
Note: This topic is to talk about Day 12 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics...
New
Aetherus
This topic is about Day 3 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
gangstead
This is my second year doing AoC in Elixir and my first year doing it with Livebook. When I was doing just plain Elixir I usually set up...
New
bjorng
Note: This topic is to talk about Day 16 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
bjorng
This topic is about Day 16 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
bjorng
Note: This topic is to talk about Day 25 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
lud
At first I was scared but I found is a simple way to compute the sides. defmodule AdventOfCode.Solutions.Y24.Day12 do alias AdventOfCo...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement