WEBVTT

00:01.070 --> 00:08.610
Now let's do our top down design for Battleship and let's start with kind of the highest level.

00:08.670 --> 00:16.890
So what we should do and we should again take our data design into account all this data that we did

00:16.890 --> 00:23.240
last time and we should try to make sure we're using that properly.

00:23.850 --> 00:34.400
So our really our top level design of it will be let's say in it's player

00:36.940 --> 00:47.840
and using one of our players play one this and that player or two and we're knitting them because if

00:47.840 --> 00:55.100
you remember in our ship we have this tie but this doesn't define the actual variable itself.

00:55.100 --> 00:55.840
Right.

00:56.030 --> 00:59.850
So we actually have to set all our ships for each player right.

00:59.900 --> 01:02.570
Since the player has all the ships.

01:02.570 --> 01:12.330
So we need to set all the ships correctly and give the player one ship or one type of ship in each elements

01:12.450 --> 01:14.690
long for this array.

01:15.320 --> 01:26.830
So off you get the players somehow and prior to well since we we've done this do time for it will say

01:26.830 --> 01:31.160
do play.

01:31.940 --> 01:36.280
Play a game and we'll play a game with the players.

01:36.560 --> 01:40.740
Play it one player or two.

01:41.820 --> 01:50.370
And again while Want to play again.

01:50.460 --> 01:50.890
Right.

01:52.290 --> 01:58.920
So we're not going to go through want to play again anymore.

01:58.930 --> 02:00.070
I think we know how to do that.

02:00.170 --> 02:06.940
Now for the top 10 design anyway you want to implement it of course.

02:07.520 --> 02:13.860
And so now this is kind of our top level design we need to initialize all the player whatever that means

02:13.860 --> 02:15.790
set up all his ships.

02:15.800 --> 02:25.090
Maybe his name is well maybe once you actually send him a name you know Player 1 as kind of the input

02:27.360 --> 02:28.250
player to

02:31.430 --> 02:32.860
write.

02:33.060 --> 02:35.600
So we have to do something like this.

02:35.730 --> 02:39.540
So it's kind of it's going to call it.

02:39.840 --> 02:51.330
I call that steps before as long as step one Lisa is kind of the main function really and let's start

02:51.330 --> 02:54.170
breaking down how to play the game.

02:54.360 --> 03:00.990
That's probably the most play your game play a game.

03:01.050 --> 03:04.240
So play again.

03:07.600 --> 03:10.310
So how would you play the game.

03:10.330 --> 03:15.460
Well he said in up here in the description

03:17.950 --> 03:21.340
we should arrange our

03:24.350 --> 03:26.730
arrange our ships for the players.

03:26.750 --> 03:30.230
Each player has to arrange their ships on the board somehow.

03:30.260 --> 03:30.900
Right.

03:31.310 --> 03:41.170
So we'll call that set up set up board and we'll do it for each player.

03:41.300 --> 03:48.530
Player 1 and set up board for a player to I know sort of looks like code.

03:48.530 --> 03:50.780
But again it's not really code.

03:50.990 --> 03:55.910
It's kind of a whenever it is pseudo code here.

03:56.080 --> 03:57.210
OK.

03:57.880 --> 04:00.810
And how so then what.

04:00.820 --> 04:04.590
So once we set up the boards we will

04:07.370 --> 04:21.160
say Do you know maybe we should draw the board of the current player chalkboards and we have something

04:21.160 --> 04:22.980
called like a current player.

04:23.090 --> 04:25.180
Whoever is taking their turn

04:28.560 --> 04:42.180
and maybe we also need to go through and get the guess that the player wants and the guests will have

04:42.220 --> 04:44.980
to be valid.

04:44.980 --> 04:50.310
So for example we couldn't guess a spot we've already guessed before right.

04:50.560 --> 04:51.990
So that would be not OK.

04:52.000 --> 04:58.960
So we need to somehow take into account that they can kind of mess up and pick a spot that maybe they've

04:58.960 --> 05:00.860
already picked up.

05:01.090 --> 05:10.660
So we'll do a loop for this kind of like we did with input with incorrect or just incorrect input and

05:10.660 --> 05:14.560
we'll say do while.

05:15.400 --> 05:18.640
So not guess is out.

05:18.970 --> 05:19.790
OK.

05:20.160 --> 05:29.820
And we'll get the guess and we'll probably have to give the players the courage later.

