video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher and you're going to need your Replit account, which you should have already set up with your parent's or carer's permission this lesson, you're also going to need a pen and paper to make any notes that you need to.

And it's a really good idea 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 use a 2D list as part of a programming challenge.

What I'd like you to do then is just start off by matching the keywords to the definitions.

Pause the video while you have a think about that.

Excellent, let's look at the answers then.

So each iterative testing, which one? Testing throughout the creation of a programme.

Final testing, testing at the end of programme creation.

Erroneous test data or erroneous, that would be better, incorrect data type for example, enter string instead of a number.

Boundary test data then, so values just inside the limits, and then normal test data is typical or expected data.

Now you're going to be programming a noughts and crosses programme this lesson.

So noughts and crosses is a two-player game that is played on a three by three grid.

Players choose to be noughts or crosses.

Each player takes in turns to place their piece on the grid.

The first player to get three in a row wins.

Pieces can be placed horizontally, vertically or diagonally to qualify for a win.

You will be creating a two-player noughts and crosses game for your next programming challenge.

So your first task is to play noughts and crosses.

Now, if you've got a member in your household that you can play noughts and crosses with, then you can do that.

If not, you can just play by yourself and just pretend but pick which one's going to win and, which one of you is going to win and which one of you is going to lose.

So you'll have to sort of split yourself up a little bit while you do that.

During game play, think about all of the little steps that need to be taking place in order for a player to get a winning position.

So pause the video while you play noughts and crosses.

Right, what I'd like you to do now then, now you've had a go at playing noughts and crosses.

I want you to think about the decomposition of this actual game.

So use the decomposed noughts and crosses section of your worksheet to break the game down into manageable steps.

And we'll revisit some of those steps in a moment.

So pause the video while you have a go at that.

Excellent, so here are some examples of the manageable steps that you might have broken it down to.

So this is pretty much how you would play the game.

So you've got some instructions, decide who is noughts and who's crosses, draw the board, player takes turn, place the piece, check if a player has won, move on to the next player and continue that until somebody wins, and then ask maybe if they want to play again as well.

So you might have had something like that.

So what we're going to do, we now have basic starting point for our decomposed, with our decomposed steps.

The next step is to start forming a structure for the game.

A structure has been created to support you with the planning of this game.

So here is a structured chart, and this is also on your worksheet as well.

So you can study this a bit longer if you need to, in a few moments.

So if you look at that, we've got noughts and crosses is the main thing that we're going to make.

And then it's been broken down into all those sections.

So it's been decomposed at this point.

You've then got the interface for each one, so we're going to be creating a function or a procedure for each thing there.

And you've got the identifier, the parameters, and the return if there is going to be one that will help you know if it's a function or just a procedure that you're going to be using there.

So this sort of gives you a little bit of an idea, a little bit of help really at getting you started thinking about how this might be coded.

So we will start by implementing these sections.

So you've got two sections here and the first task you're going to start programming this bit.

So what I want you to do is use the getting started section of your worksheet to help you think about grading the game board and checking for a win.

So pause the video while you have a go at that.

Excellent, so I'm going to go through that to see how you could have found the solution to those two bits.

Now to save time, I've skipped ahead just a little bit.

What I've done, is I've looked at the worksheet and I've copied the code of the board, and then just that little code snippet there for accessing somewhere in the board.

So adding an X on for example.

I've just put that in there.

Then I've got that procedure that, yeah, it is a procedure at the moment for displaying the board which you could find using that short link if you aren't quite sure how to get these bits of text here.

They can be quite tricky to do yourself.

So if you want to grab those off the Replit link, then of course you can do.

Now, this board only had an example of the first section, so I've had a little fiddle around just adding the extra parts of the board in.

So if I press run now, you can see that it does display the board, and I've got an X on that board too.

You can see as well in the example code, 'cause you only get given this little bit here.

And this just looks at that first row here.

So zero, zero, zero, one, zero, two.

So I've gone through and made sure that it looks at the next row and the final row there.

So now when I display the board, I've got a board there.

I'm happy with how it's displayed.

I've put an X there and I'm happy with that.

I can swap it round to a one and it'll move to that location.

