R中的If、else If、else语句和逻辑运算符以及创建函数
这件事我已经做了两天了,我简直是陷在泥里了!我正在研究在R中使用if、elseif和else语句 我创建了一个函数,可以为两名玩家随机抽取3张卡来模拟一场游戏R中的If、else If、else语句和逻辑运算符以及创建函数,r,dataframe,if-statement,logical-operators,notation,R,Dataframe,If Statement,Logical Operators,Notation,这件事我已经做了两天了,我简直是陷在泥里了!我正在研究在R中使用if、elseif和else语句 我创建了一个函数,可以为两名玩家随机抽取3张卡来模拟一场游戏 face=c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five",
face=c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace")
value=c(13, 12,11,10,9,8,7,6,5,4,3,2,1)
deck <-data.frame(face=rep(face,4),
suit=c(rep("spades", 13), rep("clubs", 13), rep("diamonds", 13), rep("hearts",
13)),
value=rep(value,4))
这是我的if语句
combo.1=c("ace", "ace", "ace")
combo.2=c("heart", "heart", "heart")
if (Sum.Player.A==combo.1){
Sum.Player.A<-Sum.Player.A*2
}else{
Sum.Player.A
}
if(Sum.Player.A==combo.2){
Sum.Player.A<-Sum.Player.A*2
}else{
Sum.Player.A
}
if (Sum.Player.B==combo.1){
Sum.Player.B<-Sum.Player.B*2
}else{
Sum.Player.B
}
if(Sum.Player.B==combo.2){
Sum.Player.B<-Sum.Player.B*2
}else{
Sum.Player.B
}
if(Sum.Player.A==combo.1){
Sum.Player.A我在牌组的创建中添加了stringsAsFactors=FALSE。抽n张随机牌一次抽牌,否则你就有抽相同牌的风险(需要不替换地完成)。最后,winner函数将A手牌和B手牌的和作为参数
face=c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace")
value=c(13, 12,11,10,9,8,7,6,5,4,3,2,1)
deck <-data.frame(face=rep(face,4),
suit=c(rep("spades", 13), rep("clubs", 13), rep("diamonds", 13), rep("hearts",
13)),
value=rep(value,4), stringsAsFactors = FALSE)
get_cards<-function(){
return(draw_n_random_cards(6))
}
draw_n_random_cards=function(n) {
s=sample(1:52, 6, replace=FALSE)
return(deck[s,])
}
winner<-function(A, B){
if(A<B){
"Player B is the winner"
}else if (A>B){
"Player A is the winner"}else {
"tie"}
}
play_game=function() {
cards=draw_n_random_cards()
Player.A=cards[1:3,]
Player.B=cards[4:6,]
Sum.Player.A<-sum(Player.A$value)
Sum.Player.B<-sum(Player.B$value)
combo.1=c("ace", "ace", "ace")
combo.2=c("heart", "heart", "heart")
if (identical(Player.A$face, combo.1) | identical(Player.A$suit, combo.2)) {
Sum.Player.A<-Sum.Player.A*2
}
if (identical(Player.B$face, combo.1) | identical(Player.B$suit, combo.2)) {
Sum.Player.B<-Sum.Player.B*2
}
winner(Sum.Player.A, Sum.Player.B)
}
这很有意义。您创建了一个获取卡片的函数,该函数将返回6张卡片。
if (Sum.Player.A==combo.1){
Sum.Player.A<-Sum.Player.A*2
}else{
Sum.Player.A
}
if(Sum.Player.A==combo.2){
Sum.Player.A<-Sum.Player.A*2
}else{
Sum.Player.A
}
if (Sum.Player.B==combo.1){
Sum.Player.B<-Sum.Player.B*2
}else{
Sum.Player.B
}
if(Sum.Player.B==combo.2){
Sum.Player.B<-Sum.Player.B*2
}else{
Sum.Player.B
}
winner<-function(){
if(Sum.Player.A<Sum.Player.B){
"Player B is the winner"
}else if (Sum.Player.A>Sum.Player.B){
"Player A is the winner"}else {
"tie"}
}
play_game<-function(){
#1 draw 3 random cards for each player
#2 score their cards
#3 compare their score to delcare a winner
}
face=c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace")
value=c(13, 12,11,10,9,8,7,6,5,4,3,2,1)
deck <-data.frame(face=rep(face,4),
suit=c(rep("spades", 13), rep("clubs", 13), rep("diamonds", 13), rep("hearts",
13)),
value=rep(value,4), stringsAsFactors = FALSE)
get_cards<-function(){
return(draw_n_random_cards(6))
}
draw_n_random_cards=function(n) {
s=sample(1:52, 6, replace=FALSE)
return(deck[s,])
}
winner<-function(A, B){
if(A<B){
"Player B is the winner"
}else if (A>B){
"Player A is the winner"}else {
"tie"}
}
play_game=function() {
cards=draw_n_random_cards()
Player.A=cards[1:3,]
Player.B=cards[4:6,]
Sum.Player.A<-sum(Player.A$value)
Sum.Player.B<-sum(Player.B$value)
combo.1=c("ace", "ace", "ace")
combo.2=c("heart", "heart", "heart")
if (identical(Player.A$face, combo.1) | identical(Player.A$suit, combo.2)) {
Sum.Player.A<-Sum.Player.A*2
}
if (identical(Player.B$face, combo.1) | identical(Player.B$suit, combo.2)) {
Sum.Player.B<-Sum.Player.B*2
}
winner(Sum.Player.A, Sum.Player.B)
}
> play_game()
[1] "Player B is the winner"