Few lessons ago, we set off to create a coin toss scheme from scratch.
The objective of the game is to guess whether the computer is going to select a heads or tails value.
And you've got two options.
So the players presented with either heads or tails, they go by starting to click one of them.
They can select either one of them.
And when they make that selection that triggers off the computer to make their random selection, make
the comparison between the player selection and the computer's selection.
And if it's correct, then the player wins.
Player gets a point.
If the player guessed wrong, then the computer wins and the computer gets a point.
So this game is just a game of luck.
So 50/50 chance to get it right or wrong.
And I'm going to walk you through all of the steps that we took in the earlier lessons to build out
this game from scratch.
So we first we started out by setting up our HTML.
So always think of it the way that what you need to present for the player in order for them to be able
to play the game.
This is a relatively simple model and we know that we needed a message so that we provide the player
some instructions as well as results from their selection.
And we also wanted to provide the player the two options, whether to guess heads or tails.
And that's all we really needed within the HTML.
Then we came to the JavaScript.
We wanted the computer to be able to select either heads or tails as well so that it would match up
with what the player had as an option.
We set up a scoring, so the scoring was an additional part to the gameplay.
It's not necessary for the core functionality of this game where we're simply guessing to see if we're
getting the same value as the computer's guessing.
We also went through and we set up a message area, so we set up some objects connecting them to the
elements using square selector to make the selection from the elements using within the DOM.
We output that content into the console.
We can get rid of that because we're not going to be needing to make use of that anymore.
And also the button values, we can output those and get rid of those from the console.
We loop through the node list that was available.
So all of the buttons, if we had more buttons, they'd be added into the Snoad list and they'd automatically
be added in that click event using this this block of code here.
And we're all sending them all.
So it doesn't matter what button gets clicked, the function toss coin gets invoked within the toss
coin.
We check to see the element that triggered the event.
We see what their inner text is.
And in this case, we've got heads, we've got tails.
If we create a random button and had whatever we wanted in their written, we would also see that text
as well.
We know with the coin, the computer coin toss, we've got a value of anywhere from zero or one.
So we've got two possibilities within this value.
And you can also extend this to be more dynamic where we've got the coin array and we can use the length
value.
And in this case, if we want to add in additional options here within the coin toss, then those would
also be provided as well.
But keeping in mind that we would still only have two buttons and you'd have to update the buttons as
well.
So going into our random values or selecting a random value from the computer for the computer, either
heads or tails, and once we make that random.
Value, that's good.
We're going to use that as the index value within the coin array and display whatever is the corresponding
value, either heads or tails.
Next, we output the content that what the computer selected within that message area element.
We create another element that will hold our output so that we can set an output here, whether we win
or lose, we set different outputs.
We check to see if the players guess is equal to the computers.
Guess if it is, the player wins.
If it's not the computer wins, we update the score accordingly and we output that final message to
the player so they know what happens.
Thanks again for taking this quick section and learning how to build out a coin toss game from scratch.