The “Book Cricket” Game Built via Modern JavaScript & HTML
Tags: javascript, beginners, tutorial, webdev
Published: 2020-01-20 18:17:41

> *Hi, I am back again with another Tech Story on “DEV” based on Modern JavaScript & a little bit of HTML.*

We will be building a Game called “Book Cricket”, which I used to play in my School Days. For those who are not aware of this Game, I will give you all a brief overview of it.

“Book Cricket” is played among 2 players & as the second half of the name ‘Cricket’ suggests that the Player with more runs wins the Game.

**How We used To Play in ‘School Days’ ?**

1. We used to select a ‘Book’ ( Thicker the Better !) & ‘Total Number of Wickets’ for each player.
2. Each Player on his/her Turn used to open the Book to a ‘Random Page Number’.
3. Then we used to note the last digit of that ‘Randomly Selected Page Number’. For example, If I opened the book randomly on to Page Number ‘102’ so my Runs for that particular turn would be calculated as ‘2 Runs’.
4. After noting down the Runs scored, Book is ‘Closed’ and is ‘Opened’ again to a ‘Random Page Number’ & follows the Steps ‘2 & 3’.
5. In Case the Player gets a ‘Page Number’, with its Last Digit as ‘0’ ( for example 120 or 40 ), then the Player lose 1 Wicket.
6. In every turn Runs are ‘Added’ onto the Runs scored by the Player in his/her ‘Previous’ turns. This Continues till the Player losses all his/her ‘Wickets’ or ‘Runs’ scored by that Player surpasses the Runs scored by other Player.
7. ‘Sole Purpose’ is to ‘Win’ the Game by scoring ‘More Runs’ than your Opponent before loosing all your ‘Wickets’.

I know, to some it might sound familiar & to some a bit confusing. But we should look at the ‘Big Picture’ that is the Logical Code to build this Game.

**So let’s begin with the Coding Part Now !!**

We will decode our Game in 2 Parts :

##First Part for HTML ,
We will build the ‘User Interface’ of our Game using basic HTML components such as :
* ‘H2’ & ‘Label’ tags for headings
* 2 ‘Input’ fields for entering Information regarding ‘Book Size’ & ‘Total Wickets’.
* 3 ‘Button’ tags for ‘Player 1’, ‘Player 2’ & ‘New Match’ with Event Handlers.

![index.html](https://thepracticaldev.s3.amazonaws.com/i/q85cpfoky9qaqrdu0wtk.png)
<figcaption> index.html </figcaption>

![Output : index.htm](https://thepracticaldev.s3.amazonaws.com/i/26jmgrksbit34pdtuk9n.png)
<figcaption> Output : index.html </figcaption>

##Second Part for JavaScript,
We will be coding “mainJS.js” in sub-parts according to their functionalities individually :
* In the **First** sub-part, we will be initializing our ‘Global Variables’.

![Global Variables Initialization](https://thepracticaldev.s3.amazonaws.com/i/l3vba6s2kay14nftejgh.png)
<figcaption> Global Variables Initialization </figcaption>

* In the **Second** sub-part, playerWin() function is defined which would compare the final scores of both the players to Decide the Winner.

![playerWin() function](https://thepracticaldev.s3.amazonaws.com/i/mme39udupu5z422dzmus.png)
<figcaption> playerWin() function </figcaption>

* In the **Third** sub-part, we will define an ‘Event Listener’ for Button linked to ‘Player 1’. This ‘Event Listener’ will fire two functions. First function is ‘calPlayerScore()’ which will calculate the Runs scored by Player for every Turn, using ‘Math Functions’ of JavaScript. Second function is ‘playerTurn1()’ which will keep Track of the Runs scored and Wickets left for ‘Player 1’.

![Event Listener for ‘Player 1’ button](https://thepracticaldev.s3.amazonaws.com/i/34yqxvu4wl2qs9zqbhgw.png)
<figcaption> Event Listener for ‘Player 1’ button </figcaption>

* In the **Fourth** sub-part, we will define an ‘Event Listener’ for Button linked to ‘Player 2’. This ‘Event Listener’ will fire two functions. First function is ‘calPlayerScore()’ which will calculate the Runs scored by Player for every Turn, using ‘Math Functions’ of JavaScript. Second function is ‘playerTurn2()’ which will keep Track of the Runs scored and Wickets left for ‘Player 2’.

![Event Listener for ‘Player 2’ button](https://thepracticaldev.s3.amazonaws.com/i/3e9z9i21kbkrjzazcve0.png)
<figcaption> Event Listener for ‘Player 2’ button </figcaption>

* In the **Fifth** sub-part, we will define an ‘Event Listener’ for ‘New Match’ Button. This ‘Event Listener’ will Clear The Console and will enable the ‘Player 1’ & ‘Player 2’ buttons for the New Match. It will also reset the Global Variables to their Default Values.

![Event Listener for ‘New Match’ button](https://thepracticaldev.s3.amazonaws.com/i/ao1d75xscsylnaxpd331.png)
<figcaption> Event Listener for ‘New Match’ button </figcaption>

##Finally, its time to see the Code in Action below :

![Player 1 Score](https://thepracticaldev.s3.amazonaws.com/i/6xmyn6z169dp6gjzvbxl.png)
<figcaption> Player 1 Score </figcaption>


![Player 2 Score](https://thepracticaldev.s3.amazonaws.com/i/kryf1jq2pf9ncrtok79i.png)
<figcaption> Player 2 Score </figcaption>

![New Match](https://thepracticaldev.s3.amazonaws.com/i/pli20qaut1uabm7ou0e1.png)
<figcaption> New Match </figcaption>

###Attaching below the code via public GitHub gist link.

{% gist https://gist.github.com/gitanshu-choudhary/5c772f26249cabe8eaa549e23a0b0fd5 %}

---

{% gist https://gist.github.com/gitanshu-choudhary/555df55826e7cf2c9cadff978af4deaa %}

> *If you liked my efforts, do encourage this post via Feedbacks & Comments.
I appreciate the support.
Thank You !*

---

{% user gitanshu_choudhary %}

---