Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Lazy poker (5 card)
- // bet money (by default you get 500)
- // password saves money
- // data stored to cookie (?)
- // lol none of the above happened
- /*
- Settings
- */
- define('NUM_CARDS', 5);
- define('MAX_LOSSES', 3);
- $cipher = 'LSAPOJKYMB';
- // Change the rest at your own peril!
- if (NUM_CARDS > 25) die("Setting this higher than 25 will cause an infinite loop.");
- /*
- Defines
- */
- define('SPADES' , 0b00);
- define('CLUBS' , 0b01);
- define('HEARTS' , 0b10);
- define('DIAMONDS' , 0b11);
- define('NO_CARD' , 0b111111); // Value used to mark that there's no card
- $deck = ['A',2,3,4,5,6,7,8,9,10,'J','Q','K'];
- $cards_given = [56]; // Initialize the list of given cards with an out of range value
- $wins = $losses = $highscore = 0;
- // Max value for card (13) is 1101
- // CARD FORMAT
- // NNNNTT
- $act = isset($_GET['act']) ? $_GET['act'] : "";
- $output = "";
- switch ($act) {
- case "": // Init
- $output = "<a href='?act=play'>Start game</a>\n\n<a href='?act=code'>Enter code</a>";
- $currentstats = "";
- break;
- // Password
- case 'code':
- if (!isset($_POST['code'])) {
- $output = "<form method='POST' action='?act=code'>".
- "Enter code: <input type='text' name='code' maxlength=10 size=20> <input type='submit' name='go' value='OK'>".
- "</form>";
- $currentstats = "";
- } else {
- $output = decode_password($_POST['code']);
- // HACK: The wins and losses are set to a random value since normally they would be 0
- // set them to 0 since we always start from after a game over
- //$wins = $losses = 0;
- $currentstats = print_scores($wins, $losses, $highscore);
- }
- break;
- case 'play':
- // Main game
- $status = load_scores();
- $losses = $status[0];
- $wins = $status[1];
- $highscore = $status[2];
- if (isset($_POST['deck0'])){
- $cards = load_deck();
- // changed marked cards
- for ($i = 0; $i < NUM_CARDS; $i++){
- if (isset($_POST["c$i"])) $cards[$i] = shuffle_deck(1, true);
- $decoded[] = decode_card($cards[$i]);
- }
- $check = false; // Checkbox to change cards
- // Scoring system crap
- $score = doscoring($decoded);
- // Change wins to score?
- switch ($score) {
- case S_ROYAL_FLUSH: $x = "a Royal Flush"; break;
- case S_STRAIGHT_FLUSH: $x = "a Straight Flush"; break;
- case S_POKER: $x = "4 of a kind"; break;
- case S_FULL_HOUSE: $x = "a Full house"; break;
- case S_FLUSH: $x = "a Flush"; break;
- case S_STRAIGHT: $x = "a Straight"; break;
- case S_THREE_OF_A_KIND: $x = "3 of a kind"; break;
- case S_TWO_PAIR: $x = "2 pairs"; break;
- case S_ONE_PAIR: $x = "1 pair"; break;
- case S_NOTHING: $x = "nothing"; break;
- }
- $output = "\n\nYou got $x!";
- if (!$score){
- $losses++;
- $output .= " YOU LOSE!";
- } else {
- $wins++;
- }
- if ($losses <= MAX_LOSSES) {
- $output .= "\n<center><input type='submit' name='go' value='Continue'></center>";
- } else {
- $output .= "\n\n<big>OAME GVER</big>";
- if ($wins > $highscore){
- $highscore = $wins;
- $output .= "\nNew high score: $highscore!";
- }
- // HACK: Wins and Losses are always reset to 0 after a game over
- // since it would be a waste, just set them to a random 8bit value
- //$wins = mt_rand(0, 255);
- //$losses = mt_rand(0, 255);
- $output .= "\nYour code is: ".encode_password($wins, $losses, $highscore)."\n\n<center><input type='submit' name='go' value='Restart'></center>";
- $wins = $losses = 0;
- }
- } else {
- $cards = shuffle_deck(NUM_CARDS);
- $check = true;
- }
- $output = print_deck($cards, $check).$output.pack_scores($wins, $losses, $highscore);
- $currentstats = print_scores($wins, $losses, $highscore);
- break;
- }
- ?><!doctype html>
- <html>
- <head>
- <title>Lazy poker game</title>
- <style type="text/css">
- body {
- background: #000;
- color: #FFF;
- }
- .red {color: #F00;}
- .black {color: #000;}
- .card {
- width: 50px;
- background: #FFF;
- border-top: 1px solid #FFF;
- border-left: 1px solid #EEE;
- border-bottom: 1px solid #BBB;
- border-right: 1px solid #CCC;
- border-radius: 3px;
- box-shadow: 8px 8px 0px #555;
- line-height: 100%;
- }
- .status {
- border: 3px double #FFF;
- }
- .statushead {
- text-align: center;
- background: #FFF;
- color: #000;
- }
- .statuscontainer {
- padding-left: 30px;
- padding-top: 50px;
- vertical-align: top;
- }
- .l {text-align: left;}
- .c {text-align: center;}
- .r {text-align: right;}
- a {color: #AFA;}
- </style>
- </head>
- <body><table style='border-spacing: 0px'><tr><td style='min-width: 284px; vertical-align: top'>
- <pre>***********
- * <b>L</b> A Z Y *
- * C <b>A</b> R D *
- * G A <b>M</b> <b>E</b> *
- ***********
- <?php echo $output ?></pre></td><td class='statuscontainer'><pre><?php echo $currentstats ?></pre></td></table>
- </body>
- </html><?php
- function drawcard($type, $num){
- global $deck;
- switch ($type) {
- case SPADES : $class = 'black'; $char = '♠'; break;
- case CLUBS : $class = 'black'; $char = '♣'; break;
- case HEARTS : $class = 'red'; $char = '♥'; break;
- case DIAMONDS : $class = 'red'; $char = '♦'; break;
- default: die(header("Location: ?"));//die("Invalid card type. ($type)");
- }
- if (!isset($deck[$num])) {
- die(header("Location: ?"));//die("Invalid card number. ($num)");
- }
- return "".
- "<table class='card $class'>".
- "<tr><td class='l'>{$deck[$num]}<small><br>$char</small></td></tr>".
- "<tr><td class='c'><big>$char</big>\n</td></tr>".
- "<tr><td class='r'><small>$char<br></small>{$deck[$num]}</td></tr>".
- "</table>";
- }
- // Get 5 random cards
- function shuffle_deck($times, $single = false){
- global $cards_given;
- for($i = 0; $i < $times; $i++){
- for($val = 56; in_array($val, $cards_given); $val = mt_rand(0, 51));
- if ($single) return $val;
- $cards_given[] = $val;
- $deck[] = $val; // Your deck
- }
- return $deck;
- }
- function print_deck($deck, $check = false){
- $cnt = count($deck);
- for ($t = $c = "", $i = 0; $i < $cnt; $i++){
- if ($deck[$i] == NO_CARD) continue;
- $card = decode_card($deck[$i]);
- $t .= "<td style='padding-left: 3px'>".drawcard($card[0], $card[1])."</td>";
- $c .= "<td style='padding-top: 6px' class='c'><input type='checkbox' name='c$i'></td>";
- }
- return "<form method='POST' action='?act=play'><table><tr>$t</tr>".($check ? "<tr>$c</tr><tr><td class='c' colspan='$cnt'><input type='submit' name='go' value='Discard'>".pack_deck($deck, true)."</td></tr>" : "")."</table>";
- }
- // Receives packed data and returns array
- function load_deck(){
- global $cards_given;
- for ($i = 0; isset($_POST["deck$i"]); $i++){
- $deck[] = ($_POST["deck$i"] ) & 0b111111; // CARD 0
- $deck[] = ($_POST["deck$i"] >> 6 ) & 0b111111; // CARD 1 ...
- $deck[] = ($_POST["deck$i"] >> 12) & 0b111111;
- $deck[] = ($_POST["deck$i"] >> 18) & 0b111111;
- $deck[] = ($_POST["deck$i"] >> 24) & 0b111111;
- }
- for($i = 0; $i < NUM_CARDS; $i++) $cards_given[] = $deck[$i];
- return $deck;
- }
- // Receives array and returns packed data
- function pack_deck($deck, $htmlout){
- $cnt = count($deck);
- for ($i = 0; $i < $cnt; $i++){//6*5 = 30bits
- $x = $i * 5; // Current multiplier
- for ($j = $x; $j < 5 + $x; $j++) if (!isset($deck[$j])) $deck[$j] = 0b111111; // check if slots for this int have cards or are empty. in the latter fill with 1111
- $pack[] = $deck[$x] | $deck[$x+1] << 6 | $deck[$x+2] << 12 | $deck[$x+3] << 18 | $deck[$x+4] << 24;
- }
- if ($htmlout) {
- $total = ceil($cnt / 5);
- for ($i = 0, $o = ''; $i < $total; $i++){
- $o .= "<input type='hidden' name='deck$i' value={$pack[$i]}>";
- //print "COMPRESSED #$i = {$pack[$i]}\n";
- }
- return $o;
- } else {
- return $pack;
- }
- }
- function decode_card($card){
- $type = $card & 0b000011;
- $num = $card >> 2;
- return [$type, $num];
- }
- function encode_card($type, $num){
- return $type | ($num << 2);
- }
- // Highscore, Wins, Losses
- function decode_password($code){
- global $cipher, $wins, $losses, $highscore;
- // decoding here
- // decoding end
- $code = (string) $code;
- $len = strlen($code);
- for($i = 0; $i < $len; $i++){
- $code[$i] = strpos($cipher, $code[$i]);
- if ($code[$i] === false) die(header("Location: ?"));
- }
- $_POST['status'] = $code;
- $stat = load_scores();
- // for the status screen function
- $highscore = $stat[2];
- $wins = $stat[1];
- $losses = $stat[0];
- // Success!
- return "The code is valid.\nYour high score has been preserved.\n\n".
- "<form method='POST' action='?act=play'>".pack_scores(0,0,$highscore)."<input type='submit' name='a' value='Continue game'></form>";
- }
- function encode_password($wins, $losses, $highscore){
- global $cipher;
- $code = pack_scores($wins, $losses, $highscore, true);
- // Encoding here
- $code = (string) $code;
- $len = strlen($code);
- for($i = 0; $i < $len; $i++){
- $code[$i] = $cipher[$code[$i]];
- }
- // Encoding end
- return $code;
- }
- function pack_scores($wins, $losses, $highscore, $print = false){
- $checksum = 0b10101010 | $highscore ^ ($wins &~ $losses);
- // do whatever load_scores does, in reverse
- $wins_e = $wins ^ 0b11110000;
- $losses_e = $losses ^ 0b10010110;
- $highscore_e = $highscore ^ 0b11000011;
- $checksum_e = $checksum ^ 0b11001100;
- // high values << x - 4
- // low values << x
- $result =
- (($checksum_e & 0b11110000) << 24) | (($losses_e & 0b1111 ) << 24) |
- (($wins_e & 0b1111 ) << 20) | (($highscore_e & 0b11110000) << 12) |
- (($wins_e & 0b11110000) << 8) | (($checksum_e & 0b1111 ) << 8) |
- (($losses_e & 0b11110000) ) | (($highscore_e & 0b1111 ) );
- //print "W$wins_e<br>L$losses_e<br>H$highscore_e<br>C$checksum_e<br>R$result";
- if ($print) return $result;
- return "<input type='hidden' name='status' value=$result>";
- }
- function load_scores(){
- /* SETTINGS: 32 bit int
- 2 1 1 2 2 1 2 1
- CCCCLLLLWWWWHHHHWWWWCCCCLLLLHHHH
- >> 28 24 20 16 12 8 4
- C - Checksum
- L - Losses
- W - Wins
- H - Max wins
- */
- if (!isset($_POST['status'])) return [0,0,0];
- // block high block low
- $wins = ((($_POST['status'] >> 8) & 0b11110000) | (($_POST['status'] >> 20) & 0b1111)) ^ 0b11110000;
- $losses = ((($_POST['status'] ) & 0b11110000) | (($_POST['status'] >> 24) & 0b1111)) ^ 0b10010110;
- $highscore = ((($_POST['status'] >> 12) & 0b11110000) | (($_POST['status'] ) & 0b1111)) ^ 0b11000011;
- $checksum = ((($_POST['status'] >> 24) & 0b11110000) | (($_POST['status'] >> 8) & 0b1111)) ^ 0b11001100;
- $checksumc = 0b10101010 | $highscore ^ ($wins &~ $losses);
- //print "W$wins<br>L$losses<br>H$highscore<br>C$checksum<br>C$checksumc<br>R".$_POST['status']."<br>";
- if ($checksum != $checksumc) die(header("Location: ?"));//die("Checksum failed"); //return [0,0,0];
- else return [$losses, $wins, $highscore];
- }
- function print_scores($wins, $losses, $highscore){
- return "".
- "<table class='status'>".
- "<tr><td class='statushead'>S T A T U S</td></tr>".
- "<tr><td> High Score: $highscore</td></tr>".
- "<tr><td> ===================== </td></tr>".
- "<tr><td> Wins: $wins</td></tr>".
- "<tr><td> Losses: $losses</td></tr>".
- "</table>";
- }
- function doscoring($decoded){
- define('S_ROYAL_FLUSH' , 0b1001);
- define('S_STRAIGHT_FLUSH' , 0b1000);
- define('S_POKER' , 0b0111);
- define('S_FULL_HOUSE' , 0b0110);
- define('S_FLUSH' , 0b0101);
- define('S_STRAIGHT' , 0b0100);
- define('S_THREE_OF_A_KIND' , 0b0011);
- define('S_TWO_PAIR' , 0b0010);
- define('S_ONE_PAIR' , 0b0001);
- define('S_NOTHING' , 0b0000); // You lose
- $num = array_fill(0, 13, 0);
- $type = array_fill(0, 4, 0);
- // Count the amount of numbers and colors
- foreach($decoded as $card){
- $type[$card[0]]++;
- $num[$card[1]]++;
- }
- // Pre-calculated values useful for scoring checks
- $straight = continuous($num);
- $flush = in_array(5, $type);
- $poker = in_array(4, $num);
- $three = in_array(3, $num);
- $two = in_array(2, $num);
- if ($straight && $flush && $num[0] && $num[12]) {return S_ROYAL_FLUSH;}
- if ($straight && $flush) {return S_STRAIGHT_FLUSH;}
- if ($poker) {return S_POKER;}
- if ($three && $two) {return S_FULL_HOUSE;}
- if ($flush) {return S_FLUSH;}
- if ($straight) {return S_STRAIGHT;}
- if ($three) {return S_THREE_OF_A_KIND;}
- if (alternated($num, 2,2)) {return S_TWO_PAIR;}
- if ($two) {return S_ONE_PAIR;}
- else {return S_NOTHING;}
- }
- function continuous($num){
- $num[13] = $num[0];
- for($i = $c = 0; $i < 14; $i++){
- if ($num[$i]){
- $c++; // Every time a value is present we increase the C. when it reaches 5 we declare it good
- if ($c == 5) return true;
- } else {
- $c = 0;
- }
- }
- return false;
- }
- // Find $val at least $times times in a deck
- function alternated($num, $val, $times){
- for($i = $c = 0; $i < 13; $i++){
- if ($num[$i] == $val){
- $c++;
- if ($c == $times) return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement