Jump to content

[SOLVED] Blackjack / 21


DeanWhitehouse

Recommended Posts

Hello

First off my code

<?php
//Blackjack.php
ob_start();
session_start();

$card_10 = array(11,12,13);
$card_type = array("c","d","h","s");

if(isset($_GET['reset']))
{
for($i = 1;$i <= $_SESSION['player_count'];$i++)
{
	unset($_SESSION['player_'.$i]);
}

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
	unset($_SESSION['comp_'.$i]);
}

unset($_SESSION['comp_count']);
unset($_SESSION['player_count']);

header("Location:test.php");
}

if(!isset($_SESSION['player_1']) || !isset($_SESSION['comp_1']))
{
$_SESSION['player_1'] = rand(1,10);
$_SESSION['player_2'] = rand(1,10);	
$_SESSION['comp_1'] = rand(1,10);
$_SESSION['comp_2'] = rand(1,10);
$_SESSION['comp_count'] = 2;
$_SESSION['player_count'] = 2;
}

echo "<br>Player<br>";

for($i = 1;$i <= $_SESSION['player_count'];$i++)
{
if($_SESSION['player_'.$i] == 10)
{
	shuffle($card_10);
	$img = $card_10[rand(0,2)];
}
else
	$img = $_SESSION['player_'.$i];

shuffle($card_type);

?>
<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$card_type[rand(0,3)]?>.gif">
<?php
$cards[] = $_SESSION['player_'.$i];
}

echo "Total: ".array_sum($cards);

echo "<br>Computer<br>";

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
$comp[] = $_SESSION['comp_'.$i];
}


if(isset($_GET['hit']))
{
if(array_sum($comp) < 17)
{
	$_SESSION['comp_count'] += 1;
	$_SESSION['comp_'.$_SESSION['comp_count']] = rand(1,10);
}
$_SESSION['player_count'] += 1;
$_SESSION['player_'.$_SESSION['player_count']] = rand(1,10);
header("Location:test.php");
}

if(isset($_GET['stick']))
{
if(array_sum($comp) < 17)
{
	$_SESSION['comp_count'] += 1;
	$_SESSION['comp_'.$_SESSION['comp_count']] = rand(1,10);
	header("Location:test.php?stick");		
}

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
	if($_SESSION['comp_'.$i] == 10)
	{
		shuffle($card_10);
		$img = $card_10[rand(0,2)];
	}
	else
		$img = $_SESSION['comp_'.$i];

	shuffle($card_type);

	?>
	<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$card_type[rand(0,3)]?>.gif">
	<?php
}	

if(array_sum($cards) == array_sum($comp))
	echo "<br>Draw";
elseif(array_sum($cards) == 21 || array_sum($comp) > 21 || array_sum($cards) > array_sum($comp) && array_sum($comp) < 22 && array_sum($comp) != 21 && array_sum($cards) != 21 && array_sum($cards) < 22)
	echo "<br>You win";
else
	echo "<br>You loose";	
}
else
{	
echo "<br>";

if(array_sum($cards) == 21 || array_sum($comp) > 21 || in_array(1,$cards) && in_array(10,$cards) && array_sum($cards) < 22)
{
	for($i = 1;$i <= $_SESSION['comp_count'];$i++)
	{
		if($_SESSION['comp_'.$i] == 10)
		{
			shuffle($card_10);
			$img = $card_10[rand(0,2)];
		}
		else
			$img = $_SESSION['comp_'.$i];

		shuffle($card_type);

		?>
		<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$card_type[rand(0,3)]?>.gif">
		<?php
	}
	echo "<br>You Win";			
}
elseif(array_sum($cards) > 21 || array_sum($comp) == 21 || in_array(1,$comp) && in_array(10,$comp) && array_sum($comp) < 22)
{
	for($i = 1;$i <= $_SESSION['comp_count'];$i++)
	{
		if($_SESSION['comp_'.$i] == 10)
		{
			shuffle($card_10);
			$img = $card_10[rand(0,2)];
		}
		else
			$img = $_SESSION['comp_'.$i];

		shuffle($card_type);

		?>
		<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$card_type[rand(0,3)]?>.gif">
		<?php
	}	

	echo "<br>You Loose";
}
else
{
	for($i = 1;$i <= $_SESSION['comp_count'];$i++)
	{
		if($i == 1)
		{
			if($_SESSION['comp_'.$i] == 10)
			{
				shuffle($card_10);
				$img = $card_10[rand(0,2)];
			}
			else
				$img = $_SESSION['comp_'.$i];

			shuffle($card_type);

			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$card_type[rand(0,3)]?>.gif">
			<?php
		}
		else
		{
			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/cardback.gif">
			<?php		
		}
	}
}
}
?>
<br>
<a href="?hit">Hit</a>
<a href="?reset">Reset</a>
<a href="?stick">Stick</a><br>

 

