So in this lecture I want to actually do all the board set up for each player.
Before that I just added the all the declarations of the functions that you wrote last time to do the
rest of the decorations.
And there's one that I kind of want to change the name of the act well.
So let's change that.
Is at.
This.
OK so I misname that last time so hopefully the DRob boards made sense.
Essentially all this is doing is it's drawing the columns.
The name of the columns here.
So you look at this all it does is draw the name of the column across here in certain amount of space.
OK.
So that's that's all these lines did.
And there's just a couple of spaces separating them.
And then for them we just go through each row and we just draw a separator line which is this whole
thing.
K so this is the separator line and we draw spacing and then we draw another separate line for the other
board because there because I wanted them next to each other and not on top of each other like they
kind of normally are in.
If you ever see the Battleship game in real life we usually kind of on top of each other not next each
other but this is just a save space mostly.
And then we just draw a space and then we actually draw the shipboard row.
So this is the shipboard this first one.
So it just draws the actual shipboard row with like a for the row.
And then any representation of the ship.
And right now there is none because none are on the board.
And then we do the same thing with the gas board.
OK.
So that's really all this is doing.
And then we draw the separate lines for the last for the last dance.
So there's nothing there's nothing too magical.
If you really just pause the video and sort of just read what I did hear it it all should sort of make
sense to you.
So now let's actually do the board set up.
So it's go all the way back here to the board.
And that's actually start implementing the set up boards.
So we're here.
So really what we want to do.
We don't want to draw the boards right away.
We want to do a four
course zero.
Less than the number of ships right.
That's plus
what we want to do is watch shaly draw the board here.
And we'll get the current ship
where ships I and we'll need variables for the ship position and orientation.
This ship Sishen
station
and we will need something called is placement.
Now this should seem foreign to you.
We've already solved this problem.
For now we're just kind of really just interpreting it.
So so do while it's not a valid placement.
What we'll do in here is we'll say we'll prompt the user and we'll say it's not clear.
Name please set the position and orientation for your ship.
So what I would like to do is actually based on the ship type get the name of the ship.
OK.
So I need another function to do this.
So I'm going to call it get ship name for ship type
current ship
type this.
So let's actually implement that function should be pretty simple.
Not just write it above here and all declared after Consta our star get ship name or ship type
or see how even if we do the top down design there's still always going to be some stuff we haven't
really thought about.
And that's.
And that was one of the things is like for your prompt should should you even output what the ship is
right.
Or a ship name.
And we really think that in our top down science so even if you have a top down design it's really again
more of a template or a blueprint for you to actually just just that helps you solve the problem in
terms of what you should do next but sometimes you have to change some things around and that's OK if
you make those mistakes.
Because everyone does.
Aircraft Carrier.
We'll just turn their
SIFF ship type
to this ship.
Time
to user
and they need to scan for when the output what ship was destroyed.
So
destroyer
and it's your
in the submarine.
And there's one more that it could be which is none.
I'm just going to say none.
Right.
There's a different.
There's one more ship type and I'm just going to if it somehow this got past the SD none could see it.
So that's it for that
Sue
we're going to now
continue on with the
set up orders.
So now that we have now they we prompted we need to get the actual position.
So we're going to need another function for this
and it's going to be called Get board position
and we again we skipped this in the top down design because we've kind of done this before.
But this one is going to be slightly different.
So going to go here is up here.
And this is actually going to return a ship position.
So and a ship position will consist of Basically we're in a prompt for a row and then they have to input
some row a to j k and they'll have to input some call them 9:59.
OK.
So that's what this is going to do.
And the new tools will already has the character with all the valid inputs so we can do all the valid
inputs being from A to J but we don't have one for the integers so we're going to make one of those.
So it's good to battleship
is
OK.
So let's go back to set up boards and implements right below and get board position.
So it will be pretty simple.
So one is going to be the row input and one's going to be the column.
But and the valid use input.
So school can be 10 of these a
DE
he.
So those will be valid row inputs and we'll need one for the valid integer inputs for the columns.
Kountze but this and these will be from 1 to 10 and we haven't made that function yet.
Well it's like
so and row input equals get character.
So please input a row
and 8 J.
That's an error string
and valid input or a row input.
It's
and the board size and actually make this uppercase
character
it's this
k.
So if we run ins build's
spoke incorrectly it's
Rose but it's misspelling things.
You know OK.
So you should be fine.
So we need to have a get integer that has the same kind of signature as this one.
OK.
So we're going to put that in here.
It's in our tildes library.
Let's say get integer anarchist's a char approach
jar's error.
Konst valid input.
An invalid it's like
it's
OK.
And effectively we're just going to copy paste surreally for the most part are going to delete some
things that although we don't need you know let's just last night and bother.
So we need the input and we need the
input each year.
And we've done this so many times now it's always fun
while failure
is input failure is false starch and we just see out the prompt.
In Butch if this does fail
then we will say in a clear seemed more
and copy one thing.
Do you want a copy of this
and nor
characters up until new line in space.
CH air
and the failures.
In other lines we were actually looped through the whole valid inputs it's the
Woss an if input equal to one of the valid inputs
then return the input.
Otherwise if we get here then we know we did something wrong.
So just put the air
and say that it was
and here this shouldn't ever get here.
During one and that's it.
Now back in Battleship we can actually say call them in Butch
get integer
please.
It's a column one to 10
this and put each
airstrip that invalid
to this sort of again column inputs
and such towards
and then return.
So now that we have this Actually since what we're getting is one of these valid inputs right.
This won't map properly to the tutelary.
Right.
Because first of all this is these are characters which is not correct.
And second of all this is kind of one off from what we need to actually index into the to the array.
So the last thing that we're going to need here is another function and we call it map board position.
And I'm going to give it the row inputs and the column inputs and so what map or position do is it's
going to actually
it's going to pass back a ship position type in the 0 2 9 range for each row and and call.
So how we do that is since we know that we always get it correctly just call a real row is the row row
inputs.
Yes this is the character
integer.
So the row input we will minus by a right because if it's a a minus a zero K and for the columns should
be easy.
Column input.
Put CH minus 1 right.
It's always a one off.
So now we just need to make a ship position
or position and all it would do is a position row to the real row and board position and column is the
real column then we return.
Igor
OK.
So that's it.
So who's still building Kirkley.
Sure.
Maybe misspelt this or something.
It's here again.
And
works.
Tertiary Wise's new problem honestly.
There's always the problem of doing live coding here.
Never know if it's going to be correct or not
re-issues is correct no one is complaining.
See
gay tells you what I did to change that.
Maybe I misspelled something.
Anyway that's fine.
So actually it's a column.
So now we have the get board position.
And now we need to do the same thing with the orientation X you need to get the orientation here or
ship or orientation.
The nation can't teach
like this.
Get a ship.
Maybe.
OK.
So we're going to need another function for the orientation.
So let's make that right now.
Here's ship.
She can type
orientation and let's define it right after the get or position here.
And this will actually just turn you back a ditch orientation
so we're going to find an orientation
this
and essentially what we're going to input is either in an H or E V and valid inputs only have two in
there.
H.
Each for
this H for horizontal V for vertical OK.
And we're going to get it to character
please choose
Jason each for
is all v or vertical.
It's air string.
And the valid input each to an uppercase
and then if the input is
valid input 0.
So it's H.
In this case the return or set return.
Yes I did that you or
we actually got this or otherwise we just return.
In it for that one.
Now you actually have the orientation and the position so the last thing you need to do is check to
see if it's valid or not.
So we're going to have to make a function call is valid placement
is valid placement is this and I'll take the where in the current ship and the ship position and you
are anti-Asian
so.
Copy it.
It's
like that and find it here to clear it up here.
And return true or false.
That's correct or not or is valid or not and I don't think you changed the player at all.
Or the ship.
No actually let me.
Scott
mission
and this is
T-shirt.
Copy that
in its pictures.
The smaller it's fine.
Looks better.
Think so.
Now we need to check to see if it's valid placement.
So let's put this here.
And let's go through.
How did this work.
So it's appear somewhere.
So we checked to see if it's horizontal and then we go through all of the columns if it is horizontal
because we're all columns to see if it will overlap with another ship or if it will go off the board
in the horizontal direction.
OK so let's go through here.
And of course vertical in the case.
Right.
So if the orientation is is all
then we'll go through all the columns that it takes up.
So it's going to have to start at the ship position.
Daut column right.
And it's going to end at the ship position
column plus the current ship got its ship size right.
OK.
Plus of them accent so it starts at the actual head of the ship.
So the ship position is really the head of the ship and then it will go however many spaces that the
ship actually takes up and it will check.
Now if the
lares ship or if there is a ship here any ship position in a row see and ship type.
So if there's nothing there or if there is something there that means it's not equal to it's not right.
Or if the column is greater than the board size then this is false.
So if it overlaps a ship that's what this says or if it goes off the board in the horizontal direction
then this is not a valid placement.
OK.
And we need to do the same thing with the vertical case else.
Our schools ship Sishen row
are less than.
So if the player ship board
are and the ship position we call them CH ship type
is not equal to you know right it's like before and our is greater than uncoated board size.
And you know it's off for
turn offs.
Like so
as we retire and
then that's it.
So check to see if it's an odd placement if it's not a valid placement.
We want to say that it's not for these
say is ouch that is not a valid placement.
These
like so
so is.
So now this is basically done.
And what we want to do is actually now place the ship on the board.
Place ship on board player in the current ship ship position in the orientation.
Copy that and
Claire up here
turns nothing.
It will change the player.
It's
and
probably won't change the current.
Well actually it will change the current chip ship that's in and it won't change ship.
And of course
it there teach
so.
But this is after here.
After here.
So like I said this one works effectively the same as this one.
So but the first thing you do is actually change the current ship's position.
It's
type ginger up here.
Type
so it isn't the position
and the orientation.
Orientation.
And now we just go through if it's horizontal
or Until then we'll go through and actually set the shipboard
so there.
Ship Board
in row
column are actually for and see are positioned column and see is less then shift position column loss.
The current search ship docked ship size
plus.
So that ship type should be the current ship type
or current ship to ship type this
and the we should say it's not ship ship.
Just in case mostly
and we'll do the same for all the other.
We're
are going to ship position
row is less than
ship and row plus current CH ship ship's size are apples plus
El-Arish board
ship visits or of our and ship
dot com
ship type is the current ship ship type and where we
he is here.
Is false.
OK.
So that will actually place it on the board and really the last thing they need to do for this is show
the actual results.
So here will say the drop boards again
where K so
this Scherf things compiling Kirkley is in the running.
So we have our ports as a ship board and this is the gas board.
So now it's asking us to input a row.
So let's put the aircraft carrier in a calm one and we will put it horizontally.
So how it actually drew it correctly so you'll see an A for.
So there's five A's because that's how big the size is.
Right.
And it's actually drawing correctly.
So let's try to put the battleship kind of off and see if we can kind of break it.
So let's put it on Robi at 8 or 9 8
hate and we'll make it horizontals well.
So see it says that was not a valid placement.
Please try again.
So it's working and it's asking us to do it.
So I didn't skip it right.
So let's just put it somewhere.
B one Birchall.
There it is there.
Now it's asking for the cruiser in we'll put it at.
See
Six Arthel it's cruiser.
Let's put this one at Jay
three Santal and let's put the submarine at its g and now 10 vertical.
So there's there's player ones board that it's asking for player 2.
So this is placing correctly.
Think so in the next lecture we're actually going to sort of finish off.
I think the rest of the code that we need to tell you already written a lot of code already but and
we've only really done the set up of the game.
But actually the rest of the game is actually much easier.
So this is really the hard part.
This is kind of more of the hump and now we will in the next lecture we will actually finish the rest
of the implementation.
See you in the next lecture.