Hi, I’ve been going through the SDK tutorials again, in the Multiple Actors tut, there is a hard coded result in the Rock/Paper/Scissors canister when calling…
- dfx canister call rock_paper_scissors contest
How would I call the battle_round to get a random result? (I’ve tried editing to main.mo bu had no luck)
The battle_round
function calls the alice
and bob
functions, which determine what Alice and Bob will play respectively. Currently these two functions return hard-coded results, with Alice always playing paper, Bob always playing rock. It’s the Hollywood principle - “You don’t call us, we’ll call you”
To turn this into an actual game, you’d have to edit these two methods, such that the choice for each round would be based on eg what a real-life player picks, or be based on the previous moves of the other player if you want to implement a computer opponent.
Chances are that, for your first draft, the battle_rounds
function can remain untouched. All it does is call alice
and bob
, and grant points according to who won the current round.
1 Like
Is there a way to generate a random number so that their decisions are more interesting?
Kind of, but it won’t be all too straight-forward.
Motoko doesn’t provide access to randomness as of now. Meaning if you want randomness you’ll need to generate it client-side (ie in JS), or implement a PRNG of your own. Both are suitable for some basic PoC game, but take some work.
Will that be added in future? Or is it an inherent constraint due to an architectural decision that the methods must be deterministic?
2 Likes
In the future it should be possible to derive randomness from dfinitys random beacon natively.
4 Likes