{"id":7177,"date":"2020-07-21T16:04:37","date_gmt":"2020-07-21T16:04:37","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7177"},"modified":"2023-02-16T19:57:02","modified_gmt":"2023-02-16T19:57:02","slug":"create-mastermind-game-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/create-mastermind-game-in-python","title":{"rendered":"Create Mastermind Game in Python &#8211; Code a code-breaking game"},"content":{"rendered":"\n<p>Mastermind is a two-player code-breaking game, in which one player hides a code consisting of colors, while the other player has to guess it using clues given by the former player for each turn. <\/p>\n\n\n\n<p>In this tutorial, we will be creating our own Mastermind Game using Python Language. In our version of Mastermind, the computer will randomly select a secret code and the user tries to guess it, based on the deterministic clues given by the computer. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Mastermind Game in Python &#8211; Demo<\/h2>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"768\" style=\"aspect-ratio: 1366 \/ 768;\" width=\"1366\" controls src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game-1.mp4\"><\/video><figcaption>Mastermind Game Demo<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<p>Without any further ado, let us get into the design part of the game.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mastermind Game Design<\/h2>\n\n\n\n<p>The original mastermind board is shown below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Mastermind.jpg\" alt=\"Mastermind\" class=\"wp-image-7196\" width=\"266\" height=\"282\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Mastermind.jpg 600w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Mastermind-283x300.jpg 283w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Mastermind-24x24.jpg 24w\" sizes=\"auto, (max-width: 266px) 100vw, 266px\" \/><figcaption>Mastermind Board<\/figcaption><\/figure><\/div>\n\n\n\n<p>The shielded end of the board hides the secret code, whereas the entirety of the board is based on guesses made by the code-breaker. The game ends as soon as the user identifies the hidden code.<\/p>\n\n\n\n<p>The right side of the board in the figure contains a series of white and red pegs, which are used to denote the clues related to each try.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Red<\/strong> &#8211; One of the chosen colors is in the right position as in the secret code<\/li><li><strong>White<\/strong> &#8211; One of the chosen colors is present in the code, but the position is incorrect.<\/li><\/ul>\n\n\n\n<p>The implementation of the board in the terminal looks like:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"420\" height=\"523\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game_design.png\" alt=\"Mastermind Game Design\" class=\"wp-image-7199\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game_design.png 420w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game_design-241x300.png 241w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><figcaption>Game Design on Terminal<\/figcaption><\/figure><\/div>\n\n\n\n<p>The top end of the board hides the secret code and is revealed when the player is out of chances or breaks the code. Until that moment, <em>&#8220;UNK&#8221;<\/em> for <em>&#8220;unknown&#8221;<\/em> is shown.<\/p>\n\n\n\n<p>Each section of the board denotes a turn made by the player. The game supports six colors, <span class=\"has-inline-color has-vivid-red-color\">RED<\/span>, <span class=\"has-inline-color has-vivid-green-cyan-color\">GREEN<\/span>, <span style=\"color:#f4e507\" class=\"has-inline-color\">YELLOW<\/span>, <span class=\"has-inline-color has-vivid-cyan-blue-color\">BLUE<\/span>, BLACK, <span style=\"color:#ff8800\" class=\"has-inline-color\">ORANGE<\/span>. The leftmost panel denotes the clues based on each turn. &#8216;W&#8217; for WHITE and &#8216;R&#8217; for RED are used with the original meaning behind them <\/p>\n\n\n\n<p>In the figure displayed above, three of the four colors selected by the user are correct, but neither of them is the correct position according to the secret code, hence three &#8216;W&#8217;s in the clues.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function to print the mastermind board\ndef print_mastermind_board(passcode, guess_codes, guess_flags):\n\n\tprint(&quot;-----------------------------------------&quot;)\n\tprint(&quot;\\t      MASTERMIND&quot;)\n\tprint(&quot;-----------------------------------------&quot;)\n\n\tprint(&quot;    |&quot;, end=&quot;&quot;)\n\tfor x in passcode:\n\t\tprint(&quot;\\t&quot; + x&#x5B;:3], end=&quot;&quot;)\n\tprint()\t\n\n\tfor i in reversed(range(len(guess_codes))):\n\t\tprint(&quot;-----------------------------------------&quot;)\n\t\tprint(guess_flags&#x5B;i]&#x5B;0], guess_flags&#x5B;i]&#x5B;1], &quot;|&quot;)\n\t\t\n\n\t\tprint(guess_flags&#x5B;i]&#x5B;2], guess_flags&#x5B;i]&#x5B;3], end=&quot; |&quot;)\n\t\tfor x in guess_codes&#x5B;i]:\n\t\t\tprint(&quot;\\t&quot; + x&#x5B;:3], end=&quot;&quot;)\n\n\t\tprint()\t\n\tprint(&quot;-----------------------------------------&quot;)\n\n<\/pre><\/div>\n\n\n<p>The above code snippet is responsible for displaying the mastermind board on the Terminal.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Data Structures &#8211; Game variables<\/h2>\n\n\n\n<p>For the convenient development of the game logic, we need a few data structures. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>colors<\/code> &#8211; A list of colors involved in the game<\/li><li><code>colors_map<\/code> &#8211; A mapping between numbers and the colors<\/li><li><code>passcode<\/code> &#8211; The secret code<\/li><li><code>show_passcode<\/code> &#8211; The secret code to be shown to the user, a list of Unknowns<\/li><li><code>guess_code<\/code> &#8211; A list of lists of guesses made by the player<\/li><li><code>guess_flags<\/code> &#8211; A list of lists of clues given to the player<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# The Main function\nif __name__ == &#039;__main__&#039;:\n\n\t# List of colors\n\tcolors = &#x5B;&quot;RED&quot;, &quot;GREEN&quot;, &quot;YELLOW&quot;, &quot;BLUE&quot;, &quot;BLACK&quot;, &quot;ORANGE&quot;]\n\n\t# Mapping of colors to numbers\t\n\tcolors_map = {1:&quot;RED&quot;, 2:&quot;GREEN&quot;, 3:&quot;YELLOW&quot;, 4:&quot;BLUE&quot;, 5:&quot;BLACK&quot;, 6:&quot;ORANGE&quot;}\n\n\t# Randomly selecting a passcode\n\trandom.shuffle(colors)\n\tpasscode = colors&#x5B;:4]\n\t\n\t# Number of chances for the player\n\tchances = 8\n\n\t# The passcode to be shown to the user\n\tshow_passcode = &#x5B;&#039;UNK&#039;, &#039;UNK&#039;, &#039;UNK&#039;, &#039;UNK&#039;]\n\n\t# The codes guessed by the player each turn\n\tguess_codes = &#x5B;&#x5B;&#039;-&#039;, &#039;-&#039;, &#039;-&#039;, &#039;-&#039;] for x in range(chances)]\n\n\t# The clues provided to the player each turn\n\tguess_flags = &#x5B;&#x5B;&#039;-&#039;, &#039;-&#039;, &#039;-&#039;, &#039;-&#039;] for x in range(chances)]\n<\/pre><\/div>\n\n\n<p>Each of these data structures come handy when dealing with game logic for our implementation of Mastermind game.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The Game loop<\/h2>\n\n\n\n<p>One of the most crucial parts of game development is the game loop, which is responsible for the proper functioning of the player moves and updates to game variables.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# The current turn\nturn = 0\n\n# The GAME LOOP\nwhile turn &lt; chances:\n<\/pre><\/div>\n\n\n<p>The Game Loop is dependent on the number of chances and the current turn. It is supposed to stop the game whenever the player&#8217;s chances are exhausted.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The Game Menu<\/h2>\n\n\n\n<p>The Game Menu is as a simple aspect of the game, which helps the programmer to interact with the player with instructions or rules.<\/p>\n\n\n\n<p>Our Game Menu looks like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"128\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game_menu.png\" alt=\"Mastermind Game Menu\" class=\"wp-image-7203\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game_menu.png 593w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/mastermind_game_menu-300x65.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><figcaption>Game Menu<\/figcaption><\/figure><\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# The GAME MENU\nprint(&quot;-----------------------------------------&quot;)\nprint(&quot;\\t\\tMenu&quot;)\nprint(&quot;-----------------------------------------&quot;)\nprint(&quot;Enter code using numbers.&quot;)\nprint(&quot;1 - RED, 2 - GREEN, 3 - YELLOW, 4 - BLUE, 5 - BLACK, 6 - ORANGE&quot;)\nprint(&quot;Example: RED YELLOW ORANGE BLACK ---&gt; 1 3 6 5&quot;)\nprint(&quot;-----------------------------------------&quot;)\nprint_mastermind_board(show_passcode, guess_codes, guess_flags)\n<\/pre><\/div>\n\n\n<p>The game menu must be plain and apt.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Handling the Player Input<\/h2>\n\n\n\n<p>Handling player input involves three basic steps: Accepting the player input, applying some sanity checks on it, and if everything is in order, storing it into our data structures.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Accepting the Player Input<\/h3>\n\n\n\n<p>As mentioned in the game menu, the player needs to enter four numbers, each corresponding to a specific color, separated by spaces. <\/p>\n\n\n\n<p>Our job is to parse this player input into a list of integers for retrieving the proper colors.  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Accepting the player input \ntry:\t\n\tcode = list(map(int, input(&quot;Enter your choice = &quot;).split()))\nexcept ValueError:\n\tclear()\n\tprint(&quot;\\tWrong choice!! Try again!!&quot;)\n\tcontinue\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong> The <code>clear()<\/code> function is responsible for keeping the terminal clean, by flushing out the previous output. It requires Python&#8217;s <code>os<\/code> library.<\/p><p>Check the complete code below for function declaration of <code>clear()<\/code>.<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Applying sanity checks<\/h3>\n\n\n\n<p>Next up is doing some sanity checks on the player input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Check if the number of colors nunbers are 4\nif len(code) != 4:\n\tclear()\n\tprint(&quot;\\tWrong choice!! Try again!!&quot;)\n\tcontinue\n\n# Check if each number entered corresponds to a number\nflag = 0\nfor x in code:\n\tif x &gt; 6 or x &lt; 1:\n\t\tflag = 1\n\nif flag == 1:\t\t\t\n\tclear()\n\tprint(&quot;\\tWrong choice!! Try again!!&quot;)\n\tcontinue\t\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Storing the player moves<\/h3>\n\n\n\n<p>After we know the player has made a valid move, we can store it in the game containers.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Storing the player moves\nfor i in range(4):\n\tguess_codes&#x5B;turn]&#x5B;i] = colors_map&#x5B;code&#x5B;i]]\t\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Set up the clues for each move<\/h2>\n\n\n\n<p>There are two set of flags to be assigned, &#8216;R&#8217; if a color is in the right position as in the secret code, and &#8216;W&#8217; if the color is right but in the wrong position.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Process to apply clues according to the player input\t\ndummy_passcode = &#x5B;x for x in passcode]\t\n\npos = 0\n\n# Loop to set up clues for the player move\nfor x in code:\n\tif colors_map&#x5B;x] in dummy_passcode:\n\t\tif code.index(x) == passcode.index(colors_map&#x5B;x]):\n\t\t\tguess_flags&#x5B;turn]&#x5B;pos] = &#039;R&#039;\n\t\telse:\n\t\t\tguess_flags&#x5B;turn]&#x5B;pos] = &#039;W&#039;\n\t\tpos += 1\n\t\tdummy_passcode.remove(colors_map&#x5B;x])\n\nrandom.shuffle(guess_flags&#x5B;turn])\t\t\t\t\n<\/pre><\/div>\n\n\n<p>One small thing to remember is rearranging the flags, since it may give hints related to the positions of colors.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Check for the win condition<\/h2>\n\n\n\n<p>All we have to do is check the latest input with the hidden code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Check for win condition\nif guess_codes&#x5B;turn] == passcode:\n\tclear()\n\tprint_mastermind_board(passcode, guess_codes, guess_flags)\n\tprint(&quot;Congratulations!! YOU WIN!!!!&quot;)\n\tbreak\n<\/pre><\/div>\n\n\n<p>As soon as the player enters the correct code, we display a win message and finish the game.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Update the turn number<\/h2>\n\n\n\n<p>One small yet very important task is to update the turn number after each successful player move.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Update turn\t\nturn += 1\t\t\t\nclear()\n<\/pre><\/div>\n\n\n<p>The last but not the least is handling the loss condition.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Check for the loss condition<\/h2>\n\n\n\n<p>When the player has exhausted the number of chances, the player loses. We need to display appropriate message when this happens. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Check for loss condiiton\t\nif turn == chances:\n\tclear()\n\tprint_mastermind_board(passcode, guess_codes, guess_flags)\n\tprint(&quot;YOU LOSE!!! Better luck next time!!!&quot;)\t\n<\/pre><\/div>\n\n\n<p>This concludes the explanation for creating mastermind using Python language.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The Complete Code<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\nimport os\n\ndef clear():\n\tos.system(&quot;clear&quot;)\n\n# Function to print the mastermind board\ndef print_mastermind_board(passcode, guess_codes, guess_flags):\n\n\n\tprint(&quot;-----------------------------------------&quot;)\n\tprint(&quot;\\t      MASTERMIND&quot;)\n\tprint(&quot;-----------------------------------------&quot;)\n\n\tprint(&quot;    |&quot;, end=&quot;&quot;)\n\tfor x in passcode:\n\t\tprint(&quot;\\t&quot; + x&#x5B;:3], end=&quot;&quot;)\n\tprint()\t\n\n\tfor i in reversed(range(len(guess_codes))):\n\t\tprint(&quot;-----------------------------------------&quot;)\n\t\tprint(guess_flags&#x5B;i]&#x5B;0], guess_flags&#x5B;i]&#x5B;1], &quot;|&quot;)\n\t\t\n\n\t\tprint(guess_flags&#x5B;i]&#x5B;2], guess_flags&#x5B;i]&#x5B;3], end=&quot; |&quot;)\n\t\tfor x in guess_codes&#x5B;i]:\n\t\t\tprint(&quot;\\t&quot; + x&#x5B;:3], end=&quot;&quot;)\n\n\t\tprint()\t\n\tprint(&quot;-----------------------------------------&quot;)\n\n# The Main function\nif __name__ == &#039;__main__&#039;:\n\n\t# List of colors\n\tcolors = &#x5B;&quot;RED&quot;, &quot;GREEN&quot;, &quot;YELLOW&quot;, &quot;BLUE&quot;, &quot;BLACK&quot;, &quot;ORANGE&quot;]\n\n\t# Mapping of colors to numbers\t\n\tcolors_map = {1:&quot;RED&quot;, 2:&quot;GREEN&quot;, 3:&quot;YELLOW&quot;, 4:&quot;BLUE&quot;, 5:&quot;BLACK&quot;, 6:&quot;ORANGE&quot;}\n\n\t# Randomly selecting a passcode\n\trandom.shuffle(colors)\n\tpasscode = colors&#x5B;:4]\n\t\n\t# Number of chances for the player\n\tchances = 8\n\n\t# The passcode to be shown to the user\n\tshow_passcode = &#x5B;&#039;UNK&#039;, &#039;UNK&#039;, &#039;UNK&#039;, &#039;UNK&#039;]\n\n\t# The codes guessed by the player each turn\n\tguess_codes = &#x5B;&#x5B;&#039;-&#039;, &#039;-&#039;, &#039;-&#039;, &#039;-&#039;] for x in range(chances)]\n\n\t# The clues provided to the player each turn\n\tguess_flags = &#x5B;&#x5B;&#039;-&#039;, &#039;-&#039;, &#039;-&#039;, &#039;-&#039;] for x in range(chances)]\n\t\n\tclear()\n\n\t# The current turn\n\tturn = 0\n\n\t# The GAME LOOP\n\twhile turn &lt; chances:\n\t\t\n\t\tprint(&quot;-----------------------------------------&quot;)\n\t\tprint(&quot;\\t\\tMenu&quot;)\n\t\tprint(&quot;-----------------------------------------&quot;)\n\t\tprint(&quot;Enter code using numbers.&quot;)\n\t\tprint(&quot;1 - RED, 2 - GREEN, 3 - YELLOW, 4 - BLUE, 5 - BLACK, 6 - ORANGE&quot;)\n\t\tprint(&quot;Example: RED YELLOW ORANGE BLACK ---&gt; 1 3 6 5&quot;)\n\t\tprint(&quot;-----------------------------------------&quot;)\n\t\tprint_mastermind_board(show_passcode, guess_codes, guess_flags)\n\n\t\t# Accepting the player input \n\t\ttry:\t\n\t\t\tcode = list(map(int, input(&quot;Enter your choice = &quot;).split()))\n\t\texcept ValueError:\n\t\t\tclear()\n\t\t\tprint(&quot;\\tWrong choice!! Try again!!&quot;)\n\t\t\tcontinue\t\n\n\t\t# Check if the number of colors nunbers are 4\n\t\tif len(code) != 4:\n\t\t\tclear()\n\t\t\tprint(&quot;\\tWrong choice!! Try again!!&quot;)\n\t\t\tcontinue\n\n\t\t# Check if each number entered corresponds to a number\n\t\tflag = 0\n\t\tfor x in code:\n\t\t\tif x &gt; 6 or x &lt; 1:\n\t\t\t\tflag = 1\n\n\t\tif flag == 1:\t\t\t\n\t\t\tclear()\n\t\t\tprint(&quot;\\tWrong choice!! Try again!!&quot;)\n\t\t\tcontinue\t\n\n\t\t# Storing the player input\n\t\tfor i in range(4):\n\t\t\tguess_codes&#x5B;turn]&#x5B;i] = colors_map&#x5B;code&#x5B;i]]\t\n\n\t\t# Process to apply clues according to the player input\t\n\t\tdummy_passcode = &#x5B;x for x in passcode]\t\n\n\t\tpos = 0\n\n\t\t# Loop to set up clues for the player move\n\t\tfor x in code:\n\t\t\tif colors_map&#x5B;x] in dummy_passcode:\n\t\t\t\tif code.index(x) == passcode.index(colors_map&#x5B;x]):\n\t\t\t\t\tguess_flags&#x5B;turn]&#x5B;pos] = &#039;R&#039;\n\t\t\t\telse:\n\t\t\t\t\tguess_flags&#x5B;turn]&#x5B;pos] = &#039;W&#039;\n\t\t\t\tpos += 1\n\t\t\t\tdummy_passcode.remove(colors_map&#x5B;x])\n\n\t\trandom.shuffle(guess_flags&#x5B;turn])\t\t\t\t\n\n\n\t\t# Check for win condition\n\t\tif guess_codes&#x5B;turn] == passcode:\n\t\t\tclear()\n\t\t\tprint_mastermind_board(passcode, guess_codes, guess_flags)\n\t\t\tprint(&quot;Congratulations!! YOU WIN!!!!&quot;)\n\t\t\tbreak\n\n\t\t# Update turn\t\n\t\tturn += 1\t\t\t\n\t\tclear()\n\n# Check for loss condiiton\t\nif turn == chances:\n\tclear()\n\tprint_mastermind_board(passcode, guess_codes, guess_flags)\n\tprint(&quot;YOU LOSE!!! Better luck next time!!!&quot;)\t\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The task of creating our own games may seem daunting at first for any novice Python programmer. We hope this article simplified certain Python concepts and made the task look achievable to the reader.<\/p>\n\n\n\n<p>Feel free to comment below for any suggestions or queries. Thank you for reading. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mastermind is a two-player code-breaking game, in which one player hides a code consisting of colors, while the other player has to guess it using clues given by the former player for each turn. In this tutorial, we will be creating our own Mastermind Game using Python Language. In our version of Mastermind, the computer [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":7221,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-7177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7177"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7221"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}