Pequenas funções R inúteis e úteis – Jogando pilhas de cartas

cupom com desconto - o melhor site de cupom de desconto cupomcomdesconto.com.br

[ad_1]

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (Você pode relatar problemas sobre o conteúdo desta página aqui)


Quer compartilhar seu conteúdo em R-bloggers? clique aqui se você tiver um blog, ou aqui se não tiver.

Esta função inútil é um jogo de cartas jogado sozinho. Ou você pode chamá-lo de vetor ou números.

Criação de uma função que irá jogar dois jogadores, ambos dados um conjunto de cartas (representadas por números).

As regras básicas são simples:
– ambos os jogadores recebem o mesmo número de cartas
– o vencedor do jogo é o jogador que pega todas as cartas
– ambos os jogadores tiram uma carta de cada vez do topo da pilha
– se ambos os jogadores puxarem a mesma carta, ambos devolvem a carta do fundo da pilha
– o jogador tira o empate se o número for maior do que os adversários
– as cartas são colocadas na pilha na seguinte ordem: a primeira carta é a do vencedor (carta própria) e a segunda é a carta do oponente

Crie o desenhar() função. Esta função fará a comparação de duas cartas e empilhará uma ou duas na parte inferior da pilha:

draw  n2) {
    val 


In addition, we need also the play() function that will look for the winner and stop the game:

play 


So if I create a stack of 6 cards per player and start the function:

set.seed(2908)
p1 


I see that it takes 30 draws for player 2 to win.

Pretty useless and boring. What if we make the function more useful by adding some statistics and a graph? This calls for another function playWithStats().

library(ggplot2)

playWithStats 


This will track number of cards each player will have after each draw and see the game development.

I will also let computer choose the cards for each player:

# converges in 229 rounds
set.seed(2908)
p1 


and produces an symmetrical graph for both users (Number of rounds per number of cards in stack for each user).

cupom com desconto - o melhor site de cupom de desconto cupomcomdesconto.com.br

On first glance it looks like the game would finish around round 40 but it keeps moving until round 190 where each player has 10 cards each. Interesting game dynamics. Eventually, game converges in 230 rounds and Player 2 wins.

What happens if we do the sampling of the cards without repetition, so that each card must only appear once in the stack?.

So, this sample(1:10,10,replace=TRUE) would become this: sample(1:10,10,replace=FALSE). I already smell problems.

All these combinations (both players have 3,4,5 or 10 cards in stack each) will never converge.

# never converges 
set.seed(2908)
p1 


Why? Because all cards get sorted in each stack the same way for both players. Let’s put it to the test:

set.seed(2908)
p1 


And will run it with breakpoints, so I can stop and check the stack every run. This seed will produce starting numbers of:

At Start:
P1: 2, 1, 3
P2: 2, 3, 1
1. Run (2,2):
P1: 1, 3, 2
P2: 3, 1, 2
2. Run (1,3):
P1: 3, 2
P2: 1, 2, 3, 1
3. Run(3,1):
P1: 2, 3, 1
P2: 2, 3, 1
All another runs are useless, since the game will not converge and end in a loop.

Same will appear when adding more cards (sample(1:5,5,replace=FALSE) or sample(1:10,10,replace=FALSE)) but essentially keeping the replace=FALSE making a deck of cards with no repetition. One can say, this is the reason, why many card games have 2 or 4 repetition of decks; to enable game to end or to change the ground rules of the game (e.g.: when players draw same card, they should hold to a draw until on wins a stack of draws).

As always, code is available at Github.

Happy R-coding! And stay healthy!



[ad_2]

cupom com desconto - o melhor site de cupom de desconto cupomcomdesconto.com.br
Leia Também  Ford vs RK da Ferrari Revisitado