So in this lecture I want to go over how to make a very simple AI and AI will just kind of randomly
guess.
And since we're changing the problem now our original problem was more for two human players.
And now it's for well we're going to be able to choose whether we want to play against a human or a
AI.
And since we're changing the problem this really changes the data.
In order for us to get this working we'll need to add something to our data.
And we're going to adding another
called player type
in this player type could be human.
Human or PTV AI.
So now that we have that rapid change or player as well and the player is going to have a player type
type.
And now we have to change a couple things around.
So in our the game function
we're going to before setting up the board when you clear the screen.
And we're going to set the player 1 player type to be human.
So that will be us.
The human hand red shoes the player type for Player 2 and we do that through functions of get player
to
share of the eye makes sense.
OK that's function in we're going to define this function here.
So you said it here somewhere cleared up here.
It's all that.
So it's going to turn to player type.
No arguments.
Copy it's in and we don't here you will find it so we're going to say Konst it's valid inputs to make
use of our function that we made or.
And you do this a different way but we can't do it.
So get integer.
Who would you like to play against.
It's put in a new line and one is human.
New line two is AI
some more new nuance.
What is your choice.
So
A.C. input it's here.
Screen it's two
and we're going to
basically want to check to see if the input is equal to 1 turn.
Human else
turn
so that's all that's going to do.
Now we have to do some changes in the play game function.
So here really we don't want to draw boards
if it's an AI OK.
So here I'm just going to make the change if the current player
is player.
Ty
is human.
Then you can draw Bortz.
And only then and here we won't want to get the yes right.
So if if he's in there I say so here we're going to say
if current here
is player type is the human
then we will do this.
Otherwise we're going to have to get a guess somehow from the AI.
So you just get a I guess current player.
So let's define the
let's define the get a I guess it's Cup.
Yes.
And here
this would be a position right.
Yeah.
In
there
is a case
in way down here.
We will implement
so we say ship is action I guess in
really all we want to do is get a random position.
So essentially you'll need the rand function is say guess RHO is right and
percent Portes it's right same the call
or side solve this and turn the gas.
So we need to include it.
So and to include it and we need to see that as well so we need to include
see lib and we need to include seedtime
like so now in Maine
is here.
We should first thing we should do is see there and I'm number airier seed Rande time no.
And now we can actually use it so that we actually get the guests properly.
So now back in the game function I'm going to add whenever I want any output for the AI so I'm going
to add here current player clear type
P-T human then you can display this right.
And down here
so after update orders and the drop boards.
So we want only want to see the current player or current player if he's a human.
If the current where player type PTZ I say
else
this.
So in this case we want to draw the board of the other player because if the once the player player
makes a guess we want to see what happens to our board.
In this case were the other point.
So
that's an see how much.
And we also want to see what they chose as well.
All right.
Where
we're name he chose rho
and in and cast this to be character
cast row.
So I didn't really go over casting too much in this course because usually not a good idea.
But in this case this is fine.
Really this will just kind of change its type to be a character instead of what this really would be
which is an integer just so it outputs as a character.
OK.
And that's one we can cast cast things
and call them yes.
And
so now in here again science is a lie.
If the current church where
type
so.
So if if the current player is an AI A We want to say that's essentially the I think you're something
right.
So in this case you want to say see Ouch.
Current player which which knows Nay I mean
sunk your
get Neme ship type
type.
Yes.
I think there there's any other changes we need to do really.
Actually there is one.
So the one we need to do is actually laying out a I's ships.
So in our set up forward here
instead we need to call different function for the.
So if so we're just going to say clipboards and we're going to say if the player does we're type is
equal to PTA II then we're going to do something special for him.
Set up a Bortz
of the player
and return.
So instead of actually going through all this and asking the high rate which is silly right AI does
need to improve.
OK so we're actually going to make this function.
It's nothing is cheek by reference here.
Here you find it.
And so we're going to need a ship position
shin shoot or teach in
K and we're going to loop through
all the ships just like we did with the human players version
and basically what we're going to do is do another live here and we're going to randomize getting position
again.
And so in fact we should probably see this say ship station type.
Turn the gas and then we're just going to instead of all this churn get it done session this way.
So here we're going to say this mission is get random position
and the orientation
is in order to cast this
and it's going to be around and modulus to
there's only two values rates here and one.
And while
not is valid placement we're still using this function
and then we're still using the police ship report as well.
So that should be it.
I think I don't think there's any other changes really really need to make.
So let's run this and see what happens.
So who would you like to play with against the I.
So again that set up a ship a one just very simply inch one on all the longtail g one horizontal
yea.
So what's my guess.
A one miss.
He chose I wrote my column.
I was a miss for him.
Be
for Miss chose F in column five.
That's Miss de
C9 MS..
So he actually hit me.
So there you go.
So you can actually hit.
He's not very smart.
He just kind of randomly chooses.
But it's better than nothing right.
So this really should conclude the Battleship game.
And if you want you can actually try to make a smarter AI.
If you if you're so inclined and try to see what you can come up with.
Otherwise I will see you in the next section.