52 Playing Card Deck Random Hand Draw (7-Card) [With Images]
This is a pretty straight forward script with images, you refresh the page or click the red face-down card to get a new hand. Pulls 7 Random cards from the deck, Clubs, Diamonds, Hearts, or Spades for the Suit and Ace, 1, 2, 3, 4, .. 9, 10, Jack, Queen, King for the number. This is a basic script using mainly PHP with some HTMl for tables, and Javascript for refreshing the page. This can be used as is, or someone can take it further possibly by making it multiplayer, and or more interactive to bring people together in a game of cards or something. Version 1.0, this took me about 25 minutes to complete, I'm going to be working on adding individual card re-draws and things like that in the future. Below is a picture along with download link(s).
This is my first release and I'd much appreciate any feedback you have, i'm somewhat new to PHP and this is the first thing i've release so let me know your thoughts! Thanks everyone.
Credits:
95% ~ Me / Skill3d
- Doing the coding and putting the script together.
5% ~ Jesse Fuchs and Tom Hart
- Card Images that are included in the package from This Website
You don't appear to have anything there that prevents the same card being selec ted more than once.
An easier way might be to set up an array of values representing all 52 cards then use the shuffle function to arrange them in random order then just return the first seven entries in the array.
NOTE: When contacting me please have the following things ready:
Thread ID (This can be found in the URL of your thread)
Budget of Project
Methods of contacting you/your organization
Preferred method if many methods are provided
Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.
NOTE: When contacting me please have the following things ready:
Thread ID (This can be found in the URL of your thread)
Budget of Project
Methods of contacting you/your organization
Preferred method if many methods are provided
Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.
NOTE: When contacting me please have the following things ready:
Thread ID (This can be found in the URL of your thread)
Budget of Project
Methods of contacting you/your organization
Preferred method if many methods are provided
Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.
displays hand in "text" view below, as well as the previous 4 hands
Stores hand in old_hands.php
deletes entries after line 46 and keeps ONLY 5 entries in old_hands.php
All the code is commented out for you to read. If you have any questions let me know via PM as i will NOT be subscribing to this page and will probably be the last time i ever come to this page.
Make sure if you do NOT download my zip file, that you create a new file called old_hands.php and leave it BLANK! dont even open it with a text editor! lol
Please donate to me if you use this script! it did take some time to make! Help a fellow coder out!
Thanks!
index.php
PHP Code:
<?php
//images are in format of:
//#x.gif
//Where # is card value
//Where "x" is card suit
//clubs = c
//diamonds = d
//hearts = h
//spades = s
//Select 7 cards with the for loop.
for( $i = 1; $i <= 7; $i++ ) {
//select card, define suit as numeric value
$card["card" . $i]['suit_num'] = rand(0,3);
//select card, define card itself as numeric value
$card["card" . $i]['card_num'] = rand(1,13);
//redefine card's suit as character value assigning either c, d, h, or s
//This is used for image filepath
switch ($card["card" . $i]['suit_num']) {
case 0:
$card["card" . $i]['suit_char'] = "c";
$card["card" . $i]['suit_str'] = "Clubs";
break;
case 1:
$card["card" . $i]['suit_char'] = "d";
$card["card" . $i]['suit_str'] = "Diamonds";
break;
case 2:
$card["card" . $i]['suit_char'] = "h";
$card["card" . $i]['suit_str'] = "Hearts";
break;
case 3:
$card["card" . $i]['suit_char'] = "s";
$card["card" . $i]['suit_str'] = "Spades";
break;
}
//define card filepath and store in "card_img"
$card["card" . $i]['card_img'] = "cards/" . $card["card" . $i]['card_num'] . $card["card" . $i]['suit_char'] . ".gif";
//Define the Card Itself in the form of words.
switch($card["card" . $i]['card_num']) {
case 1:
$card["card" . $i]['card_str'] = "Ace of ";
break;
case 2:
$card["card" . $i]['card_str'] = "Two of ";
break;
case 3:
$card["card" . $i]['card_str'] = "Three of ";
break;
case 4:
$card["card" . $i]['card_str'] = "Four of ";
break;
case 5:
$card["card" . $i]['card_str'] = "Five of ";
break;
case 6:
$card["card" . $i]['card_str'] = "Six of ";
break;
case 7:
$card["card" . $i]['card_str'] = "Seven of ";
break;
case 8:
$card["card" . $i]['card_str'] = "Eight of ";
break;
case 9:
$card["card" . $i]['card_str'] = "Nine of ";
break;
case 10:
$card["card" . $i]['card_str'] = "Ten of ";
break;
case 11:
$card["card" . $i]['card_str'] = "Jack of ";
break;
case 12:
$card["card" . $i]['card_str'] = "Queen of ";
break;
case 13:
$card["card" . $i]['card_str'] = "King of ";
break;
}
//Combine card_str and suit_str into one for the table headers
$card["card" . $i]['card_info'] = $card["card" . $i]['card_str'] . $card["card" . $i]['suit_str'];
}
//save old hand to file
//Open File
$myFile = "old_hands.php";
$lines = file($myFile);
$fh = fopen($myFile, 'w+') or die("can't open file");
//add changes to new variable
$changes = "";
for($i = 1; $i <= 7; $i++) {
$changes .= "Card " . $i . ": " . $card["card" . $i]['card_info'] . "<br>\n";
}
$changes = $changes . "<br>\n<br>\n";
//write changes to new file
fwrite($fh, $changes);
foreach ($lines as $line) { fwrite( $fh, "$line"); }
//define from what line ONWARD/DOWN to delete
$lineNum = 46;
//Delete Lines Function
function delLineFromFile($fileName, $lineNum){
//Delete 10 lines (One entry) 1 line at a time
for($i = 1; $i <= 10; $i++) {
// read the file into an array
$arr = file($fileName);
// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;
//remove the line
unset($arr["$lineToDelete"]);
// open the file for reading
$fp = fopen($fileName, 'w+');
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line); }
}
}
//we dont want any more then 5 entries to delet the last/6th entry
delLineFromFile($myFile, $lineNum);
//Only display lines you specify function
function sumarize($your_string, $limit_lines){
$count = 0;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $limit_lines) break;
}
}
?>
<html>
<head>
<title>
Coding Forums Post 266395: Card Generator
</title>
<link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
<input type="submit" value="Pick a New Hand!" onclick="javascript:location.reload(true)" />
<br>
<br>
<table id="card_table">
<tr>
<?php
//display card information in table headers
for($i = 1; $i <= 7; $i++ ) {
echo("<th>" . $card["card" . $i]['card_info'] . "</th>");
}
?>
</tr>
<tr>
<?php
//display cards
for($i = 1; $i <= 7; $i++ ) {
echo("<td class=\"card_img\"><img src=\"" . $card["card" . $i]['card_img'] . "\" /></td>");
}
?>
</tr>
<tr>
<td colspan="7" id="old_hands">
<span class="header">All hands displayed below are in order from most recent to less recent.</span>
<br>
<br>
<?php
//Only display lines 1-43 as we dont want to display the last two lines as they are <br> tags
echo(sumarize(file_get_contents($myFile), 43));
?>
</td>
</tr>
</table>
</body>
</html>
<?php
fclose($fh);
?>
NOTE: When contacting me please have the following things ready:
Thread ID (This can be found in the URL of your thread)
Budget of Project
Methods of contacting you/your organization
Preferred method if many methods are provided
Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.