Deal cards for FreeCell: Difference between revisions
Content added Content deleted
Kinitawowi (talk | contribs) No edit summary |
|||
Line 2,474: | Line 2,474: | ||
9S 3C 8S 7H 4D JS 4C QS 9C 9H 7C 6H 2C |
9S 3C 8S 7H 4D JS 4C QS 9C 9H 7C 6H 2C |
||
2S 4S TS 2H 5D JC 6C JH QH JD KS KC 4H |
2S 4S TS 2H 5D JC 6C JH QH JD KS KC 4H |
||
</pre> |
|||
=={{header|PHP}}== |
|||
<lang php>class FreeCell_Deal { |
|||
protected $deck = array( |
|||
'AC', 'AD', 'AH', 'AS', '2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S', |
|||
'4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S', '6C', '6D', '6H', '6S', |
|||
'7C', '7D', '7H', '7S', '8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S', |
|||
'TC', 'TD', 'TH', 'TS', 'JC', 'JD', 'JH', 'JS', 'QC', 'QD', 'QH', 'QS', |
|||
'KC', 'KD', 'KH', 'KS' |
|||
); |
|||
protected $game; // Freecell Game Number |
|||
protected $state; // Current state of the LCG |
|||
public $deal = array(); // Generated card sequence to deal |
|||
function __construct( $game ) { |
|||
$this->game = max( min( $game, 32000 ), 1 ); |
|||
// seed RNG with game number |
|||
$this->state = $this->game; |
|||
while ( ! empty( $this->deck ) ) { |
|||
// choose random card |
|||
$i = $this->lcg_rnd() % count( $this->deck ); |
|||
// move random card to game deal pile |
|||
$this->deal[] = $this->deck[ $i ]; |
|||
// move last card to random card spot |
|||
$this->deck[ $i ] = end( $this->deck ); |
|||
// remove last card from deck |
|||
array_pop( $this->deck ); |
|||
} |
|||
} |
|||
protected function lcg_rnd() { |
|||
return ( $this->state = ( $this->state * 214013 + 2531011 ) % 2147483648 ) >> 16; |
|||
} |
|||
function print( $cols = 8 ) { |
|||
echo str_pad( " Game " . $this->game . " ", $cols * 3 - 1, '=', STR_PAD_BOTH ), PHP_EOL; |
|||
foreach ( array_chunk( $this->deal, $cols ) as $row ) { |
|||
echo implode( " ", $row ), PHP_EOL; |
|||
} |
|||
echo PHP_EOL; |
|||
} |
|||
} |
|||
$tests = array( 1, 617, 11982 ); |
|||
foreach ( $tests as $game_num ) { |
|||
$deal = new FreeCell_Deal( $game_num ); |
|||
$deal->print(); |
|||
} |
|||
</lang> |
|||
{{Out}} |
|||
<pre> |
|||
======= Game 1 ======== |
|||
JD 2D 9H JC 5D 7H 7C 5H |
|||
KD KC 9S 5S AD QC KH 3H |
|||
2S KS 9D QD JS AS AH 3C |
|||
4C 5C TS QH 4H AC 4D 7S |
|||
3S TD 4S TH 8H 2C JH 7D |
|||
6D 8S 8D QS 6C 3D 8C TC |
|||
6S 9C 2H 6H |
|||
====== Game 617 ======= |
|||
7D AD 5C 3S 5S 8C 2D AH |
|||
TD 7S QD AC 6D 8H AS KH |
|||
TH QC 3H 9D 6S 8D 3D TC |
|||
KD 5H 9S 3C 8S 7H 4D JS |
|||
4C QS 9C 9H 7C 6H 2C 2S |
|||
4S TS 2H 5D JC 6C JH QH |
|||
JD KS KC 4H |
|||
===== Game 11982 ====== |
|||
AH AS 4H AC 2D 6S TS JS |
|||
3D 3H QS QC 8S 7H AD KS |
|||
KD 6H 5S 4D 9H JH 9S 3C |
|||
JC 5D 5C 8C 9D TD KH 7C |
|||
6C 2C TH QH 6D TC 4S 7S |
|||
JD 7D 8H 9C 2H QD 4C 5H |
|||
KC 8D 2S 3S |
|||
</pre> |
</pre> |
||