So I can just try different locations out just to make sure that it works and that it looks good wherever the X actually is, and I think it looks fine.

So I'm just going to keep this bit of code here 'cause I might need it later.

Also for testing purposes as well, you could just change the values here that are up here.

So this is just assigning a new value to those.

Or I can just manually type it in there without this bit of code, but I might need this bit of code later.

So I definitely want to keep it here.

Now the next bit, so I've done, I'm just going through what, I'm just looking at my piece of paper down here to see what I have done.

So I've got the board set up and now I'm looking at checking for a win.

Now I'm not going to sit and write down all the combinations 'cause I've already coded this before.

So I'm not going to write all the combinations though.

But I'm just going to show you sort of the starting point and what you would need to do.

So it gives you a code snippet And this is the code snippet that you'll give and I'm running out of space here on my screen 'cause I've got such a large font.

So this is the code snippet that you are given.

And this just literally checks for that first win.

And if you think about how the board works, so this is checking if someone has got an X in row or the player, whatever the player value is, is in that top row.

So if you think about this top row here, if there is an X in those three, then it's going to say won is true.

So then you've got to work your way through every scenario of winning and have a big load of if statements and elifs, and then else, to work out the winning bit.

And it's all just about using this similar structure again.

So your next one would be an elif, and then you work through the next one.

So you do the next row and then the next row, and then you do the ones going, the columns and the columns and the columns, and then the two diagonal ones that you've got.

So you'd put those in here, and I'm just going to paste those in now.

So as if by magic, I'm going to paste in my other bits of code for that.

And you can see now, now it's quite tricky to see on my screen, but you can see that I have gone through each of those options for winning and the very end it will return won.

So it will either be true or it will be false.

So it sets it to false at the beginning, and then if any of these options become true, then one will be true.

So that's what's happening there with that bit of code.

So I've done the checking for a win bit, that bit of code but now I actually need to test it and see if it works.

So I'm just checking I've done everything, complete this function using.

Yes, I've done that.

So the next one is test your function by editing your board so that X wins for each possible combination.

For example, to test the first combination set the board like this.

And it tells me to put an X here in each of these rows.

Whoops! Want a capital X 'cause it's looking for a capital X, I think There we go.

So I've put an X in the top row and it does say, 'cause I don't think it's going to work yet.

No, it's not saying won or anything at this point.

So we're not that far.

It's just put the Xs in at the top at the moment, it says tip.

You will need to create a variable for the player to ensure that this argument is passed to the check win function.

Make sure that the player holds the value X or your programme will not work.

So I am going to put that here for now.

So player equals X, okay? To make sure that your code works, you will need to print won and see if it is true when a winning combination is used.

So here is an example.

So I'm going to have right at the bottom here.

Yeah, I'm going to have right at the bottom here, print, and then I'm going to have an F string, won? and then I'm going to print the value that is held in won, like so.

Still not sure it's going to work, let's just see what happens now though.

Yeah, it's not working because I haven't properly, I haven't returned this won to use it.

So I think what I've got to do is, where is this check win? Right, okay.

So what I've got to do is I'm calling this function check win, but I'm actually not returning it.

So I haven't called the function check win.

So this isn't actually doing anything at the moment.

So I need to call the function, check win, board and player.

So check win, board, player.

But as you know already, I can't just call the function like that.

I have to hold the value in a variable.

So I'm going to put one equals, and let's see what happens now.

Okay, so it's saying true.

So I can test this works by just deleting these, and I could put it in another row, X, X, X, and just see if it does won.

Yep, and what happens if I don't have anything in there? Does it say false? Yeah, it says false.

So if nobody's won it's saying false, and if I have won, it's saying true.

And obviously you'll have time to just sit and go through every single combination there to check that it works.

So that's where you should be at this point.

At this stage you have now made a start at two sections of the game.

You can see these are now complete on the structured chart.

So you're going to use the complete noughts and crosses section of the worksheet to finish making the game.

So good luck.

This is a challenge, it's meant to be a challenge, but use those instructions on the worksheet to support you in all of those outputs as well to give you an idea of what you're doing.

And then I'm going to go through the solution with you after you've had a go.

Now this is meant to take quite some time.