Ok, to explain the code will basically play against you when playing 21/blackjack.

 

This all works fine, the problem i am having is that the card type (e.g. Diamonds) constantly changes, when you hit stick or hit, therefore the cards you have could all be hearts then change to diamonds.

 

I don't have any idea how i could avoid this, anyone?

 

Second off when the player or computer's card is a ten we have it show a king, queen or jack. This ,like above , changes constantly so one turn you could have a king then when you hit it could change to a queen.

Again any ideas?

 

If you wish to see what i mean here it is

 

http://gangster.game-design.djw-designs.com/test.php

 

Thanks,

Blade

Link to comment
Share on other sites

i believe that everytime you use:

 

shuffle($card_type);

 

it changes the keys of the array, so that you no longer have what you started with...

 

<?php 
$card_type[0] = "c";
$card_type[1] = "d";
$card_type[2] = "h";
$card_type[3] = "s";

changes to:

$card_type[3] = "c";
$card_type[2] = "d";
$card_type[1] = "h";
$card_type[0] = "s";
// (randomizes, so the key gives a diff value every time)

 

 

 

Link to comment
Share on other sites

what about...

<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$card_type[rand(0,3)]?>.gif">

 

is that snippet, saying choose a random image type? eg; pick a random type (CLUBS, HEARTS, DIAMONDS, or SPADES) and "show it".... hence you have different card type every time it refreshes ?

 

 

Link to comment
Share on other sites

<?php
//Blackjack.php
ob_start();
session_start();

$card_10 = array(10,11,12,13);
$card_type = array("c","d","h","s");

shuffle($card_type);

shuffle($card_10);
if(isset($_GET['reset']))
{
for($i = 1;$i <= $_SESSION['player_count'];$i++)
{
	unset($_SESSION['player_'.$i]);
	unset($_SESSION['card_p_'.$i]);
}

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
	unset($_SESSION['comp_'.$i]);
	unset($_SESSION['card_c_'.$i]);
}

unset($_SESSION['comp_count']);
unset($_SESSION['player_count']);

header("Location:test.php");
}

if(!isset($_SESSION['player_1']) || !isset($_SESSION['comp_1']))
{
$_SESSION['player_1'] = rand(1,10);
$_SESSION['card_p_1'] = $card_type[rand(0,3)];
$_SESSION['player_2'] = rand(1,10);	
$_SESSION['card_p_2'] = $card_type[rand(0,3)];

$_SESSION['comp_1'] = rand(1,10);
$_SESSION['card_c_1'] = $card_type[rand(0,3)];
$_SESSION['comp_2'] = rand(1,10);
$_SESSION['card_c_2'] = $card_type[rand(0,3)];

$_SESSION['comp_count'] = 2;
$_SESSION['player_count'] = 2;
}

echo "<br>Player<br>";

for($i = 1;$i <= $_SESSION['player_count'];$i++)
{
if($_SESSION['player_'.$i] == 10)
{
	$img = $card_10[rand(0,2)];
}
else
	$img = $_SESSION['player_'.$i];

	$type = $_SESSION['card_p_'.$i];
?>
<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
<?php
$cards[] = $_SESSION['player_'.$i];
}

echo "Total: ".array_sum($cards);

echo "<br>Computer<br>";

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
$comp[] = $_SESSION['comp_'.$i];
}


if(isset($_GET['hit']))
{
if(array_sum($comp) < 17)
{
	$_SESSION['comp_count'] += 1;
	$_SESSION['comp_'.$_SESSION['comp_count']] = rand(1,10);
	$_SESSION['card_c_'.$_SESSION['comp_count']] = $card_type[rand(0,3)];
}
$_SESSION['player_count'] += 1;
$_SESSION['player_'.$_SESSION['player_count']] = rand(1,10);
$_SESSION['card_p_'.$_SESSION['player_count']] = $card_type[rand(0,3)];
header("Location:test.php");
}

if(isset($_GET['stick']))
{
if(array_sum($comp) < 17)
{
	$_SESSION['comp_count'] += 1;
	$_SESSION['comp_'.$_SESSION['comp_count']] = rand(1,10);
	$_SESSION['card_c_'.$_SESSION['comp_count']] = $card_type[rand(0,3)];
	header("Location:test.php?stick");		
}

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
	if($_SESSION['comp_'.$i] == 10)
	{
		$img = $card_10[rand(0,2)];
	}
	else
		$img = $_SESSION['comp_'.$i];

	$type = $_SESSION['card_c_'.$i];


	?>
	<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
	<?php
}	

if(array_sum($cards) == array_sum($comp))
	echo "<br>Draw";
elseif(array_sum($cards) == 21 || array_sum($comp) > 21 || array_sum($cards) > array_sum($comp) && array_sum($comp) < 22 && array_sum($comp) != 21 && array_sum($cards) != 21 && array_sum($cards) < 22)
	echo "<br>You win";
else
	echo "<br>You loose";	
}
else
{	
echo "<br>";

if(array_sum($cards) == 21 || array_sum($comp) > 21 || in_array(1,$cards) && in_array(10,$cards) && array_sum($cards) < 22)
{
	for($i = 1;$i <= $_SESSION['comp_count'];$i++)
	{
		if($_SESSION['comp_'.$i] == 10)
		{
			$img = $card_10[rand(0,2)];
		}
		else
			$img = $_SESSION['comp_'.$i];

		$type = $_SESSION['card_c_'.$i];

		?>
		<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
		<?php
	}
	echo "<br>You Win";			
}
elseif(array_sum($cards) > 21 || array_sum($comp) == 21 || in_array(1,$comp) && in_array(10,$comp) && array_sum($comp) < 22)
{
	for($i = 1;$i <= $_SESSION['comp_count'];$i++)
	{
		if($_SESSION['comp_'.$i] == 10)
		{
			$img = $card_10[rand(0,2)];
		}
		else
			$img = $_SESSION['comp_'.$i];

		$type = $_SESSION['card_c_'.$i];

		?>
		<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
		<?php
	}	

	echo "<br>You Loose";
}
else
{
	for($i = 1;$i <= $_SESSION['comp_count'];$i++)
	{
		if($i == 1)
		{
			if($_SESSION['comp_'.$i] == 10)
			{
				$img = $card_10[rand(0,2)];
			}
			else
				$img = $_SESSION['comp_'.$i];

			$type = $_SESSION['card_c_'.$i];

			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
			<?php
		}
		else
		{
			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/cardback.gif">
			<?php		
		}
	}
}
}
?>
<br>
<a href="?hit">Hit</a>
<a href="?reset">Reset</a>
<a href="?stick">Stick</a><br>

 

Now saves the card suite, but not what 10 is worth (either 10, king, queen or jack).

 

Any ideas?

Link to comment
Share on other sites

Sorry to keep confusing people, but same problem , updated code

<?php
//Blackjack.php
ob_start();
session_start();

$card_10 = array(10,11,12,13);
$card_type = array("c","d","h","s");

shuffle($card_type);

shuffle($card_10);

if(!isset($_SESSION['done']))
{
//this is where the winnings will be done
}

if(!isset($_SESSION['player_1']) || !isset($_SESSION['comp_1']))
{
$_SESSION['player_1'] = rand(1,10);
$_SESSION['card_p_1'] = $card_type[rand(0,3)];
$_SESSION['player_2'] = rand(1,10);	
$_SESSION['card_p_2'] = $card_type[rand(0,3)];

$_SESSION['comp_1'] = rand(1,10);
$_SESSION['card_c_1'] = $card_type[rand(0,3)];
$_SESSION['comp_2'] = rand(1,10);
$_SESSION['card_c_2'] = $card_type[rand(0,3)];

$_SESSION['comp_count'] = 2;
$_SESSION['player_count'] = 2;
}


