Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,25 @@
type DeckDesign{T<:Integer,U<:String}
rlen::T
slen::T
ranks::Array{U,1}
suits::Array{U,1}
hlen::T
end
type Deck{T<:Integer}
cards::Array{T,1}
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)
s = ["\u2663", "\u2666", "\u2665", "\u2660"]
DeckDesign(13, 4, r, s, 5)
end
function fresh(des::DeckDesign)
Deck(collect(1:des.rlen*des.slen), des)
end

View file

@ -0,0 +1,14 @@
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)
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, " ")
end

View file

@ -0,0 +1,25 @@
function deal!{T<:Integer}(d::Deck, hlen::T)
if hlen < length(d)
hand = Deck(d.cards[1:hlen], d.design)
d.cards = d.cards[hlen+1:end]
else
hand = d
empty!(d)
end
return hand
end
function deal!(d::Deck)
deal!(d, d.design.hlen)
end
function pretty(d::Deck)
s = ""
llen = d.design.rlen
dlen = length(d)
for i in 1:llen:dlen
j = min(i+llen-1, dlen)
s *= print(d[i:j])*"\n"
end
chop(s)
end

View file

@ -0,0 +1,20 @@
d = fresh(pokerlayout())
println("A new poker deck:")
println(pretty(d))
shuffle!(d)
println()
println("The deck shuffled:")
println(pretty(d))
n = 4
println()
println("Deal ", n, " hands:")
for i in 1:n
h = deal!(d)
println(pretty(h))
end
println()
println("And now the deck contains:")
println(pretty(d))

View file

