Do you want to improve your coding, howabout creating a battleship game? You can even play against your computer.

Previously we created a battleship game and we've created quite a bit of codealready, in the last video we coded so the computer could shoot, in this videowe're going to do tactical shooting.

Now on your battleship board when you hit aboat then you know afterwards one of the four squares either side or above orbelow is also going to hit, so we know after one hit, we can tactically shoot bylimiting our shooting to one of the four options, if we miss then it reduces to 3and so on.

Now if we hit that means that we knowthe direction is it going to be horizontal or vertical.

So we cantactically shoot once we know we've hit and we don't need to look at the otherdirection.

So once we've shot and we know it is ahit, we've already got a hit, so we've got two hits together, we can look upthere plus one of the minus 2 positions.

Now if we were to hit three times thenwe know we can look in the plus one or minus three positions remembering alsowe can do this vertically as well as horizontally.

Okay so let's have a lookat tactical shooting.

The computer can take random shots it's not going to beata player because it plays no tactics.

We're going to start off by telling thecomputer when there's been a hit or when the battleship has been sunk.

So ina check shot we had missed equals one so we could change that to zero, so if it'sa miss if it's zero then that will be appended to miss but instead of changingthe missed in the the loop before theif-statement we're gonna put it in when it hits the boat so we can say missedequals one when it hits and when it sinks a ship it could be two, and thatway if we pass that back when we return it when the function is called then wecan know if the shot missed the boat completely if it's hit or itsunk the ship.

Now that's important because in our tactic we need to knowwhen the boats been hit.

If we look at our for loop in our check shop we'regoing to have missed and after that we're gonna see if miss equals equals one we're going to dosomething.

If it's two, let's line that up, we could do something and if it's zeromaybe.

So what we're going to do? If we're going to tell the computer whereit might want to shoot we really need to create a list let's call it tactics, sothis list we can contain the candidate places on the board where we're going totell the computer to shoot.

Now if it's hit we need to give the computerinformation if it's sunk the ship that information is go back to being a randomguess so we can say tactics is an empty set if missed equals two.

Now whathappens when the boats been hit? So we know the next guest should be one above.

below, left or right if it hasn't been used before and it's still on the board.

So we're going to calculate what the tactics are in a function, which we were doing a minute and thenwe're gonna pass those suggestions candidates for shooting to the get compshot.

Now let's assume they're in order of which we want to use so when we getthe shot from the computer we only need to add tactics, the first place whichwould tactics zero as the shot.

So if tactics if it's empty we want toignore it so we can say if length of tactics is greater than zero then youwant the shot so we call tactics zero.

Now if it's not, just use a randomamount, so it's not going to be in guesses so the get shot comp only needsthis slight amendment this slight change.

So now we've got the missed so we know ifit's hit we know if it's sunk the ship we're going to use these tactics and wewant the first element in this list to be the place on the board we want thecomputer to try to shoot and we know if it hits we've missed equals one thevariable missed equals one and we know we want to try to shoot one above, below, either side.

So in our function we can say tactics equals and then we want tocalculate the tactics, we can give it the tactics.

So if we give it the shot, pass the shot we don't need to put it into that list so let's create ourfunction called calculate tactics.

Define calculate tactics with the shotand tactics.

Now if the tactics, if the length oftactics, if it's empty list then we know if it's the first hit we only just needto look one above, below, either side.

If it's more than the first hit then weneed to do something else so if the tactics list is empty then we know it'sthe first here we just need to create a list where it has the the numbers eitherside of the shot so we're gonna create temporary list so we have shot – oneshot plus one shot – 10 and shop +10.

So that's created the places above, below and either side of our hit.

So we need to check to see if these values areon the board and not in guesses, so we will need to pass guesses so let's dothat.

Calc tactics I'm gonna have guesses and in our loop for you don't know thelength of temp this temporary list is the potential candidates where we wantthe computer to shoot so that could be a different length so for I in range oflength of temp, if I not in guesses and I is less than 100 actually it should betemp I shouldn't it so if the value is less than 100 and the value is greaterthan minus one so it's on the board then we can append the candidate into a newlist.

So let's define so C A N D standsfor candidate so we're saying and out of this temporarylist if these values are on the board and not in guesses then they can go inthis candidate list.

Now what order are we gonna put them in? Well it could beany order at the moment we've got them in the order that we've created hereminus 1, plus 1, minus 10, plus 10, but we could shuffle them for example so if weimport you go to the top and import random So we want to shuffle the candidatevalues.

So now we pass back that list of candidate values after a hitand then when we get the shot from the computer if there's a value in that listthen it will use the first value as a shot, if the list is empty then it willuse a random value.

We've accidentally used square brackets that obviouslyshould have been round brackets there.

