Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we'll create a simple ping pong game to demonstrate another use case for while loops.
To Win
- player must reach a score of 11
- player must be a minimum of 2 points higher than opponent
WHILE those conditions are NOT met, a single player will receive 1 point at the end of each round.
-
0:00
I'm going to show you some other loops that I prefer
-
0:03
when it comes to looping through arrays.
-
0:05
But I don't want you to think that while loops are unimportant.
-
0:08
Let's create a simple ping pong game to demonstrate another use case for
-
0:14
while loops.
-
0:15
Let's create a new file named pingpong.php We add our PHP tags.
-
0:27
And then we're ready to start programming the game.
-
0:30
We'll start with two players, each with an initial score of 0.
-
0:36
$player1 = 0.
-
0:38
And $player2 = 0.
-
0:42
I also want to track how many rounds each game takes.
-
0:45
So I'm going to add another variable.
-
0:47
$round = 0.
-
0:52
Now let's add some comments.
-
0:55
To win, a player must reach the score of 11.
-
0:58
While also being a minimum of 2 points higher than their opponent.
-
1:02
So while those conditions are not met.
-
1:05
I'm going to randomly increment one of the player's scores each round.
-
1:10
To check for the difference between the player scores,
-
1:13
I can subtract one score from the other.
-
1:16
This will give me a number that can be positive or
-
1:18
negative depending on which score is higher.
-
1:22
We can then find the absolute value of that number.
-
1:26
Let me demonstrate.
-
1:28
Let's change $player2 = 5.
-
1:32
Then we'll do a var_dump.
-
1:37
$player1- $player2.
-
1:41
Then we'll also var dump $player2- $player1.
-
1:51
Let's run the script.
-
1:55
I see that the first var dump shows -5 and this the second var dump shows 5.
-
2:02
The difference between these numbers is 5.
-
2:06
All I care about is that the difference between these numbers is greater than 2.
-
2:10
I don't care which is the higher number.
-
2:13
So, I can use a function to determine the absolute value, abs.
-
2:21
I'll add this to the first var_dump.
-
2:25
Now, when I run my script.
-
2:28
You can see that the absolute value gives me the number itself,
-
2:33
without the negation before it.
-
2:36
I can use this absolute value in my while loop.
-
2:39
Let’s comment out these var dumps.
-
2:48
So I want to continue to loop while the absolute value is less than 2.
-
2:58
I also want to continue to loop while both scores are less than 11.
-
3:04
So I'm going to add a second expression.
-
3:06
I’ll combine these with the double pipe
-
3:11
operator because I want either or to be true.
-
3:17
$player1 is less than 11 and
-
3:22
$player2 is less than 11.
-
3:26
I need to put parenthesis around these two expressions so
-
3:29
that they evaluate as a single conditional.
-
3:34
$player1 must be less than 11 and
-
3:37
$player2 must be less than 11 to continue my loop.
-
3:43
So if the difference between the players is less than 2, we'll continue to loop.
-
3:48
Or if $player1 and $player2 are both less than 11 we will continue to loop.
-
3:56
The first thing I want to do is to start incrementing the round.
-
4:00
This would be round 1.
-
4:02
So adding 1 to 0 = 1.
-
4:07
Let's also let the user know what's going on with each loop.
-
4:11
So let's start with the heading for the round.
-
4:14
I want to view this in the browser, so let's add the HTML tags.
-
4:20
Round $round, And close my tags.
-
4:29
Notice that I used the double quote, so that my variable is expanded.
-
4:34
Now we want to randomly add a point to either $player1 or $player2.
-
4:40
PHP has a function named RAND that will generate a random integer.
-
4:46
We can specify a minimum and a maximum for this random integer.
-
4:51
So we can say if {rand(0,1)
-
4:56
then we're going to do something.
-
5:02
The min and max are inclusive, meaning it will include the minimum and
-
5:08
the maximum in its random number generation.
-
5:11
With a minimum of 0 and a maximum of 1,
-
5:14
we will randomly receive either 0 or 1.
-
5:19
Since 0 evaluates to false and 1 evaluates to true,
-
5:24
we can use this in our conditional.
-
5:27
If 1 is returned and our expression is true, we can add 1 to $player1.
-
5:35
Else we can add 1 to $player2.
-
5:42
Now, let's display the current score for this round.
-
5:47
Player1 = $player1 score and a line break
-
5:58
Player2 = $player2 score and a line break.
-
6:05
And that's all we need for a while loop.
-
6:07
So let's preview this script in a browser.
-
6:12
We can see each round with a point being awarded to a random player.
-
6:17
And the final score ending with Player1 = 2 and Player2 having 11.
-
6:25
This is okay, but let's add a final summary.
-
6:29
After our while loop, we should add our comment back up in the while loop.
-
6:40
And after our comment we can add echo header 1.
-
6:48
If $player1 > $player2,
-
6:56
we'll echo Player1.
-
7:02
Else we'll echo Player2.
-
7:10
And finally we'll echo,
-
7:14
is the winner after $round rounds.
-
7:20
And close our header.
-
7:24
Let's preview our script again to see the final result.
-
7:29
Player 2 is the winner after 9 rounds.
You need to sign up for Treehouse in order to download course files.
Sign up