video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher for the Programming Part 6 unit.

For this lesson, you are going to need your success criteria from the other lesson, your design work, as well, from the other lesson, and you're going to need your Repl.

it account that you should have already set up with your parent or carer's permission.

It would be good idea to have a pen and paper handy as well, to make notes and to try and remove as many distractions as you possibly can so that you can really focus in this lesson.

Once you've got all of that ready, we can begin.

In this lesson, you will create a solution for the Battle Boats programme.

First of all, then, why do you think it might be important to keep a log of your progress with this project? Pause the video while you have a think about that.

Well, a code log allows to keep track of the decisions that you have made along the way.

They also help you to remember where you are in your project, if you need to stop and revisit it later.

Periodically, you should ask yourself, what has worked well? What has been challenging? And what are my next steps? So what I want you to do is set up a code log.

So either electronically or using pen and paper, decide where you will keep your code log and set it up so that it is ready to use when you need it.

Copy the questions from the previous slide to support you with this.

Pause the video while you have a go at that.

So, it is now time to start coding the solution to the Battle Boats game.

This is meant to take around four hours to complete.

You can try and solve the whole problem in one session, or you might want to break it up over several days or weeks.

It's up to you.

After the next pause screen, I will go through the solution to each task.

Do not watch this section of the video until you have attempted all of the tasks yourself.

So use your success criteria and design work to code the solution to the Battle Boats game.

Good luck.

So let's see how you could have solved this problem.

Here is the completed Battle Boats game, then, up to task seven.

So this doesn't include those explorer tasks, and you'll be able to access the link to this file after this in the video, so that you can study it yourself, too.

So what I'm going to do is I'm going to just try my best to talk you through it so you can see how it works.

And this will hopefully confirm that you've done things how you should've done them, or it might give you a bit of support if you're struggling with some aspects of this project.

So, first of all, you've got some input statements at the top, which you're going to need throughout the game.

You've then got a function call.

Sorry, not a function call, you're defining those subroutines there and defining another one, defining another one.

There's lots of subroutines in this game, and lots of code, too.

Defining another one, then another one, and another one, another one.

There's lots going on here.

But the very first thing that is called there is menu.

So right at the bottom, you've got menu.

So on line 262 on this code, you've got a function called menu.

So, the first thing that will be actioned is that subroutine menu, so we need to just find menu.

I think menu is at the top.

Yes, it is.

So, then it goes to menu and you've got here a bit of a graphical representation of a boat just to make it a little bit more interesting.

So welcome to Battle Boats, giving them some options, would you like to play, resume, read the instructions, or quit? And then you've got a selection statement there to decide what's going to happen.

And if they're type of 1, it's going to go to the play subroutine.

If they resume the game, it's going to go to resume.

And if they type 3, then it's going to go to the instruction.

So if I just run that, you can see the Battle Boats game there on the screen.

If I want to do the instructions, if I want to type a 3, it's going to display those instructions and it just says press Enter to return.

And if I press play the game, then I'm going to start getting some gaming, the beginnings of that game.

So that's just that subroutine, that procedure there, working and getting it going.

So if they do put, let's just go for instructions first, 'cause that's probably the simplest one.

So if they type a 3, then it's going to go to the instructions.

And I think the instructions subroutine is right at the bottom, yep, there it is.

So, instructions is just going to go all the way through those instructions there.

And then right at the end, you've got another subroutine call to the menu, which is going to go right back to the top, back to the menu again, giving them those options again.

Now, this is where, if they do want to play the game, then it's going to go to the play subroutine.

So now I'm going to find the play subroutine; here it is.

So when the play subroutine is called, it's got no parameters passing to it, but it starts off by creating a blank player grid.

And this is quite similar to the Noughts & Crosses game, where it did the blank Noughts & Crosses grid.

It's just a much larger grid, isn't it? 'Cause Noughts & Crosses is much smaller than this.

It then allows some boats to be added to the fleet grid.

So if you look here, we've got now a subroutine call and we going to pass it the fleet grid.

So at the moment, the fleet grid has just got those coordinate parts there, and it's got those blank spaces in, for where the user is going to put their battle boats.

So, then we go to the add boats subroutine, so let's find add boats, and there it is, and fleet grid has been passed to it, so this 2D list here has been passed to this subroutine.

And then it's got an extra line here of code so that you can see the title for what this thing is, and it's the player's fleet, and then it says display grid, and then it's passing the fleet grid to it.

So let's just see what display grid does.

And display grid is, again, similar to the Noughts & Crosses code, but obviously much, much larger, because the fleet grid and the Battle Boats grid is much larger than the Noughts & Crosses one, but it's pretty similar code, isn't it, in terms of how it displays it? So it's got a line for each one, it's got coordinates, it's got those.

Where it is, the row or column of that specific item.

If you think back to how it looks in its structure, that's its structure originally, isn't it? So this is your 2D list, and this is it displaying that 2D list in a nice, neat way on the screen.

