Define some functions

Playsesssion 4

zork
R
Author

Charles T. Gray

Published

November 10, 2024

Playsession highlight: still haven’t cracked the operators or there is no way to combine the lunch with the nasty knife.

>open sack          
Opening the brown sack reveals:
  A clove of garlic.
  A lunch.
>hide knife in lunch
I don't understand that.
>look at lunch
I see nothing special about the lunch.
>put knife in bottle
I can't reach inside.
>use knife on lunch
I don't understand that.
>use knife with lunch
I don't understand that.
>combine knife and lunch
I don't understand that.

Have repeated enough to want some helper functions now. Will draft them in this post, but I suppose next play session I suppose I’ll need to be packaging it.

get data function

Code
get_nodes_and_edges_raw <- function() {

  googlesheets4::gs4_deauth()

  gs_url <- "https://docs.google.com/spreadsheets/d/10PSDN97MdqDH_QaofhVh5Te8n4MxpYf6XJM3JsGetxI/edit?gid=1960240112#gid=1960240112"
  
  message('\n*** zork nodes ***\n')
  zork_nodes <- googlesheets4::read_sheet(gs_url, 'nodes')
  print(head(zork_nodes, 3))

  message('\n*** zork edges ***\n')
  zork_edges <- googlesheets4::read_sheet(gs_url, 'edges')
  print(head(zork_edges, 3))

  return(
    list(
      zork_nodes = zork_nodes,
      zork_edges = zork_edges
    )
  )
} 
Code
# print() doesn't respect newline even with escape, but message handles newline
# Also learnt, use message() for the text, but use print() for structured data 
zork_nodes_and_edges <- get_nodes_and_edges_raw()

*** zork nodes ***
✔ Reading from "zork-graph-data".
✔ Range ''nodes''.
# A tibble: 3 × 4
  perspective_location region interactivity   loot   
  <chr>                <chr>  <chr>           <chr>  
1 west of house        forest solved: mailbox leaflet
2 north of house       forest none            none   
3 forest               forest song bird?      none   

*** zork edges ***
✔ Reading from "zork-graph-data".
✔ Range ''edges''.
# A tibble: 3 × 4
  perspective_location perspective_to_target target_location discovered         
  <chr>                <chr>                 <chr>           <dttm>             
1 west of house        north                 north of house  2024-10-30 00:00:00
2 north of house       east                  behind house    2024-10-30 00:00:00
3 behind house         west                  kitchen         2024-10-30 00:00:00

zork graph function

Code
create_zork_graph <- function(zork_nodes_and_edges, selected_region = 'all') {
  zork_nodes <- zork_nodes_and_edges$zork_nodes
  zork_edges <- zork_nodes_and_edges$zork_edges

   if (selected_region != 'all') {
    zork_nodes <- 
      zork_nodes |>  
        dplyr::filter(region %in% c(selected_region, 'unexplored'))
  }

  zork_base_graph <- 
    zork_edges |> 
      dplyr::mutate(
        from = perspective_location,
        to = target_location
      ) |>
      # does order matter?
      dplyr::select(from, to, everything()) |>
      tidygraph::as_tbl_graph()
  # Join the nodes data frame to the nodes of the graph
  zork_graph <- zork_base_graph |>
    tidygraph::activate(nodes) |>
    # add attributes to nodes
    dplyr::inner_join(zork_nodes, by = c("name" = "perspective_location"))

  print(zork_graph)

  # report regions discovered
  message('\n*** zork regions discovered (zork_ggraph selected_region) ***\n')
  print(
    zork_nodes |>
      dplyr::filter(!(region %in% c('unexplored', 'inaccessible'))) |>
      dplyr::pull(region) |>
      unique()

  )
  message('\n***\n')

  return(zork_graph)

}
Code
zork_graph <- create_zork_graph(zork_nodes_and_edges)
# A tbl_graph: 18 nodes and 51 edges
#
# A directed multigraph with 1 component
#
# Node Data: 18 × 4 (active)
   name                  region     interactivity            loot               
   <chr>                 <chr>      <chr>                    <chr>              
 1 west of house         forest     solved: mailbox          leaflet            
 2 north of house        forest     none                     none               
 3 behind house          forest     window to house          none               
 4 kitchen               house      sack                     water, lunch, garl…
 5 living room           house      rug, locked door to west sword, lantern     
 6 cellar                cellar     none                     none               
 7 west of chasm         cellar     unknown                  unknown            
 8 gallery               cellar     painting                 none               
 9 studio                cellar     note, 69 colours         none               
