Jump to content

Is my logic right?


yacobm8

Recommended Posts

I want to ask the user questions by using a form. I have a text field where the user inputs either 1 or 2 and then they press submit. I can get this to work but I need to keep asking them questions based off there last answer to the previus question.

 

I am making a game where you start with a number ex 5 and then you either choose to take away 1 or two from that number. so we take away 2 now the number is 3 and then the computer has his go using the same number. He chooses to take away 1 which makes the number 2. Now we take away 2 and win the game for being the first one to get the number down to 0. Its a very simple game that im trying to do just to brush up on my php skills.

 

my code so far....

<p> please enter a number to begin the subtraction game</p>
<form name="submit1" method="post">
	<input type="text" name="startingNumber">
	<input type="submit" name='submit1'>
</form>
<?php if (!empty($_POST['startingNumber'])) {?> 
		<p>your starting number is: <?php echo $_POST['startingNumber']?></p>
		<?php 
		while(!$_POST['startingNumber'] == 0){?>
		<p> Would you like to minus one or two from <?php echo $_POST['startingNumber'];
		?></p>
		<form name="submit2" method="post">
			<input type="text" name="oneOrTwo">
			<input type="submit" name='submit2'>
	    </form>
	    
<?php } }?>

As you can see i am no where near close to getting this right. Im thinking i have to use a while loop to keep asking the question minus 1 or 2 until they reach 0. Inside this while loop im thinking i need something like a waituntil user has clicked submit.

 

In fact i have written the same game in c++ so if it helps here is that. (it works just how i want it too)

 

the problem im having is that in c++ you can use cin to get a input from the user and it waits until the user types in a value but im struggling to find anything like  that in php.

Any help would be greatly appreciated thank you.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{   int number, oneOrTwo, remainder;
    cout << "please enter a number to begin the subtraction game";
    cin >> number;
    while (number != 0){
          cout <<"your turn, subtract 1 or 2 from " << number << endl;
          cin >> oneOrTwo;
          if (oneOrTwo == 1 || oneOrTwo == 2){
             number = number - oneOrTwo;
          }else{
            cout << "sorry you have entered a incorrect number. Please enter either 1 or 2" << endl;
            continue;      
          }
          if (number == 0){
             cout << "congratulations! you won the game"; 
             break;          
          }
          remainder = number % 3;
          if (remainder % 3 == 0){
             remainder = 2;              
          }
          cout << "now my turn im going to subtract " << remainder << " from " << number << endl;
          number = number - remainder;
          if (number == 0){
             cout << "sorry the computer won this time";      
          }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


Edited by yacobm8
Link to comment
Share on other sites

When you use PHP the code is running on the server. When the PHP has run the output is displayed on the client browser screen.

 

Each time you input 1 or 2 you then have to send this to the server (submit the form) where it will be processed and the new result then sent for output in the browser.

 

You would continue going round this cycle until the game is over.

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.