05:34.850 --> 05:37.790
And inside we will.

05:39.010 --> 05:48.780
The current Each player or a guess and get the guess or just a guess.

05:48.820 --> 05:52.910
Calls get this this

05:56.080 --> 06:01.670
and yeah now we do for now.

06:04.760 --> 06:13.410
And the while condition for this one and this one simple while not so great.

06:14.900 --> 06:26.000
In the Loop While we're still playing the game now down here maybe we should update the board or update

06:26.010 --> 06:34.890
the boards with the guess of the current player and remember that if it was a hit we should also update

06:34.980 --> 06:36.390
the other player.

06:36.420 --> 06:38.070
So maybe we have another

06:41.450 --> 06:44.090
another variable that's the other player.

06:44.150 --> 06:44.590
OK.

06:44.600 --> 06:50.110
And those were not using player 1 in player 2 because we don't know whose turn it is right whoever's

06:50.150 --> 06:55.950
the current player that's that's their player that's taking their turn.

06:56.260 --> 07:02.000
So we're kind of distinguishing between player 1 and player or here and other player and current player

07:03.050 --> 07:09.880
so update the board somehow with the guess and then maybe draw the board again.

07:11.990 --> 07:12.510
Onboard

07:15.120 --> 07:18.550
current players want to see their results right.

07:19.720 --> 07:23.360
See your result gets

07:26.630 --> 07:30.660
an then we should see.

07:30.830 --> 07:34.870
Maybe if ship was sunk

07:38.670 --> 07:43.930
on current players turn

07:47.920 --> 07:54.910
right we ship the output at the

07:57.730 --> 08:02.900
player sunk that ship somehow.

08:03.140 --> 08:13.420
Again not actual code just pseudo code just this is really just the template for the plan right for

08:13.720 --> 08:15.360
our implementation.

08:16.360 --> 08:25.560
And I'm actually going to use something that we kind of only briefly went over which is wait for key

08:25.570 --> 08:26.350
press.

08:26.380 --> 08:33.410
So that the player the current player can just like type something and then and then all switch turns

08:33.630 --> 08:46.420
OK and then switch the player's right because we want the other guy some k

08:50.060 --> 08:53.250
and then once the game is over again will display the winner.

08:53.250 --> 08:59.610
It's like this like normal.

08:59.900 --> 09:01.690
Hi that J.

09:02.990 --> 09:05.380
So that's play game.

09:06.970 --> 09:13.520
Now let's break down break down the set up board start from the top here.

09:14.560 --> 09:28.960
It's set up for and we just take the player into whatever the player is or who or whoever reads K and

09:28.970 --> 09:31.150
how would we set up the board.

09:32.630 --> 09:37.550
Well we should maybe clear the board first.

09:38.270 --> 09:43.760
Because what if we already played a game and we wanted to play again while all the ships would still

09:43.760 --> 09:52.130
be in that in those positions we kind of want to clear the board so everything's set back to the initial

09:52.130 --> 09:53.700
state.

09:53.800 --> 10:02.130
Clear the board of the player and then for all the ships right.

10:02.720 --> 10:03.940
We should

10:05.950 --> 10:20.700
maybe draw the board draw boards for it player and then we should have a current ship's Eric Hernshaw

10:20.920 --> 10:21.580
is

10:24.910 --> 10:27.760
to get current CH

10:31.830 --> 10:34.180
ship for the loop.

10:34.740 --> 10:43.360
And we should place the ship somehow K and like it said up here.

10:50.400 --> 10:52.580
Hear ships cannot overlap right.

10:52.620 --> 11:03.310
So we should prompt the user to input a location and orientation for the ship on the board.

11:03.650 --> 11:04.260
OK.

11:04.370 --> 11:12.110
So say he chose Jay won something and he wanted to do it vertically.

11:12.330 --> 11:13.040
Right.

11:13.440 --> 11:20.930
Well you can't have a ship go down vertically if you're on Jay.

11:20.930 --> 11:21.350
Right.

11:21.360 --> 11:27.230
Because it'll definitely be off the board.

11:27.570 --> 11:31.100
So that would be a problem and that's not a valid placement.

11:31.110 --> 11:35.170
So we need to kind of loop to

11:38.560 --> 11:43.740
get the position and orientation of the ship you want to place on the board.

11:44.030 --> 11:48.800
And if it's not a valid placement then we should try again.

11:48.810 --> 11:49.370
OK.

