Skip to content

Commit

Permalink
Add rock-paper-scissors example
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons committed Nov 9, 2023
1 parent 5652b9d commit 388b50f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,49 @@ parse X I K (concat "(" StrY " " StrZ ")") :-
goal Str :- parse senten 1 6 Str.
`.trim();

export const ROCK_PAPER_SCISSORS = `
# Rock, paper, scissors
# We can play the regular version...
outcome rock crushes scissors.
outcome scissors cuts paper.
outcome paper covers rock.
# ...or choose to play the expanded variant
variant is { normal, expanded }.
outcome rock crushes lizard :- variant is expanded.
outcome lizard poisons spock :- variant is expanded.
outcome spock smashes scissors :- variant is expanded.
outcome scissors decapitates lizard :- variant is expanded.
outcome lizard eats paper :- variant is expanded.
outcome paper disproves spock :- variant is expanded.
outcome spock vaporizes rock :- variant is expanded.
player player1.
player player2.
move Move :- outcome Move _ _.
#builtin INT_PLUS plus.
round 1.
plays P N is { Move? } :- round N, player P, move Move.
# If the players make the same move, we go to the next round
round (plus Round 1) :-
plays player1 Round is Move,
plays player2 Round is Move.
# If the players make different moves, outcomes tells us who won.
# The "wins" and "round" constants are just a cheap hack to make
# the result look like a sentence without string concatenation.
eventually Winner wins round Round Move1 Defeats Move2 :-
outcome Move1 Defeats Move2,
plays Winner Round is Move1,
plays _ Round is Move2.
# Only return games where there are three or more rounds
#demand round 3.
`.trim();

export const GRAPH_GENERATION_EXAMPLE = `
# Generating graphs
#builtin NAT_SUCC s
Expand Down
5 changes: 4 additions & 1 deletion src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CHARACTER_CREATION_EXAMPLE,
CKY_PARSING_EXAMPLE,
GRAPH_GENERATION_EXAMPLE,
ROCK_PAPER_SCISSORS,
} from './examples';
import { Declaration, check } from './datalog/syntax';
import { compile } from './datalog/compile';
Expand Down Expand Up @@ -91,10 +92,12 @@ class SessionTabs {
const uuid1 = crypto.randomUUID();
const uuid2 = crypto.randomUUID();
const uuid3 = crypto.randomUUID();
const uuid4 = crypto.randomUUID();
localStorage.setItem(SessionTabs.LS_SESSION_TEXT(uuid1), CHARACTER_CREATION_EXAMPLE);
localStorage.setItem(SessionTabs.LS_SESSION_TEXT(uuid2), CKY_PARSING_EXAMPLE);
localStorage.setItem(SessionTabs.LS_SESSION_TEXT(uuid3), ROCK_PAPER_SCISSORS);
localStorage.setItem(SessionTabs.LS_SESSION_TEXT(uuid3), GRAPH_GENERATION_EXAMPLE);
localStorage.setItem(SessionTabs.LS_SESSION_LIST, `${uuid1},${uuid2},${uuid3}`);
localStorage.setItem(SessionTabs.LS_SESSION_LIST, `${uuid1},${uuid2},${uuid3},${uuid4}`);
}
this.sessionList = localStorage.getItem(SessionTabs.LS_SESSION_LIST)!.split(',');
this.activeSession = localStorage.getItem(SessionTabs.LS_SESSION_ACTIVE) ?? this.sessionList[0];
Expand Down

0 comments on commit 388b50f

Please sign in to comment.