Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-01-2008, 07:51 PM Deck of cards class
dlaroche22's Avatar
Skilled Talker

Posts: 84
Name: Dustin Laroche
Trades: 0
I'm writting a class to represent a deck of cards, however, I have ran into an error and don't know how to get around it. The class file Deck.php code is as follows:

PHP Code:
<?php
class Deck {
 
// Members
 
private $shuffledDeck;
 private 
$deck;
 private 
$cardsLeft 0;
 private 
$suites = array( 
     
0=> 'Hearts'
     
1=> 'Diamonds'
     
2=> 'Spades'
     
3=> 'Clubs' );
 
 private 
$cardValues = array( 
     
0=> 'Ace'
     
1=> 1
     
2=> 2
     
3=> 3
     
4=> 4
     
5=> 5
     
6=> 6
     
7=> 7
     
8=> 8
     
9=> 9
     
10=> 10
     
11=> 'Jack'
     
12=> 'Queen'
     
13=> 'King' );
 
 
// Methods 
 
public function getShuffledDeck() {
  return 
$shuffledDeck;
 }
 
 public function 
shuffleDeck() {
 
 }
 
 public function 
createDeck() {
 
  for( 
$i 0$count 52$i++ ) {
   
$deck$i ][ ] = $cardValues$i ];
   
$deck$i ][ ] = $suites$i ];
  }
 
 
 }
 
 
}
?>
I call the class from another php file called tester.php and that code is as follows:

PHP Code:
<?php
require_once( "Deck.php" );
$deck = new Deck();
$deck->createDeck();
?>
Now the problem that I am running in to is the fatal error that I am getting when I run tester.php. The error is "Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40 bytes) in C:\xampp\htdocs\testscripts\poker\Deck.php on line 43". Could someone please explain or point me in the right direction to resolve this error. Thanks in advance.
__________________
Under Construction, But A Work In Progress
Webhosting isn't cheap, sponsors are important

Please login or register to view this content. Registration is FREE
dlaroche22 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-01-2008, 08:54 PM Re: Deck of cards class
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I'm not sure where you think you're going, but your code is jacked up all over the place.

First, private variables must be accessed via $this-> , not just calling them.
Second, "suits" is spelled wrong (not a code problem, but worth knowing).
Third, Ace & 1 are the same card.
Fourth, whatever you think createDeck is doing, you're wrong. Take a closer look at that routine.
Fifth, you can't just count from 1 to 52 and expect $this->suits[$i] or $this->cardValues[$i] to have any value.

I have no idea where your memory error is coming from, but I doubt it's the code you've shown us here. What you're seeing is a symptom of another problem. The symptom shows itself here, but the problem is that something is using 33mb of memory -- that thing is likely elsewhere in your script, so you'll need to do some tracking there to figure it out.

General advice: Don't just sit down and code. Take the time to model things out on paper first. Sketch a few examples. Then, when you do sit down to code you'll have something more than a general idea of where you're going and what needs to be done to get there. createDeck would never have been written the way you have if you had first tried to figure out how to create all 52 cards given 13 numbers and 4 suits by hand.

OOPS. Meant to add this in to help you get started, but createDeck is left for you as an exercise.

PHP Code:
<?php
class Deck {
 
// Members
 
private $shuffledDeck;
 private 
$deck;
 private 
$cardsLeft 0;
 private 
$suits = array( 
     
0=> 'Hearts'
     
1=> 'Diamonds'
     
2=> 'Spades'
     
3=> 'Clubs' );
 
 private 
$cardValues = array( 
     
0=> 'Ace'
     
1=> 1
     
2=> 2
     
3=> 3
     
4=> 4
     
5=> 5
     
6=> 6
     
7=> 7
     
8=> 8
     
9=> 9
     
10=> 10
     
11=> 'Jack'
     
12=> 'Queen'
     
13=> 'King' );
 
 
// Methods 
 
public function getShuffledDeck() {
  return 
$this->shuffledDeck;
 }
 public function 
shuffleDeck() {
 
 }
 public function 
createDeck() {
  for( 
$i 0$count 52$i++ ) {
   
$this->deck$i ][ ] = $this->cardValues$i%13 ];
   
$this->deck$i ][ ] = $this->suits$i%];
  }
 }
}
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-01-2008, 10:24 PM Re: Deck of cards class
dlaroche22's Avatar
Skilled Talker

Posts: 84
Name: Dustin Laroche
Trades: 0
Thanks for the help, it is greatly appreciated. I am new in OOP with PHP so this is an exercise for me to help me learn. I noticed that there isn't too many websites that give tutorials about planning out OOP. If you know any that I could look into that would also be greatly appreciated.
__________________
Under Construction, But A Work In Progress
Webhosting isn't cheap, sponsors are important

Please login or register to view this content. Registration is FREE
dlaroche22 is offline
Reply With Quote
View Public Profile
 
Old 08-01-2008, 10:37 PM Re: Deck of cards class
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I don't know about any books. I learned OOP in college. Modeling out your program's logic, though, is very important.

Best wishes!
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Deck of cards class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49349 seconds with 12 queries