Skip to content

Commit

Permalink
feat: PPN notation to rotate pieces around a reference location/piece
Browse files Browse the repository at this point in the history
There is now support for rotating pieces around a reference location/pieces (#86):

* ``PieceId@>Angle|Location`` means rotating ``PieceId`` pieces ``Angle`` angle around ``Location`` location
* ``PieceId1@>Angle$PieceId2`` is a shortcut for ``PieceId1@>Angle|&PieceId2``
* ``PieceId$>Angle`` is a shortcut for ``PieceId@>Angle$PieceId``

Closes #86
  • Loading branch information
trevorld committed Feb 8, 2021
1 parent b200738 commit 712c442
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Encoding: UTF-8
Package: ppgames
Type: Package
Title: Piecepack Games
Version: 0.6.0-17
Version: 0.6.0-18
Author: Trevor L Davis.
Maintainer: Trevor L Davis <trevor.l.davis@gmail.com>
Description: This is an R package designed to make piecepack game diagrams.
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ New features
- ``N?PieceSpec`` is now a shortcut for ``N&?PieceSpec``
- An empty PieceSpec now refers to the "last active" piece(s)
- Brace expansions are now expanded before semi-colons are
- There is now support for rotating pieces around a reference location/pieces (#86):

* ``PieceId@>Angle|Location`` means rotating ``PieceId`` pieces ``Angle`` angle around ``Location`` location
* ``PieceId1@>Angle$PieceId2`` is a shortcut for ``PieceId1@>Angle|&PieceId2``
* ``PieceId$>Angle`` is a shortcut for ``PieceId@>Angle$PieceId``

* New function ``view_game()`` provides a simple command-line PPN viewer/editor.
* New function ``cat_game()`` renders a plaintext animation of a game within the terminal.
Expand Down
35 changes: 31 additions & 4 deletions R/ppn.R
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ get_coords_from_piece_id <- function(piece_id, df, state = create_state(df)) {
process_submove <- function(df, text, state = create_state(df)) {
if (text == "") {
df
} else if (str_detect(text, "\\$>")) {
process_percent_rotate_move(df, text, state)
} else if (str_detect(text, "@>")) {
process_rotate_move(df, text, state)
} else if (str_detect(text, "@%")) {
Expand Down Expand Up @@ -709,15 +711,40 @@ process_submove <- function(df, text, state = create_state(df)) {
process_rotate_move <- function(df, text, state = create_state(df), clockwise = TRUE) {
id_angle <- str_split(text, "@>")[[1]]
piece_id <- id_angle[1L]
angle <- as.numeric(id_angle[2])
angle <- ifelse(clockwise, -angle, angle)

indices <- get_indices_from_piece_id(piece_id, df, state)
df$angle[indices] <- df$angle[indices] + angle
angle <- id_angle[2L]
if (str_detect(angle, "\\|")) {
angle_coords <- str_split(angle, "\\|", n=2L)[[1L]]
angle <- as.numeric(angle_coords[1L])
location <- get_xy(angle_coords[2L], df, state)
} else if (str_detect(angle, "\\$")) {
angle_coords <- str_split(angle, "\\$", n=2L)[[1L]]
angle <- as.numeric(angle_coords[1L])
location <- get_xy(paste0("&", angle_coords[2L]), df, state)
} else {
angle <- as.numeric(angle)
location <- NULL
}
angle <- ifelse(clockwise, -angle, angle)
if (!is.null(location)) {
p <- piecepackr:::Point2D$new(x = df$x[indices], y = df$x[indices])
p <- p$translate(-location$x, -location$y)$rotate(angle)$translate(location$x, location$y)
df$x[indices] <- p$x
df$y[indices] <- p$y
}
df$angle[indices] <- (df$angle[indices] + angle)
state$active_id <- df$id[indices]
df
}

process_percent_rotate_move <- function(df, text, state = create_state(df)) {
pa <- str_split(text, "\\$>")[[1L]]
piece_spec <- pa[1L]
angle <- pa[2L]
text <- str_glue("{piece_spec}@>{angle}${piece_spec}")
process_rotate_move(df, text, state)
}

process_hash_move <- function(df, text, state = create_state(df)) {
id1_id2 <- str_split(text, "#")[[1]]
piece_id1 <- id1_id2[1L]
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test_ppn.R
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,19 @@ test_that("rotations work as expected", {
expect_equal(df$angle, 0)
df <- process_move(df, "b2@>-90")
expect_equal(df$angle, 90)
df <- initialize_df(df_none())
df <- process_move(df, "C3@(1.5,1.5) S@a1 M@b2 3?C3@>180|b2")
expect_equal(df$angle, rep(-180, 3))
expect_equal(df$x, c(2.5,3,2))
expect_equal(df$y, c(2.5,3,2))
df <- process_move(df, "3?C3@>-180$?M")
expect_equal(df$angle, rep(0, 3))
expect_equal(df$x, c(1.5,1,2))
expect_equal(df$y, c(1.5,1,2))
df <- process_move(df, "3?C3$>180")
expect_equal(df$angle, rep(-180, 3))
expect_equal(df$x, c(1.5,2,1))
expect_equal(df$y, c(1.5,2,1))
})

test_that("address works as expected", {
Expand Down

0 comments on commit 712c442

Please sign in to comment.