※ 引述《cano7127 (頭兒)》之銘言:
: 程式諮詢
: (做21點的遊戲)
: [軟體熟悉度]:
: 新手
: [問題敘述]:
: 電腦對電腦玩,目前卡在
: sample(52)
: cards<-sample(52)
: num<-cards%%13
: num
: (52\13的餘數分配數字)
: 要怎麼分配花色跟數字
: 其實可以說根本還沒開始
: 有人可以指點方向嗎?
: 謝謝
get_num = function(cards){
factor(cards %% 13, levels = 0:12, labels = c(1:10, "J", "Q", "K"))
}
get_suit = function(cards){
factor((cards-1) %/% 13, levels = 0:3,
labels = c("spade", "heart", "diamend","club"))
}
desk = sample(1:52)
n_player = 2
num_cards_out = 0
player_cards = vector('list', n_player)
for (i in 1:n_player)
{
player_cards[[i]] = data.frame(
suit = get_suit(desk[(num_cards_out+1):(num_cards_out+2)]),
num = get_num(desk[(num_cards_out+1):(num_cards_out+2)])
)
num_cards_out = num_cards_out + 2
}
calculate_points = function(cards){
## 計算點數
}
for (i in 1:n_player)
{
while (calculate_points(player_cards[[i]]) < 21 && somerules)
{
# 發牌給玩家
}
}
# 最後比大小...