Scripts

Card Class

Voor de liefhebbers een kleine card class scriptje. Ik heb zelf ook het script gebruikt om een soort van blackjack te maken. Als er vraag naar is wil ik die ook wel posten ;-) ps, mijn Engels is niet erg best Op en aanmerkingen zijn welkom. Script + kaarten http://dzhome.freehostia.com/ en dan download kaarten komen van http://www.jfitz.com/cards/

card-class
<?php
############################################################################
#		CardClass by Paskov 												#
#						2009												#
#																			#
#		Added cards coming from: http://www.jfitz.com/cards/				#
#																			#
#			Function discription											#
#																			#
#	cards =  All cards in a array											#
#		- Joker		(true/false)/ Turns Joker ON							#
#		- Oldcards	(true/false)/ 											#
#																			#
#	convert = flip the values from cards									#
#		- sort  (false,img/sort)											#
#									/convert to the value that is givin up.	#
#									or by false the reverse.				#
#		- name		 (name)			/name of the card						#									
#																			#									
#	cardname = Gives the full name of the card								#
#  		- name 		 (name)								     				#
#																			#
#	pickcard = Pick a (random) card											#
#		- cardname 	(name)		/if you want a specific	card				#
#		- sort		(img/false) /If you want to return the other			#
#								 value	of your card						#									
#		- joker		(true/false)/Joker 										#
#############################################################################

if(!session_start()){
echo "Sessions are required!";
exit;
}
class cards
{
	//Look if the Session for the used cards is there
	function __construct(){
			if(!empty($_SESSION['oldcards']) OR !isset($_SESSION['oldcards'])){
				$_SESSION['oldcards'] = array();
			}
		// The directory of your cards and end with the a '/'  !
		$this->dirname = 'cards/';
	}
	// The cards
	
	function cards ($cardname = false, $joker = true,$oldcards = false){
		$cards = 
		
		//K = Klaver
		//S = Schoppen
		//H = Harten
		//R = Ruiten
		//J = Joker
		// The cards
			array(
			'k.1' => $this->dirname.'1.png'	,'s.1' => $this->dirname.'2.png',  'h.1' => $this->dirname.'3.png' ,'r.1' => $this->dirname.'4.png', 
			'k.k' => $this->dirname.'5.png'	,'s.k' => $this->dirname.'6.png',  'h.k' => $this->dirname.'7.png' ,'r.k' => $this->dirname.'8.png', 
			'k.q' => $this->dirname.'9.png'	,'s.q' => $this->dirname.'10.png', 'h.q' => $this->dirname.'11.png','r.q' => $this->dirname.'12.png', 
			'k.j' => $this->dirname.'13.png','s.j' => $this->dirname.'14.png', 'h.j' => $this->dirname.'15.png','r.j' => $this->dirname.'16.png', 
			'k.10' =>$this->dirname.'17.png','s.10' =>$this->dirname.'18.png', 'h.10' =>$this->dirname.'19.png','r.10' => $this->dirname.'20.png', 
			'k.9' => $this->dirname.'21.png','s.9' => $this->dirname.'22.png', 'h.9' => $this->dirname.'23.png','r.9' => $this->dirname.'24.png', 
			'k.8' => $this->dirname.'25.png','s.8' => $this->dirname.'26.png', 'h.8' => $this->dirname.'27.png','r.8' => $this->dirname.'28.png', 
			'k.7' => $this->dirname.'29.png','s.7' => $this->dirname.'30.png', 'h.7' => $this->dirname.'31.png','r.7' => $this->dirname.'32.png', 
			'k.6' => $this->dirname.'33.png','s.6' => $this->dirname.'34.png', 'h.6' => $this->dirname.'35.png','r.6' => $this->dirname.'36.png',
			'k.5' => $this->dirname.'37.png','s.5' => $this->dirname.'38.png', 'h.5' => $this->dirname.'38.png','r.5' => $this->dirname.'40.png',
			'k.4' => $this->dirname.'41.png','s.4' => $this->dirname.'42.png', 'h.4' => $this->dirname.'43.png','r.4' => $this->dirname.'44.png', 
			'k.3' => $this->dirname.'45.png','s.3' => $this->dirname.'46.png', 'h.3' => $this->dirname.'47.png','r.3' => $this->dirname.'48.png', 
			'k.2' => $this->dirname.'49.png','s.2' => $this->dirname.'50.png', 'h.2' => $this->dirname.'51.png','r.2' => $this->dirname.'52.png' 
			);
		//Jokers ON/OFF
		if($joker == true){
			$cards['j.r'] =	$this->dirname.'53.png';
			$cards['j.b'] = $this->dirname.'54.png';
		}
		//IF you want to have 1 deck, when the deck is empty it wil automatic start a new deck.
		if($oldcards == true){
			$oldcards = $_SESSION['oldcards'];
				for($i=0;$i < count($oldcards);$i++){
					// look if the array is empty
					if(!empty($oldcards[$i])){
					//look if the value is the same as in the array 
						if(isset($cards[$oldcards[$i]])){
							//Delete the used cards out of the array
							unset($cards[$oldcards[$i]]);
						}else{
							//One or more values are inconvert
							echo"<script>alert('There is an error in the cardclass, please contact the administrator');</script>";}
					}
				}
		}
		return $cards;
	}
//Convert the 
	function convert($sort,$name)
	{
	//Check if it's not empty
	if(!empty($name) AND !empty($sort)){
			//when the $sort is 'img' and the value of $name isn't then it will be converted.
				if($sort == 'img'){
					if(!file_exists($this->dirname.$name)){
					//if $name exist as file it will problably be an image
						$nam = $this->cards();
						$name = $nam[$name];
					}
				//$name will be converted if it's wrong.
				}elseif($sort == 'name'){		
					if(file_exists($name)){
						//if the value is wrong
						$cards = array_flip($this->cards());
						$name = $cards[$name];
					}else{
					//when the value is already correct
						$name = $name;
					}
				}
			return $name;
			}
		}
	
