Elixir Quiz

Weekly programming problems to help you learn Elixir

Problems

Poker part 2: Finding a winner

Welcome to the fifth installment of Elixir Quiz. This week, we continue with our 5 card draw poker game. We will be building hands from our deck, and then comparing those hands to determine a winner.

A player's hand

A players hand consists of 5 cards taken from the standard deck of cards. Cards are traditionally dealt to players in turn, one at a time, such that no player has 2 cards until each person has 1. Some house rules allow players to recieve their cards in batches of 2 or 3, until they have their allotment of 5.

What beat's what?

Unlike other casino games, poker is one in which players play against one another, rather than the house. We need to be able to compare a players hand to those of the other players to determine a winner.

The value of a poker hands hand is determined by the probability of that hand occurring. The hands from highest to lowest are:

  • Straight Flush: This hand occurs when a player has 5 cards of the same suit in sequence. Two flushes are compared based on the value of their highest card. An Ace high or Royal flush beats a King high flush.
  • Four of a kind: A hand that contains four cards of the same value, and one other card. When comparing two four of a kind hands together, the value of the 4 cards is used to determine the winner. A hand with 4 fives beats a hand with 4 twos.
  • Full House: A hand that contains three cards with one value, and two cards of another value. When comparing two full houses, the hand with the higher value three matching cards wins. For example, a hand with 3 tens and 2 threes beats one with 3 fives and 2 kings.
  • Flush: The flush occurs when all cards in the hand have the same suit, but the values are not in sequence. When two flushes are compared, the winner is determined as if they were high card hands (which we’ll see below).
  • Straight: This hand occurs when a player has 5 cards in sequence, but the suit is not the same across all cards. When two straights are compared, the hand with the highest card wins. Two straights with the same high card are considered equal. An Ace can be positioned either before the 2 or after the King in a straight.
  • Three of a kind: A hand that contains three cards of matching value, and two other cards with non-matching values. When comparing two three of a kind hands, the hand with the highest value triple cards wins. For example, three Kings beats three Jacks.
  • Two Pair: This hand occurs when a player has two cards of the same value, plus another two cards of the same value (but not matching the first pair), plus any other card. When comparing two of these types of hand, the player with the higest value pair will win. If both players have matching pairs, such as both having a pair of 2s and a pair of 4s, then the remaining card determines the winner.
  • One Pair: One pair occurs when a hand consists of two cards of matching value, plus three other cards whose value doesn’t match the value of any other card in the hand. When compairing pairs, the higer value pair wins. If two pairs have the same value paired cards, then the highest of the remaining cards determines the winner.
  • High Card: The high card hand, which is the lowest value hand, consists of 5 cards, each of different values, and containing at least 2 suits. When comparing these hands, the winner is determined by the hand with the highest value card. If both hands have the same value high card, then the second highest card is compared, and so on until all cards are compared.

The problem

Given that we already have a deck of cards, write a function that can deal hands to a number of players, between 2 and 6. Also write a function that can take this list of hands and determine the winner.

How do I enter?

The Poker part 2 quiz runs from Saturday September 6th 2014 until Friday September 13th, 2014.

To enter, just complete the problem and post the code to our subreddit. Add this code to the project that you started last week.

Example solutions

After the quiz period ends on September 13th, I will update this section and talk about some interesting solutions that were posted to our subreddit


Poker part 1: A deck of cards

Welcome to the fourth installment of Elixir Quiz. This week, we embark on our first multi-week project, building a game of 5 card draw poker. This week we will be building the deck of cards.

An introduction to 5 card draw

5 card draw is one of the simplest variations of poker. It is the game that is most likely to have been played in people’s homes up until the rise in popularity of Texas Hold’em.

The game begins with each player being dealt 5 cards face down. After each player is dealt their cards, they then assess their initial hands. Players take turns discarding any of their cards and receiving replacements.

Players then compare their hands against one another to determine the winning hand.

For our first step, we will build ourselves a deck of cards.

If you haven’t explored testing with ExUnit yet, the poker project provides a great opportunity to start.

The problem

Create a module that will allow us to create a shuffled deck of cards. The deck should contain the 52 cards included in a regular deck. How the cards and deck are represented are completely up to you.

Since we’re working with the cards this week, also ensure that you have a function to compare the value of 2 individual cards. Card values, in high -> low order, are Ace, King, Queen, Jack, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

How do I enter?

The Poker part 1 quiz runs from Saturday August 30th 2014 until Friday September 5th, 2014.

To enter, just complete the problem and post the code to our subreddit. I’d suggest this week storing your project in a source control system, as we’ll be working on this for a few weeks.

GitHub can be useful for hosting your solutions, but anywhere is fine.

Example solutions

After the quiz period ends on September 5th, I will update this section and talk about some interesting solutions that were posted to our subreddit


Heightmap

Welcome to the third installment of Elixir Quiz. This week we will be creating heightmaps.

About Heightmaps

A height map is a system for storing terrain data. Height maps are typically stored as grayscale raster images, in which darker pixels are at a lower height than lighter pixels.

Another method involves storing separate levels of detail in separate colour channels. Large grained terrain changes may be stored in the red channel, medium grained terrain changes in the green channel, and fine grained changes in the blue channel.

Height maps are popular in video games, for storing terrain data. They are heavily used in video games that take advantage of procedural content generation. These games, such as Minecraft, make use of these techniques to ensure that every game that a person plays is different.

The problem

Write a program that takes a single number (A) as input, and produces a randomly generated heightmap in an AxA grid. Output can take any form, such as ascii or a bitmap.

The choice of algorithm is up to you. Some algorithms that are commonly used are the diamond-square algorithm and the perlin noise algorithm.

Example Output

Heightmap

How do I enter?

The Heightmap quiz runs from Saturday August 23rd 2014 until Friday August 30th, 2014.

To enter, just complete the problem and post the code to our subreddit. For short solutions, you can put the code directly into your comment in the quiz thread.

For larger solutions, please host your code elsewhere and link to it.

GitHub gists can be useful for hosting your solutions, but anywhere is fine.

Example solutions

After the quiz period ends on August 30th, I will update this section and talk about some interesting solutions that were posted to our subreddit