Title: | Use R for Fun |
---|---|
Description: | This is a collection of R games and other funny stuff, such as the classic Mine sweeper and sliding puzzles. |
Authors: | Yihui Xie [aut, cre] |
Maintainer: | Yihui Xie <[email protected]> |
License: | GPL |
Version: | 0.3.1 |
Built: | 2024-08-23 05:47:23 UTC |
Source: | https://github.com/yihui/fun |
This is a collection of R games and other funny stuff, such as the classical Mine sweeper and sliding puzzles.
New games are always welcome; if you know GIT and want to join the development, please go to https://github.com/yihui/fun; or simply contribute ideas at https://github.com/yihui/fun/issues.
Yihui Xie, Taiyun Wei, and Yixuan Qiu
## See the examples in each function, or check out the demos demo(package = "fun")
## See the examples in each function, or check out the demos demo(package = "fun")
Please try hard to find the letter "N" in 300 "M"s, one "6" in 300 "9"s, etc.
alzheimer_test( char1 = c("9", "O", "M", "I", "F", "D"), char2 = c("6", "C", "N", "T", "E", "O"), nr = 10, nc = 30, seed = NULL, ... )
alzheimer_test( char1 = c("9", "O", "M", "I", "F", "D"), char2 = c("6", "C", "N", "T", "E", "O"), nr = 10, nc = 30, seed = NULL, ... )
char1 |
the 'background' character |
char2 |
the character to be found out |
nr |
number of rows of the character rectangle |
nc |
number of columns |
seed |
seed for random number generation |
... |
other arguments passed to |
If at least one test item has been passed, a data.frame will be returned telling the result of the test.
Don't be too serious about this test. I'm no doctor, but I think this will be a good present to your friends.
Yihui Xie <https://yihui.org>
## Not run: x = alzheimer_test() ## End(Not run)
## Not run: x = alzheimer_test() ## End(Not run)
There are two players in this game who play one after the other using black and white stones respectively. The winner is the first player to get an unbroken row of five stones horizontally, vertically, or diagonally.
gomoku(n = 19)
gomoku(n = 19)
n |
the number of rows and columns in the board (the default 19 generates the standard board) |
NULL
The players should judge the winner by themselves; this function does not do this job (patches are welcome, of course).
Yihui Xie <https://yihui.org>; modified from the code by pklin
https://d.cosx.org/d/104750, https://en.wikipedia.org/wiki/Gomoku
gomoku()
gomoku()
The characters c("&", '"', "'", "<", ">")
will be replaced with
c("&", """, "'", "<", ">")
, respectively.
htmlspecialchars(string)
htmlspecialchars(string)
string |
the string with (or w/o) HTML special chars |
the string with special chars replaced.
Yihui Xie <https://yihui.org>
https://www.php.net/manual/en/function.htmlspecialchars.php
htmlspecialchars("<a href = 'https://yihui.org'>Yihui</a>") # <a href = 'https://yihui.org'>Yihui</a>
htmlspecialchars("<a href = 'https://yihui.org'>Yihui</a>") # <a href = 'https://yihui.org'>Yihui</a>
By default, the white squares in the plot denote the lights that are on, and black ones for the off. When you click on a light, this light as well as the four neighbors will switch theirs status. Your mission is to close all the lights.
lights_out( width = 5, height = 5, steps = 3, cheat = FALSE, col.off = "black", col.on = "white", col.frame = "lightblue", seed = NULL )
lights_out( width = 5, height = 5, steps = 3, cheat = FALSE, col.off = "black", col.on = "white", col.frame = "lightblue", seed = NULL )
width |
number of lights in x axis |
height |
number of lights in y axis |
steps |
number of “seed” lights to initialize the puzzle. In general,
the larger |
cheat |
logical. If |
col.off |
color when lights off |
col.on |
color when lights on |
col.frame |
color of lights border |
seed |
seed for random number generator |
Linux/Mac users have to use X11(type = 'Xlib')
or the Cairo
graphics device Cairo()
in the package cairoDevice.
Yixuan Qiu [email protected]
https://en.wikipedia.org/wiki/Lights_Out_(game)
## should use Xlib for the x11() device under *nix, e.g if (interactive()) { if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib") lights_out() }
## should use Xlib for the x11() device under *nix, e.g if (interactive()) { if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib") lights_out() }
The controls should be familiar to you: Click the left mouse button to dig in an area, and right button to mark or unmark the area with flags.
mine_sweeper(width = 10, height = 10, mines = 20, cheat = FALSE)
mine_sweeper(width = 10, height = 10, mines = 20, cheat = FALSE)
width |
number of grids in horizontal axis |
height |
number of grids in vertical axis |
mines |
number of mines |
cheat |
logical. If |
Linux/Mac users have to use X11(type = 'Xlib')
or the Cairo
graphics device Cairo()
in the package cairoDevice.
Yixuan Qiu [email protected]
https://en.wikipedia.org/wiki/Minesweeper_(computer_game)
## should use Xlib for the x11() device under *nix, e.g if (interactive()) { if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib") mine_sweeper() }
## should use Xlib for the x11() device under *nix, e.g if (interactive()) { if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib") mine_sweeper() }
This function generates a random passord sampled from the ASCII table.
random_password(length = 12, replace = FALSE, extended = TRUE)
random_password(length = 12, replace = FALSE, extended = TRUE)
length |
length of the password |
replace |
sample from the ASCII table with ( |
extended |
if |
a character string
Yihui Xie <https://yihui.org>
random_password() # set the seed to get fixed password every time; you may just remember the seed # and forget the real password because it's reproducible set.seed(123) random_password() # long password random_password(20, TRUE)
random_password() # set the seed to get fixed password every time; you may just remember the seed # and forget the real password because it's reproducible set.seed(123) random_password() # long password random_password(20, TRUE)
There is a command shutdown
in both Windows and Linux, and this
function uses it to shut down a computer.
shutdown(wait = 0)
shutdown(wait = 0)
wait |
time to wait before shutting down (in seconds); passed to
|
After the time wait
has passed, R will execute shutdown -s -t
0
(for Windows) or shutdown -h now
to shut down the computer.
The status code of system
.
Yihui Xie <https://yihui.org>
if (interactive()) { # when your code is extremely time-consuming, you may need this function; e.g. # you go to sleep, and R is running long computation... complex graphics... and # long long computation... at last, shutdown() # the next day you wake up, 'thank you, R' :) }
if (interactive()) { # when your code is extremely time-consuming, you may need this function; e.g. # you go to sleep, and R is running long computation... complex graphics... and # long long computation... at last, shutdown() # the next day you wake up, 'thank you, R' :) }
Use R to play sliding puzzle
sliding_puzzle(size = c(3, 3), bg = "lightblue", z = NULL)
sliding_puzzle(size = c(3, 3), bg = "lightblue", z = NULL)
size |
two dimensional vector, the size of sliding puzzle. Note: the
element of |
bg |
the background color of blocks. |
z |
the matrix of sliding puzzle, if z is specified, |
If size
is specified and z
is NULL
, then the function
will generate a solvable sliding puzzle.
Linux/Mac users have to use X11(type = 'Xlib')
or the Cairo graphics device Cairo()
in the package
cairoDevice.
Taiyun Wei
About the sliding puzzle: https://en.wikipedia.org/wiki/Sliding_puzzle
## should use Xlib for the x11() device under *nix, e.g if (interactive()) { if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib") sliding_puzzle() sliding_puzzle(z = matrix(0:11, 3, 4)) }
## should use Xlib for the x11() device under *nix, e.g if (interactive()) { if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib") sliding_puzzle() sliding_puzzle(z = matrix(0:11, 3, 4)) }
Use R to write tag data (tag words, frequency, hyperlinks and colors, etc) into JavaScript, and the JavaScript code will generate a Flash movie. Finally the tag cloud can be created with fantastic 3D rotation effect.
tag_cloud( tagData, htmlOutput = "tagCloud.html", SWFPath = "tagcloud.swf", JSPath = "swfobject.js", divId = "tagCloudId", width = 600, height = 400, transparent = FALSE, tcolor = "333333", tcolor2 = "009900", hicolor = "ff0000", distr = "true", tspeed = 100, version = 9, bgcolor = "ffffff", useXML = FALSE, htmlTitle = "Tag Cloud", noFlashJS, target = NULL, scriptOnly = FALSE, encode = FALSE, reserved = FALSE )
tag_cloud( tagData, htmlOutput = "tagCloud.html", SWFPath = "tagcloud.swf", JSPath = "swfobject.js", divId = "tagCloudId", width = 600, height = 400, transparent = FALSE, tcolor = "333333", tcolor2 = "009900", hicolor = "ff0000", distr = "true", tspeed = 100, version = 9, bgcolor = "ffffff", useXML = FALSE, htmlTitle = "Tag Cloud", noFlashJS, target = NULL, scriptOnly = FALSE, encode = FALSE, reserved = FALSE )
tagData |
a data.frame containing at least 3 columns: |
htmlOutput |
filename of the HTML output |
SWFPath |
path of the SWF source file (‘tagcloud.swf’); see
|
JSPath |
path of the JavaScript file (‘swfobject.js’); see
|
divId |
id of the tag cloud div (HTML layer) |
width , height
|
width and height of the tag cloud |
transparent |
logical; whether to use transparent backgroud for the Flash movie? |
tcolor , tcolor2 , hicolor , distr , tspeed
|
see Details |
version |
the required Flash version |
bgcolor |
backgroud color of the Flash movie |
useXML |
use XML file for the tag information or just a string; this will be passed to the Flash object as a variable |
htmlTitle |
title of the HTML file |
noFlashJS |
text to show if Flash or JavaScript is not supported |
target |
target window of the hyperlinks; possible values are
|
scriptOnly |
print the script in the console only? (if |
encode |
encode the tag XML or not? (with
|
reserved |
should reserved characters be encoded? see
|
This function is based on the WordPress plugin “wp-cumulus”. If there are any arguments you don't understand, please check the reference.
NULL
Yihui Xie <https://yihui.org>
About the WordPress plugin: https://wordpress.org/plugins/wp-cumulus/
Usage of the SWFObject: http://blog.deconcept.com/swfobject/
An example of visualizing tags in my blog: https://yihui.org/en/2009/06/creating-tag-cloud-using-r-and-flash-javascript-swfobject/
data(tagData) htmlFile = paste(tempfile(), ".html", sep = "") if (file.create(htmlFile)) { tag_cloud(tagData, htmlFile) if (!interactive()) browseURL(htmlFile) }
data(tagData) htmlFile = paste(tempfile(), ".html", sep = "") if (file.create(htmlFile)) { tag_cloud(tagData, htmlFile) if (!interactive()) browseURL(htmlFile) }
Tag data collected from Yihui Xie's Blog, containing the tag words, frequency and hyperlinks, etc.
A data frame with 45 observations on the following 5 variables.
a character vector; the tag words
a character vector; hyperlinks of tags
a numeric vector; the frequency of tags in blogs (see Details)
a character vector in hexadecimal format specifying the RGB component of tag colors
a character vector similar to color
; the color when
mouse hangs over the tag
The count
was multiplied by 4 in the data in order that the tag cloud
could be more clear.
https://yihui.org/en/ (accessed on June 10, 2009)
hist(tagData$count/4, 10) # extremely right skewed # see help(tag_cloud) for the example of creating tag cloud with this data
hist(tagData$count/4, 10) # extremely right skewed # see help(tag_cloud) for the example of creating tag cloud with this data
This function uses the recursive algorithm to solve the Tower of Hanoi puzzle, and demonstrates the game in animation.
tower_of_hanoi(n = 7)
tower_of_hanoi(n = 7)
n |
an integer indicating the number of disks on the rot. |
This function was written by Linlin Yan <[email protected]> in a Chinese forum (See 'References') to show the usage of recursive algorithm.
Linlin Yan <[email protected]>
Original code: https://d.cosx.org/d/101199
About the Tower of Hanoi: https://en.wikipedia.org/wiki/Tower_of_Hanoi
## Not run: tower_of_hanoi(7) ## End(Not run)
## Not run: tower_of_hanoi(7) ## End(Not run)