@ -0,0 +1,58 @@
local tPlayers = {} -- cards of players
local tBoard = {} -- cards in a board
local nPlayers = 5 -- number of players
local tDeck = {
'2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', 'Ad', -- DIAMONDS
'2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', 'As', -- SPADES
'2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', 'Ah', -- HEARTS
'2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', 'Ac'} -- CLUBS
local function shuffle() -- FisherYates shuffle
i = #tDeck
while i > 1 do
i = i - 1
j = math.random(1, i)
tDeck[j], tDeck[i] = tDeck[i], tDeck[j]
end
return tDeck
end
local function cardTransfer(to, amount, from)
for f = 1, amount do
table.insert(to, #to+1, from[#from])
from[#from] = nil
end
end
----||EXAMPLE OF USE||----
print('FRESH DECK \n', table.concat(tDeck, ' '), '\n')
shuffle()
print('SHUFFLED DECK \n', table.concat(tDeck, ' '), '\n')
for a = 1, nPlayers do
tPlayers[a] = {}
cardTransfer(tPlayers[a], 2, tDeck)
end
cardTransfer(tBoard, 5, tDeck)
print('BOARD\n', table.concat(tBoard, ' '), '\n')
for b = 1, nPlayers do
print('PLAYER #'..b..': ', table.concat(tPlayers[b], ' '))
end
print('\nREMAINING\n', table.concat(tDeck, ' '), '\n')
for c = 1, #tPlayers do
for d = 1, #tPlayers[c] do
cardTransfer(tDeck, d, tPlayers[c])
end
end
cardTransfer(tDeck, 5, tBoard)
print('ALL CARDS IN THE DECK\n', table.concat(tDeck, ' '), '\n')

View file

@ -0,0 +1,20 @@
FRESH DECK
2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac
SHUFFLED DECK
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h Qh 4c 3s As Kc Qs Ad Th 4s Jd Ac Qc 2c 9s 6c
BOARD
Kc As 3s 4c Qh
PLAYER #1: 6c 9s
PLAYER #2: 2c Qc
PLAYER #3: Ac Jd
PLAYER #4: 4s Th
PLAYER #5: Ad Qs
REMAINING
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h
ALL CARDS IN THE DECK
7c 3d 8h 7h 7s 9c 8c Ks 8s 2s 5s 8d 2h 3h Jc 6h Td Ts Jh Tc 6s Kd 7d 4h 4d 5d Qd 5h 5c Kh 9d 2d Ah 6d 3c Js 9h 9s 6c Qc 2c Jd Ac Th 4s Qs Ad Qh 4c 3s As Kc

View file

@ -1,36 +1,36 @@
/*REXX pgm shows methods (subroutines) to build/shuffle/deal a card deck*/
call buildDeck ; say ' new deck:' newDeck /*new 52-card deck*/
call shuffleDeck; say 'shuffled deck:' theDeck /*shuffled deck. */
call dealHands 5,4 /*5 cards, 4 hands*/
/*REXX pgm shows a method to build/shuffle/deal a standard 52─card deck.*/
box = build(); say ' box of cards:' box /*a new box of 52─cards.*/
deck=shuffle(); say 'shuffled deck:' deck /*randomly shuffled deck*/
call deal 5, 4 /* ◄═════════════════════════════════ 5 cards, 4 hands*/
say; say; say right('[north]' hand.1,50)
say; say '[west]' hand.4 right('[east]' hand.2,60)
say; say right('[south]' hand.3,50)
say; say; say; say 'remainder of deck:' theDeck
say; say; say; say 'remainder of deck: ' deck
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BUILDDECK subroutine────────────────*/
buildDeck: _=''; ranks='A 2 3 4 5 6 7 8 9 10 J Q K' /*ranks. */
if 1=='f1'x then suits='h d c s' /*EBCDIC?*/
else suits=' ' /*ASCII. */
do s=1 for words(suits)
do r=1 for words(ranks)
_=_ word(ranks,r)word(suits,s)
end /*dealR*/
end; /*dealS*/ newDeck=_
return
/*──────────────────────────────────SHUFFLEDECK subroutine──────────────*/
shuffleDeck: theDeck=''; _=newDeck; #cards=words(_)
/*──────────────────────────────────BUILD subroutine────────────────────*/
build: _=; ranks= "A 2 3 4 5 6 7 8 9 10 J Q K" /*ranks. */
if 5=='f5'x then suits= "h d c s" /*EBCDIC? */
else suits= "♥ ♦ ♣ ♠" /*ASCII. */
#ranks=words(ranks); do s=1 for words(suits); @=word(suits,s)
do r=1 for #ranks
_=_ word(ranks,r)@
end /*s*/
end /*r*/
return _
/*──────────────────────────────────SHUFFLE subroutine──────────────────*/
shuffle: y=; _=box; #cards=words(_) /*define REXX vars.*/
do shuffler=1 for #cards /*shuffle all the cards in deck. */
r=random(1,#cards+1-shuffler) /*random # decreases each time. */
theDeck=theDeck word(_,r) /*sufffled deck, 1 card at-a-time*/
_=delword(_,r,1) /*delete the just-chosen card. */
?=random(1,#cards+1-shuffler) /*each shuffle, random# decreases*/
y=y word(_, ?) /*shuffled deck, 1 card at─a─time*/
_=delword(_, ?, 1) /*delete the just─chosen card. */
end /*shuffler*/
return
/*──────────────────────────────────DEALHANDS subroutine────────────────*/
dealHands: parse arg numberOfCards,hands; hand.=''
do numberOfCards /*deal the hand to the players. */
do player=1 for hands /*deal a card to the players. */
hand.player=hand.player subword(theDeck,1,1) /*deal top card.*/
theDeck=subword(theDeck,2 ) /*diminish deck, remove one card.*/
end /*player*/
end /*numberOfCards*/
return y
/*──────────────────────────────────DEAL subroutine─────────────────────*/
deal: parse arg #cards, hands; hand.=
do #cards /*deal the hand to the players. */
do player=1 for hands /*deal some cards to the players.*/
hand.player=hand.player word(deck, 1) /*deal top card.*/
deck=subword(deck, 2) /*diminish deck, remove one card.*/
end /*player*/
end /*#cards*/
return

View file

@ -1,52 +1,47 @@
class Card
# class constants
Suits = ["Clubs","Hearts","Spades","Diamonds"]
Pips = ["2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"]
# class constants
SUITS = %i[ Clubs Hearts Spades Diamonds ]
PIPS = %i[ 2 3 4 5 6 7 8 9 10 Jack Queen King Ace ]
# class variables (private)
@@suit_value = Hash[ Suits.each_with_index.to_a ]
@@pip_value = Hash[ Pips.each_with_index.to_a ]
# class variables (private)
@@suit_value = Hash[ SUITS.each_with_index.to_a ]
@@pip_value = Hash[ PIPS.each_with_index.to_a ]
attr_reader :pip, :suit
attr_reader :pip, :suit
def initialize(pip,suit)
@pip = pip
@suit = suit
end
def initialize(pip,suit)
@pip = pip
@suit = suit
end
def to_s
"#{@pip} #{@suit}"
end
def to_s
"#{@pip} #{@suit}"
end
# allow sorting an array of Cards: first by suit, then by value
def <=>(card)
(@@suit_value[@suit] <=> @@suit_value[card.suit]).nonzero? or \
@@pip_value[@pip] <=> @@pip_value[card.pip]
end
# allow sorting an array of Cards: first by suit, then by value
def <=>(other)
(@@suit_value[@suit] <=> @@suit_value[other.suit]).nonzero? or
@@pip_value[@pip] <=> @@pip_value[other.pip]
end
end
class Deck
def initialize
@deck = []
for suit in Card::Suits
for pip in Card::Pips
@deck << Card.new(pip,suit)
end
end
end
def initialize
@deck = Card::SUITS.product(Card::PIPS).map{|suit,pip| Card.new(pip,suit)}
end
def to_s
"[#{@deck.join(", ")}]"
end
def to_s
"[#{@deck.join(", ")}]"
end
def shuffle!
@deck.shuffle!
self
end
def shuffle!
@deck.shuffle!
self
end
def deal(*args)
@deck.shift(*args)
end
def deal(*args)
@deck.shift(*args)
end
end
deck = Deck.new.shuffle!

View file

@ -1,125 +1,63 @@
extern crate rand;
use std::fmt;
use std::rand::{task_rng, Rng};
use rand::Rng;
use Pip::*;
use Suit::*;
#[deriving(Clone)]
enum Pip {
Ace,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
}
#[derive(Copy, Clone, Debug)]
enum Pip { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King }
impl fmt::Show for Pip {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = match *self {
Ace => "Ace",
Two => "Two",
Three => "Three",
Four => "Four",
Five => "Five",
Six => "Six",
Seven => "Seven",
Eight => "Eight",
Nine => "Nine",
Ten => "Ten",
Jack => "Jack",
Queen => "Queen",
King => "King"
};
#[derive(Copy, Clone, Debug)]
enum Suit { Spades, Hearts, Diamonds, Clubs }
write!(f, "{}", name)
}
}
#[deriving(Clone)]
enum Suit {
Spades,
Hearts,
Diamonds,
Clubs
}
impl fmt::Show for Suit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = match *self {
Spades => "Spades",
Hearts => "Hearts",
Diamonds => "Diamonds",
Clubs => "Clubs"
};
write!(f, "{}", name)
}
}
#[deriving(Clone)]
struct Card {
pip: Pip,
suit: Suit
}
impl Card {
fn new(pip: Pip, suit: Suit) -> Card {
Card {pip: pip, suit: suit}
}
}
impl fmt::Show for Card {
impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} of {}", self.pip, self.suit)
write!(f, "{:?} of {:?}", self.pip, self.suit)
}
}
#[deriving(Clone)]
struct Deck(Vec<Card>);
impl Deck {
fn new() -> Deck {
let mut cards:Vec<Card> = Vec::with_capacity(52);
for suit in [Spades, Hearts, Diamonds, Clubs].iter() {
for pip in [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King].iter() {
cards.push(Card::new(*pip, *suit));
for &suit in &[Spades, Hearts, Diamonds, Clubs] {
for &pip in &[Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King] {
cards.push( Card{pip: pip, suit: suit} );
}
}
Deck(cards)
}
fn deal(&mut self) -> Option<Card> {
let &Deck(ref mut cards) = self;
cards.pop()
self.0.pop()
}
fn shuffle(&mut self) {
let &Deck(ref mut cards) = self;
let mut rng = task_rng();
rng.shuffle(cards.as_mut_slice());
rand::thread_rng().shuffle(&mut self.0)
}
}
impl fmt::Show for Deck {
impl fmt::Display for Deck {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &Deck(ref cards) = self;
let mut text = String::new();
let mut i = 0;
for card in cards.iter() {
text.push_str(format!("{}", card).as_slice());
i += 1;
if i < cards.capacity() {
text.push_str("\n");
}
for card in self.0.iter() {
writeln!(f, "{}", card);
}
write!(f, "{}", text)
write!(f, "")
}
}
fn main() {
let mut deck = Deck::new();
deck.shuffle();
//println!("{}", deck);
for _ in 0..5 {
println!("{}", deck.deal().unwrap());
}
}