Hey, welcome back.
Let's play through the game to determine what we need to do next in order to accomplish the next move
in the game.
So we've got the start game button.
We asked the player to select a case.
So this is going to be their hidden case.
And this is the one that they can choose to kind of play out until the end or take the offers as they're
presented.
And we've already loaded a number of different offers.
We've built out the game board and now we've got a fully functional game board.
So we do have already one of these values is selected, but the player has no idea what they've already
selected.
So the objective is to select a bunch of cases and then they get a bunch they get offers presented to
them.
So we don't have that offer being presented to them because it's said select one case and we selected
four cases now and we haven't seen any of the offers being presented.
The rest of the graphics and the view part is working.
And the next step in order to check to see if there's an offer available is going to be when they select
the case and they close this.
This is where we can present the offer so we can present it within that close action.
And that's really where they're moving next and that's how they're moving through the game.
So already have that close popup action happening there where we're showing and we're displaying that
logic of hiding the popup.
But now we can apply some logic and we can offer a games, make an offer at this point, or we can check
to see if there is an offer available or should be made at this point.
And if there isn't, then they can continue to click through until we do have an offer or until the
game is over and they run out of cases.
And this is another thing that we need to account for if they've selected the last case or the second
last case that we need to end the game so all of this can be done within the close pop up, because
this is where we do the moves in the game.
So we're going to output all of the game offers length.
So as the player moves through the game, we're going to output the game offers.
So right now we have five and we still have five.
So we haven't been able to move through that.
And the game offers are all coming from here.
And notice that as well, that we removed out.
We filtered out the ones that didn't match up with the number of values that we have.
So that's why we've got the five items there within the offer.
And we need to check to make sure that if an offer is available, we need to be able to make that offer.
So let's have a condition to do that.
And this condition is going to check the game and use that holding array.
So the holding array, remember, is the one that we set from before where we've got all of the values
as we constructed the game board and we need to check to see the length.
And this is really how many items are still left or how many cases are still in play.
And then we're taking our game offers value and we're checking to see which offer we're on.
So we know that if we haven't made any offers, we're going to be on the first one.
This is using an index value and with JavaScript, the arrays are zero based index value.
So we can see that if those are equal, if we're on the offers and the offers are made and remember,
we sorted the offers.
So the first offer is going to pop up at fourteen and we're checking to see if this array length is
going to be fourteen and if it is that we know that we need to add an offer.
So let's make sure that that is working as expected and then within the console and offer.
So this is where the offer will take place.
Let's see if it works.
So now we should be sending one.
We've got fifteen left and four at fourteen.
So we send the offer.
So that was working so confirmed to be working and sometimes you have to do it step by step and play
the game over and over again.
So that's why it's always good that you play a game that you're enjoying.
So let's do a game offer.
We're putting this global.
This is a brand new game offer this this is what the game is going to offer.
And we're going to calculate out a value that we should be offering and we're going to create a brand
new function to calculate the offer.
And what this will do is this is going to look through all of the available author offers and it will
come up with a value that we want to present to the user.
So let's move down and create that function without passing any parameters.
But we are going to be returning back the calculation and we can create a variable, call it total,
so we'll send that value back.
So let's see how we can work it total.
And the way that we want to work the total is we want to loop through and get all of the total values
of the remaining cases and we're going to divide that by two.
And that's going to be the offer that we're going to make or we're not going to divide it by two.
We're going to divide it by the remaining items in the game holding array.
And so.
That will give us a rough average of how much is left.
So let's create total and total is from the game select.
So this is the player's selected case value.
And the reason we're starting with this is that we want to add all the other ones.
And because the player doesn't know which one they've selected and we need to add this in to get an
accurate total of all of the values that are currently still in play.
So taking the holding array and looping through each one of the items within the holding array and putting
a value of L.
So we're looping through all of these and all we're going to do is we're going to total it.
So just do a quick total of whatever the value of total plus total plus total until we get a value of.
So now we've got value that's being returned back and we can continue to do our calculation.
So over here we've got where we're calculating the offer.
We need to divide it by the game holding array length value plus one, because that plus one is going
to represent the users case.
So let's try that and console log out the game offer.
So now we need to select so the send offer and we're offering 35 at this point.
And that's a fair evaluation because we did get rid of a few of the larger one and I got a log out the
total so that we can have a better idea of what the total is that we're sending back before we're sending
it back.
And also do a console log adding and a bunch of ostracizes because we've got quite a few console logs,
messages going.
So it's a little hard to distinguish.
So there we've got our total and we're dividing it by the number within the array.
So the more that we pick and eventually we need to get another offer as well, that we need to move
it to the next index value of game offers.
So we'll take our game offer made.
We're going to increment it by one because we have made an offer or we will be making an offer in this
case.
And now let's determine what we want to output for the offer.
So good to have a condition there and then using the pop up and we're going to create a brand new pop
up.
So pop up too.
And this is going to be for the offer pop up.
So we already have the pop up one and we want to duplicate this for pop up number two so that we can
have both pop up values to going back into here.
And let's do this as pop up, too, and it will update this and have a message block and we'll update
this as well as needed.
But for now, we're going to keep it as message pop up, too, and we can send over that same HTML.
So let's construct some HTML.
And this is, again, that message back to the user.
So always think of what you want to present to the user and I want to present them how many offers I've
made.
So offer no whatever offer value that we have.
And getting that game offer value, offer made value is going to tell us how many offers we've made
and notice that we've already incremented it here.
So that means that even though it's zero based, we're going to start out at one because we've already
done the increase of the value there and then concatenating it together to div.
So we're sending over this HTML string to the user.
So this is the information we're providing to them and the game offer.
So this is the offer that we're actually going to be sending over, and that's the one that we just
spent all that time calculating.
So offer this so the user can see what was offered to them because of course that is important for them
to make the decision.
And also we need to output whatever cases are next.
So how many more cases they need to select?
So we calculate that.
So that's a temp case next.
So how many cases are next to the next offer?
So that's, again, more information that the user needs to know.
And creating that, let's set up that variable.
So kempt case next.
And this can just be a value.
So right now we're just set and we can set it up to zero.
So and do the calculation here on how many temporary cases are next.
And we could actually also we might want to have an option for last offer.
So this might not even necessarily need to equal anything and we can make an update on it.
So let's just declare the variable and do a quick update.
As for this one, this is going to be that it's the last case.
So I don't want to give them how many cases are left because, of course, the last case, so there's
no cases left so that we need a customized message and we'll just put the last offer so the user or
the player knows how many offers are next.
And then this is the last offer.
So we're need to set a variable of game last offer and we're going to set that one to two.
So that's the same one that we're set to false earlier.
So this is our game last offer and we're going to pass over this value into the very.
Simple message that's in the pop up message.
So now let's do the one for the typical case where we need to calculate how many offers are left and
we'll use that same variable, that case variable and started it.
So this can be a string as well and giving them how many cases are left.
So this, again, is going to be a calculation and we have all the information that we need to make
that calculation.
So we've got all of that existing information where we just do one more calculation of this, because
we know it's not the last case.
I'm going to wrap it in a bracket and I'm subtracting from that number one number, should I subtract?
And that's just the game offers me.
So that will give me from that game offers array.
It's going to give me the one before it and then the one that we're currently looking at.
And give me a calculated value because this one is going to be a larger value.
So that's where we're subtracting it.
So in this case, going in here, it's going to be 14 minus 12.
And so it's going to give me a value of two.
So it's going to go or select two.
And let's finish the wording for the player.
More cases sort of telling the player how many more cases they can select if they don't want to take
the off.
And I do need to make that condition in here and make sure that the game offer made.
So we did incremented there.
So keep that in mind that the game offer made and we've incremented it by one game offers length.
So the offer made is not going to be equal to the offers length.
And we know that we've got five offers there that are loaded.
So again, thinking dynamically when we're building this game, we're going to see that if this increments,
if we're on number four, then we don't have anything left which is going to equal the game, offers
length so fully dynamic.
And that means that it's the last offer and there's no more offers after that.
And we're not going to be expecting any more offers.
And we're setting the last offer to True.
Which will trigger the end of game, because that's the last offer that we have available to the player.
And one more thing just out in the bracket there so that we can pop up value and now we're just sending
it over to the pop up.
So let's try it.
So selected case number three, select one more case.
Great job.
Now we need a message.
So we've got to accept content or decline it.
So we've got offer one at thirty five or select two more cases.
So why isn't this coming through on pop up to.
Let's check it out and we see that we're updating the mortal body and we're not updating mortal body
too.
So that was the error there.
Great job.
And now we see we've got the offer so offer one at thirty six or select two more cases.
So next, we need to work in what happens, accept or decline.
And as you can see, we can't play any further because we need to enable these as options.
So that's the next step in the game development.
So go ahead, add all of this stuff into your game and you can be ready to move on to the next one where
we let the user make a decision.
And if they accept the offer, then the game is over.
They walk away with thirty six.
If they decline it, then they keep playing and we're going to tell them how many more cases.
And in two more cases we're going to have another offer for them.
And that's going to be based on what the existing values are on the screen.