2015-11-29 33 views
1
# rm(list=ls()) 
Ace <- c(rep("Waterfall", 4)) 
Two <- c(rep("For You",4)) 
Three <- c(rep("For Me",4)) 
Four <- c(rep("On The Floor",4)) 
Five <- c(rep("For Guys",4)) 
Six <- c(rep("For Chicks",4)) 
Seven <- c(rep("Heaven",4)) 
Eight <- c(rep("Pick A Mate",4)) 
Nine <- c(rep("Make A Rime",4)) 
Ten <- c("Cars","Bands","Books","Capital Cities") 
Jack <- c(rep("Make A Rule",4)) 
Queen <- c(rep("You're The Quizmaster",4)) 
King <- c(rep("Fill Up The Cup",4)) 
deck <- c(Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King) 

shuffle <- function(deck) {return(sample(deck,length(deck)))} 


readinteger <- function(){ 
    n<- readline("Continuing?") 
} 

repeat 
{ 
    response <- as.character(readinteger()) 
    if(response=="no") 
    { 
    print("Game Over"); 
    break 
    } else { 
    sample(deck,1,replace=TRUE) 
    } 
} 

我們模擬紙牌遊戲kingscup一個隨機變量,我們必須適應在其他功能的樣品有問題,因爲它沒有給我們一個隨機卡,因爲我們希望。創建IF ... ELSE功能

+5

我已經評論了危險的'rm'。 –

+0

你想用'sample(deck,1,replace = TRUE)'來做什麼?(現在你不會把它保存在一個對象中)。如果你只想要一個隨機的話,爲什麼你要設置'replace = TRUE'? – jogo

+0

我們認爲如果我們設置replace = True,那麼該變量將被替換。 我們如何得到一個隨機? –

回答

0

你需要插入一個print語句實際上得到的輸出:

repeat 
{ 
    response <- as.character(readinteger()) 
    if(response=="no") 
    { 
    print("Game Over"); 
    break 
    } else { 
    out = sample(deck,1,replace=TRUE) 
    print(out) 
    } 
} 

在這裏你去:

Continuing? 
[1] "Fill Up The Cup" 
Continuing? 
[1] "For Chicks" 
Continuing? 
[1] "Pick A Mate" 
Continuing? 
[1] "On The Floor" 
Continuing?no 
[1] "Game Over" 

享受!