Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -1,11 +1,23 @@
|
|||
ranks: 1..13
|
||||
suits: ['♣', '♦', '♥', '♠']
|
||||
|
||||
define :card [rank, suit][
|
||||
init: method [rank, suit][
|
||||
ensure ->
|
||||
and? -> contains? ranks rank
|
||||
-> contains? suits suit
|
||||
rank?: $ => [in? & ranks]
|
||||
suit?: $ => [in? & suits]
|
||||
|
||||
interleave: $[
|
||||
xs :block :dictionary :range
|
||||
ys :block :dictionary :range
|
||||
apply :literal
|
||||
][
|
||||
flatten map xs 'x
|
||||
-> map ys 'y
|
||||
-> call apply @[x y]
|
||||
]
|
||||
|
||||
define :Card [
|
||||
init: method [rank :integer suit :char][
|
||||
ensure -> rank? rank
|
||||
ensure -> suit? suit
|
||||
|
||||
this\rank: rank
|
||||
this\suit: suit
|
||||
|
|
@ -23,37 +35,37 @@ define :card [rank, suit][
|
|||
]
|
||||
]
|
||||
|
||||
define :deck [][
|
||||
init: [
|
||||
this\cards: []
|
||||
loop ranks 'rank ->
|
||||
loop suits 'suit ->
|
||||
this\cards: this\cards ++ to :card @[rank, suit]
|
||||
]
|
||||
card: $[rank :integer suit :char][
|
||||
to :Card [rank suit]!
|
||||
]
|
||||
|
||||
shuffleUp: function [this :deck][
|
||||
this\cards: shuffle this\cards
|
||||
]
|
||||
|
||||
deal: function [this :deck, cnt :integer][
|
||||
if cnt > size this\cards ->
|
||||
panic "Not enough cards in deck"
|
||||
|
||||
cards: []
|
||||
do.times: cnt [
|
||||
dealt: sample this\cards
|
||||
'cards ++ dealt
|
||||
this\cards: remove this\cards dealt
|
||||
define :Deck [
|
||||
init: method [][
|
||||
this\cards: interleave ranks suits 'card
|
||||
]
|
||||
|
||||
shuffle: method [][
|
||||
shuffle 'this\cards
|
||||
]
|
||||
|
||||
deal: method [][
|
||||
ensure.that: "Not enough cards in deck"
|
||||
-> not? empty? \cards
|
||||
|
||||
\shuffle
|
||||
pop 'this\cards
|
||||
]
|
||||
|
||||
dealMany: method [times :integer][
|
||||
map 1..times 'x -> \deal
|
||||
]
|
||||
return cards
|
||||
]
|
||||
|
||||
; create a new deck
|
||||
Deck: to :deck []
|
||||
deck: to :Deck []!
|
||||
|
||||
; and shuffle it
|
||||
shuffleUp Deck
|
||||
deck\shuffle
|
||||
|
||||
; deal 5 cards
|
||||
print deal Deck 5
|
||||
print deck\dealMany 5
|
||||
|
|
|
|||
51
Task/Playing-cards/AutoIt/playing-cards.au3
Normal file
51
Task/Playing-cards/AutoIt/playing-cards.au3
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
|
||||
#AutoIt3Wrapper_Change2CUI=y
|
||||
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
|
||||
#include <Array.au3>
|
||||
|
||||
; ## GLOBALS ##
|
||||
Global $SUIT = ["D", "H", "S", "C"]
|
||||
Global $FACE = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
|
||||
Global $DECK[52]
|
||||
|
||||
; ## CREATES A NEW DECK
|
||||
Func NewDeck()
|
||||
|
||||
For $i = 0 To 3
|
||||
For $x = 0 To 12
|
||||
_ArrayPush($DECK, $FACE[$x] & $SUIT[$i])
|
||||
Next
|
||||
Next
|
||||
|
||||
EndFunc ;==>NewDeck
|
||||
|
||||
; ## SHUFFLE DECK
|
||||
Func Shuffle()
|
||||
|
||||
_ArrayShuffle($DECK)
|
||||
|
||||
EndFunc ;==>Shuffle
|
||||
|
||||
; ## DEAL A CARD
|
||||
Func Deal()
|
||||
|
||||
Return _ArrayPop($DECK)
|
||||
|
||||
EndFunc ;==>Deal
|
||||
|
||||
; ## PRINT DECK
|
||||
Func Print()
|
||||
|
||||
ConsoleWrite(_ArrayToString($DECK) & @CRLF)
|
||||
|
||||
EndFunc ;==>Print
|
||||
|
||||
|
||||
#Region ;#### USAGE ####
|
||||
NewDeck()
|
||||
Print()
|
||||
Shuffle()
|
||||
Print()
|
||||
ConsoleWrite("DEALT: " & Deal() & @CRLF)
|
||||
Print()
|
||||
#EndRegion ;#### USAGE ####
|
||||
177
Task/Playing-cards/COBOL/playing-cards.cob
Normal file
177
Task/Playing-cards/COBOL/playing-cards.cob
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
identification division.
|
||||
program-id. playing-cards.
|
||||
|
||||
environment division.
|
||||
configuration section.
|
||||
repository.
|
||||
function all intrinsic.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
77 card usage index.
|
||||
01 deck.
|
||||
05 cards occurs 52 times ascending key slot indexed by card.
|
||||
10 slot pic 99.
|
||||
10 hand pic 99.
|
||||
10 suit pic 9.
|
||||
10 symbol pic x(4).
|
||||
10 rank pic 99.
|
||||
|
||||
01 filler.
|
||||
05 suit-name pic x(8) occurs 4 times.
|
||||
|
||||
*> Unicode U+1F0Ax, Bx, Cx, Dx "f09f82a0" "82b0" "8380" "8390"
|
||||
01 base-s constant as 4036985504.
|
||||
01 base-h constant as 4036985520.
|
||||
01 base-d constant as 4036985728.
|
||||
01 base-c constant as 4036985744.
|
||||
|
||||
01 sym pic x(4) comp-x.
|
||||
01 symx redefines sym pic x(4).
|
||||
77 s pic 9.
|
||||
77 r pic 99.
|
||||
77 c pic 99.
|
||||
77 hit pic 9.
|
||||
77 limiter pic 9(6).
|
||||
|
||||
01 spades constant as 1.
|
||||
01 hearts constant as 2.
|
||||
01 diamonds constant as 3.
|
||||
01 clubs constant as 4.
|
||||
|
||||
01 players constant as 3.
|
||||
01 cards-per constant as 5.
|
||||
01 deal pic 99.
|
||||
01 player pic 99.
|
||||
|
||||
01 show-tally pic zz.
|
||||
01 show-rank pic z(5).
|
||||
01 arg pic 9(10).
|
||||
|
||||
procedure division.
|
||||
cards-main.
|
||||
perform seed
|
||||
perform initialize-deck
|
||||
perform shuffle-deck
|
||||
perform deal-deck
|
||||
perform display-hands
|
||||
goback.
|
||||
|
||||
*> ********
|
||||
seed.
|
||||
accept arg from command-line
|
||||
if arg not equal 0 then
|
||||
move random(arg) to c
|
||||
end-if
|
||||
.
|
||||
|
||||
initialize-deck.
|
||||
move "spades" to suit-name(spades)
|
||||
move "hearts" to suit-name(hearts)
|
||||
move "diamonds" to suit-name(diamonds)
|
||||
move "clubs" to suit-name(clubs)
|
||||
|
||||
perform varying s from 1 by 1 until s > 4
|
||||
after r from 1 by 1 until r > 13
|
||||
compute c = (s - 1) * 13 + r
|
||||
evaluate s
|
||||
when spades compute sym = base-s + r
|
||||
when hearts compute sym = base-h + r
|
||||
when diamonds compute sym = base-d + r
|
||||
when clubs compute sym = base-c + r
|
||||
end-evaluate
|
||||
if r > 11 then compute sym = sym + 1 end-if
|
||||
move s to suit(c)
|
||||
move r to rank(c)
|
||||
move symx to symbol(c)
|
||||
move zero to slot(c)
|
||||
move zero to hand(c)
|
||||
end-perform
|
||||
.
|
||||
|
||||
shuffle-deck.
|
||||
move zero to limiter
|
||||
perform until exit
|
||||
compute c = random() * 52.0 + 1.0
|
||||
move zero to hit
|
||||
perform varying tally from 1 by 1 until tally > 52
|
||||
if slot(tally) equal c then
|
||||
move 1 to hit
|
||||
exit perform
|
||||
end-if
|
||||
if slot(tally) equal 0 then
|
||||
if tally < 52 then move 1 to hit end-if
|
||||
move c to slot(tally)
|
||||
exit perform
|
||||
end-if
|
||||
end-perform
|
||||
if hit equal zero then exit perform end-if
|
||||
if limiter > 999999 then
|
||||
display "too many shuffles, deck invalid" upon syserr
|
||||
exit perform
|
||||
end-if
|
||||
add 1 to limiter
|
||||
end-perform
|
||||
sort cards ascending key slot
|
||||
.
|
||||
|
||||
display-card.
|
||||
>>IF ENGLISH IS DEFINED
|
||||
move rank(tally) to show-rank
|
||||
evaluate rank(tally)
|
||||
when 1 display " ace" with no advancing
|
||||
when 2 thru 10 display show-rank with no advancing
|
||||
when 11 display " jack" with no advancing
|
||||
when 12 display "queen" with no advancing
|
||||
when 13 display " king" with no advancing
|
||||
end-evaluate
|
||||
display " of " suit-name(suit(tally)) with no advancing
|
||||
>>ELSE
|
||||
display symbol(tally) with no advancing
|
||||
>>END-IF
|
||||
.
|
||||
|
||||
display-deck.
|
||||
perform varying tally from 1 by 1 until tally > 52
|
||||
move tally to show-tally
|
||||
display "Card: " show-tally
|
||||
" currently in hand " hand(tally)
|
||||
" is " with no advancing
|
||||
perform display-card
|
||||
display space
|
||||
end-perform
|
||||
.
|
||||
|
||||
display-hands.
|
||||
perform varying player from 1 by 1 until player > players
|
||||
move player to tally
|
||||
display "Player " player ": " with no advancing
|
||||
perform varying deal from 1 by 1 until deal > cards-per
|
||||
perform display-card
|
||||
add players to tally
|
||||
end-perform
|
||||
display space
|
||||
end-perform
|
||||
display "Stock: " with no advancing
|
||||
subtract players from tally
|
||||
add 1 to tally
|
||||
perform varying tally from tally by 1 until tally > 52
|
||||
perform display-card
|
||||
>>IF ENGLISH IS DEFINED
|
||||
display space
|
||||
>>END-IF
|
||||
end-perform
|
||||
display space
|
||||
.
|
||||
|
||||
deal-deck.
|
||||
display "Dealing " cards-per " cards to " players " players"
|
||||
move 1 to tally
|
||||
perform varying deal from 1 by 1 until deal > cards-per
|
||||
after player from 1 by 1 until player > players
|
||||
move player to hand(tally)
|
||||
add 1 to tally
|
||||
end-perform
|
||||
.
|
||||
|
||||
end program playing-cards.
|
||||
|
|
@ -17,7 +17,7 @@ struct Card {
|
|||
immutable pipOrder = (!rankAceTop) ?
|
||||
pip :
|
||||
(pip ? pip - 1 : 12);
|
||||
return pipOrder * suits.length + suit;
|
||||
return pipOrder * cast(int)suits.length + suit;
|
||||
}
|
||||
|
||||
bool opEqual(in Card rhs) const pure nothrow {
|
||||
|
|
@ -61,7 +61,7 @@ final class Deck {
|
|||
}
|
||||
|
||||
Deck dealTop(Deck toDeck = null) pure nothrow {
|
||||
return deal(length - 1, toDeck);
|
||||
return deal(cast(int)length - 1, toDeck);
|
||||
}
|
||||
|
||||
Card opIndex(in int loc) const pure nothrow {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ proc show .
|
|||
.
|
||||
proc shuffle .
|
||||
for i = 52 downto 2
|
||||
r = random i
|
||||
r = random 1 i
|
||||
swap deck[i] deck[r]
|
||||
.
|
||||
top = 52
|
||||
|
|
|
|||
|
|
@ -1,21 +1,22 @@
|
|||
type DeckDesign{T<:Integer,U<:String}
|
||||
using Random
|
||||
|
||||
struct DeckDesign{T<:Integer,U<:String}
|
||||
rlen::T
|
||||
slen::T
|
||||
ranks::Array{U,1}
|
||||
suits::Array{U,1}
|
||||
ranks::Vector{U}
|
||||
suits::Vector{U}
|
||||
hlen::T
|
||||
end
|
||||
|
||||
type Deck{T<:Integer}
|
||||
cards::Array{T,1}
|
||||
mutable struct Deck{T<:Integer}
|
||||
cards::Vector{T}
|
||||
design::DeckDesign
|
||||
end
|
||||
|
||||
Deck(n::Integer, des::DeckDesign) = Deck([n], des)
|
||||
|
||||
function pokerlayout()
|
||||
r = [map(string, 2:10), "J", "Q", "K", "A"]
|
||||
r = map(utf8, r)
|
||||
r = append!(map(string, 2:10), ["J", "Q", "K", "A"])
|
||||
s = ["\u2663", "\u2666", "\u2665", "\u2660"]
|
||||
DeckDesign(13, 4, r, s, 5)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
Base.isempty(d::Deck) = isempty(d.cards)
|
||||
Base.empty!(d::Deck) = empty!(d.cards)
|
||||
Base.length(d::Deck) = length(d.cards)
|
||||
Base.endof(d::Deck) = endof(d.cards)
|
||||
Base.shuffle!(d::Deck) = shuffle!(d.cards)
|
||||
shuffle!(d::Deck) = Random.shuffle!(d.cards)
|
||||
Base.sort!(d::Deck) = sort!(d.cards)
|
||||
Base.getindex(d::Deck, r) = Deck(getindex(d.cards, r), d.design)
|
||||
Base.size(d::Deck) = (d.design.rlen, d.design.slen)
|
||||
function Base.print(d::Deck)
|
||||
sz = size(d)
|
||||
r = map(x->d.design.ranks[ind2sub(sz, x)[1]], d.cards)
|
||||
s = map(x->d.design.suits[ind2sub(sz, x)[2]], d.cards)
|
||||
join(r.*s, " ")
|
||||
function Base.show(io::IO, d::Deck)
|
||||
r = d.design.ranks
|
||||
s = d.design.suits
|
||||
for c in d.cards
|
||||
rix = mod1(c, d.design.rlen)
|
||||
six = div(c-1, d.design.rlen) + 1
|
||||
print(io, r[rix], s[six], " ")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
function deal!{T<:Integer}(d::Deck, hlen::T)
|
||||
function deal!(d::Deck, hlen::T) where T<:Integer
|
||||
if hlen < length(d)
|
||||
hand = Deck(d.cards[1:hlen], d.design)
|
||||
d.cards = d.cards[hlen+1:end]
|
||||
|
|
@ -19,7 +19,7 @@ function pretty(d::Deck)
|
|||
dlen = length(d)
|
||||
for i in 1:llen:dlen
|
||||
j = min(i+llen-1, dlen)
|
||||
s *= print(d[i:j])*"\n"
|
||||
s *= string(d[i:j]) * "\n"
|
||||
end
|
||||
chop(s)
|
||||
strip(s)
|
||||
end
|
||||
|
|
|
|||
480
Task/Playing-cards/PowerShell/playing-cards.ps1
Normal file
480
Task/Playing-cards/PowerShell/playing-cards.ps1
Normal file
|
|
@ -0,0 +1,480 @@
|
|||
<#
|
||||
.Synopsis
|
||||
Creates a new "Deck" which is a hashtable converted to a dictionary
|
||||
This is only used for creating the deck, to view/list the deck contents use Get-Deck.
|
||||
.DESCRIPTION
|
||||
Casts the string value that the user inputs to the name of the variable that holds the "deck"
|
||||
Creates a global variable, allowing you to use the name you choose in other functions and allows you to create
|
||||
multiple decks under different names.
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Deck
|
||||
cmdlet New-Deck at command pipeline position 1
|
||||
Supply values for the following parameters:
|
||||
YourDeckName: Deck1
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Deck -YourDeckName Deck2
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Deck -YourDeckName Deck2
|
||||
|
||||
PS C:\WINDOWS\system32> New-Deck -YourDeckName Deck3
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#>
|
||||
function New-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# Name your Deck, this will be the name of the variable that holds your deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$False,
|
||||
Position=0)]$YourDeckName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$Suit = @(' of Hearts', ' of Spades', ' of Diamonds', ' of Clubs')
|
||||
$Pip = @('Ace', 'King', 'Queen', 'Jack', '10', '9', '8', '7', '6', '5', '4', '3', '2')
|
||||
|
||||
#Creates the hash table that will hold the suit and pip variables
|
||||
$Deck = @{}
|
||||
|
||||
#creates counters for the loop below to make 52 total cards with 13 cards per suit
|
||||
[int]$SuitCounter = 0
|
||||
[int]$PipCounter = 0
|
||||
[int]$CardValue = 0
|
||||
|
||||
}
|
||||
Process
|
||||
{
|
||||
#Creates the initial deck
|
||||
do
|
||||
{
|
||||
#card2add is the rows in the hashtable
|
||||
$Card2Add = ($Pip[$PipCounter]+$Suit[$SuitCounter])
|
||||
|
||||
#Addes the row to the hashtable
|
||||
$Deck.Add($CardValue, $Card2Add)
|
||||
|
||||
#Used to create the Keys
|
||||
$CardValue++
|
||||
|
||||
#Counts the amount of cards per suit
|
||||
$PipCounter ++
|
||||
if ($PipCounter -eq 13)
|
||||
{
|
||||
#once reached the suit is changed
|
||||
$SuitCounter++
|
||||
#and the per-suit counter is reset
|
||||
$PipCounter = 0
|
||||
}
|
||||
else
|
||||
{
|
||||
continue
|
||||
}
|
||||
}
|
||||
#52 cards in a deck
|
||||
until ($Deck.count -eq 52)
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
#sets the name of a variable that is unknown
|
||||
#Then converts the hash table to a dictionary and pipes it to the Get-Random cmdlet with the arguments to randomize the contents
|
||||
Set-Variable -Name "$YourDeckName" -Value ($Deck.GetEnumerator() | Get-Random -Count ([int]::MaxValue)) -Scope Global
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Lists the cards in your selected deck
|
||||
.DESCRIPTION
|
||||
Contains a Try-Catch-Finally block in case the deck requested has not been created
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Get-Deck -YourDeckName deck1
|
||||
|
||||
8 of Clubs
|
||||
5 of Hearts
|
||||
--Shortened--
|
||||
King of Clubs
|
||||
Jack of Diamonds
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Get-Deck -YourDeckName deck2
|
||||
|
||||
deck2 does not exist...
|
||||
Creating Deck deck2...
|
||||
|
||||
Ace of Spades
|
||||
10 of Hearts
|
||||
--Shortened--
|
||||
5 of Clubs
|
||||
4 of Clubs
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> deck deck2
|
||||
|
||||
Ace of Spades
|
||||
10 of Hearts
|
||||
--Shortened--
|
||||
4 of Spades
|
||||
6 of Spades
|
||||
Queen of Spades
|
||||
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#>
|
||||
function Get-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Deck')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
#Brings the Vairiable in from Get-Deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourDeckName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
|
||||
}
|
||||
Process
|
||||
{
|
||||
# Will return a terminal error if the deck has not been created
|
||||
try
|
||||
{
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Host
|
||||
Write-Host "$YourDeckName does not exist..."
|
||||
Write-Host "Creating Deck $YourDeckName..."
|
||||
New-Deck -YourDeckName $YourDeckName
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
$temp | select Value | ft -HideTableHeaders
|
||||
}
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
Write-Verbose "End of show-deck function"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Shuffles a deck of your selection with Get-Random
|
||||
.DESCRIPTION
|
||||
This function can be used to Shuffle any deck that has been created.
|
||||
This can be used on a deck that has less than 52 cards
|
||||
Contains a Try-Catch-Finally block in case the deck requested has not been created
|
||||
Does NOT output the value of the deck being shuffled (You wouldn't look at the cards you shuffled, would you?)
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Shuffle-Deck -YourDeckName Deck1
|
||||
Your Deck was shuffled
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Shuffle NotMadeYet
|
||||
|
||||
NotMadeYet does not exist...
|
||||
Creating and shuffling NotMadeYet...
|
||||
Your Deck was shuffled
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#>
|
||||
function Shuffle-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Shuffle')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
#The Deck you want to shuffle
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourDeckName)
|
||||
|
||||
Begin
|
||||
{
|
||||
Write-Verbose 'Shuffles your deck with Get-Random'
|
||||
}
|
||||
Process
|
||||
{
|
||||
# Will return a missing variable error if the deck has net been created
|
||||
try
|
||||
{
|
||||
#These two commands could be on one line using the pipeline, but look cleaner on two
|
||||
$temp1 = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction stop
|
||||
$temp1 = $temp1 | Get-Random -Count ([int]::MaxValue)
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Host
|
||||
Write-Host "$YourDeckName does not exist..."
|
||||
Write-Host "Creating and shuffling $YourDeckName..."
|
||||
New-Deck -YourDeckName $YourDeckName
|
||||
$temp1 = Get-Variable -name "$YourDeckName" -ValueOnly
|
||||
$temp1 = $temp1 | Get-Random -Count ([int]::MaxValue)
|
||||
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
#Gets the actual value of variable $YourDeckName from the New-Deck function and uses that string value
|
||||
#to set the variables name
|
||||
Set-Variable -Name "$YourDeckName" -value ($temp1) -Scope Global
|
||||
}
|
||||
}
|
||||
End
|
||||
{
|
||||
Write-Host "Your Deck was shuffled"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Creates a new "Hand" which is a hashtable converted to a dictionary
|
||||
This is only used for creating the hand, to view/list the deck contents use Get-Hand.
|
||||
.DESCRIPTION
|
||||
Casts the string value that the user inputs to the name of the variable that holds the "hand"
|
||||
Creates a global variable, allowing you to use the name you choose in other functions and allows you to create
|
||||
multiple hands under different names.
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Hand -YourHandName JohnDoe
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> New-Hand JaneDoe
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
>
|
||||
#>
|
||||
function New-Hand
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# Name your Deck, this will be the name of the variable that holds your deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$False,
|
||||
Position=0)]$YourHandName
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
$Hand = @{}
|
||||
}
|
||||
Process
|
||||
{
|
||||
}
|
||||
|
||||
End
|
||||
{
|
||||
Set-Variable -Name "$YourHandName" -Value ($Hand.GetEnumerator()) -Scope Global
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Lists the cards in the selected Hand
|
||||
.DESCRIPTION
|
||||
Contains a Try-Catch-Finally block in case the hand requested has not been created
|
||||
.EXAMPLE
|
||||
#create a new hand
|
||||
PS C:\WINDOWS\system32> New-Hand -YourHandName Hand1
|
||||
|
||||
PS C:\WINDOWS\system32> Get-Hand -YourHandName Hand1
|
||||
Hand1's hand contains cards, they are...
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
#This hand is empty
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> Get-Hand -YourHandName Hand2
|
||||
|
||||
Hand2 does not exist...
|
||||
Creating Hand Hand2...
|
||||
Hand2's hand contains cards, they are...
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> hand hand3
|
||||
hand3's hand contains 4 cards, they are...
|
||||
|
||||
5 of Spades
|
||||
4 of Spades
|
||||
6 of Spades
|
||||
Queen of Diamonds
|
||||
#>
|
||||
function Get-Hand
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Hand')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
#Brings the Vairiable in from Get-Deck
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourHandName
|
||||
)
|
||||
Begin
|
||||
{
|
||||
}
|
||||
Process
|
||||
{
|
||||
# Will return a missing variable error if the deck has net been created
|
||||
try
|
||||
{
|
||||
$temp = Get-Variable -name "$YourHandName" -ValueOnly -ErrorAction stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Write-Host
|
||||
Write-Host "$YourHandName does not exist..."
|
||||
Write-Host "Creating Hand $YourHandName..."
|
||||
New-Hand -YourHandName $YourHandName
|
||||
$temp = Get-Variable -name "$YourHandName" -ValueOnly
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
$count = $temp.count
|
||||
Write-Host "$YourHandName's hand contains $count cards, they are..."
|
||||
$temp | select Value | ft -HideTableHeaders
|
||||
}
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
Write-Verbose "End of show-deck function"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Draws/returns cards
|
||||
.DESCRIPTION
|
||||
Draws/returns cards from your chosen deck , to your chosen hand
|
||||
Can be used without creating a deck or hand first
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> DrawFrom-Deck -YourDeckName Deck1 -YourHandName test1 -HowManyCardsToDraw 10
|
||||
|
||||
|
||||
|
||||
PS C:\WINDOWS\system32>
|
||||
.EXAMPLE
|
||||
DrawFrom-Deck -YourDeckName Deck2 -YourHandName test2 -HowManyCardsToDraw 10
|
||||
Deck2 does not exist...
|
||||
Creating Deck Deck2...
|
||||
|
||||
test2 does not exist...
|
||||
Creating Hand test2...
|
||||
test2's hand contains cards, they are...
|
||||
|
||||
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\WINDOWS\system32> draw -YourDeckName deck1 -YourHandName test1 -HowManyCardsToDraw 5
|
||||
|
||||
|
||||
|
||||
#>
|
||||
function Draw-Deck
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[Alias('Draw')]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# The Deck in which you want to draw cards out of
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourDeckName,
|
||||
|
||||
#The hand in which you want to draw cards to
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$YourHandName,
|
||||
|
||||
|
||||
#Quanity of cards being drawn
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]$HowManyCardsToDraw
|
||||
|
||||
)
|
||||
Begin
|
||||
{
|
||||
Write-Verbose "Draws a chosen amount of cards from the chosen deck"
|
||||
|
||||
#try-catch-finally blocks so the user does not have to use New-Deck and New-Hand beforehand.
|
||||
try
|
||||
{
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Get-Deck -YourDeckName $YourDeckName
|
||||
$temp = Get-Variable -name "$YourDeckName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
finally
|
||||
{
|
||||
Write-Host
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$temp2 = Get-Variable -name "$YourHandName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
catch
|
||||
{
|
||||
Get-Hand -YourHandName $YourHandName
|
||||
$temp2 = Get-Variable -name "$YourHandName" -ValueOnly -ErrorAction Stop
|
||||
}
|
||||
finally
|
||||
{
|
||||
Write-Host
|
||||
}
|
||||
}
|
||||
|
||||
Process
|
||||
{
|
||||
Write-Host "you drew $HowManyCardsToDraw cards, they are..."
|
||||
|
||||
$handValues = Get-Variable -name "$YourDeckName" -ValueOnly
|
||||
$handValues = $handValues[0..(($HowManyCardsToDraw -1))] | select value | ft -HideTableHeaders
|
||||
$handValues
|
||||
|
||||
}
|
||||
End
|
||||
{
|
||||
#sets the new values for the deck and the hand selected
|
||||
Set-Variable -Name "$YourDeckName" -value ($temp[$HowManyCardsToDraw..($temp.count)]) -Scope Global
|
||||
Set-Variable -Name "$YourHandName" -value ($temp2 + $handValues) -Scope Global
|
||||
}
|
||||
}
|
||||
45
Task/Playing-cards/Rebol/playing-cards.rebol
Normal file
45
Task/Playing-cards/Rebol/playing-cards.rebol
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Playing cards"
|
||||
file: %Playing_cards.r3
|
||||
url: https://rosettacode.org/wiki/Playing_cards
|
||||
note: "Based on Red language solution"
|
||||
needs: 3.0.0
|
||||
]
|
||||
random/seed 1 ;; Seed the random number generator for reproducibility
|
||||
|
||||
make-deck: function/with [] [
|
||||
new-deck: make block! 52 ;; Create an empty block for 52 cards
|
||||
foreach s suit [ ;; For each suit...
|
||||
foreach r rank [ ;; ...and for each rank...
|
||||
append new-deck join r s ;; ...make a card by joining rank and suit (e.g., "Q♠") and add to deck
|
||||
]
|
||||
]
|
||||
][
|
||||
rank: "A23456789TJQK" ;; All ranks in a suit (Ace, 2 .. 9, Ten, Jack, Queen, King)
|
||||
suit: "♣♦♥♠" ;; All four suits: clubs, diamonds, hearts, spades
|
||||
]
|
||||
|
||||
deal: func [
|
||||
other-deck [block!] ;; Deck to deal card(s) into
|
||||
deck [block!] ;; Deck to deal card(s) from
|
||||
][
|
||||
unless empty? deck [
|
||||
append other-deck take deck ;; If cards remain, take the top card and append to other deck
|
||||
]
|
||||
]
|
||||
|
||||
contents: func [deck [block!]] [
|
||||
repeat i length? deck [
|
||||
prin [deck/:i SP] ;; Print each card with
|
||||
if zero? i % 12 [prin LF] ;; Newline after every 12 cards for readability
|
||||
]
|
||||
]
|
||||
|
||||
deck: random make-deck ;; Create and shuffle a new deck
|
||||
|
||||
print "40 cards from a deck:"
|
||||
loop 5 [ ;; 5 lines total
|
||||
prin LF ;; Blank line to separate each row
|
||||
loop 8 [prin [take deck SP]] ;; Deal 8 cards per line, remove from deck and print
|
||||
]
|
||||
prin "^/^/remaining: " contents deck ;; Print all remaining cards in deck, 12 per line
|
||||
66
Task/Playing-cards/VBScript/playing-cards-1.vbs
Normal file
66
Task/Playing-cards/VBScript/playing-cards-1.vbs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
class playingcard
|
||||
dim suit
|
||||
dim pips
|
||||
end class
|
||||
|
||||
class carddeck
|
||||
private suitnames
|
||||
private pipnames
|
||||
private cardno
|
||||
private deck(52)
|
||||
private nTop
|
||||
|
||||
sub class_initialize
|
||||
dim suit
|
||||
dim pips
|
||||
suitnames = split("H,D,C,S",",")
|
||||
pipnames = split("A,2,3,4,5,6,7,8,9,10,J,Q,K",",")
|
||||
cardno = 0
|
||||
|
||||
for suit = 1 to 4
|
||||
for pips = 1 to 13
|
||||
set deck(cardno) = new playingcard
|
||||
deck(cardno).suit = suitnames(suit-1)
|
||||
deck(cardno).pips = pipnames(pips-1)
|
||||
cardno = cardno + 1
|
||||
next
|
||||
next
|
||||
nTop = 0
|
||||
end sub
|
||||
|
||||
public sub showdeck
|
||||
dim a
|
||||
redim a(51-nTop)
|
||||
for i = nTop to 51
|
||||
a(i) = deck(i).pips & deck(i).suit
|
||||
next
|
||||
wscript.echo join( a, ", ")
|
||||
end sub
|
||||
|
||||
public sub shuffle
|
||||
dim r
|
||||
randomize timer
|
||||
for i = nTop to 51
|
||||
r = int( rnd * ( 52 - nTop ) )
|
||||
if r <> i then
|
||||
objswap deck(i),deck(r)
|
||||
end if
|
||||
next
|
||||
end sub
|
||||
|
||||
public function deal()
|
||||
set deal = deck( nTop )
|
||||
nTop = nTop + 1
|
||||
end function
|
||||
|
||||
public property get cardsRemaining
|
||||
cardsRemaining = 52 - nTop
|
||||
end property
|
||||
|
||||
private sub objswap( a, b )
|
||||
dim tmp
|
||||
set tmp = a
|
||||
set a = b
|
||||
set b = tmp
|
||||
end sub
|
||||
end class
|
||||
17
Task/Playing-cards/VBScript/playing-cards-2.vbs
Normal file
17
Task/Playing-cards/VBScript/playing-cards-2.vbs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
dim pack
|
||||
set pack = new carddeck
|
||||
wscript.echo "--before shuffle"
|
||||
pack.showdeck
|
||||
pack.shuffle
|
||||
wscript.echo "--after shuffle"
|
||||
pack.showdeck
|
||||
|
||||
dim card
|
||||
for i = 1 to 52
|
||||
set card = pack.deal
|
||||
next
|
||||
wscript.echo "--dealt a card, it's the", card.pips, "of", card.suit
|
||||
wscript.echo "--", pack.cardsRemaining, "cards remaining"
|
||||
if pack.cardsRemaining <> 0 then
|
||||
pack.showdeck
|
||||
end if
|
||||
Loading…
Add table
Add a link
Reference in a new issue