Jump to content

Lets Play Cards.. Help figuring out what is in your hand


webdevdea
Go to solution Solved by webdevdea,

Recommended Posts

Ok so I have only one suit of cards say Ace through King, 

I am creating a game like five card draw, and the user clicks the checkbox to choose the cards that he keeps, and then you get another deal, my problem is trying to figure out what the hand is this is what I have came up with thus far, Its generating the images and everything works except the part that scores the hand.. Help please

<?php session_start() ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>poker dice</title>
<link rel = "stylesheet"
      type = "text/css"
      href = "pd.css" />
</head>

<body>

<h1>pokerDice.php</h1>

<?php


//set things up if it's the first time here, otherwise play
if (filter_has_var(INPUT_POST, "doNext")){
  play();
} else {
  startGame();
} // end if

function play(){
  //alternate control between firstPass and secondPass
  //functions based on $doNext
  $doNext = filter_input(INPUT_POST, "doNext");

  if ($doNext == "firstPass"){
    firstPass();
  } else {
    secondPass();
    evaluate();
  } // end if
} // end play

function startGame(){
  // if it's the first time here, set up initial cash,
  // and do firstPass
  $cash = 100;
  $_SESSION["cash"] = 100;
  firstPass();  
} // end reset

function firstPass(){
  $cash = $_SESSION["cash"];
  print <<< HERE
      <h2>First Pass</h2>
        <form method = "post"
        action = "">
    <fieldset>

HERE;

  for ($i = 0; $i < 5; $i++){
    $c[$i] = rand(1, 13);
    //print "$i: $c[$i] ";
    print <<< HERE

      <div class = "dieImage">
        <img src = "c$c[$i].gif"
             alt = "$i" />
        <input type = "checkbox"
               name = "keepIt[$i]"
               value = "$c[$i]" />
      </div>
      
HERE;
   
  } // end for
  print <<< HERE
      <input type = "hidden"
             name = "doNext"
             value = "secondPass" />
             
      <button type = "submit">
        go
      </button>
      <p>Cash: $cash</p>
    </fieldset>
  </form>
  
HERE;
  $_SESSION["cash"] = $cash;
} // end firstPass

function secondPass(){
  global $ccs;
  //get cash from session variable
  $cash = $_SESSION["cash"];
  //print "cash: $cash <br />";
    
  print <<< HERE
  <h2>Second Pass</h2>           
  <form method = "post"
        action = "">
    <fieldset>

HERE;
  //check to see if keepIt exists 
  // (which happens if any of the checkboxes is checked)
  if (filter_has_var(INPUT_POST, "keepIt")){
    //pull all values from form
    $formVals = filter_input_array(INPUT_POST);
    //extract $keepIt array (easiest way to handle array input)
    $keepIt = $formVals["keepIt"];
  
    for ($i = 0; $i < 5; $i++){
      //if any values are empty, replace them with zero
      if (empty($keepIt[$i])){
        $keepIt[$i] = 0;
      } // end if
      //print "$i) $keepIt[$i] <br />";
    } // end for loop
  } else {
    //keepIt doesn't exist, so make it with
    //all zero values
    $keepIt = array(0, 0, 0, 0, 0);
  } // end if 
  
       
  for ($i = 0; $i < 5; $i++){
    //replace the image if the current value
    //of keepIt is non-zero
    if ($keepIt[$i] == 0){  
      $c[$i] = rand(1, 13);
    } else {
      $c [$i] = $keepIt[$i];
    } // end if
    
    print <<< HERE
      <div class = "dieImage">
        <img src = "c$c[$i].gif"
             alt = "$i" />
      </div>
      
HERE;
   
  } // end for
  print <<< HERE
      <input type = "hidden"
             name = "doNext"
             value = "firstPass" />
      <button type = "submit">
        go
      </button>
    </fieldset>
  </form>
  
HERE;

} // end secondPass