	// The function the it will return the full name of the card by insert the 'fake' name
	function cardname($name)
	{
	//If $name is empty
		if(!empty($name)){
			//Split the name for checking
			$part = explode('.',$name);
			$name = $this->convert('name',$name);
				if($part['0'] != 'j'){
						$names = array('k' =>'Klaver', 's' => 'Schoppen','h' => 'Harten', 'r' => 'Ruiten', 'j' => 'Boer', 'q' => 'Vrouw' , 'k' => 'Heer');
							if(is_numeric($part['1'])){
								$part1 = $part['1'];
							}else{
								$part1 = $names[$part['1']];
							}
					$name = $names[$part['0']].' '.$part1 ;
				}else{
					$names = array('r' => 'rood', 'b' => 'zwart');
					$name = 'Joker '.$names[$part['1']];
				}
		return $name;
		}
	}
	
	function pickcard($name = false, $sort = false, $joker = true, $deck = false){
		if($deck =! false AND $deck =! true){
			$deck = false;
		}
			// if all cards are already in use then it wil automatic make a new one
			if($deck == true){
				if(count($_SESSION['oldcards']) == 52 AND $joker == false OR  count($_SESSION['oldcards']) == 54 AND $joker == true){
					//make a new value (array) for the Session
					$_SESSION['oldcards'] = array();
				}
			}			
			if($joker){
				//pick cards with joker
				$card = array_rand($this->cards(false,true,$deck),'1');
			}else{
				//pick cards without joker
				$card = array_rand($this->cards(NULL,false,$deck),'1');
				}		
			if($name){
				$card = $this->cardname($card);
			}
			if($sort == 'img'){
				$card = $this->convert($card);
			}
		//Add the card to the session oldcards
		if($deck == true){
			$_SESSION['oldcards'][] = $card;
		}
		return $card;
	}
}
?>

Reacties

0
Nog geen reacties.