Jump to content

Recommended Posts

 

 

 

hi guys !

 

how are you?

 

feeling little geek  :P

and want to make a hi-lo game !

 

but it didn't work, and I don't know where to put my loop in ! loool

 

this is my code

 

<?php


error_reporting(E_ALL & ~ E_NOTICE);


// $a = 1000;

// $b = 25;

// $c = $a/100 * $b;


if (isset($_POST['BTN'])) {

    $max = 100;
    $min = 0;
    $start = 50;

    // first try
    echo "is it $start ?";
    echo '  <br />
		  <form action="" method="post">
		  <input name="yes1" type="submit" value="yes1" />
		  <br />
		  <input name="no1" type="submit" value="no1" />
		  </form>';

    
    if(isset($_POST['no1'])) {
        
            echo '<form action="" method="post">
		  <input name="more" type="submit" value="more?" />
		  <input name="less" type="submit" value="less?" />
		  </form>';
            if (isset($_POST['less'])) {
                $start = $start / 2;

            } elseif (isset($_POST['more'])) {
                $a = $start;
                $b = 50;
                $c = $a / 100 * $b;

            }
        

    }
    if(isset($_POST['yes1']))
    {
echo 'game over';

}
}

else
{
echo '
<h4> pick a number and I will guess it for you </h4>
<br />

<form action="" method="post">
  <input type="submit" name="BTN" value="let\'s me guess" />
  </form>
  <br />
  <br />';
}

?>

Here's a simply hi low to the way i know it.

 

<?php
session_start();
error_reporting( E_ALL );

echo '<h2>Hi Low Game</h2>';

//Settings
$max = 10;
$min = 1;

function show_page()
{
global $max,$min;
echo '<br><br>';
if( isset($_SESSION['hi.lo']) )
{
	echo 'Winnings: '.$_SESSION['hi.lo'].'<br><br>';
}
else
{
	//Do nothing
}
echo 'Pick a number between '.$min.' & '.$max.'.<br><br>
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
	<input type="text" name="btn">
	<br>
	<input type="submit" value="Lets Play!">
</form>';
}

if( isset($_POST['btn']) )
{	
$post = abs(@intval($_POST['btn']));

if($post > $max)
{
	echo 'Your number is higher than '.$max.'.';
	exit;
}
if($post < $min)
{
	echo 'Your number is lower than '.$min.'.';
	exit;
}

$srad = rand($min,$max);

if($post == $srad)
{
	echo 'You guessed right!';
	if( isset($_SESSION['hi.lo']) )
	{
		$_SESSION['hi.lo']++;
	}
	else
	{
		$_SESSION['hi.lo'] = 1;
	}
}
show_page();
}
else
{
show_page();
}

?>

 

Just coded that right now while i was bored.

Are you having problems with the logic or the syntax?

 

thank you for responding ! :)

 

I just want someone to review my code !

because when I choose No or Yes, it returns to the if statement (isset($_POST['BTN'])) !

I tried removing the last else, and let the first form to be in the header of the page, but it didn't work either !

I guess I should use sessions, or make 7 pages or so ! to let it moves the generated value to the next page with no complicated issues !

 

second thing, that I want to know where and how to put the while() loop !

Here's a simply hi low to the way i know it.

 

<?php
session_start();
error_reporting( E_ALL );

echo '<h2>Hi Low Game</h2>';

//Settings
$max = 10;
$min = 1;

function show_page()
{
global $max,$min;
echo '<br><br>';
if( isset($_SESSION['hi.lo']) )
{
	echo 'Winnings: '.$_SESSION['hi.lo'].'<br><br>';
}
else
{
	//Do nothing
}
echo 'Pick a number between '.$min.' & '.$max.'.<br><br>
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
	<input type="text" name="btn">
	<br>
	<input type="submit" value="Lets Play!">
</form>';
}

if( isset($_POST['btn']) )
{	
$post = abs(@intval($_POST['btn']));

if($post > $max)
{
	echo 'Your number is higher than '.$max.'.';
	exit;
}
if($post < $min)
{
	echo 'Your number is lower than '.$min.'.';
	exit;
}

$srad = rand($min,$max);

if($post == $srad)
{
	echo 'You guessed right!';
	if( isset($_SESSION['hi.lo']) )
	{
		$_SESSION['hi.lo']++;
	}
	else
	{
		$_SESSION['hi.lo'] = 1;
	}
}
show_page();
}
else
{
show_page();
}

?>

 

Just coded that right now while i was bored.

 

thank you for your try_to_help!

 

you're smart, :)

I just guessed that it needs session to handle it!

but you know? I tried it and didn't work !  :-[

 

also, I don't want a field there for the user to fill! like the field btn, which is used to pick a number !

 

I want the computer to pick the half of the max, which will be divided by 2!

then prints it to the user with yes? or no?

if yes, game over then!

if no,

then print to the user and see less? or more?

then if user choose less, then we need the midpoint of the value ! so it's going to be $value/2 !

if more, then we need to add 50% of the value, to the value !

 

i know it's complicated !  ???

 

never mind guys ! I gave up !  :-\

 

