RKF
RKF

Reputation: 141

Subtract one list from another

I'm trying to 'simulate' a poker game. First I've created a deck with 52 cards as a data frame. Therefore I used three columns: Cardvalue, Cardcolor and Rank (1-52).

Now two players draw each five cards - and the player with the highest card (rank) wins the game. So I wrote a function called 'poker' and defined two players, every player draws 5 cards from the deck.

The problem is, that player 2 can also draw the 7 diamonds if player 1 got it already. My idea was to subtract the results from player one from the deck so that only the remaining cards could be drawn - but every time I have got an error, the type list is not valid. I hope that you can help me! Thank you so much!

poker <- function() {deckmat3 player1 <- deckmat3[sample(nrow(deckmat3),5),] player2 <- deckmat3[sample(nrow(deckmat3),5),] return(ifelse(max(player1$rank) > max(player2$rank),"player1","player2"))}

my idea was:

poker <- function() {poker <- function() {deckmat3 player1 <- deckmat3[sample(nrow(deckmat3),5),] player2 <- deckmat3[deckmat3$player1][sample(nrow(deckmat3),5),] return(ifelse(max(player1$rank) > max(player2$rank),"player1","player2"))} 

But it doesn't work :/

Edit.:

I hope, I can provide useful data:

  1. Vector for cardvalues cv:

    cv <-rep(2,4),rep(3,4),rep(4,4),rep(5,4),rep(6,4),rep(7,4),rep(8,4),rep(9,4),rep(10,4),rep(11,4),rep(12,4),rep(13,4),rep(14,4))

  2. Dataframe deckmat3:

    deckmat3 <- data.frame(cardvalue=c(cv),cardcolor=c("Diamond", "Club", "Heart", "Spade"),rank=1:52)

Then I add the data to the function 'poker' I mentioned above. The results for player 1 and for player 2 are two sublists from the original data frame 'deckmat3' with 5 rows. The problem is, that player 2 can draw cards which have been already drawn by player 1. This means, that the sublist 'player 1' has to be subtracted from 'deckmat3' ... and this is the problem. I can't remove a list from a list :(

Upvotes: 0

Views: 418

Answers (1)

Thomas Wire
Thomas Wire

Reputation: 282

I'd try it a different way. You'd just need to fix the cardRank column (Not sure how you want to determine this).

cardValue <- rep(1:13, 4)
cardSuit <-
c(rep("diamond", 13),
  rep("heart", 13),
  rep("spade", 13),
  rep("club", 13))

cardColor <- c(rep("red", 26), rep("black", 26))

cardRank <- 1:52


winnerList <- c()

simulateGames <- function(iterations) {
for (it in 1:iterations) {
    cards <- data.frame(cardValue, cardSuit, cardColor, cardRank)

    player1Cards <- cards[sample(nrow(cards), 5),]

    cards <-
        cards[!rownames(cards) %in% rownames(player1Cards),]

    player2Cards <- cards[sample(nrow(cards), 5),]

    winnerList <-
        c(winnerList, ifelse(
            max(player1Cards$cardRank) > max(player2Cards$cardRank),
            1,
            2
        ))

}
winnerList
}

simulateGames(1000)

Upvotes: 0

Related Questions