So once it's displayed it, then that's the end of that subroutine, so we're going to go back here and it's going to say, build your fleet by entering coordinates for each boat.

And at the beginning, boats are equal to 0 because it's obviously starting off.

There's no boats on the grid, so it's going to start off with 0.

And then we have a Boolean variable here, adding boats equals boats less than 5.

So, obviously boats is less than 5 the first time this runs so this is going to be true.

So this is going to run at least once, so it's going to add boats until there are 5 boats.

So if I just run this code again, come on, here we go.

So, now when I go to play a game, you can almost see what's happening now behind it and what's happening there.

So, you've got that blank grid set up.

It's then displayed that blank grid 'cause I haven't entered anything in there.

And now it's saying, build your fleet by entering coordinates for each boat, enter Battle Boat coordinates.

So for example, A1, so if we wanted to put in A1, I could type A1 here and it should put a B there, but let's just see what happens here.

So it says enter Battle Boat coordinates, for example, A1, which is what we've got there.

It's asking for some input and the prompt is there.

And then it's got coordinates equals grid location coordinates.

So now we're looking for that subroutine.

So, where's that subroutine? Let's see, it's not up there.

Where is it? Here we go, grid location coordinates.

So, calculates the 2D list location based on the given coordinates, 'cause they're going to type in something like A1, for example.

So if coordinate 0, so if that first item in that string, the element in that string is an A, then it's going to say the column is 1, if it's a B is going to be 2, and so on and so forth.

The reason it's 1 is because if you look, you've already got 0 used for the coordinate heading, so that's why it starts at 1 and not 0.

So, then you've got, the row column is worked out here.

So the column, let's just go back up to the top, where's the column? So the column is sorted out for you here.

And where's the row? Ah, here we go.

So here's the bit for the row.

So, the row is looking at the second element of that string that is typed in, and then that's going to add that bit to it.

So, row column equals row and column.

So if I type an A1 in here, it's going to first of all say that A is going to set the variable column to 1.

And then when it gets to here, it's going to say that 1 makes the row 1.

So we're going to have 1, 1 in here, and then it's going to return that list, row, column.

Back to the previous one.

So where were we? I should have kept it highlighted, shouldn't I? Where were we? Oh, I did keep it highlighted.

That was handy, so, grid location coordinates, and it's pulled back that list of coordinates, so that 1, 1.

So, now it's going to use, the row is coordinates 0 and the column is coordinates 1.

So it's just putting that list, those items from that list, now, back into some variables called row and column, and then it says if fleet grid row column is equal to B, then it print occupied because you don't want to put.

If they've already got a B there, then we don't want them to be able to put two Bs in there 'cause that would be silly.

That won't work, so it says occupied.

Otherwise, it's going to add B in that location.

So, if I just put A1 and I press the Enter key now, you can see that a new board has been displayed and this time a B is there.

And the reason a new board is being displayed is because, if you look here, we've got that display grid fleet grid again.

So, we can go back to that code display grid, which is here, and the new fleet grid with that B in that first location is now there, so when it's displayed, you see that B.

And it's going to.

We've got here, the Boolean variable appears again, so that if it has now got to more than 5 or 5, it's going to stop this loop from running.

But this loop is going to keep running until it gets to 5.

So I'm just going to put a boat on A2, A3, A4, A5, and then it should stop, yeah.

So, it's printed the locations of the opponent's grid, and this is for testing purposes.

I'll show you this little bit of code in a few moments, but at this point, I've got my 5 and now it's moved on.

It's returned the fleet grid based on where those positions currently are.

And now I can go back to play.

So it's just trying to figure out where I am now.

Ah, here we go.

So, we're just on that bit to set up the fleet grid and now we're looking at the opponent, which is computer fleet.

So now I've got to find the computer fleet one, so where is it? So here's the computer fleet and the computer fleet is randomly deciding the location of the computer's fleet.

So you've got an initialised list of fleet and then you've got columns and rows.

Boats is 0 at the top, at the start, and we've while boats is less than 5, so this is very similar, this little bit, to where the players set up, but then this is quite different because what it's doing is it's just choosing something random from the columns, choosing something random from the rows, it's creating it as a coordinate and if it's not already in the fleet, then it's going to append it and then it's going to return it.

So, this is the way we get a computer to do it, which is very different to how we get human to do it, who types in A1.

With the computer doing it, it's got to be random, hasn't it, and chosen that way? And it's also checking, it's making sure that it's not putting the same boat at the same location each time.

So that's what's happening there.

And then we just return back to our code where we were.

Where are you? There we go.

So then it's going print opponent and as I said, not required for the actual game.

So that's what's happened there.

So if this was going to be the real game and you were getting somebody to actually play it, then you would make sure that you got rid of that bit of code.

You could put a hashtag in front of it so it didn't appear.