So you might have given yourself an hour for this lesson.

It might take you longer than an hour to programme this, and that's completely normal.

I would expect it to take probably two hours actually.

So if you do want to split this lesson over two days or two weeks, then that's fine if that's what you need to do.

Or you can just sit and try and code it all at once now, if that's what you really want to do.

But pause the video now, while you get on with that.

What I'm going to do now then is I'm going to take you through the solution.

So I'm going to show you the completed code and you can access this yourself as well.

So if you want to look at this code and explore it yourself, then you can do, but I'm just going to talk you through it and talk you through how it works and on what I did there to get to the final solution.

And so hopefully that will help give you some idea for your own programme if you haven't quite got there yet, or maybe give you a bit of clarification that you've got in the right direction.

So let's take a look.

When you click on that short link, you're going to be presented with quite a lot of code.

There's a lot of functions there, there's a lot of lines of code.

And if you've just been working quite hard to solve this problem, you'll probably have lots of lines of code as well.

And that's why functions and procedures are really, really useful, because they allow you to break your programme down.

So you can start looking at things in little sections rather than the whole thing altogether.

So what I'm going to do is I'm going to talk you through this code, and hopefully, while I talk you through the code, if you've been struggling a little bit it's going to help you as well, and you can also access this code too and look at it yourself properly as well when you've got a bit of time.

So this is the whole block of code.

Now, as you know, these are just function, this is where they're defined.

So I'm going to try and look for the first function calls.

I'm going to go all the way through it.

So I've got lots and lots of functions here.

You can see everything that's going on.

And then we've got this final one at the bottom, which is play.

And then we've actually only got in our main programme, we've got one function call.

So that's that bottom one, play.

And it's going to take me to this function.

Is it a function, let me just look.

No, it's not returning anything, it's a procedure.

So it's going to take me to this procedure, and at the very beginning, it sets up a board.

Now you might have already gathered that your board initially needs to have those numbers in it so that you can tell the user the instructions of what number to type in for the placement of the game.

And if I just run this, you'll see, I've got my instructions and you'll see that board in a second.

There we go.

So that's, by having those numbers in that grid there initially, it allows the user to know what the numbers one to nine mean, and where the positions are on the board, so that's quite handy.

So and then you've got here.

Straight away, another call.

So this one is saying instructions and it's sending the values, the list board, and it's passing that to the function.

So let's go to instructions, scroll all the way up to instructions, where is instructions, here we go.

So we go to instructions and it's got that one to nine.

It's going to go through the instructions, that's what you've just seen there.

And it's got these sleeps in just to give the user a bit more time to read the instructions as well.

So that's just pausing it for five minutes, and then display board is just displaying the board there, and it's using what we've passed into the procedure in order to display that, and then to choose a position for your piece, enter the location number.

Now notice that you don't enter the location number in that procedure 'cause this is just the instructions.

So once it gets to that end of this procedure, it will then go back to the next line of code which is actually all the way down here.

'Cause we've called this and it's executed that code that was above in the instructions procedure.

Now it's going to this line of code.

So setting won to false, because of course at the beginning of any game nobody's won, so won is going to be set to false.

And it's also set the current player to X.

So whoever starts, the first player is always going to be X.

Then we've actually got a loop, which you're going to need to loop in order to keep running the game until somebody actually wins the game.

So I've got a Boolean condition here.

So while not won.

So while won isn't true, keep playing the game.

And then we've got here, player equals current player.

So we've got a new variable called player, and what's being held in it is current player.

And the first time we run this game, current player is X.

Then you've got move board player.

And so this is a nother sub-routine call.

So move board player.

So now we want to go all the way up and look what's being sent to it.

So we've got board, so this board here and player.

So let's go all the way up, so where's move, there it is.

So move board player.

So that board with the one to nine is being passed to it.

And the player, which is currently X has been passed to it.

And it says position equals read position.

So actually we have got another call here.

So it feels like a little bit of a maze, but when we work in through, we're going to see what happens.

So we're doing a move and it's got read position, player.

So let's go to the read position.

So if we go up here now it's saying positions are one to nine.

It gives some instructions, players turn, where would you like to place your piece? And then the player types in a number between one to nine.

