Deal cards for FreeCell: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
|||
Line 2,962: | Line 2,962: | ||
JD 7D 8H 9C 2H QD 4C 5H |
JD 7D 8H 9C 2H QD 4C 5H |
||
KC 8D 2S 3S</pre> |
KC 8D 2S 3S</pre> |
||
=={{header|R}}== |
|||
<lang R>## Linear congruential generator code not original - |
|||
## copied from |
|||
## http://www.rosettacode.org/wiki/Linear_congruential_generator#R |
|||
## altered to allow seed as an argument |
|||
library(gmp) # for big integers |
|||
rand_MS <- function(n = 1, seed = 1) { |
|||
a <- as.bigz(214013) |
|||
c <- as.bigz(2531011) |
|||
m <- as.bigz(2^31) |
|||
x <- rep(as.bigz(0), n) |
|||
x[1] <- (a * as.bigz(seed) + c) %% m |
|||
i <- 1 |
|||
while (i < n) { |
|||
x[i+1] <- (a * x[i] + c) %% m |
|||
i <- i + 1 |
|||
} |
|||
as.integer(x / 2^16) |
|||
} |
|||
## ============================= |
|||
## New code follows: |
|||
## ============================= |
|||
dealFreeCell <- function(seedNum) { |
|||
deck <- paste(rep(c("A",2,3,4,5,6,7,8,9,10,"J","Q","K"), each = 4), c("C","D","H","S"), sep = "") |
|||
cards = rand_MS(52,seedNum) |
|||
for (i in 52:1) { |
|||
cardToPick <- (cards[53-i]%% i)+1 # R indexes from 1, not 0 |
|||
deck[c(cardToPick,i)] <- deck[c(i, cardToPick)] |
|||
} |
|||
deck <- rev(deck) # flip the deck to deal |
|||
deal = matrix(c(deck,NA,NA,NA,NA),ncol = 8, byrow = TRUE) |
|||
# using a matrix for simple printing, but requires filling with NA |
|||
# if implementing as a game, a list for each pile would make more sense |
|||
print(paste("Hand numer:",seedNum), quote = FALSE) |
|||
print(deal, quote = FALSE, na.print = "") |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
> dealFreeCell(1) |
|||
[1] Hand numer: 1 |
|||
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] |
|||
[1,] JD 2D 9H JC 5D 7H 7C 5H |
|||
[2,] KD KC 9S 5S AD QC KH 3H |
|||
[3,] 2S KS 9D QD JS AS AH 3C |
|||
[4,] 4C 5C 10S QH 4H AC 4D 7S |
|||
[5,] 3S 10D 4S 10H 8H 2C JH 7D |
|||
[6,] 6D 8S 8D QS 6C 3D 8C 10C |
|||
[7,] 6S 9C 2H 6H |
|||
> dealFreeCell(617) |
|||
[1] Hand numer: 617 |
|||
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] |
|||
[1,] 7D AD 5C 3S 5S 8C 2D AH |
|||
[2,] 10D 7S QD AC 6D 8H AS KH |
|||
[3,] 10H QC 3H 9D 6S 8D 3D 10C |
|||
[4,] KD 5H 9S 3C 8S 7H 4D JS |
|||
[5,] 4C QS 9C 9H 7C 6H 2C 2S |
|||
[6,] 4S 10S 2H 5D JC 6C JH QH |
|||
[7,] JD KS KC 4H |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |