Skip to content

Instantly share code, notes, and snippets.

@Adesubomi
Created February 14, 2024 13:22
Show Gist options
  • Save Adesubomi/2ea4891df62cd148b638d60ba2a9993b to your computer and use it in GitHub Desktop.
Save Adesubomi/2ea4891df62cd148b638d60ba2a9993b to your computer and use it in GitHub Desktop.
Recruitment Test (Go) - Dice game

Build a simple dice game Rest API

In this exercise, we would be building a simple dice game in Go.

When a game is started, a commitment fee of 12 sats (satoshis) is deducted from the user’s wallet, and a number is generated randomly between 2 and 12 and stored. This number should be visible to the user.

After a game has been started, the user is allowed to roll a dice (twice for every session), and the result of both dice rolls is summed up. If the sum of the 2 numbers (in this session) is equal to the number generated at game start-up, the user wins 16 sats.

The user can roll dice as many times as he wants in a single game; however, the user is charged 3 sats at the beginning of every 2 dice rolls.

The game can be ended at anytime; however, the user is charged 12 sats for starting a new game.

Example.

  • The user funds his wallet and receives 155 sats.
  • The user starts a game, which costs him 12 sats. I.e. 20 sat is deducted from the user’s wallet bringing the user’s balance down to 143 sats. Let’s assume that the number generated here is 7.
  • A dice roll session is initiated for the user; on this first roll, the user is charged 3 sat bringing the user’s wallet balance down from 143 sat to 140 sat. Let’s assume the die roll result is 2. (For the user to win, we can only hope that the user rolls 5 on the next roll.)
  • The user rolls the die again; this time he is not charged because we’ve already been charged for this dice roll session. Let us assume the user rolls 5.
  • The sum of the first and second die rolls (5+2=7) is 7, which means the user just won, and should thereby be awarded 16 dice, bringing the user’s wallet balance up from 140 sat to 156 sat.
  • At this point, the dice roll duo session has been completed, but our game is still active. Which means we can keep rolling.
  • If the user’s dual roll does not win, well, he just lost the 3 sats committed for the dice roll session, and can try again.

Endpoints

  1. Fund wallet - Should fund a users wallet with 155 sats.
  2. Get wallet balance - This shows the balance of the user. This endpoint should return the user’s balance, balance in string format, and an additional field to indicate the wallet’s asset (which is sats)
  3. Start Game - Start a game; when a game starts, a random number is generated. This number is kept in storage. Can only start a game when no game is active.
  4. Roll Dice - This endpoint simply rolls a die. Can only roll dice if a game is active.
  5. End Game - This endpoint closes an active game. Can only end a game if there is an active game.

Constraints

  • A user should not be allowed to roll a dice if a game has not been started.
  • A user is allowed to fund his wallet from the gas-station (for free) only when there is no active game.
  • A user is allowed to fund his wallet from the gas-station when user’s balance is 35 sats or less.
  • Dice rolling sessions should be automatic withing an active game.
  • Besides the user's wallet balance, there should be no limits to the number of sessions (and subsequently the number of dice rolls) in a game.
  • Wallet balance should be integer, not float.

Not Important

  • Authentication is not required.
  • Any routing library/framework of the candidate’s choosing.
  • If you decide to use a storage engine/ORM, any storage engine or/and ORM preferred by the candidate is fine (Sql, noSql, in-memory, etc).

Nice to have

Endpoint to check if a game is in session Transactions log, a log of every time the user gets funded or debited, with an endpoint to observe it.

Due Date

Your chances increase with how early you’re able to deliver on the assessment. If you would need an extension, reply to the mail sent to you stating the reason(s) why.

Submission

  • You should create a repository on your Github account, and make it a public repository
  • Your most relevant codebase should be on the main branch
  • Submit the url of this github repository to hr@apexnetwork.ng
  • Due date for submission is 7 days after issueance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment