DEV Community

Cover image for How to code a Mastermind game in HTML, CSS and plain Javascript
Gustave
Gustave

Posted on

How to code a Mastermind game in HTML, CSS and plain Javascript

Mastermind is a timeless classic among strategy games, renowned for its blend of logic, deduction, and sheer exhilaration. Created by Mordecai Meirowitz in 1970, this captivating board game has stood the test of time, captivating players of all ages with its simple yet endlessly engaging gameplay.

At its core, Mastermind is a battle of wits between two players: the code-maker and the code-breaker. The code-maker concocts a secret combination of colored pegs, while the code-breaker employs deductive reasoning and keen observation to unravel this enigmatic sequence within a limited number of attempts.

With each guess, the code-breaker receives vital feedback in the form of colored pegs, offering crucial insights into the accuracy of their deductions. Through a series of careful guesses and strategic adjustments, the code-breaker endeavors to decipher the code in 12 tries, showcasing their mastery of logic and pattern recognition.

While Mastermind may seem deceptively simple on the surface, its depths reveal a rich tapestry of possibilities, challenging players to think several steps ahead and to outwit their opponent through cunning strategy and shrewd intuition.

In this article, we embark on a journey to recreate the essence of Mastermind in the digital realm, harnessing the power of HTML, CSS, and JavaScript to bring this iconic game to life on the screen. Through a step-by-step exploration, we'll delve into the intricacies of game design and programming, empowering you to craft your very own digital rendition of this beloved classic. So, prepare to unravel the mysteries of code-breaking and embark on a thrilling adventure in the world of Mastermind.

HTML

To set up the game interface, you'll need several components organized on your page. We'll divide it into distinct sections:

  • Game Rows: The main gameplay area consists of twelve rows, each featuring four placeholders. These placeholders are where players can place their guesses for the secret code.
  • Feedback Section: Adjacent to each row, there are four smaller placeholders. - These are reserved for the codemaster's feedback on the color combination chosen by the player.
  • Hidden Code Section: At the top of the interface, there's a concealed section containing the actual secret code. This code will only be revealed at the end of the game.
  • Color Selection: Located at the bottom of the page, there are six color options that players can choose from to make their guesses.
  • Confirmation Button: To finalize their guess, there's a button provided for players to confirm their selection and proceed with the game. By organizing the interface in this manner, players can easily interact with the game, placing their guesses, receiving feedback, and ultimately attempting to crack the secret code.

CSS

Hey, for this part you can do wathever you feel like! This is how I did it! You can just copy paste my css from my github if you want!

Image description

And now the fun part: (plain) Javascript

Let's break it down step by step:

  1. Initialization: It initializes some variables and retrieves elements from the HTML document.
  2. generateCode() function: It randomly generates a secret code using colors from a predefined set.
  3. round() function: This function manages the game rounds. It involves selecting colors, drawing a confirmation button, and placing pawns in rows based on user input.
  4. selectColor() function: It allows the player to select a color by clicking on it.
  5. changeSelectedColor() function: It visually indicates the selected color by adding a CSS class.
  6. Drawing and removing the confirmation button: Functions drawConfirmationButton() and removeConfirmationButton() handle this user interface aspect.
  7. checkActiveRow() function: It checks which row is currently active in the game, i.e., which row the player is currently interacting with.
  8. placePawn() function: It enables the player to place pawns (colored markers) in the active row.
  9. colorPawn() function: It assigns a color to a pawn when the player clicks on a placeholder.
  10. checkForFullRow() function: It checks if the player has filled all placeholders in the active row with colors, indicating that they've made their guess.
  11. drawTips() function: It provides feedback to the player by drawing black and white markers indicating correct colors in correct positions and correct colors in wrong positions, respectively.
  12. showAnswer() function: It reveals the secret code after the player has made a correct guess.

Conclusion

In conclusion, by harnessing the power of HTML, CSS, and JavaScript, we've embarked on a journey to recreate the timeless classic of Mastermind in the digital realm. Through careful organization of the game interface, meticulous attention to detail in styling, and precise implementation of game logic, we've crafted an immersive and engaging experience for players of all ages. As you explore the intricacies of code-breaking and strategy, remember that the adventure of creating your own Mastermind game is not just about the destination, but the thrill of the journey itself. So, dive in, unleash your creativity, and let the challenge of Mastermind inspire you to new heights of digital gaming mastery.

Top comments (0)