Jump to content

Basic problem


Basso

Recommended Posts

Hey all,

 

First of all, i'm a complete noob in PHP, i started learning it yesterday 8)

So... I decided it would be a nice practice to write a little game, most people know it i think. You get a number, and you have to decide wether the next number will be higher or lower.

Well, the game itself works just fine, but when i decided to add a scoring system it went wrong, and i cant figure out what exactly went wrong ;p :confused:

 

The problem is, i don't get an error, so the script runs without any trouble, but it doesnt add any points, tho i think it shoud...

So my 'game' exists of two files. One called 'hi_lo.php' and one called 'hi_lo_proccess.php'

 

hi_lo.php:

<html>
<head>
<title>Higher/Lower</title>
</head>
<body style="text-align: center;">

<?php
// Check if this is a replay, or a first time player
$replay = $_POST["replay"];
if ($replay == "Play again!") {
$score = $_POST["score"];
echo "Your total score so far: " . $score . " points!<br />";
}
$current_int = rand(1,15);
echo "<h1>" . $current_int . "</h1>";

?>
<br />
<form action=hi_lo_proccess.php method="post">
<input type="hidden" value="<?php echo $score; ?>" name="score">
<input type="submit" value="Higher!" name="choice">
<input type="submit" value="Equal!" name="choice">
<input type="submit" value="Lower!" name="choice">	
</form>
</body>
</html>

 

 

hi_lo_proccess.php:

<html>
<body style="text-align: center;">

<?php 
// Create a new integer.
$new_int = rand(1,15);
// Receive the old integer.
$old_int = $_POST["int"];
// Echo the stats.
echo "The previous number was: <br /> <h1>" . $old_int . "</h1><br />";
echo "The new number is:<br /> <h1>" . $new_int . "</h1><br />";

// Receive and echo players choice.
$your_choice = $_POST["choice"];
echo "Your choice was: <br /> <h3>" . $your_choice . "</h1><br />";

// Decide which choice is right.
if ($new_int > $old_int) {
$right_choice = "Higher!";
} elseif ($new_int < $old_int) {
$right_choice = "Lower!";
} else {
$right_choice = "Equal!";
}

// Receive the players score
$score = $_POST["score"];

// Compare right_choice to your_choice
if ($your_choice == $right_choice) {
echo "<H1>Congratulations! You win!</H1><br />";
$score ++;
} else {
echo "<H1>Oops! You lose!</H1><br />";
$score --;
}

// Set score == no if there are no points.
if ($score < 1) {
$score = "no";
}

// Show current score
if ($score == 1) {
echo "<H3> You currently have " . $score . " point!</H3><br />";
} else {
echo "<H3> You currently have " . $score . " points!</H3><br />";
}
?>

<form action=hi_lo.php method="post">
<input type="hidden" value="<?php echo $score; ?>" name="score">
<input type="submit" value="Play again!" name="replay">
</form>
</body>
</html>

 

I can't figure out what's causing the trouble... can any1 help?!  :confused:

 

Greetz, Basso

Link to comment
https://forums.phpfreaks.com/topic/198238-basic-problem/
Share on other sites

Thanks alot! I defined $score in hi_lo.php now, and it works just fine :) Thanks and sorry for wasting your time with a noob question  :-[

 

I also had to fix something in the hi_lo_proccess.php:

 

	// Set score == no if there are no points.
if ($score < 1) {
$score = "no";
}

 

Became

 

	// Set score == no if there are no points.
if ($score < 1) {
$score = "0";
}

 

Because if $score = "no" then $score++ won't result in $score = 1.

Link to comment
https://forums.phpfreaks.com/topic/198238-basic-problem/#findComment-1040142
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.