function evaluate(){
  global $die;
  //set up payoff
  $payoff = 0;
  //create $numVals
  for($i = 1; $i <= 13; $i++){
    $numVals[$i] = 0;
  } // end for loop
  
  
  //count the dice
  for ($theVal = 1; $theVal <= 13; $theVal++){
  	for ($cNum = 0; $cNum < 13; $cNum++){
  	  if ($c[$cNum] == $theVal){
  	    $numVals[$theVal]++;
  	  } // end if
  	} // end cNum for loop
  } // end theVal for loop
  
  //print out results
  //  for ($i = 1; $i <= 6; $i++){
  //  	print "$i:  $numVals[$i]<br />\n";
  //  } // end for loop
  
  //count how many pairs, threes, fours, fives
  $numPairs = 0;
  $numThrees = 0;
  $numFours = 0;
  $numFives = 0;
  
  for ($i = 1; $i <= 13; $i++){
    switch ($numVals[$i]){
      case 2:
        $numPairs++;
        break;
      case 3:
        $numThrees++;
        break;
      case 4:
        $numFours++;
        break;
      case 5:
        $numFives++;
        break;
    } // end switch
  } // end for loop

  print "<p> \n";
  
  //check for two pairs
  if ($numPairs == 2){
    print "You have two pairs!<br />\n";
    $payoff = 1;
  } // end if
  
  //check for three of a kind and full house
  if ($numThrees == 1){
    if ($numPairs == 1){
      //three of a kind and a pair is a full house
      print "You have a full house!<br />\n";
      $payoff = 5;
    } else {
      print "You have three of a kind!<br />\n";
      $payoff = 2;
    } // end 'pair' if
  } // end 'three' if

  //check for four of a kind
  if ($numFours == 1){
    print "You have four of a kind!<br />\n";
    $payoff = 5;
  } // end if
  
  //check for five of a kind
  if ($numFives == 1){
    print "You got five of a kind!<br />\n";
    $payoff = 10;
  } // end if
  
  //check for straights
  if (($numVals[1] == 1)
    && ($numVals[2] == 1)
    && ($numVals[3] == 1)
    && ($numVals[4] == 1)
    && ($numVals[5] == 1)){
    print "You have a straight!<br />\n";
    $payoff = 10;
  } // end if

  if (($numVals[2] == 1)
    && ($numVals[3] == 1)
    && ($numVals[4] == 1)
    && ($numVals[5] == 1)
    && ($numVals[6] == 1)){
    print "You have a straight!<br />\n";
    $payoff = 10;
          
  } // end if
  $cash = $_SESSION["cash"];
  //print "Cash: $cash<br />\n";
  //subtract some money for this roll
  $cash -= 2;
  print "You bet 2<br />\n";
  print "Payoff is $payoff<br />\n";
  $cash += $payoff;
  print "Cash: $cash<br />\n";
	print "</p> \n"; 
	
	//store cash back to session:
	$_SESSION["cash"] = $cash;
} // end evaluate

?>
</body>
</html>

    
Link to comment
Share on other sites

Yeah, I agree with ginerjm. But, to answer your question in a general sense, I would use session data to store the cards. Whenever there is a shuffle, create all the cards as part of the "deck" in an array in the session. Then, when cards are given to the player(s) remove them from the deck and assign to the player(s) - again in the session. Then, when players decide to exchange cards, remove them from the player array and get additional cards from the deck array.

Link to comment
Share on other sites

Way too hard to decipher not knowing what your whole app looks like. How about telling us what section is not right and what you ARE getting that is wrong?

Its not telling the user what hand they got, its like a card game but the deck only has 14 cards, now ou can still get two 8's or two a's but you only have the 14 cards anyway its not calculating what they got in their hand  and it keeps taking the money lol 

Link to comment
Share on other sites

And what section should we be focusing on? Has your own error-checking pointed you to any line numbers?

Help us to help you.

It runs I have attached the link where I have it running, but its not running correctly, It would be easier if an error message would pop up, its not calculating the hand the user has been delt in the end. 

Link to comment
Share on other sites

Here is the complete code, I am going to put some instructions on there I think but it works correctly, I renamed the pictures and set up the case for the rest of the deck and it works great now, thanks for the inspiration you are wonderful on here.. 

http://www.ctcsports.org/upload/Summer2013/CIST2351/900104329/Assignment5/Assignment5.php

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.