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