But for testing purposes, it's very useful to have it there.

Then you've got target grid and target tracker is now run, so we've got to find the target tracker.

Where are you? There you are.

So, this tracks the hits and misses on the computer's fleet.

So that's what's happening here, and it's going to go.

There's the target grid initially and it's going to return that target grid at that point, so that's what that does.

Then you're going to have player casualties, computer casualties, 'cause we've got to keep a track of those so that we can see who's won.

Because obviously, once one of those numbers gets to 5, that means that 5 boats have been hit, so there's going to be a win at that point.

And then we also need to store the player moves and the computer moves so that it can keep track of what's been typed in.

And then it's going to start off initially with a turn for the player because the player is going to start first, not the computer.

And this is the actual gameplay.

So now you can see where those player casualty variables come into play here.

So, while player causalities less than 5 and computer casualties less than 5, we're going to keep playing this game.

So, as soon as somebody gets 5 casualties, somebody's won and somebody's lost.

So, if turn is equal to player, you've got all of these options.

So this is all to do with the player.

Elif turn is equal to computer, then it's going to go through all of these bits of code.

So this is just the player taking a turn, so it's going to show the target tracker on the screen.

And this is to keep track of the hits and misses.

We've got another display grid, but this time it's sending it target grid, which is not fleet grid this time, it's a target grid to keep track of it.

Get ready to fire, enter target coordinate, so it could be A1, for example, and then they input those target coordinates.

So, if I input F6, 'cause I know that that one's been used, you can see that it says hit there.

And then the computer, when it's the computer's turn, the computer is going to randomly come up with a hit.

So, you can see, we've just skipped a lot of things now, we're just going to the computer so I'm just going to show you what happens when the computer takes a turn.

So it takes an aim, it does a hit.

So let's look at computer hit and see what that actually does.

So if you look at computer hit it's almost exactly the same as when the computer set up the fleet.

So it's a very similar bit of code here where it's setting up the fleet.

But this time it's just picking one at random and it's randomly picking it, and then it's going to return that coordinate that has been randomly picked.

Okay, then it says, while top computer target in computer moves, computer target, computer hit.

And that's just checking if it's already made that move, it's going to run it again because you can't obviously make the same move twice.

And then it's going to print the computer target.

So it's going to say, G5 is the one that came up and it was a miss in this situation.

And then it will keep those moves in a list so that you can keep track of them and use them up here when it's doing the next hit, and if it's a hit, it's going to add it, and if it's a miss, it's going to ignore it in this situation.

So, then it goes to the next player.

So I'm going to put, this time, where was it? Which one was it? H1.

Let me put H1, and it's a hit again, so it's put a H in their, computer takes aim.

So all that code is running again, it's randomly chosen D8 and that's a miss.

So now I get to choose again, and this time, I'm definitely going to choose something that's not there, just so that you can see the other option.

So what's not there? So I'm going to go with.

Oh, I've already done A1? I can't remember.

No, I haven't, so A1.

So I missed this time and you can see an M is in there rather than H.

So I've got two of their boats and they still missed mine.

They haven't got mine yet.

Get ready to fire.

Probably should have written these coordinates down so I didn't have to keep going back.

So F7, B7, B5, I won't remember that.

F7.

Oh, perhaps if you press the Enter key.

B7, I think was the next one, oops! B7.

I can't remember the next one.

I'm going to have to go back up and check.

So it's still missing.

It hasn't got my fleet yet.

I'm going to have to go all the way up.

B5 is the last one, so I'm going to put in B5.

Okay, and then it just says "YOU WIN!" there at the bottom.

So, because we've gone through game play and I've got, or the computer has got 5 casualties, this while loop has terminated, and let's just see what's right at the bottom.

If player casualties is equal to 5, then computer wins, else you win, because if the computer hasn't won, then you've won, haven't you? So that's the other option.

So that's just a brief whiz-through of how this code works.

Hopefully that's helped you out a little bit if you've needed to, and on the next slide, you'll see the short link to this and you can have a good look at it too, but yeah, use that to help you if you need to.

Hopefully you've already had a good go and you've made good progress with this programme, but if you needed that for a little bit of extra support, then hopefully that's helped you out.

So, here is that short link for you to have a look at the code yourself as well, if you want to.

Well done! That was a huge, huge project.

So, even just trying and giving it a go, don't worry if you didn't get all seven tasks done, you might've got to three, you might've got to four.

The perseverance is what I'm the most proud of, and I hope that you found my little going through the code, as well, a little bit more useful.

So, you're quite welcome to spend a bit more time looking at that solution and trying to build that in.

And maybe once you've understood that, you could have a go at some of those explorer tasks as well.

Now, if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and #LearnwithOak, 'cause I would really love to see what you've been getting up to, and the code that you've done and how far you've got, so that I can celebrate your progress as well.

And I'll see you again soon for next lesson!.