Skip to content

Instantly share code, notes, and snippets.

@rekursed
Created January 26, 2024 14:25
Show Gist options
  • Save rekursed/4d2567b7591e3515dfe3c10e688faf8f to your computer and use it in GitHub Desktop.
Save rekursed/4d2567b7591e3515dfe3c10e688faf8f to your computer and use it in GitHub Desktop.

Connect4

Connect4 is a grid-based game, where players take it in turns to add tokens. The winner is whoever manages to line up 4 pieces, in any direction, first. Diagonals are allowed.

https://en.wikipedia.org/wiki/Connect_Four

Here is a quick 20 second video: https://www.youtube.com/watch?v=ylZBRUJi3UQ

Tasks

The aim is to develop an interactive version of the game which two players can play together.

A simple, text-based, representation of the grid is encouraged to start with. For example, you might use '.' for empty slots, and 'x' and 'o' for pieces of the two players. Players can input their turns via test assertions.

A minimal version of the game should support:

  • a 6 x 7 grid (6 rows, 7 columns).
  • players play pieces sequentially
  • the game identifies winning turns and ends at that point

Bonus rounds

Pop Out

In addition to the existing rules, players may 'pop' one of the pieces out from the bottom of the board for their turn. All the pieces above shift downwards. Victory conditions remain the same as before.

5-in-a-row

Instead of four pieces in a row for victory, 5 are now required! Adapt the grid to be 6 x 9 to accommodate this.

Power up

In addition to the usual pieces, players also receive specially marked 'Power Checkers' pieces. This can be played once per game. One example of a 'Power Checker' is an Anvil - this removes all pieces below it when played, leaving the Anvil at the bottom row of the board.

Implement the Anvil.

Invent your own 'Power Checker' and add it to the game!

Election results

It's election night! Exciting! We have a feed of election results from a data supplier. They will supply us a file which will be updated throughout the night as results come in.

Results format

The fields in the file will be separated by commas but each row will vary in length as described below.

A result will consist of:

  1. A constituency
  2. A repeating set of pairs with the party code and the votes cast

So for example:

Cardiff West, 11014, C, 17803, L, 4923, UKIP, 2069, LD
Islington South & Finsbury, 22547, L, 9389, C, 4829, LD, 3375, UKIP, 3371, G, 309, Ind

We want to transform this into a standard result that shows:

  • the constituency name
  • translates the party code into a full name
  • shows the share of the vote as a percentage of all the votes cast

Codes

  • C - Conservative Party
  • L - Labour Party
  • UKIP - UKIP
  • LD - Liberal Democrats
  • G - Green Party
  • Ind - Independent
  • SNP - SNP

Validation

If there is a problem with the format of the results file then all good entries should result in output and the error should go to a separate error log with the problem explained in non-technical language that a journalist might be able to understand and report back to the results service.

Enhancements

The results service may be behind the actual results or may contain an error. We want to be able to combine the results file with an "override" file. If a constituency has an entry for a party in the override file that value should be used instead of the result file.

If the constituency is not present in the results file the result should be added entirely from the override file.

Swing-o-meter

Suppose we have a result file from the previous election in the same format. Add the percentage changes in the vote to the results.

Calculate the average change towards or away from the parties and then use this average swing to predict the result of elections that are in the previous election's result file but not in the current one.

Extension

Discuss (but do not solve in code) how you would amend this solution to deal with the fact that the number of eligible voters changes from election to election.

Fruit machine We are going to create a virtual fruit machine. To make things easier instead of symbols we are going to use colours: black, white, green, yellow.

Each time a player plays our fruit machine we display four 'slots' each with a randomly selected colour in each slot.

If the colours in each slot are the same then the player wins the jackpot which is all of the money that is currently in the machine.

Implement a basic machine, along with the concept of a player who has a fixed amount of money to play the machine.

Floats and prizes We are now going to add a "float" to our fruit machine, this is an initial sum of money that the machine has. In addition, we are going to implement a prize system.

If each slot has a different colour then the machine should pay out half the current money in the machine.

If a given play results in two or more adjacent slots containing the same colour then the machine should pay out a prize of 5 times the cost of a single play.

If the machine does not have enough money to pay a prize it should credit the player with a number of free plays equal to the difference between the full prize and the amount of money available. This does not affect a jackpot win.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment