Jump to content

Recommended Posts

Hey,

I am working on a small PHP blackjack and have just converted it to OOP.

 

I have cut out some of the script and kept just the main bits that you need to see.

 

<?php
class Blackjack
{	
public $card_suite = array("c","d","h","s");
public $cards = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13);

function GenerateCard()
{
	shuffle($this->card_suite);

	$card = rand(1,10);
	if($card > 9)
		$card_face = rand(10,13);
	else
		$card_face = $card;

	return array("suite" => $this->card_suite[rand(0,3)],"value" => $card,"face" => $this->cards[$card_face]);
}

function new_game()
{
	if(isset($_SESSION['done']))
		unset($_SESSION['done']);
	if(isset($_SESSION['player']))
		unset($_SESSION['player']);
	if(isset($_SESSION['bet']))	
		unset($_SESSION['bet']);
	if(isset($_SESSION['computer']))		
		unset($_SESSION['computer']);
	if(isset($_SESSION['AI']))
		unset($_SESSION['AI']);

	$_SESSION['player'][]  = self::GenerateCard();
	$_SESSION['player'][]  = self::GenerateCard();

	$_SESSION['computer'][]  = self::GenerateCard();
	$_SESSION['computer'][]  = self::GenerateCard();	

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

function getTotal($player)
{
	foreach($player as $counting)
	{
		$current_total += $counting['value'];
	}

	foreach($player as $counting)
	{
		if($counting['value'] == 1 && $current_total < 11)
			$counting['value'] = 11;
		if($counting['value'] == 11 && $current_total > 11)
			$counting['value'] = 1;

		$new_total += $current_value;
	}
	return $new_total;
}

function PrintCards($player,$limit = 0)
{
	$i = 1;
	if($limit == 0)
		$limit = 20;
	foreach($player as $cards)
	{
		if($i <= $limit)
		{
			$img = $cards['face']."-".$cards['suite'];
			echo "<img src=\"http://gangster.game-design.djw-designs.com/images/Cards/".$img.".gif\">";
		}
		else
			echo "<img src=\"http://gangster.game-design.djw-designs.com/images/Cards/cardback.gif\">";
		$i++;
	}
}

function Hit($player = "")
{
	if(self::getTotal($_SESSION['computer']) < 17)
		$_SESSION['computer'][]  = self::GenerateCard();

	$_SESSION['player'][]  = self::GenerateCard();

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

function GetEndResult($player1,$player2,$final = false)
{
	if($final)
	{
		if($player1 == $player2)
		{
			$_SESSION['done'] = true;
			return "Drew";
		}
		elseif($player1 == 21 || $player2 > 21 || $player1 < 22 && $player1 > $player2)
		{
			$_SESSION['done'] = true;
			$this->bet *= 2;
			return "Won";
		}
		elseif($player1 > 21 || $player2 == 21 || $player2 < 22 && $player2 > $player1)
		{
			$_SESSION['done'] = true;
			$this->bet = 0;
			return "Lost";	
		}
	}
	else
	{
		if($player1 == 21 || $player2 > 21)
			return "Won";
		elseif($player2 == 21 || $player1 > 21)
			return "Lost";
		else
			return false;
	}
}

function stick($player = "")
{
	if(self::getTotal($_SESSION['computer']) < 17)
	{
		$_SESSION['computer'][]  = self::GenerateCard();
		header("Location:test.php?stick");
	}

	self::PrintCards($_SESSION['computer']);

	return self::GetEndResult(self::getTotal($_SESSION['player']),self::getTotal($_SESSION['computer']),true);
}
}

class player extends Blackjack
{
public $bet;
public $cash;
public $player_name;

function GetPlayer()
{
	$this->player_name = "Guest";
	return $this->player_name;
}

function TakeBet($bet)
{
	$this->bet = $bet;
	$this->cash = 100;
	if($this->bet <= $this->cash)
		$this->cash -= $this->bet;
}
}

class computer extends Blackjack
{
private $players = array("Paul","John","Dean","Tom","Bradly");
public $ai_name;
function GeneratePlayer()
{
	shuffle($this->players);
	$_SESSION['AI'] = $this->players[rand(0,5)];
	$this->ai_name = $_SESSION['AI'];
	return $this->ai_name;
}
}


if(isset($_POST['place_bet']))
$_SESSION['bet'] = $_POST['bet'];

if(isset($_SESSION['bet']))
{
$blackjack = new Blackjack;

if(!isset($_SESSION['player']) || !isset($_SESSION['computer']) || isset($_GET['reset']))
	$blackjack -> new_game();

$player = new player;

$player  -> bet = $_SESSION['bet'];
$player -> TakeBet($player -> bet);

echo "<br>".$player  -> GetPlayer()."<br>";

$blackjack -> PrintCards($_SESSION['player']);

echo "<br>Total: ".$blackjack -> getTotal($_SESSION['player']);

$computer = new computer;

echo "<br>".$computer -> GeneratePlayer()."<br>";

if(isset($_GET['hit']) && !isset($_SESSION['done']))
	$blackjack -> hit();
if(isset($_GET['stick']))
	echo "<br>You ".$blackjack -> stick();
else
{	
	echo "<br>";
	if($blackjack -> GetEndResult($blackjack -> getTotal($_SESSION['player']),$blackjack -> getTotal($_SESSION['computer'])) == false)
		$blackjack -> PrintCards($_SESSION['computer'],1);
	else
	{
		$blackjack -> PrintCards($_SESSION['computer']);
		echo "<br>You ".$blackjack -> GetEndResult($blackjack -> getTotal($_SESSION['player']),$blackjack -> getTotal($_SESSION['computer']));
	}
}
}
?>

 

Can anyone explain if i am using it correctly, and if not why not and how can i.

 

Thanks,

Blade

Link to comment
https://forums.phpfreaks.com/topic/138697-oop-am-i-using-it-correctly/
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.