Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,33 @@
|
|||
shared void freeCellDeal() {
|
||||
|
||||
//a function that returns a random number generating function
|
||||
function createRNG(variable Integer state) =>
|
||||
() => (state = (214_013 * state + 2_531_011) % 2^31) / 2^16;
|
||||
|
||||
void deal(Integer num) {
|
||||
// create an array with a list comprehension
|
||||
variable value deck = Array {
|
||||
for(rank in "A23456789TJQK")
|
||||
for(suit in "CDHS")
|
||||
"``rank````suit``"
|
||||
};
|
||||
value rng = createRNG(num);
|
||||
for(i in 1..52) {
|
||||
value index = rng() % deck.size;
|
||||
assert(exists lastIndex = deck.lastIndex);
|
||||
//swap the random card with the last one
|
||||
deck.swap(index, lastIndex);
|
||||
//print the last one
|
||||
process.write("``deck.last else "missing card"`` " );
|
||||
if(i % 8 == 0) {
|
||||
print("");
|
||||
}
|
||||
//and shrink the array to remove the last card
|
||||
deck = deck[...lastIndex - 1];
|
||||
}
|
||||
}
|
||||
|
||||
deal(1);
|
||||
print("\n");
|
||||
deal(617);
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
PROGRAM FREECELL
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
DIM CARDS%[52]
|
||||
|
||||
PROCEDURE XRANDOM(SEED->XRND)
|
||||
POW31=2^31
|
||||
POW16=2^16
|
||||
SEED=SEED*214013+2531011
|
||||
SEED=SEED-POW31*INT(SEED/POW31)
|
||||
XRND=INT(SEED/POW16)
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE DEAL(CARDS%[],GAME_NUM)
|
||||
LOCAL I%,J%,S%
|
||||
SEED=GAME_NUM
|
||||
FOR I%=1 TO 52 DO
|
||||
CARDS%[I%]=52-I%
|
||||
END FOR
|
||||
FOR I%=1 TO 51 DO
|
||||
XRANDOM(SEED->XRND)
|
||||
J%=52-XRND MOD (53-I%)
|
||||
S%=CARDS%[I%]
|
||||
CARDS%[I%]=CARDS%[J%]
|
||||
CARDS%[J%]=S%
|
||||
END FOR
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE SHOW(CARDS%[])
|
||||
LOCAL INDEX%
|
||||
FOR INDEX%=1 TO 52 DO
|
||||
PRINT(MID$(SUITS$,CARDS%[INDEX%] MOD 4+1,1);MID$(NUMS$,CARDS%[INDEX%] DIV 4+1,1);" ";)
|
||||
IF INDEX% MOD 8=0 OR INDEX%=52 THEN
|
||||
PRINT
|
||||
END IF
|
||||
END FOR
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
PRINT(CHR$(12);)
|
||||
SUITS$="♣♦♥♠"
|
||||
NUMS$="A23456789TJQK"
|
||||
GAME_NUM=1982 ! if missing command line
|
||||
IF CMDLINE$<>"" THEN GAME_NUM=VAL(CMDLINE$) END IF
|
||||
SEED=1
|
||||
DEAL(CARDS%[],GAME_NUM)
|
||||
PRINT("Hand ";GAME_NUM)
|
||||
SHOW(CARDS%[])
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
' version 04-11-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
' to seed ms_lcg(seed > -1)
|
||||
' to get random number ms_lcg(-1) or ms_lcg() or just ms_lcg
|
||||
Function ms_lcg(seed As Integer = -1) As UInteger
|
||||
|
||||
Static As UInteger ms_state
|
||||
|
||||
If seed <> -1 Then
|
||||
ms_state = seed Mod 2 ^ 31
|
||||
Else
|
||||
ms_state = (214013 * ms_state + 2531011) Mod 2 ^ 31
|
||||
End If
|
||||
|
||||
Return ms_state Shr 16
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As UByte card(51)
|
||||
Dim As String suit = "CDHS", value = "A23456789TJQK"
|
||||
Dim As Long i, c, s, v, game = 1
|
||||
Dim As ULong game_nr(1 To 2) = { 1, 617}
|
||||
|
||||
Do
|
||||
|
||||
ms_lcg(game_nr(game)) ' seed generator
|
||||
Print "game #"; game_nr(game)
|
||||
game = game + 1
|
||||
|
||||
For i = 0 To 51 ' set up the cards
|
||||
card(i) = i
|
||||
Next
|
||||
|
||||
For i = 51 To 0 Step -1 ' shuffle
|
||||
c = ms_lcg Mod (i +1)
|
||||
Swap card(i), card(c)
|
||||
Next
|
||||
|
||||
c = 0
|
||||
Do
|
||||
For i = 0 To 7
|
||||
s = card(51 - c) Mod 4
|
||||
v = card(51 - c) \ 4
|
||||
Print Chr(value[v]); Chr(suit[s]); " ";
|
||||
c = c +1
|
||||
If c > 51 Then Exit Do
|
||||
Next
|
||||
Print
|
||||
Loop
|
||||
Print : Print
|
||||
|
||||
Loop Until game > UBound(game_nr)
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
29
Task/Deal-cards-for-FreeCell/Nim/deal-cards-for-freecell.nim
Normal file
29
Task/Deal-cards-for-FreeCell/Nim/deal-cards-for-freecell.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import sequtils, strutils, os
|
||||
|
||||
proc randomGenerator(seed: int): iterator: int =
|
||||
var seed = seed
|
||||
return iterator: int =
|
||||
while true:
|
||||
seed = (seed.int64 * 214013 + 2531011) and int32.high
|
||||
yield seed shr 16
|
||||
|
||||
proc deal(seed): seq[int] =
|
||||
const nc = 52
|
||||
result = toSeq countdown(nc - 1, 0)
|
||||
var rnd = randomGenerator seed
|
||||
for i in 0 .. <nc:
|
||||
let r = rnd()
|
||||
let j = (nc - 1) - r mod (nc - i)
|
||||
swap result[i], result[j]
|
||||
|
||||
proc show(cards) =
|
||||
var l = newSeq[string]()
|
||||
for c in cards:
|
||||
l.add "A23456789TJQK"[c div 4] & "CDHS"[c mod 4]
|
||||
for i in countup(0, cards.high, 8):
|
||||
echo " ", l[i..min(i+7, l.high)].join(" ")
|
||||
|
||||
let seed = if paramCount() == 1: paramStr(1).parseInt else: 11982
|
||||
echo "Hand ", seed
|
||||
let deck = deal seed
|
||||
show deck
|
||||
Loading…
Add table
Add a link
Reference in a new issue