So we've got here, X's turn, where would you like to place your piece? And you can type in one to nine.

And if I type in 10, it says you must enter a number between one and nine.

And that's because we've got a list here with all the positions in, and it's saying while position isn't in positions, keep asking that question.

So if I put three, it should allow me to do it.

And it's returned the value position.

So this one is a function, isn't it? So that position is then returned back to where we were, which was this one.

So now we've got three in here.

I probably shouldn't have pressed the enter key 'cause it's jumped ahead now.

We've now got a three in here.

So three is being held here.

So let's take a look at this huge selection statement here.

So now we've got, if position is equal to one, two, three, and what it's done is, because I entered a three there, what it's done is it's put the player, which is X, at this board position zero and two.

And you can see it's the zero row at column two.

So it's put it on X and then it says, nought turn, where would you like to place your piece? So we've gone through this function, it's returned that, it's gone through all these and these return the board so that it's displayed that board.

And let's, now it's gone back to play, which was here.

So now we've got here, won, check win.

So even though our output has jumped ahead a little bit something did happen before it said zero's or nought's turn.

So we've got here won equals check win board, player.

So remember it returned that board, which now had the X there and it returned the player, which is X.

So now we go to our one won routine.

So let's go to won.

And this is the won, hold on, was it won or was a check win, let's just double check.

Won equals check win.

So we're going to the check when sub-routine and we're going to see if it's won or not.

So it goes all the way up here.

And because it's right at the beginning of the game, the X is only in that one position.

So of course it's going to go all the way through this and it's going to no, none of those are true.

It's going to return won and won is still going to be false because it hasn't changed to true yet.

Then it's going to display the board.

We've got a winner equals current player at the moment.

Now that is probably there to help with, if they win or lose for later on.

So if current player is X do this otherwise, O, else current player is X.

So now we get this loop has got to the end.

So it's going to go back to the top.

And now it's saying, while not won.

So at the moment, won is false.

So that loop is going to execute again, and it's going to swap to nought turn.

So I'm going to make sure that nought doesn't win.

So I'm going to put, where would you like to put it? So I'm going to put four and then X's turn, where would you like to go? One X is zero, O's turn, where would you like to go? I'm going to put five.

X's turn, where would you like to go, I'm going to put two.

So now it's saying the winner is X, would you like to play again? So because now win is true, this loop terminates and it goes to this part here.

It says the winner is winner, so whoever that current player was.

Would you like to play again, and if I do, then it will let me, and if I don't, then it will just end the game.

So it's a little bit confusing, I know.

Hopefully I've explained it fairly well for you.

And now if you were struggling and you just need a little bit of help with some of those bits of code, then you can just go to that short link yourself, take a look at it, take a bit of time to read it through and incorporate any bits that you need to.

And the final thing that I'd like you to do is to thoroughly test that your programme works.

There's a lot of different things that can happen, different inputs, different combinations of winners, and all of those things.

So can nought win at any point, does that side of it work.

Have you tried every single combination of win for each player to see if it works, all those things.

Can they type in gobbledygook for X, and does it still allow them to do it? Does the code break? Is there anything you can do to fix that? So there's all sorts of things that you can do to start testing your programme.

And it's a really good idea to use the test plan that I've given you in the worksheet to think of all the different tests that you might do, and work your way through testing to see if that programme actually works.

And then once you've tested it, you could get somebody probably in your household to have a go at playing it, or with parent or carer's permission you might be able to share that code with somebody if you'd like them to, and they can have a go that way if you want to, it's up to you.

So that is the end of this.

So you should have done your testing table by now and you should have a fully working noughts and crosses game.

So it's one of your first proper working games.

So well done, this must have been a real challenge for you, this lesson.

So well done for persevering with it.

And if you'd like to please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tag in @OakNational and #LearnwithOak.

And that is the end of this unit, so part five, and the next unit that you'll want to do after this one is programming part six, which is the final part in this whole programming units.

So the six parts to it altogether.

And the next part is that final part where you'll be given quite a big challenge there at the end to have a go at.

Hopefully this is a bit of a warmup for you and hopefully I'll see you again soon for that unit.