Always have to debug your codeokay so temp we've not defined so obviously in here we're going to putsomething if tactics is longer so if we've hit a boat more than once.

Let's seeif we run that what errors there are.

So it does come out with the board sothat's a good sign.

Now what happens if we've guessed oneeither side of a hit and it's missed? Well we've already got our list createdso if our missed is not one or two so we've got zero meaning we've took a shotand it's missed the boat, then the tactics if the length of the tactics is, it's notempty, then all we need to do is get rid of the first, the location, the number in the firstplace in the list tactics, and then the guess we'll go to the the place in thesecond index of the list.

So the list tactics has got the candidate places andif the first one is wrong it moves on to the second one.

So we can do tactics dotpop and then zero so that's our tactics if we've hit, if we've hit then missed andif we've completed the boat, so now what we need to do is work out what happenswhen we've got hit and then we've hit again.

So we need to pass heat the listof hits to our count tactics.

Now in here we've got your length of tactics isgreater than or even less than one so if it's more than one then we need to checkto see if each shot each candidate place is in hit.

So if our shot minus one is in heat or if a shot plus one is in hit thenremember we're looking at two hits together and the candidate should beeither side of them.

So if we're a horizontal we've had two horizontal hitsthen the next shot should be either side of them if they're vertical and a shotthe next shot should be one above or one below, so we can say if shot minus one isin hit remember we passed that to count tactics.

So if that's in hits then our temp it'sgoing to equal, instead of equaling the four values we're not gonna have theup-and-down values it's gonna be not just minus one but it's gonna be minus 2and plus 1.

So we've got two hits and our candidate are either side.

Now we dothat we've all four positions so if shot is minus -1, plus 1 inhit then it's going to be plus 2 minus 1.

So those are our horizontals and now wedo the same except for instead of going across we're going to go up and down soit's going to be minus 10 from plus 10 minus 20 and plus 10 and plus 20 and minus 10.

So now we've got what happens if we'vegot one hit we've got what happens if we've got two hitswhat happens if we've got hit and a miss and what happens if we sunk the bowl.

So now we've got an idea of our tactics.

So let's have a look at ourboard perhaps at the end and also maybe have a look at our board of boats thatwere created by the computer.

So let's put that first so we're going to seeafter it's run and once we've finished our full loop of 50 shots we can see theboard how it's finished and the original board of the boats.

So let's save that, now let's run that.

Okay so we can see our original board here, it's notcompleted but you can see for example the boat of length 5 it's managed to hitall five so it's definitely an improvement by using these tacticsbut it's not finished.

So our tactics has dealt with two hits but it hasn't dealtwith three hits or four hits so we know if we had for example three hitstogether in the shop minus 1 and the shot if they're both in here then thethird hit would be shot minus 2.

Although it's quite a bit of coding weneed to do this we need to look to see if we've got more than the two hitstogether.

So if shot – 2 is in hit then it's going to be slightly different from if there's justone.

So we've got ten equals shot – two, shot plus one, if there's twohits together but if they're three it's gonna be shot minus three.

So we need todo that we've all four positions so let's copy and paste the code and changeit to the correct direction.

So what I'm doing here I'm copying andpasting in each section but you can see I'm leaving the previous code in there tomake sure that I'm doing it correctly, it is very easy to make a mistake here andit's not so easy to debug so let's hope it's going to work.

So the last one we'regoing to put in so we've got shot plus 20 and minus 10 so minus 10 and plus 30 all right now let's hope that works.

Let's increase the amount of shots tolet's say 80 so there's a possibility we might even finish the game even.

So let'scheck that let's run that to see if there's any problems.

Okay now we have the board and we havenearly finished all of them very close let's try again.

So on this gameit's actually completed all our board so we need to run it a few times.

Perhaps we can add the code to see if the ships is empty.

So inside our loop weneed to run a function to see if the ships are empty.

So I've copied and I'mgonna paste in some code from the internet that checks if a list of listsis empty, so I didn't even write this code I've just copied it from theinternet.

So this code will return true if it's empty so if it's true we canjust give a message saying the end of game and maybe a good idea to see howmany moves that's in and then we want to break out of the loop.

So that means thatif the game's finished we'll get a message saying how many iterations ofthe loop has gone.

So we need to test that a few times to see if our code iscorrect.

So let's have a look here it's not finished you can seecouple of boats not not being hit, but you can see our tactics has drasticallyimproved the computers form.

Now this one for example end of game in sixty fivemoves so therefore the computers being able tofind all the ships quickly so it's managed to find when it's found a hitto get two hits then three hits for example.

One more time there it hasn'tfinished let's see, now end of game 64 moves, so it looks like it's workingusing the tactics.

Thank you for watching and if you haven't already pleasesubscribe.

.