11:49.580 --> 12:04.260
So essentially we want to get the board position then get the ship orientation this is positioned for

12:05.040 --> 12:08.550
kind of head of the ship yes.

12:11.690 --> 12:17.010
And the positions really are going to be the head of the ship and then whatever orientation we choose

12:17.010 --> 12:23.320
we'll just go either down if it's vertical or to the right if it's horizontal.

12:24.170 --> 12:33.100
And we should check to see if it's a valid placement if valid.

12:34.270 --> 12:43.420
Is valid placement of the ship current ship

12:45.970 --> 12:48.600
and the say

12:51.900 --> 12:52.490
sition.

12:52.490 --> 12:56.800
Is this an option.

12:56.820 --> 13:03.850
Is this the position orientation.

13:04.260 --> 13:09.220
And I guess we need the player as well because the player has the boards.

13:09.390 --> 13:20.260
So if it's not a valid placement and we should say put it's

13:22.610 --> 13:29.900
easier to player not a valid placement

13:33.050 --> 13:35.990
so we do this while

13:40.680 --> 13:42.210
it's not a valid placement.

13:42.210 --> 13:42.590
Right.

13:47.240 --> 13:49.930
So copy all this

13:52.910 --> 13:55.270
is probably like a Boolean or something.

13:58.380 --> 14:03.930
Take that out and say Is this just a variable placement

14:13.050 --> 14:15.010
goons.

14:17.440 --> 14:20.110
So it's not a valid placement.

14:25.280 --> 14:34.660
And while it's not about laser.

14:34.920 --> 14:44.750
And then once we get a valid placement then we'll place the ship on board the ship on board somehow.

14:46.080 --> 14:47.730
Well here are ship

14:50.290 --> 14:54.980
and Sishen orientation

15:01.200 --> 15:04.260
and that's really how you set up the board.

15:04.260 --> 15:06.260
That's all I can think of to do.

15:09.620 --> 15:14.140
So notice I'm kind of I am sort of getting into the implementation of it kind of.

15:14.150 --> 15:17.450
But it's really just based on

15:20.990 --> 15:23.390
our past experience.

15:24.260 --> 15:30.670
So maybe we should break down what else should we break down.

15:30.740 --> 15:34.370
See from.

15:34.380 --> 15:36.840
Game.

15:39.110 --> 15:42.440
All I see is yes valley something that's

15:47.660 --> 15:52.520
from play game.

15:52.790 --> 15:56.220
Well actually let's break down some stuff from this set of boards first.

15:56.220 --> 16:00.120
Actually So there's a couple of things.

16:00.460 --> 16:04.580
Is valid placement is one right now.

16:05.490 --> 16:05.650
It's

16:10.870 --> 16:11.950
current.

16:15.440 --> 16:18.360
Mission th

16:28.200 --> 16:33.270
plate.

16:33.300 --> 16:35.140
So let's break this down.

16:36.900 --> 16:43.930
So how would you check to see if this is a valid placement.

16:44.430 --> 16:53.310
Well we should note that because of two cases really it will be you know sort of dependent on the orientation

16:53.350 --> 16:59.470
I think of the ship want so maybe you'll have two cases.

16:59.730 --> 17:06.030
So if the orientation is horizontal

17:11.380 --> 17:20.320
and otherwise it's two cases and we'll say

17:22.570 --> 17:35.370
for all the Hollonds he current ship would take up.

17:35.520 --> 17:35.940
Right.

17:36.180 --> 17:40.370
Because ships have a certain size right and aircraft carriers five.

17:40.500 --> 17:44.320
So they take up five slots or five positions.

17:44.340 --> 17:47.790
So for all of them that they would take up if the current

17:53.090 --> 18:08.460
If the current position on the air ship board is taken or if

18:11.970 --> 18:18.610
the ship will be off board was onto the

18:21.710 --> 18:26.960
then will just return false because that's not about placement.

18:27.200 --> 18:36.160
OK so if the current position is taken meaning like if we overlap really this is what this means if

18:40.100 --> 18:47.120
if the ship will overlap another ship

18:51.180 --> 18:55.750
or a ship will go off the board horizontally then return false and we do the exact same thing here for

18:55.770 --> 19:00.480
vertical and for all the rows.

19:00.480 --> 19:03.110
In this case.

19:09.370 --> 19:10.900
Would take up

19:20.750 --> 19:25.950
would take up.