10 north south crawlway  cellar     hole above, no climbing  unknown            
11 the troll room        cellar     troll                    none               
12 maze                  cellar     unknown                  unknown            
13 east west passageway  cellar     unknown                  unknown            
14 ravine crossing       cellar     none                     none               
15 circular 8 directions cellar     compass needle spins     none               
16 attic                 house      none                     rope, knife, clay  
17 inaccessible          unexplored none                     none               
18 unexplored            unexplored unknown                  unknown            
#
# Edge Data: 51 × 6
   from    to perspective_location perspective_to_target target_location
  <int> <int> <chr>                <chr>                 <chr>          
1     1     2 west of house        north                 north of house 
2     2     3 north of house       east                  behind house   
3     3     4 behind house         west                  kitchen        
# ℹ 48 more rows
# ℹ 1 more variable: discovered <dttm>

*** zork regions discovered (zork_ggraph selected_region) ***
[1] "forest" "house"  "cellar"

***

zork ggraph

Code
zork_ggraph <- function(zork_nodes_and_edges, region = 'all') {
  zork_graph <- create_zork_graph(zork_nodes_and_edges, region)

  cols_f <- colorRampPalette(RColorBrewer::brewer.pal(8, 'Dark2'))

  n_regions <- zork_nodes_and_edges$zork_nodes |> 
    dplyr::pull(region) |>
    unique() |>
    length()

  zork_graph |> 
    ggraph::ggraph() +
    ggraph::geom_edge_fan(
      ggplot2::aes(label = perspective_to_target), 
      label_dodge = ggplot2::unit(2.5, 'mm'),
      alpha = 0.8,
      colour = 'grey',
      label_size = 6,
      label_colour = 'darkgrey',
      family = 'Courier',
      arrow = ggplot2::arrow(length = ggplot2::unit(2, 'mm')), 
      end_cap = ggraph::circle(1, 'cm')
    ) +
    ggraph::geom_node_text(
      ggplot2::aes(
        label = name,
        colour = region
      ),
      repel = TRUE,
      size = 8,
      family = 'Courier'
    ) +

    # theming
    ggraph::scale_edge_colour_manual(values = cols_f(n_regions)) + 
    ggplot2::scale_colour_manual(values = cols_f(n_regions)) +
    ggplot2::theme_minimal(
      base_family = 'Courier',
      base_size = 20
    ) +
    ggplot2::theme(
      axis.ticks = ggplot2::element_blank(),
      axis.text = ggplot2::element_blank(),
      panel.grid = ggplot2::element_blank() #,
      # plot.margin = margin(30, 30, 30, 30)  # Adjust plot margins
  ) +
      ggplot2::scale_x_continuous(expand = ggplot2::expansion(mult = 0.2)) +  # Add space around x-axis
      ggplot2::scale_y_continuous(expand = ggplot2::expansion(mult = 0.2)) +  # Add space around y-axis

    ggplot2::labs(
      title = "Paths of Zork",
      x = '',
      y = '',
      caption = "Angle of edge determined by ggraph, not direction"
    )


}

playsession

Today I’m impatient to get to the cellar and try out putting the nasty knife in the food I find in the sack and I should try talking to the troll.

get to the cellar

I’ll note what I haven’t explored and get to the cellar.

Code
zork_ggraph(zork_nodes_and_edges, 'forest')
# A tbl_graph: 5 nodes and 11 edges
#
# A directed acyclic multigraph with 2 components
#
# Node Data: 5 × 4 (active)
  name           region     interactivity   loot   
  <chr>          <chr>      <chr>           <chr>  
1 west of house  forest     solved: mailbox leaflet
2 north of house forest     none            none   
3 behind house   forest     window to house none   
4 inaccessible   unexplored none            none   
5 unexplored     unexplored unknown         unknown
#
# Edge Data: 11 × 6
   from    to perspective_location perspective_to_target target_location
  <int> <int> <chr>                <chr>                 <chr>          
1     1     2 west of house        north                 north of house 
2     2     3 north of house       east                  behind house   
3     1     5 west of house        south                 unexplored     
# ℹ 8 more rows
# ℹ 1 more variable: discovered <dttm>

*** zork regions discovered (zork_ggraph selected_region) ***
[1] "forest"

***
Using "sugiyama" as default layout

get to the troll

Make a beeline for the troll.

