Elixir Quiz

Weekly programming problems to help you learn Elixir

Problems

Tic Tac Toe part 1: The Game

Welcome to the fourteenth edition of Elixir Quiz. This week we will be starting another multi-week project in which we create a multi player game of Tic Tac Toe.

In previous weeks we built our first multi-week project, where we completed the poker game. The point of the poker game was to learn more about pattern matching. This time we’re building a much simpler game, so that we can focus on the multi-user party of the application, and learn more about OTP.

Tic Tac Toe

Tic Tac Toe is a traditional pen and paper game played by 2 players.

The game takes place on a 3x3 grid, with players taking turns placing their token (either an X or a O) in an available square. A winner is determined when one player places 3 tokens in a row, either vertically, horizontally or diagonally. A draw is declared if all 9 squares are populated with tokens without a line of 3 existing for either player.

The problem

Create a project that allows a person to play a game of Tic Tac Toe against a computer player. The AI doesn’t need to be sophisticated, and can be as simple as choosing random squares to place it’s token.

Remember that we’re planning on expanding this into a larger, multi-user system. We will eventually want to have many different games running simultaneously, so having a game run as a process, with connected processes for each player may be a good idea.

For reference on how a multiplayer game can hang together in an OTP application, check out Martin Schurrer’s talk at ElixirConf 2014.

How do I enter?

Part 1 of the Tic Tac Toe quiz runs from Saturday November 22nd 2014 until Friday November 28th, 2014.

To enter, just complete the problem and post a link to the code on Twitter, and mention @elixir_quiz. As always, you can host your code anywhere (like Github Gists), or post your solution directly into the subreddit post.

Example solutions

After the quiz period ends on November 28th, I will update this section and talk about some interesting solutions that were posted on Twitter.


Conway's Game of Life

Welcome to the thirteenth edition of Elixir Quiz. This week we will be creating the cellular automata, Conway’s Game of Life.

Conway's Game of Life

Conway’s Game of Life is a cellular automata designed in 1970 by the British mathemetician John Conway.

The game is designed to simulate a population evolving over time, taking into account factors such as overcrowding, reproduction and under-population. The game isn’t really played by anyone, other than a human creating the initial cell configuration for the simulation.

The game shows that complex systems can emerge from very simple rules, as we’ll see in the next section. The game produces interesting patterns that can migrate across the world, be completely self sustainable, or even create complex patterns that generate new migrating patterns. These patterns are well documented

The problem

Write a module that reads in a text file, which contains the initial state of the game. The initial grid should follow these rules:

  • An empty cell is represented by a space character ( )
  • A filled cell is represented by a hash (or pound) character (#)
  • The grid must be at least 5x5, an no bigger than 40x40

Once the initial configuration is read in, the simulation begins and the user should be able to observe it in real time.

The simulation should follow the traditional rules of the game, in which each step of the simulation is created using the following transformation:

  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by overcrowding.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

How do I enter?

The Conway’s Game of Life quiz runs from Saturday November 1st 2014 until Friday November 7th, 2014.

To enter, just complete the problem and post a link to the code on Twitter, and mention @elixir_quiz. As always, you can host your code anywhere (like Github Gists), or post your solution directly into the subreddit post.

Example solutions

After the quiz period ends on November 7th, I will update this section and talk about some interesting solutions that were posted on Twitter.


Tournament Generation

Welcome to the twelfth edition of Elixir Quiz. This week we will be generating a tournament schedule for a set of teams.

Tournament schedule

In competitions with several teams, a round robin system is commonly used to determine the winner of the competition. In this system, each team plays every other team in turn. In many cases, this pattern is repeated a second time. This ensures that no one team has a particualr advantage, by allowing each team to play every other team once at their own home ground, and once at the opposition’s.

Points are awarded to each team based on the outcome of the match played, and once all the matches are completed, a winner can be determined. In most modern competitions, 3 points are awarded to the winning team, with 0 points being awarded to the losing team. In the case of a tie, both teams earn 1 point.

Other scoring systems exist, and are designed by the organisers to encourage or discourage certain styles of play. For example, the 3 point for a win system was originally adopted to encourage more attacking play in English football. It was thought that teams would be less likely to settle for a tie if the reward for winning a match was greater. Similarly in Australian domestic cricket, more attacking play is encouraged by awarding bonus points to teams whose scoring rate exceeds the opposition by certain amounts.

The problem

Given a list of unique team names, generate a tournament schedule with the following rules

  • Each team must play each other twice. Given 4 teams, each team would play 6 matches (2 matches against the other 3 teams) and there would be 12 matches in total
  • If there are an odd number of teams, one team in the group must be given a ‘bye’ each round.
  • No team may play against a team for a second time until it has played all other teams at least once.

How do I enter?

The Tournament Schedule quiz runs from Saturday October 25th 2014 until Friday October 31st, 2014.

To enter, just complete the problem and post the code to our subreddit. As always, you can host your code anywhere (like Github Gists), or post your solution directly into the subreddit post.

Example solutions

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