Title: | Miscellaneous Functions for Japanese Mahjong |
---|---|
Description: | A collection of miscellaneous functions for Japanese mahjong that wraps C++ sources of 'shanten-number' <https://github.com/tomohxx/shanten-number> and 'cmajiang' <https://github.com/TadaoYamaoka/cmajiang>. |
Authors: | Akiru Kato [aut, cre], tomo hxx [ctb] (shanten-number), Tadao Yamaoka [ctb] (cmajiang), Lee Thomason [ctb] (tinyxml2) |
Maintainer: | Akiru Kato <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.0 |
Built: | 2024-11-22 03:10:38 UTC |
Source: | https://github.com/paithiov909/shikakusphere |
Calculate score of hand
calc_defen( pai, baopai, libaopai = "", rongpai = "", rule = default_rule(), zhuangfeng = c("z1", "z2", "z3", "z4"), menfeng = c("z2", "z3", "z4", "z1"), lizhi = c("none", "lizhi", "double-lizhi"), yifa = FALSE, qianggang = FALSE, lingshang = FALSE, haidi = c("none", "haidimoyue", "hedilaoyu"), tianhe = c("none", "tianhe", "dihe"), changbang = 0L, lizhibang = 0L )
calc_defen( pai, baopai, libaopai = "", rongpai = "", rule = default_rule(), zhuangfeng = c("z1", "z2", "z3", "z4"), menfeng = c("z2", "z3", "z4", "z1"), lizhi = c("none", "lizhi", "double-lizhi"), yifa = FALSE, qianggang = FALSE, lingshang = FALSE, haidi = c("none", "haidimoyue", "hedilaoyu"), tianhe = c("none", "tianhe", "dihe"), changbang = 0L, lizhibang = 0L )
pai |
A string scalar. This param is not vectorized. |
baopai |
A character vector. "Dora" indicators. |
libaopai |
A character vector. "Ura-dora" indicators. Leave empty if there is no libaopai. |
rongpai |
A string scalar such as |
rule |
A list; a rule set. Defaults to |
zhuangfeng |
A string scalar; "ba-kaze" tile. |
menfeng |
A string scalar; "ji-kaze" tile. |
lizhi |
A string scalar. Either "none", "lizhi", or "double-lizhi". |
yifa |
A logical scalar; flag for "ippatsu". |
qianggang |
A logical scalar; flag for "chankan". |
lingshang |
A logical scalar; flag for "rinshan-kaihou". |
haidi |
A string scalar. Either "none", "haidimoyue", or "hedilaoyu". |
tianhe |
A string scalar; Either "none", "tianhe", or "dihe". |
changbang |
An integer scalar; the number of counter sticks that indicates "honba". |
lizhibang |
An integer scalar; the number of 1,000-point sticks on the table. |
A data frame.
calc_defen("m345567p234s3378", baopai = "z1", rongpai = "s9=")
calc_defen("m345567p234s3378", baopai = "z1", rongpai = "s9=")
Calculate xiangting number of hands
calc_xiangting(pai)
calc_xiangting(pai)
pai |
A character vector. |
A tibble.
calc_xiangting(c("m345567p234s3378", "p222345z1234567"))
calc_xiangting(c("m345567p234s3378", "p222345z1234567"))
Collect tiles that can decrease the xiangting number of hands if they are drawn. In case there are no tingpais for the hand, the function warns about it and returns an empty character vector.
collect_tingpai(pai)
collect_tingpai(pai)
pai |
A character vector. |
A list of character vectors.
collect_tingpai(c("m345567p234s3378", "p222345z1234567"))
collect_tingpai(c("m345567p234s3378", "p222345z1234567"))
Converts symbols from 'mjai' format into 'cmajiang' format.
To read 'mjai' format logs, use convlog::read_tenhou6()
.
mjai_target(perspective, target) mjai_conv(type, pai, consumed, target)
mjai_target(perspective, target) mjai_conv(type, pai, consumed, target)
perspective |
An integer vector that indicates the plyaer's perspective. |
target |
An integer vector that represents the target of 'mjai' events. |
type |
A character vector that represents the type of 'mjai' events. |
pai |
A character vector of tiles to be converted from 'mjai' format into 'cmajiang' format. |
consumed |
A list of character vectors that represent the 'consumed' tiles in 'mjai' format. |
A character vector.
if (requireNamespace("dplyr", quietly = TRUE)) { data("ranked_game") ranked_game[["paifu"]] |> dplyr::filter( type %in% c("tsumo", "dahai", "chi", "pon", "daiminkan", "kakan", "ankan") ) |> dplyr::mutate( target = mjai_target(actor, target), pai = trans_tile(pai) ) |> dplyr::group_by(game_id, round_id, actor) |> dplyr::mutate(pai = mjai_conv(type, pai, consumed, target)) |> dplyr::ungroup() }
if (requireNamespace("dplyr", quietly = TRUE)) { data("ranked_game") ranked_game[["paifu"]] |> dplyr::filter( type %in% c("tsumo", "dahai", "chi", "pon", "daiminkan", "kakan", "ankan") ) |> dplyr::mutate( target = mjai_target(actor, target), pai = trans_tile(pai) ) |> dplyr::group_by(game_id, round_id, actor) |> dplyr::mutate(pai = mjai_conv(type, pai, consumed, target)) |> dplyr::ungroup() }
Create default rule set
default_rule()
default_rule()
A list.
https://github.com/kobalab/majiang-core/wiki/%E3%83%AB%E3%83%BC%E3%83%AB
Detect specific tiles
tile2rank(x) tile2suit(x) is_fulou(x) is_tsumogiri(x) is_lizhi(x) is_suit(x, suit = c("m", "p", "s", "z")) is_yaojiu(x, negate = FALSE) is_zhongzhang(x) is_suzhi(x, suzhi = c("23", "34", "45", "56", "67", "78"))
tile2rank(x) tile2suit(x) is_fulou(x) is_tsumogiri(x) is_lizhi(x) is_suit(x, suit = c("m", "p", "s", "z")) is_yaojiu(x, negate = FALSE) is_zhongzhang(x) is_suzhi(x, suzhi = c("23", "34", "45", "56", "67", "78"))
x |
A character vector. |
suit |
A string scalar. Either "m", "p", "s", or "z". |
negate |
A logical scalar. |
suzhi |
A string scalar. Either "23", "34", "45", "56", "67", or "78". |
For tile2rank
and tile2suit
: a character vector
(an empty string for "_").
For other functions: a logical vector.
This function is a short hand for paistr(pai) |> plot()
hand2img(pai, ...)
hand2img(pai, ...)
pai |
A character vector. |
... |
Other arguments passed to |
List of hupai
hupai
hupai
An object of class tbl_df
(inherits from tbl
, data.frame
) with 54 rows and 3 columns.
A tibble that contains the list of hupai.
The list includes the following columns:
id
: The id of the hupai.
en
: The English name of the hupai.
jp
: The Japanese name of the hupai.
Convert integers to tiles
int2tile(x = seq_len(38) - 1, origin = c("zero", "one"))
int2tile(x = seq_len(38) - 1, origin = c("zero", "one"))
x |
An integer vector. |
origin |
A string scalar. Either "zero" or "one". |
A factor.
int2tile(c(0, 1, 25, 37))
int2tile(c(0, 1, 25, 37))
Check if each element is a valid tile
is_valid_tile(x)
is_valid_tile(x)
x |
A character vector to be checked. |
A logical vector.
Arrange all tiles from a data frame.
lineup(x)
lineup(x)
x |
A data frame with columns |
An object of x$tile
class.
rand_hands()(3) |> paistr() |> tidy() |> lineup()
rand_hands()(3) |> paistr() |> tidy() |> lineup()
Compose hands from character vectors while ignoring invalid tiles. This function can handle any number of tiles in each hand, but cannot more than 5 identical tiles. If there are more than 5 identical tiles, arises an error.
lipai(x)
lipai(x)
x |
A list of character vectors or a character vector. |
A character vector.
lipai(list(c("m1", "m2", "m3"), c("p1", "p2", "p3")))
lipai(list(c("m1", "m2", "m3"), c("p1", "p2", "p3")))
Modify the default rule set by patch
modify_rule(patch = list())
modify_rule(patch = list())
patch |
A list. |
A list.
paistr
vectorCreate a paistr
vector from a character vector
that represents player's hands.
paistr(x = character()) is_paistr(x) ## S3 method for class 'skksph_paistr' calculate(x, ...) ## S3 method for class 'skksph_paistr' plot(x, y, ...) ## S3 method for class 'skksph_paistr' tidy(x, ...)
paistr(x = character()) is_paistr(x) ## S3 method for class 'skksph_paistr' calculate(x, ...) ## S3 method for class 'skksph_paistr' plot(x, y, ...) ## S3 method for class 'skksph_paistr' tidy(x, ...)
x |
|
... |
Other arguments for |
y |
|
Note that the validation of this function is not so strict.
For example, paistr("z0")
still produces a valid paistr
vector
even though "z0" is not a tile that actually exists.
These paistr
are simply ignored by the 'cmajiang' function wrapper.
The number of tiles displayed when print()
is not always accurate,
so if you need to count the actual number of tiles, use tidy()
.
For paistr()
: An object of class skksph_paistr
.
For is_paistr()
: A logical scalar.
For calculate()
: A tibble.
For plot()
: A bitmap image that internally converted
by magick::image_read_svg()
is invisibly returned.
For tidy()
: A tibble.
pai <- paistr(c("m055z7z7,m78-9,z5555,z666=", "m123s789z1117*,p5550")) print(pai) is_paistr(pai) calculate(pai) tidy(pai)
pai <- paistr(c("m055z7z7,m78-9,z5555,z666=", "m123s789z1117*,p5550")) print(pai) is_paistr(pai) calculate(pai) tidy(pai)
Parse comma separated chains of hupai ids into a list of factors.
parse_hupai(str, lang = c("en", "jp"))
parse_hupai(str, lang = c("en", "jp"))
str |
A character vector. |
lang |
A string scalar. Either "en" or "jp". |
A list of factors.
Proceed state of hands
proceed(qipai, zimo, dapai)
proceed(qipai, zimo, dapai)
qipai |
A list of character vectors where each element represents a player's qipai. |
zimo |
A list of character vectors where each element represents a players's zimo. |
dapai |
A list of character vectors where each element represents a players's dapai. |
A paistr
vector.
if (requireNamespace("dplyr", quietly = TRUE)) { qipai <- paistr("m123456789p1s1z11") |> tidy() |> dplyr::mutate(tile = as.character(tile)) |> lineup() zimo <- list(c("p2", "p3")) dapai <- list(c("z1", "z1")) proceed(qipai, zimo, dapai) }
if (requireNamespace("dplyr", quietly = TRUE)) { qipai <- paistr("m123456789p1s1z11") |> tidy() |> dplyr::mutate(tile = as.character(tile)) |> lineup() zimo <- list(c("p2", "p3")) dapai <- list(c("z1", "z1")) proceed(qipai, zimo, dapai) }
Create a function to randomly generate hands
rand_hands( hupai = c("pinghe", "zhuangfeng", "menfeng", "fanpai", "duanyaojiu", "yibeikou", "sansetongshun", "yiqitongguan", "hunquandaiyaojiu", "qiduizi", "duiduihu", "sananke", "sangangzi", "sansetongke", "hunlaotou", "xiaosanyuan", "hunyise", "chunquandaiyaojiu", "erbeikou", "qingyise", "guoshiwushuang", "sianke", "dasanyuan", "xiaosixi", "dasixi", "ziyise", "lvyise", "qinglaotou", "sigangzi", "jiulianbaodeng"), zhuangfeng = c("z1", "z2", "z3", "z4"), menfeng = c("z2", "z3", "z4", "z1"), rule = default_rule(), seed = sample.int(10000, 1) )
rand_hands( hupai = c("pinghe", "zhuangfeng", "menfeng", "fanpai", "duanyaojiu", "yibeikou", "sansetongshun", "yiqitongguan", "hunquandaiyaojiu", "qiduizi", "duiduihu", "sananke", "sangangzi", "sansetongke", "hunlaotou", "xiaosanyuan", "hunyise", "chunquandaiyaojiu", "erbeikou", "qingyise", "guoshiwushuang", "sianke", "dasanyuan", "xiaosixi", "dasixi", "ziyise", "lvyise", "qinglaotou", "sigangzi", "jiulianbaodeng"), zhuangfeng = c("z1", "z2", "z3", "z4"), menfeng = c("z2", "z3", "z4", "z1"), rule = default_rule(), seed = sample.int(10000, 1) )
hupai |
A string scalar. |
zhuangfeng |
A string scalar; "ba-kaze" tile. |
menfeng |
A string scalar; "ji-kaze" tile. |
rule |
A list; a rule set. Defaults to |
seed |
An integer scalar. Used for initializing the random number generator. |
A function inheriting class purrr_function_partial
that takes single arguments n
: number of hands to be generated.
Generate hands at random
rand_qipai(nrow, rule = default_rule(), seed = sample.int(10000, 1))
rand_qipai(nrow, rule = default_rule(), seed = sample.int(10000, 1))
nrow |
An integer scalar. |
rule |
A list; a rule set. Defaults to |
seed |
An integer scalar. Used for initializing the random number generator. |
A tibble that consists of 4 columns of paistr
vector.
rand_qipai(4)
rand_qipai(4)
Sample data of mahjong game log
ranked_game
ranked_game
An object of class list
of length 3.
A named list that contains sample data of mahjong game log.
The data was coverted from 'tenhou.net/6' format
using read_tenhou6()
function
in convlog package.
https://github.com/Equim-chan/mjai-reviewer/blob/master/convlog/tests/testdata/ranked_game.json
calc_xiangting()
Test cases for calc_xiangting()
testdat_xiangting
testdat_xiangting
An object of class tbl_df
(inherits from tbl
, data.frame
) with 40000 rows and 4 columns.
A tibble that contains test cases for calc_xiangting()
.
Contents may vary depending on package version.
https://github.com/TadaoYamaoka/cmajiang/tree/main/tests_cpp/data
Lookup table for tiles conversion
tiles
tiles
An object of class tbl_df
(inherits from tbl
, data.frame
) with 38 rows and 4 columns.
A tibble that contains the lookup table for tiles conversion.
Translate tiles from one format to another
trans_tile( x, from = c("mjai", "tenhou_int", "id", "cmajiang"), to = c("cmajiang", "mjai", "tenhou_int", "id") )
trans_tile( x, from = c("mjai", "tenhou_int", "id", "cmajiang"), to = c("cmajiang", "mjai", "tenhou_int", "id") )
x |
A vector of tiles to be translated. |
from |
A string scalar. Either "mjai", "tenhou_int", "id", or "cmajiang". |
to |
A string scalar. Either "cmajiang", "mjai", "tenhou_int", or "id". |
For to = "cmajiang"
and to = "mjai"
: A character vector.
For to = "tenhou_int"
: An integer vector.
trans_tile(c("m0", "p1", "z1", "_"), from = "cmajiang", to = "mjai") trans_tile(c("5mr", "1p", "E", "?"), from = "mjai", to = "cmajiang") trans_tile(c(51, 21, 41, 0), from = "tenhou_int", to = "mjai")
trans_tile(c("m0", "p1", "z1", "_"), from = "cmajiang", to = "mjai") trans_tile(c("5mr", "1p", "E", "?"), from = "mjai", to = "cmajiang") trans_tile(c(51, 21, 41, 0), from = "tenhou_int", to = "mjai")