Jump to content

hamburgerlove413

Members
  • Posts

    29
  • Joined

  • Last visited

Everything posted by hamburgerlove413

  1. hello, I'm trying to create a simple word guessing game but I'm having a problem where when I load in a text file, when I put the lines into an array, it adds 2 extra empty spots to the end of the array, so when I display the placeholders for the phrase to be guessed, there are two extras. Anyone have any idea of where I'm going wrong? The code is below and I'm also attaching the text file in case theres a problem there. Also theres a next phrase button thats there along with some code for it but I don't have that working yet. I don't think that's related to my problem though. <?php session_start(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Project 2</title> <link href="css/layout.css" rel="stylesheet" type="text/css"> </head> <body> <div id="container" class="clearfix"> <?php print "<form method='post'>"; function inputscreen($guesses) { print "<span>Guess a letter!</span>"; print "<span> You have ". $guesses . " guesses left! </span>"; print "<input type='text' name='guess' />"; print "<input type='submit' name='action' value='GUESS' />"; print "<input type='submit' name='action' value='REVEAL' />"; print "<input type='submit' name='action' value='NEXT PHRASE'>"; print "</form>"; } function printTable($letterCount, $visibleArray, $wordArray) { // print table for word display print "\n \n <table> \n \n \t <tr>"; for ($i = 0; $i < $letterCount; $i++) { if ($visibleArray[$i] == 1) { print "\n \n \t \t <td>" .$wordArray[$i]. "</td> \n"; } else if ($wordArray[$i] == " ") { print "\n \n \t </tr> \n \n</table> \n \n <table> \n \n \t <tr>"; } else { print "\n \n \t \t <td> </td>"; } } print "\n \n \t </tr> \n \n </table> \n \n"; } //text loader function textLoader($whatfile) { if(!file_exists($whatfile)) { print "Sorry, can't find " . $whatfile; exit; } else { $textarray= file($whatfile); return $textarray; } } // global variables $theText = textLoader('words.txt'); $action = $_POST['action']; $team = $_SESSION['team']; $visibleArray = $_SESSION['visible']; $guesses = $_SESSION['guesses']; $word = $theText[0]; $wordArray = str_split($word); $letterCount = count($wordArray); if (!isset($action)) { $guesses = 5; for ($i = 0; $i < $letterCount; $i++) { $visibleArray[$i] = 0; } } else if ($action == "GUESS") { $guess = strtoupper($_POST['guess']); if (preg_match("/^[a-zA-Z]$/", $guess)) { if (in_array($guess, $wordArray)) { for ($i=0; $i < $letterCount; $i++) { if ($guess == $wordArray[$i]) { $visibleArray[$i] = 1; } } print "<span>You guessed the letter " .$guess . " and are correct! </span>"; } else { print "<span>Nope! Try Again!</span> "; $guesses -= 1; } } else { print "<span>You need to guess a letter!</span>"; } } else if ($action == "REVEAL") { for ($i =0; $i < $letterCount; $i++) { $visibleArray[$i] = 1; } print "<span>The phrase was </span>"; print "<div class='reveal'>" . $word . "</div>"; print "<input type='submit' name='action' value='NEXT PHRASE'>"; } else if ($action == "NEXT PHRASE") { $nextPhrase++; for ($i = 0; $i < $letterCount; $i++) { $visibleArray[$i] = 0; } } inputscreen($guesses); printTable($letterCount, $visibleArray, $wordArray); $_SESSION['visible'] = $visibleArray; $_SESSION['team'] = $team; $_SESSION['guesses'] = $guesses; ?> </div> </body> </html> words.txt
  2. Ok, I'm trying to understand this. Is this correct? you created a function called deal that passes the arguments min and max. In this function, you create a variable called suits which is assigned the value of an array consisting of the four suits. you then create a variable called suit which takes a random item from the suits array. Then you create a variable called value which randomly gets a number between min and max. You then create another array called faceCards which assigns the values to their corresponding strings. Then you check to see if the random number from value is located in the faceCards array. If it is, replace that with the string that is associated with that number. else, use the original value from $value. You then put the value, whether its a face card or not, and its suit in an array called result which is what the function returns. A function is created called cardValue which passes the argument cardAry. It returns face and the suit. A variable called highCard is created that calls the deal function and sets min and max to 3 and 14. A variable called lowCard is created calls the deal function and sets the min to 1 and the max to the high card's value - 2. These two come back with values that are arrays, which includes all of the information needed. finally, you send the lowCard and highCard through cardAry, which separates out the array information into face and suit and then print the results.
  3. Thank you both! As you can see, I'm an very much just starting out with this, and my logic is often off. The infinite loop thing you were talking about also explains why every once in a while when refreshing, the browser would freeze up.
  4. Hello, I wasn't sure if there was a "beginner php questions" area, so sorry if this is not put in the right spot, but I have a very basic "game" where it prints out a randomly generated low card and high card. The low card has to be of a lower value than the high card, and they cant equal each other or be within 1 of each other. I have this all working fine. The problem is, when I try to add in any kind of way to change numbers, say, 11-14 to Jack, Queen, King, Ace, the whole lower card being lower than the high card goes out the window. I feel like I'm maybe approaching this from the completely wrong direction or something, so if anyone could give me any kind of advice, I'd be very grateful. <?php function deal() { $card = rand(1,52); return $card; } $highCard = deal(); do { $lowCard = deal(); } while ($lowCard > $highCard || $highCard - $lowCard <= 1); function cardValue($card) { if ($card % 14 === 0 || $card % 14 === 1) { return $card = "an Ace"; } else if ($card % 14 === 13) { return $card = "a King"; } else if ($card % 14 === 12) { return $card = "a Queen"; } else if ($card % 14 === 11) { return $card = "a Jack"; } else { return $card % 13; } } print "Your low card is " .cardValue($lowCard). " and your high card is " .cardValue($highCard); ?>
×
×
  • 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.