Learn R Programming

lightsout (version 0.3.2)

play: Play (press) a single light or multiple lights on a board

Description

In classic mode, pressing a light will toggle it and its four adjacent lights. In variant mode, pressing a light will toggle it and all other lights in its row and column. Toggling a light means switching it from on to off or from off to on.

Usage

play(board, row, col, matrix)

Value

A new lightsout board object after the given lights are pressed.

Arguments

board

A lightsout board

row

The row of the light to press. To press multiple lights, use a list of row numbers. If a list is provided, then the col argument must also be a list of the same length.

col

The column of the light to press. To press multiple lights, use a list of column numbers. If a list is provided, then the row argument must also be a list of the same length.

matrix

Instead of using row and col, a matrix can be used to specify which lights to press. The matrix must have the same dimensions as the board. Any position in the given matrix with a value of 1 will result in a press of a light in the same position in the board.

See Also

solve_board empty_board new_board random_board

Examples

Run this code
# Create a 5x5 board with all lights switched off and then press some lights

board <- empty_board(5)
board

# Press the light at (2,1)
newboard <- play(board, 2, 1)
newboard

# Press the light at (2,1) and then at (3,4)
newboard <- board %>% play(2, 1) %>% play(3, 4)
newboard

# Press both lights with one call
newboard <- play(board, c(2, 3), c(1, 4))
newboard

# Press both lights using a matrix instead of specifying rows and columns
newboard <- play(board, matrix = matrix(
                          c(0, 0, 0, 0, 0,
                            1, 0, 0, 0, 0,
                            0, 0, 0, 1, 0,
                            0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0),
                          nrow = 5, byrow = TRUE))
newboard

# Press the same lights, but this time when the game mode is not classic,
# and the whole row/column get toggled
empty_board(5, classic = FALSE) %>% play(2, 1)
empty_board(5, classic = FALSE) %>% play(c(2, 3), c(1, 4))

Run the code above in your browser using DataLab