Chapter 3: Reset Mechanism, Rounds, and a more conditional Kernel Reveal
In this chapter, we’ll implement a reset mechanism that allows the Walnut to be reused in multiple rounds, ensuring each game session starts fresh. We’ll also track contributors per round so that only players who participated in cracking the Walnut can call look()
. By the end, we’ll have a fully functional round-based walnut game where the kernel remains shielded until conditions are met! Estimated time: ~15 minutes.
The need for a Reset mechanism
Right now, once the Walnut is cracked, there’s no way to reset it. If a game session were to continue, we’d have no way to start fresh—the shell would remain at 0, and the kernel would be permanently revealed.
To solve this, we need to introduce:
✅ A reset
function that restores the Walnut to its original state.
✅ Round tracking, so each reset creates a new round.
The need for a contributor check
While the reset mechanism and round tracking allow us to restart the Walnut for continuous gameplay, they still don’t address who should be allowed to call the look()
function.
Right now, any player can call look()
once the shell is cracked, even if they didn’t participate in hitting it during the current round. This creates the following issues:
Fairness: Players who didn’t contribute should not be able to reap the benefits of seeing the kernel.
Incentivizing Contribution: The game needs to encourage active participation by ensuring that only those who helped crack the Walnut in a specific round are rewarded with access to the kernel.
The solution to this is implementing a conditional check on look()
which allows only those players who contributed in hitting the shell for a particular round (i.e., players whose hit count is >0 for that round) to view the kernel after the walnut is cracked.
Implementing the Reset Mechanism
The reset mechanism allows the Walnut to be reused for multiple rounds, with each round starting fresh. It restores the Walnut’s shell and kernel to their original states and increments the round counter to mark the beginning of a new round.
Here’s how we can implement the reset function:
What’s Happening Here?
Condition for Reset (
requireCracked
): The reset function can only be called once the Walnut’s shell is cracked, enforced by therequireCracked
modifier.Restoring Initial State: The shell strength and kernel are reset to their original values (
initialShellStrength
andinitialKernel
), ensuring the Walnut starts afresh for the next round.Round Tracking: The
round
counter increments each time the Walnut is reset, allowing us to distinguish between rounds.
Modifying hit() to track contributions
To enforce fair access to the kernel, we’ll track the number of hits each player contributes in a given round. This is achieved using the hitsPerRound
mapping:
Every time a player calls the hit()
function, we update their contribution in the current round:
What’s Happening Here?
Tracking Contributions: The
hitsPerRound
mapping records each player’s hits in the current round. This ensures we can verify who participated when the Walnut was cracked.Replayable Rounds: Because contributions are tracked by round, the game can fairly reset and start fresh without losing player data from previous rounds.
Restricting look() with a contributor check
To ensure only contributors can reveal the kernel, we’ll use a modifier called onlyContributor
:
We’ll then apply this modifier to the look()
function:
Congratulations! You made it through to writing the entire shielded smart contract for a multiplayer, multi-round, walnut app!
Final Walnut contract
Now, onto testing the contract!
Last updated