Code
zork_ggraph(zork_nodes_and_edges, 'cellar')
# A tbl_graph: 12 nodes and 36 edges
#
# A directed multigraph with 1 component
#
# Node Data: 12 × 4 (active)
   name                  region     interactivity           loot   
   <chr>                 <chr>      <chr>                   <chr>  
 1 cellar                cellar     none                    none   
 2 west of chasm         cellar     unknown                 unknown
 3 gallery               cellar     painting                none   
 4 studio                cellar     note, 69 colours        none   
 5 north south crawlway  cellar     hole above, no climbing unknown
 6 the troll room        cellar     troll                   none   
 7 maze                  cellar     unknown                 unknown
 8 east west passageway  cellar     unknown                 unknown
 9 ravine crossing       cellar     none                    none   
10 circular 8 directions cellar     compass needle spins    none   
11 inaccessible          unexplored none                    none   
12 unexplored            unexplored unknown                 unknown
#
# Edge Data: 36 × 6
   from    to perspective_location perspective_to_target target_location
  <int> <int> <chr>                <chr>                 <chr>          
1     1     6 cellar               east                  the troll room 
2     1     2 cellar               south                 west of chasm  
3     1    11 cellar               west                  inaccessible   
# ℹ 33 more rows
# ℹ 1 more variable: discovered <dttm>

*** zork regions discovered (zork_ggraph selected_region) ***
[1] "cellar"

***
Using "stress" as default layout

overview

Code
zork_ggraph(zork_nodes_and_edges)
# A tbl_graph: 18 nodes and 51 edges
#
# A directed multigraph with 1 component
#
# Node Data: 18 × 4 (active)
   name                  region     interactivity            loot               
   <chr>                 <chr>      <chr>                    <chr>              
 1 west of house         forest     solved: mailbox          leaflet            
 2 north of house        forest     none                     none               
 3 behind house          forest     window to house          none               
 4 kitchen               house      sack                     water, lunch, garl…
 5 living room           house      rug, locked door to west sword, lantern     
 6 cellar                cellar     none                     none               
 7 west of chasm         cellar     unknown                  unknown            
 8 gallery               cellar     painting                 none               
 9 studio                cellar     note, 69 colours         none               
10 north south crawlway  cellar     hole above, no climbing  unknown            
11 the troll room        cellar     troll                    none               
12 maze                  cellar     unknown                  unknown            
13 east west passageway  cellar     unknown                  unknown            
14 ravine crossing       cellar     none                     none               
15 circular 8 directions cellar     compass needle spins     none               
16 attic                 house      none                     rope, knife, clay  
17 inaccessible          unexplored none                     none               
18 unexplored            unexplored unknown                  unknown            
#
# Edge Data: 51 × 6
   from    to perspective_location perspective_to_target target_location
  <int> <int> <chr>                <chr>                 <chr>          
1     1     2 west of house        north                 north of house 
2     2     3 north of house       east                  behind house   
3     3     4 behind house         west                  kitchen        
# ℹ 48 more rows
# ℹ 1 more variable: discovered <dttm>

*** zork regions discovered (zork_ggraph selected_region) ***
[1] "forest" "house"  "cellar"

***
Using "stress" as default layout

lost in the maze

In this playsession, I ended up in a maze twice, so I think I’ve found a new region.

>go south
You are in a narrow east-west passageway.  There is a narrow staircase
leading down at the north end of the room.
>go down
You are in a deep ravine at a crossing with an east-west crawlway.
Some stone steps are at the south of the ravine and a steep staircase
descends.
>look
You are in a deep ravine at a crossing with an east-west crawlway.
Some stone steps are at the south of the ravine and a steep staircase
descends.
>go south
You are in a narrow east-west passageway.  There is a narrow staircase
leading down at the north end of the room.
>go east
You are in a circular room with passages off in eight directions.
Your compass needle spins wildly, and you cannot get your bearings.
>look
You are in a circular room with passages off in eight directions.
Your compass needle spins wildly, and you cannot get your bearings.
>go west
Unfortunately, it is impossible to tell directions in here.
You are in a maze of twisty little passages, all alike.
>go east
You are in a maze of twisty little passages, all alike.
>go north
You are in a maze of twisty little passages, all alike.
>go north
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go south
You are in a maze of twisty little passages, all alike.
>go east
You are in a maze of twisty little passages, all alike.
>go east
Dead end.
>go north
There is a wall there.
>go south
You are in a maze of twisty little passages, all alike.
>go east
Dead end.
>go west
There is a wall there.
>go south
You are in a maze of twisty little passages, all alike.
>go east
Dead end.
>go west
There is a wall there.
>go south
You are in a maze of twisty little passages, all alike.
>go east
Dead end.
>go west
There is a wall there.