if(isset($_GET['reset']))
{
unset($_SESSION['done']);
if(isset($_SESSION['player_count']))
{
	for($i = 1;$i <= $_SESSION['player_count'];$i++)
	{
		unset($_SESSION['player_'.$i]);
		unset($_SESSION['card_p_'.$i]);
	}
	unset($_SESSION['player_count']);
}

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
	unset($_SESSION['comp_'.$i]);
	unset($_SESSION['card_c_'.$i]);
}

unset($_SESSION['comp_count']);

header("Location:test.php");
}

echo "<br>Player<br>";

for($i = 1;$i <= $_SESSION['player_count'];$i++)
{
	if($_SESSION['player_'.$i] == 10)
	{
		$img = $card_10[rand(0,2)];
	}
	else
		$img = $_SESSION['player_'.$i];

		$type = $_SESSION['card_p_'.$i];
	?>
	<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
	<?php
	$cards[] = $_SESSION['player_'.$i];
}

echo "Total: ".array_sum($cards);

echo "<br>Computer<br>";

for($i = 1;$i <= $_SESSION['comp_count'];$i++)
{
	$comp[] = $_SESSION['comp_'.$i];
}


if(isset($_GET['hit']) && !isset($_SESSION['done']))
{
	if(array_sum($comp) < 17)
	{
		$_SESSION['comp_count'] += 1;
		$_SESSION['comp_'.$_SESSION['comp_count']] = rand(1,10);
		$_SESSION['card_c_'.$_SESSION['comp_count']] = $card_type[rand(0,3)];
	}
	$_SESSION['player_count'] += 1;
	$_SESSION['player_'.$_SESSION['player_count']] = rand(1,10);
	$_SESSION['card_p_'.$_SESSION['player_count']] = $card_type[rand(0,3)];
	header("Location:test.php");
}

if(isset($_GET['stick']))
{
		if(array_sum($comp) < 17)
		{
			$_SESSION['comp_count'] += 1;
			$_SESSION['comp_'.$_SESSION['comp_count']] = rand(1,10);
			$_SESSION['card_c_'.$_SESSION['comp_count']] = $card_type[rand(0,3)];
			header("Location:test.php?stick");		
		}
		for($i = 1;$i <= $_SESSION['comp_count'];$i++)
		{
			if($_SESSION['comp_'.$i] == 10)
			{
				$img = $card_10[rand(0,2)];
			}
			else
				$img = $_SESSION['comp_'.$i];

			$type = $_SESSION['card_c_'.$i];


			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
			<?php
		}	

	if(array_sum($cards) == array_sum($comp))
		echo "<br>Draw";
	elseif(array_sum($cards) == 21 || array_sum($comp) > 21 || array_sum($cards) > array_sum($comp) && array_sum($comp) < 22 && array_sum($comp) != 21 && array_sum($cards) != 21 && array_sum($cards) < 22)
	{
		echo "You Win";
		$_SESSION['done'] = true;
	}
	else
	{
		echo "You Loose";
		$_SESSION['done'] = true;
	}
}
else
{	
	echo "<br>";

	if(array_sum($cards) == 21 || array_sum($comp) > 21 || in_array(1,$cards) && in_array(10,$cards) && array_sum($cards) == 11 && array_sum($cards) < 22)
	{
		for($i = 1;$i <= $_SESSION['comp_count'];$i++)
		{
			if($_SESSION['comp_'.$i] == 10)
			{
				$img = $card_10[rand(0,2)];
			}
			else
				$img = $_SESSION['comp_'.$i];

			$type = $_SESSION['card_c_'.$i];

			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
			<?php
		}
		echo "You Win";
		$_SESSION['done'] = true;
	}
	elseif(array_sum($cards) > 21 || array_sum($comp) == 21 || in_array(1,$comp) && in_array(10,$comp) && array_sum($comp) < 22 && array_sum($comp) == 11)
	{
		for($i = 1;$i <= $_SESSION['comp_count'];$i++)
		{
			if($_SESSION['comp_'.$i] == 10)
			{
				$img = $card_10[rand(0,2)];
			}
			else
				$img = $_SESSION['comp_'.$i];

			$type = $_SESSION['card_c_'.$i];

			?>
			<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
			<?php
		}	
		$_SESSION['done'] = true;
		echo "You Loose";
	}
	else
	{
		for($i = 1;$i <= $_SESSION['comp_count'];$i++)
		{
			if($i == 1)
			{
				if($_SESSION['comp_'.$i] == 10)
				{
					$img = $card_10[rand(0,2)];
				}
				else
					$img = $_SESSION['comp_'.$i];

				$type = $_SESSION['card_c_'.$i];

				?>
				<img src="http://gangster.game-design.djw-designs.com/images/Cards/<?=$img."-".$type?>.gif">
				<?php
			}
			else
			{
				?>
				<img src="http://gangster.game-design.djw-designs.com/images/Cards/cardback.gif">
				<?php		
			}
		}
	}
}
?>
<br>
<?php	
if(!isset($_SESSION['done']))
{
?>
<a href="?hit">Hit</a>
<a href="?stick">Stick</a>
<?php
}
?>
<a href="?reset">Reset</a>

