Skip to content

Instantly share code, notes, and snippets.

@cmontella
Created September 13, 2016 21:30
Show Gist options
  • Save cmontella/6a32ec7b88a8d1a385887d80589c612a to your computer and use it in GitHub Desktop.
Save cmontella/6a32ec7b88a8d1a385887d80589c612a to your computer and use it in GitHub Desktop.
rock-paper-scissors.eve
# Rock Scissor Paper
Based on https://rosettacode.org/wiki/Rock-paper-scissors
Rules are simple:
The winner is decided by a set of rules:
* Rock beats scissors
* Scissors beat paper
* Paper beats rock
```
match
[#session-connect]
commit
[#win-condition winner: "rock" loser: "scissor"]
[#win-condition winner: "scissor" loser: "paper"]
[#win-condition winner: "paper" loser: "rock"]
[#choice kind: "rock", id: 0]
[#choice kind: "scissor", id: 1]
[#choice kind: "paper", id: 2]
[@app aiScore: 0, playerScore: 0]
```
## Present the user a choice
```
match
choice = [#choice kind]
app = [@app]
bind
[#div text: "Choose your weapon: " children:
[#button @choice choice text: "{{kind}}"]
[#div app text: "Player-Score: {{app.playerScore}}, AI-Score: {{app.aiScore}}"]]
```
## What happens, when the user clicks on a choice?
```
match
anybutton = [#button @choice]
[#click #direct-target element: anybutton]
choice = anybutton.choice
app = [@app]
bind
app.player := choice.kind
```
## The AI chooses, when the user made a choice
```
match
app = [@app player]
rnd = random[seed: 1] * 1000
id = round[value: mod[value: rnd, by: 2]]
ai_choice = [#choice id]
bind
app.ai := ai_choice.kind
```
## Evaluate the winning condition:
```
match
app = [@app player ai playerScore aiScore]
(winner, pscore, ascore) = if [#win-condition winner: player loser: ai]
then ("player",playerScore + 1, aiScore)
else if [#win-condition winner: ai loser: player]
then ("AI", playerScore, aiScore + 1)
else ("Nobody, it's a draw.", playerScore, aiScore)
commit
app.playerScore := pscore
app.aiScore := ascore
app.winner := winner
app.playerChoice := player
app.aiChoice := ai
```
## Display the match results
```
match
[@app playerChoice aiChoice winner]
bind
[#div text: "Player: {{playerChoice}} AI: {{aiChoice}} Winner: {{winner}}"]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment