Now let's do our top down design for Battleship and let's start with kind of the highest level.
So what we should do and we should again take our data design into account all this data that we did
last time and we should try to make sure we're using that properly.
So our really our top level design of it will be let's say in it's player
and using one of our players play one this and that player or two and we're knitting them because if
you remember in our ship we have this tie but this doesn't define the actual variable itself.
Right.
So we actually have to set all our ships for each player right.
Since the player has all the ships.
So we need to set all the ships correctly and give the player one ship or one type of ship in each elements
long for this array.
So off you get the players somehow and prior to well since we we've done this do time for it will say
do play.
Play a game and we'll play a game with the players.
Play it one player or two.
And again while Want to play again.
Right.
So we're not going to go through want to play again anymore.
I think we know how to do that.
Now for the top 10 design anyway you want to implement it of course.
And so now this is kind of our top level design we need to initialize all the player whatever that means
set up all his ships.
Maybe his name is well maybe once you actually send him a name you know Player 1 as kind of the input
player to
write.
So we have to do something like this.
So it's kind of it's going to call it.
I call that steps before as long as step one Lisa is kind of the main function really and let's start
breaking down how to play the game.
That's probably the most play your game play a game.
So play again.
So how would you play the game.
Well he said in up here in the description
we should arrange our
arrange our ships for the players.
Each player has to arrange their ships on the board somehow.
Right.
So we'll call that set up set up board and we'll do it for each player.
Player 1 and set up board for a player to I know sort of looks like code.
But again it's not really code.
It's kind of a whenever it is pseudo code here.
OK.
And how so then what.
So once we set up the boards we will
say Do you know maybe we should draw the board of the current player chalkboards and we have something
called like a current player.
Whoever is taking their turn
and maybe we also need to go through and get the guess that the player wants and the guests will have
to be valid.
So for example we couldn't guess a spot we've already guessed before right.
So that would be not OK.
So we need to somehow take into account that they can kind of mess up and pick a spot that maybe they've
already picked up.
So we'll do a loop for this kind of like we did with input with incorrect or just incorrect input and
we'll say do while.
So not guess is out.
OK.
And we'll get the guess and we'll probably have to give the players the courage later.
And inside we will.
The current Each player or a guess and get the guess or just a guess.
Calls get this this
and yeah now we do for now.
And the while condition for this one and this one simple while not so great.
In the Loop While we're still playing the game now down here maybe we should update the board or update
the boards with the guess of the current player and remember that if it was a hit we should also update
the other player.
So maybe we have another
another variable that's the other player.
OK.
And those were not using player 1 in player 2 because we don't know whose turn it is right whoever's
the current player that's that's their player that's taking their turn.
So we're kind of distinguishing between player 1 and player or here and other player and current player
so update the board somehow with the guess and then maybe draw the board again.
Onboard
current players want to see their results right.
See your result gets
an then we should see.
Maybe if ship was sunk
on current players turn
right we ship the output at the
player sunk that ship somehow.
Again not actual code just pseudo code just this is really just the template for the plan right for
our implementation.
And I'm actually going to use something that we kind of only briefly went over which is wait for key
press.
So that the player the current player can just like type something and then and then all switch turns
OK and then switch the player's right because we want the other guy some k
and then once the game is over again will display the winner.
It's like this like normal.
Hi that J.
So that's play game.
Now let's break down break down the set up board start from the top here.
It's set up for and we just take the player into whatever the player is or who or whoever reads K and
how would we set up the board.
Well we should maybe clear the board first.
Because what if we already played a game and we wanted to play again while all the ships would still
be in that in those positions we kind of want to clear the board so everything's set back to the initial
state.
Clear the board of the player and then for all the ships right.
We should
maybe draw the board draw boards for it player and then we should have a current ship's Eric Hernshaw
is
to get current CH
ship for the loop.
And we should place the ship somehow K and like it said up here.
Hear ships cannot overlap right.
So we should prompt the user to input a location and orientation for the ship on the board.
OK.
So say he chose Jay won something and he wanted to do it vertically.
Right.
Well you can't have a ship go down vertically if you're on Jay.
Right.
Because it'll definitely be off the board.
So that would be a problem and that's not a valid placement.
So we need to kind of loop to
get the position and orientation of the ship you want to place on the board.
And if it's not a valid placement then we should try again.
OK.
So essentially we want to get the board position then get the ship orientation this is positioned for
kind of head of the ship yes.
And the positions really are going to be the head of the ship and then whatever orientation we choose
we'll just go either down if it's vertical or to the right if it's horizontal.
And we should check to see if it's a valid placement if valid.
Is valid placement of the ship current ship
and the say
sition.
Is this an option.
Is this the position orientation.
And I guess we need the player as well because the player has the boards.
So if it's not a valid placement and we should say put it's
easier to player not a valid placement
so we do this while
it's not a valid placement.
Right.
So copy all this
is probably like a Boolean or something.
Take that out and say Is this just a variable placement
goons.
So it's not a valid placement.
And while it's not about laser.
And then once we get a valid placement then we'll place the ship on board the ship on board somehow.
Well here are ship
and Sishen orientation
and that's really how you set up the board.
That's all I can think of to do.
So notice I'm kind of I am sort of getting into the implementation of it kind of.
But it's really just based on
our past experience.
So maybe we should break down what else should we break down.
See from.
Game.
All I see is yes valley something that's
from play game.
Well actually let's break down some stuff from this set of boards first.
Actually So there's a couple of things.
Is valid placement is one right now.
It's
current.
Mission th
plate.
So let's break this down.
So how would you check to see if this is a valid placement.
Well we should note that because of two cases really it will be you know sort of dependent on the orientation
I think of the ship want so maybe you'll have two cases.
So if the orientation is horizontal
and otherwise it's two cases and we'll say
for all the Hollonds he current ship would take up.
Right.
Because ships have a certain size right and aircraft carriers five.
So they take up five slots or five positions.
So for all of them that they would take up if the current
If the current position on the air ship board is taken or if
the ship will be off board was onto the
then will just return false because that's not about placement.
OK so if the current position is taken meaning like if we overlap really this is what this means if
if the ship will overlap another ship
or a ship will go off the board horizontally then return false and we do the exact same thing here for
vertical and for all the rows.
In this case.
Would take up
would take up.
And again if ship
another ship or the ship
off the board vertically anime will return false
otherwise I guess here here we just returned true.
Because clearly it went through all the spots that the ship would take up and we found that it didn't
take up a thing and it's about placement.
So
so
now
we could you please ship on board but it's going to look actually quite similar to to is valid placement
but maybe you should do it anyway.
So we're just a place ship on board where that ship the ship in orientation.
So I actually set the ship's position either position.
Right and I'm using a dot here because that's what the data is.
Position we access struct members through the dot operator so ship and then your station as well
Asian
we say if the orientation
is horizontal
and we know the position orientation is valid because we've already kind of checked it all.
So you've already checked it also.
We know that everything will be kind of fine if we just place it for all the columns.
Ship.
Essentially what we have to do is set the ship port on the board at position dot row in the current
column parents are going to go through all the columns and then set it at that row and then add that
column
and we'll do the same for the case for all the rows ship Jake will forget how to do this after implementation
said.
Ship or food at
the current row in position call.
So like I said it looks quite similar to this one.
So now what should we break down.
We know how to get a gas will do the update Portes actually wouldn't be that hard.
So update ords
where the other player
came.
So what do you do here.
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
apply the damage and all that.
So if it's mission or if the gas
on board has
or if the the other players
ship board at Cas
is a ship then that's it.
OK.
We hit
hit.
Case Kansas set hit on the current players.
Guess board right.
And
an apply damage to the other players shipboard great.
And then if you didn't it
then all we do is sad said Miss the current players get bored.
He's missed.
That's it.
And one thing we may want to do is actually return back
the type of ship that was hit possibly in that so we can check to see if the current player sunk ship
so possibly that maybe be one thing we want to do.
So I say return
ship type
here.
Actually in this case.
And then we could return ship or s t here.
Because we didn't hit one.
So this is one of the reasons why I wanted as he see none.
It's actually for to see to check to see if there is an actual ship that was hit on your ship board.
So most of the ship board is empty and that's what really Esti none will mean.
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
none is really just means in our ship board.
There's no ship there.
OK.
That's why we have the ship type here.
One of the reasons why we have the ship type here.
So so that if if there is no ship at all then you can't possibly have hit it right.
OK.
So.
So that's why S-D none exists.
So what else.
So maybe is game over.
Break down.
And they're not going to break down and draw the boards because I always feel like drawing functions
should be kind of just done on implementation at least for these kind of games because you kind of want
to toy with it a little bit and tweak it and it's really really not going to help you that much if you
kind of break it down first.
So it's game over with player 1 and maybe player 2 as input.
And really it's over when all are all ships sunk.
Player one or if all the ships are sent to
Nazi yet.
Now we have to break.
Of course break down all ships sunk
player.
So for all players ships
if not is sunk.
Here current ship
then return
false.
So if there there's one that wasn't sunk then he can't possibly have or not all of his ships sunk so
otherwise return true for get here.
And for is sunk
ship for this one.
We will again check to see if the ship's orientation
Kishen of its horizontal
than seriously what we'll do is we're just checked to see for all columns that the ship takes up.
If that's the position current position
on the ship board is not hit
then we return false
and then we do the same for the other case as
for Rose and ship.
Current position on the ship board to hit
turn
turn false
was if we got here then just return true and they're all it is current.
So it's kind of it.
The side again decides the draw draw function.
OK and again I know that did this pretty quickly.
That's really just to speed things along and I don't think you want to wait and have me explain absolutely
everything.
Again I'm not going to go over to play winner.
I'm not going to go over.
Want to play again.
You've already done that a bunch of times switch players is probably the only other thing that you might
be wondering how we do.
But if you watch one of the in the pointer's practice problems I actually used a I actually implemented
a swap function.
So are you just going to use that swap or at least a variation on that swap function.
So that's kind of it's I think we have a good plan at least and in the next probably couple of lectures
we'll start implementing this.