all my programmings I don't give up !

 

but this time ! I don't know, maybe it's impossible !

With fair knowledge it's do able. I just do not have time right now to do exactly what you want. Maybe later on i will make some thing if no one else has.

 

well, I didn't gave up !

I'm trying no to do it with the field that the user would pick a number and enter the game with !

 

I'm trying and will update you with what I came up with !

Was really....really bored at work.

 

Not the best coded and could use improvements for efficiency etc, but should work:

<?php
session_start();
if (isset($_GET['reset'])) {
foreach ($_SESSION as $key => $val) 
	unset($_SESSION[$key]);

unset($_POST['submot']);
}

$output = "";
if (!isset($_POST['submit']) || isset($_GET['error'])) {
$error = isset($_SESSION['error'])?$_SESSION['error'] . "<br />":'';
unset($_SESSION['error']);

$output = <<<OUTPUT
	<div style="color:red;">{$error}</div>
	<div align="center">Welcome, please enter a number from 0-100 in the form below.<br /><br /></div>
	<div align="center">
		<form method="POST" action="{$_SERVER['PHP_SELF']}">
			<input type="text" name="number" value="" size="5" /><br />
			<input type="submit" name="submit" value="Let's Start!" />			
		</form>
	</div>
OUTPUT;
}elseif (!isset($_SESSION['number'])) {
// initial settings. Check first if the number is valid
$number = isset($_POST['number'])?(int) $_POST['number']:null;
if (is_null($number) || $number < 0 || $number > 100) {
	// nope. show error
	$_SESSION['error'] = "The number has to be between 0 and 100. Please try again.";
	header("Location: {$_SERVER['PHP_SELF']}?error=1");
	die();
}

// yep let's initialize our values and make our first guess	
$_SESSION['number'] = $number;
$_SESSION['range'] = range(0, 100);
$_SESSION['last_guess'] = array_rand($_SESSION['range']);

// We do not want to guess it right away 
while ($_SESSION['last_guess'] == $number) 
	$_SESSION['last_guess'] = array_rand($_SESSION['range']);

$next_guess = $_SESSION['last_guess'];
}

// If this is te first guess we do not want to be in there.
if (isset($_SESSION['number']) && !isset($next_guess)) {
$last_guess = isset($_SESSION['last_guess'])?$_SESSION['last_guess']:'nill'; // just incase something flaky happens.

// Yep we guessed it, let them know.
if ($last_guess == $_SESSION['number']) {
	$output = <<<OUTPUT
		<div align="center" style="color:red;">{$_SESSION['error']}<br /></div>
		<div align="center">
		EUREKA! Your number is: {$last_guess}!<br /><Br />
		It took me {$_SESSION['counter']} trys to guess it!<br /><br />
		<a href="{$_SERVER['PHP_SELF']}?reset=ok">Play Again?</a>
		</div>
OUTPUT;
}else {
	$action = isset($_GET['action'])?$_GET['action']:'';
	if ($action == "higher") {
		// if it is higher, we limit the array to just those values
		for ($i=$last_guess; $i>=0; $i--) 
			unset($_SESSION['range'][$i]);
	}elseif ($action == "lower") {
		// if it is lower, lets limit the array to those values
		for ($i=$last_guess; $i<101; $i++) 
			unset($_SESSION['range'][$i]);
	}

	// Let's make our guess and increment our counter
	$next_guess = $_SESSION['last_guess'] = array_rand($_SESSION['range']);
	$_SESSION['counter']++;

	// Uh oh, the user lead us wrong.
	if (count($_SESSION['range']) < 1 && $next_guess != $_SESSION['number']) {
		$_SESSION['error'] = "You must have tried to cheat!";
		$next_guess = $_SESSION['number']; 
	}

	// we just guessed it!
	if (($next_guess == $_SESSION['number'])) {
		$_SESSION['last_guess'] = $next_guess;
		header("Location: {$_SERVER['PHP_SELF']}");
	}
}
}

// Display to them to tell us if it is higher or lower.
if (isset($next_guess) && isset($_SESSION['number'])) {
$output = <<<OUTPUT
		<div align="center">Time to guess your number! Simply press if your number is higher than the one displayed or lower!<br /><br />My Guess:<b>{$next_guess}</b><br /> (Your number is {$_SESSION['number']} as a reminder)<br /></div>
		<div align="center">
			<form method="POST" action="{$_SERVER['PHP_SELF']}?action=higher">
				<input type="submit" name="higher" value="Higher!" />			
			</form>		
			<form method="POST" action="{$_SERVER['PHP_SELF']}?action=lower">
				<input type="submit" name="lower" value="Lower!" />  
			</form>
		</div>
		<br /><br /> <a href="{$_SERVER['PHP_SELF']}?reset=ok">Start Over?</a>

OUTPUT;
}

// echo it out.
echo $output;

die();
?>

That's great work man !

 

but very advance, I'm still a little boy in the php world !

 

thank you very much :)

 

I wish my knowledge over php language more than now to understand the whole code !

 

but thank you for your help, and sorry for you of being bored at work !

 

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.