-
-
Snex – Multiplayer Snake
It's been a few years since I built anything in Elixir so I decided this weekend to refresh my memory.
Rather than build yet another location-based API, I decided to try a game.
Combining super-simple gameplay and Phoenix Channels, I eventually ended up with Snex - Multiplayer Snake.
On page load, you are assigned a random session ID so you can copy-paste the URL and share it with any one to play on the same board.
I've currently got it deployed on Gigalixir.
The performance on the deployed version isn't great due to network latency. Essentially, the game ticks every 100ms and sends an update to each player so if you're moving left then press down, typically the server will process one more
left
event before yourdown
arrives. There are plenty of blog posts about handling latency, not just in multiplayer online games but also specific discussions on multiplayer online snake. I decided I could either dig into refreshing my knowledge of that or stick with refreshing my knowledge of Elixir and Phoenix. I went with the latter. -
Operations: A Maths Game
Operations
1+ players
The aim is to get the highest number possible after using each of your tokens.
There is 1 die
Each player has 4 tokens with different symbols on:
+
−
×
÷
Each player rolls the die and the number they get is their starting number.
Lowest score starts. If there's a draw, youngest of those starts.
Each round:
- Roll the die
- Choose one of your operations.
- Perform your operation with the new number and your existing number. Try to get the highest score
- Discard your operation token. You only get to use each operation once.
Note: When the calculation is a division with a remainder, you can either discard the remainder or continue with decimals, depending on who is playing.
Example game:
2 players.
A rolls 2, B rolls 3. A has the lowest starting number so they start
- Round 1
- A rolls a 4. They decide to use their
+
operation. They now have 6. - B rolls a 1. They use their
÷
. They still have 3.
- A rolls a 4. They decide to use their
- Round 2
- A rolls 6. They use their
×
. They have 36. - B rolls 5. They use their
×
. B now has 15
- A rolls 6. They use their
- Round 3
- A rolls another 6. They've already used their
×
so they have to either subtract 6 or divide by 6. They use−
. They have 30 - B rolls 2. They
+
it. B has 17
- A rolls another 6. They've already used their
- Round 4
- A rolls another 6! Now they only have their
÷
left. They have to divide 30 by 6. Now they have 5. - B rolls 3. They have their
−
left. B has 14.
- A rolls another 6! Now they only have their
B wins.
Variations
- For advanced maths, add in the power and root symbols
^
√
- Try to get the lowest score instead of the highest.
- Try to get the lowest score without going below zero.
-
Pi-ku
A what? A pi-ku?
To quote Maths Week Scotland:
A pi-ku is a poem that follows the form of a haiku, but instead of the 5-7-5 haiku pattern, the syllables in a pi-ku follow the number of digits in the mathematical constant pi (π).
So, instead of 5-7-5, a pi-ku would follow the pattern 3-1-4 (-1-5-9-2…etc.)
Of course, I couldn't avoid having a go myself, could I?
Approximation of Pi
Pi is three
Well...
Three and a bit.
…ish.
The 'bit' is quite small.
Full-time Score of the Final of the World Circle Geometry Ratio Tournament 2023
Radius:
2
Diameter:
1
Subjectively Reviewing areas of Mathematics
Algebra?
Fine.
Geometry?
Fun.
Trigonometry?
Partial Differential Equations?
Both Good.
Fractal Geometry?
Looks simple at first.
Gets... tricky
When you look closer.
Marginal
Fermat's Last?
Gasp!
I Found a Proof!
Which,
Unfortunately...
This poem is too short to contain. -
RFID Timesheet
I've done a lot of projects over the holidays. This is a quick collection of notes to remind myself later.
I used an RC522 RFID scanner (originally part of a Tonuino project) and wired it to a Particle Photon. Whenever an RFID tag was held to it, it would publish an event containing the ID of the card to the Particle Cloud. When the card was removed, it would publish a blank event. This is the code from the Photon:
// Photon RFID-RC522 // A2 SDA // A3 SCK // A4 MISO // A5 MOSI // D2 RST // GND GND // 3V3 3.3V #include
#include #define LED_PIN D7 constexpr uint8_t RST_PIN = D2; // Configurable, see typical pin layout above constexpr uint8_t SS_PIN = A2; // Configurable, see typical pin layout above MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance uint32_t lastMillis = 0; bool wasPresent = false; void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(9600); // Initialize serial communications with the PC while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); } void loop() { // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { if(wasPresent) { if(! mfrc522.PICC_IsNewCardPresent()) { Serial.println("No card"); Particle.publish("rfid_scan", "", 60, PRIVATE); wasPresent = false; } } else { } return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } char cardID[32] = ""; for (byte i = 0; i < mfrc522.uid.size; i++) { char hex[4]; snprintf(hex, sizeof(hex), "%02x", mfrc522.uid.uidByte[i]); strncat(cardID, hex, sizeof(cardID)); } if (millis() - lastMillis < 1000) { return; } lastMillis = millis(); if(!wasPresent) { wasPresent = true; Particle.publish("rfid_scan", cardID, 60, PRIVATE); Serial.printlnf("Card: %s", cardID); // Turn on the LED digitalWrite(LED_PIN, HIGH); // Leave it on for one second delay(1s); // Turn it off digitalWrite(LED_PIN, LOW); // Wait one more second delay(1s); } // Dump debug info about the card; PICC_HaltA() is automatically called //mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); } I then used IFTTT to read these events and write them to a Google Spreadsheet. This is the IFTTT Excel code:
{{CreatedAt}} |||{{EventContents}}||| =IF(ISODD(ROW()), "Started", "Stopped") ||| =IF(ISEVEN(ROW()),ROUND(((DATEVALUE(REGEXEXTRACT(INDIRECT(ADDRESS(ROW(),COLUMN()-3,4)), "w+ d{2}, d{4}")) + TIMEVALUE(REGEXEXTRACT(INDIRECT(ADDRESS(ROW(),COLUMN()-3,4)), "d{2}:d{2}[A|P]M$"))) - ( DATEVALUE(REGEXEXTRACT(INDIRECT(ADDRESS(ROW()-1,COLUMN()-3,4)), "w+ d{2}, d{4}")) + TIMEVALUE(REGEXEXTRACT(INDIRECT(ADDRESS(ROW()-1,COLUMN()-3,4)), "d{2}:d{2}[A|P]M$")))) * 24, 2),"")||| =IFERROR(VLOOKUP(INDIRECT(ADDRESS(ROW(), COLUMN()-3),4), I$3:J$10, 2, FALSE), "")
We now have a physical time tracker that can be used to log time spent on individual projects. Super-handy for Jenni's freelance work.