Link to comment
Share on other sites

This is how I would do it. It is only made for 1 player at the moment, but I'm sure you will see what I did :).

 

<?php

session_start();

// 0 - Clubs
// 1 - Diamonds
// 2 - Hearts
// 3 - Spades

function shuffleCards()
{

	$cardTypes[0] = "";
	$cardTypes[1] = "";
	$cardTypes[2] = "";
	$cardTypes[3] = "";

	for( $n = 0; $n < 4; $n++ )
	{
		$strCards = "1,2,3,4,5,6,7,8,9,10,11,12,13";
		$cardTypes[ $n ] = explode( ",", $strCards );

		shuffle( $cardTypes[ $n ] );			
	}

	return $cardTypes;

}

function removeCard( $deck, $card )
{		
	if( $card != count( $deck ) )
	{
		for( $i = $card; $i < count( $deck ); $i++ )
		{
			$nextNum = $i + 1;
			$deck[ $i ] = $deck[ $i + 1 ];
		}
	}

	unset( $deck[ count( $deck ) - 1 ] );

	return $deck;
}

function getDeck( $deck )
{
	switch( $deck )
	{
		case 0:
			return "Clubs";
			break;
		case 1:
			return "Diamonds";
			break;
		case 2:
			return "Hearts";
			break;
		case 3:
			return "Spades";
			break;
	}
}

function getCardName( $card )
{
	if ( $card == 1 )
		return "Ace";
	elseif ( $card == 11 )
		return "Jack";
	elseif ( $card == 12 )
		return "Queen";
	elseif ( $card == 13 ) 
		return "King";
	else 
	return $card;
}

if( !isset( $_SESSION[ 'deck' ] ) )	
$arrCards = shuffleCards();
else
$arrCards = $_SESSION[ 'deck' ];

echo "<h1>Player</h1>";

$arrPlayer[ 0 ] = $_SESSION[ 'player1' ];

if( !isset( $arrPlayer[ 0 ] ) )
{

for( $i = 0; $i < 2; $i++ )
{

	$deck = rand( 0, 3 );
	$card = rand( 0, count( $arrCards[ $deck ] ) - 1 );

	$num = count( $arrPlayer[ 0 ] );

	$arrPlayer[ 0 ][ $num ][ "deck" ] = $deck;
	$arrPlayer[ 0 ][ $num ][ "card" ] = $arrCards[ $deck ][ $card ];

	$arrCards[ $deck ] = removeCard( $arrCards[ $deck ], $card );

}

}

for( $i = 0; $i < count( $arrPlayer[ 0 ] ); $i++ )
{
echo $i+1 . "<br />";
echo "Deck: " . getDeck( $arrPlayer[ 0 ][ $i ][ "deck" ] ) . "<br />" ;
echo "Card: " . getCardName( $arrPlayer[ 0 ][ $i ][ "card" ] ) . "<br />";
}

$_SESSION[ 'player1' ] = $arrPlayer[ 0 ];
$_SESSION[ 'deck' ] = $arrCards;
?>

 

Basically, you shuffle the cards only if the session isn't set and it also removes the card meaning it cannot be used again.

 

Take a look, use and abuse it, but I'm sure this will set you in the right direction. I'm probably going to continue to write this myself, so if you get stuck, I'll have a working version soon :).

 

P.S. I changed how I was coding it half way through, so it might look a bit different in places... Tiredness sucks! :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.