19:26.220 --> 19:31.040
And again if ship

19:35.690 --> 19:38.750
another ship or the ship

19:42.990 --> 19:51.400
off the board vertically anime will return false

19:56.380 --> 20:03.070
otherwise I guess here here we just returned true.

20:03.280 --> 20:09.950
Because clearly it went through all the spots that the ship would take up and we found that it didn't

20:10.160 --> 20:12.660
take up a thing and it's about placement.

20:12.800 --> 20:13.100
So

20:17.600 --> 20:18.410
so

20:21.800 --> 20:22.510
now

20:26.760 --> 20:34.810
we could you please ship on board but it's going to look actually quite similar to to is valid placement

20:36.430 --> 20:38.340
but maybe you should do it anyway.

20:38.680 --> 20:55.200
So we're just a place ship on board where that ship the ship in orientation.

20:56.750 --> 21:04.060
So I actually set the ship's position either position.

21:04.410 --> 21:10.030
Right and I'm using a dot here because that's what the data is.

21:10.610 --> 21:21.880
Position we access struct members through the dot operator so ship and then your station as well

21:26.570 --> 21:27.360
Asian

21:30.250 --> 21:33.550
we say if the orientation

21:35.780 --> 21:37.610
is horizontal

21:42.930 --> 21:50.880
and we know the position orientation is valid because we've already kind of checked it all.

21:50.910 --> 21:52.860
So you've already checked it also.

21:52.980 --> 22:00.260
We know that everything will be kind of fine if we just place it for all the columns.

22:02.100 --> 22:04.290
Ship.

22:11.600 --> 22:27.930
Essentially what we have to do is set the ship port on the board at position dot row in the current

22:28.140 --> 22:36.890
column parents are going to go through all the columns and then set it at that row and then add that

22:36.910 --> 22:37.330
column

22:40.980 --> 23:01.540
and we'll do the same for the case for all the rows ship Jake will forget how to do this after implementation

23:02.860 --> 23:03.850
said.

23:04.330 --> 23:09.430
Ship or food at

23:13.210 --> 23:19.400
the current row in position call.

23:21.870 --> 23:26.010
So like I said it looks quite similar to this one.

23:27.780 --> 23:33.160
So now what should we break down.

23:33.220 --> 23:42.250
We know how to get a gas will do the update Portes actually wouldn't be that hard.

23:42.360 --> 23:44.170
So update ords

23:52.010 --> 23:55.100
where the other player

23:59.020 --> 24:02.990
came.

24:03.020 --> 24:06.300
So what do you do here.

24:06.410 --> 24:15.860
Well really we just take the gas and see if we've hit a ship on the board and if we did then we will

24:15.860 --> 24:17.670
apply the damage and all that.

24:17.720 --> 24:23.330
So if it's mission or if the gas

24:26.310 --> 24:28.980
on board has

24:32.990 --> 24:36.590
or if the the other players

24:40.300 --> 24:45.870
ship board at Cas

24:51.100 --> 24:54.870
is a ship then that's it.

24:55.280 --> 24:56.290
OK.

24:56.370 --> 24:56.990
We hit

24:59.580 --> 25:00.030
hit.

25:00.040 --> 25:10.000
Case Kansas set hit on the current players.

25:10.500 --> 25:11.780
Guess board right.

25:13.360 --> 25:14.200
And

25:17.540 --> 25:33.800
an apply damage to the other players shipboard great.

25:33.900 --> 25:35.450
And then if you didn't it

25:39.570 --> 25:52.350
then all we do is sad said Miss the current players get bored.

25:52.760 --> 25:53.960
He's missed.

25:54.020 --> 25:54.860
That's it.

26:00.200 --> 26:08.060
And one thing we may want to do is actually return back

26:10.840 --> 26:25.680
the type of ship that was hit possibly in that so we can check to see if the current player sunk ship

26:26.760 --> 26:30.790
so possibly that maybe be one thing we want to do.

26:31.130 --> 26:33.510
So I say return

26:38.590 --> 26:39.490
ship type

26:42.810 --> 26:46.570
here.

26:46.840 --> 26:48.860
Actually in this case.

26:50.670 --> 26:56.690
And then we could return ship or s t here.

26:57.510 --> 26:59.120
Because we didn't hit one.

26:59.240 --> 27:04.000
So this is one of the reasons why I wanted as he see none.

27:04.040 --> 27:12.570
It's actually for to see to check to see if there is an actual ship that was hit on your ship board.

27:12.590 --> 27:19.230
So most of the ship board is empty and that's what really Esti none will mean.

27:19.340 --> 27:30.830
So if we look up here as none we define and I told you you know I was going to get to it later in SD

27:30.870 --> 27:36.700
none is really just means in our ship board.

27:36.900 --> 27:38.490
There's no ship there.

27:38.760 --> 27:39.170
OK.

27:39.170 --> 27:41.560
That's why we have the ship type here.

27:41.660 --> 27:44.720
One of the reasons why we have the ship type here.

27:45.830 --> 27:53.040
So so that if if there is no ship at all then you can't possibly have hit it right.

27:53.470 --> 27:55.240
OK.

27:55.240 --> 27:56.920
So.

27:56.930 --> 27:59.060
So that's why S-D none exists.

28:01.760 --> 28:04.730
So what else.

28:06.630 --> 28:08.930
So maybe is game over.

28:10.660 --> 28:12.530
Break down.

28:12.730 --> 28:18.340
And they're not going to break down and draw the boards because I always feel like drawing functions

28:18.340 --> 28:25.120
should be kind of just done on implementation at least for these kind of games because you kind of want

28:25.120 --> 28:32.980
to toy with it a little bit and tweak it and it's really really not going to help you that much if you

28:33.010 --> 28:35.320
kind of break it down first.

28:35.320 --> 28:43.660
So it's game over with player 1 and maybe player 2 as input.

28:44.970 --> 28:58.800
And really it's over when all are all ships sunk.

28:59.000 --> 29:06.060
Player one or if all the ships are sent to

29:12.370 --> 29:13.320
Nazi yet.

29:13.430 --> 29:14.590
Now we have to break.

29:14.610 --> 29:17.780
Of course break down all ships sunk

29:24.570 --> 29:30.330
player.

29:30.360 --> 29:34.830
So for all players ships

29:40.450 --> 29:44.260
if not is sunk.

29:45.550 --> 29:49.330
Here current ship

29:52.510 --> 29:53.830
then return

29:57.460 --> 29:58.330
false.

30:00.410 --> 30:06.560
So if there there's one that wasn't sunk then he can't possibly have or not all of his ships sunk so

30:07.580 --> 30:10.740
otherwise return true for get here.

30:11.620 --> 30:13.210
And for is sunk

30:18.070 --> 30:22.180
ship for this one.

30:22.180 --> 30:27.820
We will again check to see if the ship's orientation

30:31.160 --> 30:33.530
Kishen of its horizontal

30:35.810 --> 30:51.030
than seriously what we'll do is we're just checked to see for all columns that the ship takes up.

30:51.440 --> 30:58.070
If that's the position current position

31:00.570 --> 31:06.630
on the ship board is not hit

31:09.700 --> 31:11.380
then we return false

31:14.530 --> 31:17.890
and then we do the same for the other case as

31:20.080 --> 31:28.340
for Rose and ship.

31:33.180 --> 31:40.910
Current position on the ship board to hit

31:43.940 --> 31:44.720
turn

31:50.600 --> 31:52.130
turn false

31:54.510 --> 32:01.400
was if we got here then just return true and they're all it is current.

32:01.450 --> 32:03.580
So it's kind of it.

32:03.600 --> 32:08.590
The side again decides the draw draw function.

32:08.930 --> 32:14.340
OK and again I know that did this pretty quickly.

32:14.750 --> 32:20.460
That's really just to speed things along and I don't think you want to wait and have me explain absolutely

32:20.460 --> 32:21.990
everything.

32:22.520 --> 32:25.110
Again I'm not going to go over to play winner.

32:25.110 --> 32:26.020
I'm not going to go over.

32:26.060 --> 32:26.880
Want to play again.

32:26.880 --> 32:34.020
You've already done that a bunch of times switch players is probably the only other thing that you might

32:34.020 --> 32:37.600
be wondering how we do.

32:37.650 --> 32:47.010
But if you watch one of the in the pointer's practice problems I actually used a I actually implemented

32:47.040 --> 32:48.710
a swap function.

32:49.060 --> 32:56.650
So are you just going to use that swap or at least a variation on that swap function.

32:56.650 --> 33:07.690
So that's kind of it's I think we have a good plan at least and in the next probably couple of lectures

33:07.690 --> 33:09.430
we'll start implementing this.
