{"id":6296,"date":"2020-06-18T13:18:09","date_gmt":"2020-06-18T13:18:09","guid":{"rendered":"https:\/\/www.askpython.com\/?p=6296"},"modified":"2023-02-16T19:57:04","modified_gmt":"2023-02-16T19:57:04","slug":"tic-tac-toe-using-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/tic-tac-toe-using-python","title":{"rendered":"Tic-tac-toe using Python"},"content":{"rendered":"\n<p>In this article, we will be going through the steps of creating Tic-tac-toe using Python Language from scratch.<\/p>\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\/06\/tic_tac_toe.mp4\"><\/video><figcaption>Tic Tac Toe Gameplay <\/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<h2 class=\"wp-block-heading\">About the game<\/h2>\n\n\n\n<p>Tic-tac-toe is a two-player game, that is played on a 3&#215;3 square grid. Each player occupies a cell in turns, with the objective of placing three marks in a horizontal, vertical, or diagonal pattern. One player uses cross <code>'X'<\/code> as his marker, while the other uses a naught <code>'O'<\/code>. <\/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\">Step 1: Tic-tac-toe Design<\/h2>\n\n\n\n<p>We will be playing Tic-tac-toe on the command line, therefore, the first thing we have to do is create a design for our tic-tac-toe. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"207\" height=\"191\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/design.png\" alt=\"Design\" class=\"wp-image-6302\"\/><figcaption>Tic-tac-toe Design<\/figcaption><\/figure><\/div>\n\n\n\n<p>If a player has to mark a particular box, he must enter the corresponding number shown in the grid. Suppose, we wish to occupy the center block, then we will input <code>5<\/code> in the terminal. This grid can be generated by:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function to print Tic Tac Toe\ndef print_tic_tac_toe(values):\n\tprint(&quot;\\n&quot;)\n\tprint(&quot;\\t     |     |&quot;)\n\tprint(&quot;\\t  {}  |  {}  |  {}&quot;.format(values&#x5B;0], values&#x5B;1], values&#x5B;2]))\n\tprint(&#039;\\t_____|_____|_____&#039;)\n\n\tprint(&quot;\\t     |     |&quot;)\n\tprint(&quot;\\t  {}  |  {}  |  {}&quot;.format(values&#x5B;3], values&#x5B;4], values&#x5B;5]))\n\tprint(&#039;\\t_____|_____|_____&#039;)\n\n\tprint(&quot;\\t     |     |&quot;)\n\n\tprint(&quot;\\t  {}  |  {}  |  {}&quot;.format(values&#x5B;6], values&#x5B;7], values&#x5B;8]))\n\tprint(&quot;\\t     |     |&quot;)\n\tprint(&quot;\\n&quot;)\n<\/pre><\/div>\n\n\n<p>In the code above, the function creates our tic-tac-toe game according to the values delivered as an argument. Here the argument, <code>values<\/code> is a list containing the status of each cell in the grid. <\/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\">Step 2: Store information using data structures<\/h2>\n\n\n\n<p>The core of any game is the game mechanics behind it. Since this is a fairly easy game to create, the mechanics involved are simple too. <\/p>\n\n\n\n<p>At any instant of time, we need two crucial information:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Status of the grid &#8211; <\/strong>We must have a data structure that stores each cell&#8217;s state, that is, whether it is occupied or vacant.<\/li><li><strong>Each player&#8217;s moves<\/strong> <strong>&#8211;<\/strong> We must somehow have the knowledge of each  player&#8217;s past and present moves, that is, the positions occupied by <code>'X'<\/code> and <code>'O'<\/code>.<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><span style=\"color:#8c9fa7\" class=\"has-inline-color\"><strong>Note:<\/strong> Both the information could have been accessed using the status of the grid, but it would have required to traverse it every time we need the player&#8217;s positions. This can be called as time vs. space complexity trade-off. It is a general technique to conserve time. <\/span><\/p><\/blockquote>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function for a single game of Tic Tac Toe\ndef single_game(cur_player):\n\n\t# Represents the Tic Tac Toe\n\tvalues = &#x5B;&#039; &#039; for x in range(9)]\n\t\n\t# Stores the positions occupied by X and O\n\tplayer_pos = {&#039;X&#039;:&#x5B;], &#039;O&#039;:&#x5B;]}\n<\/pre><\/div>\n\n\n<p><strong>Status of the grid<\/strong> is managed by a list of characters, which can have three possible values,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <code>' '<\/code> &#8211; A vacant cell<\/li><li><code>'X'<\/code> &#8211; A cell occupied by player X<\/li><li><code>'O'<\/code> &#8211; A cell occupied by player O<\/li><\/ul>\n\n\n\n<p><strong>Each player&#8217;s moves<\/strong> are stored as a dictionary of a list of integers. The keys are <code>'X'<\/code> and <code>'O'<\/code> for the respective player. Their corresponding lists contain the numbers given to the grid cells, they occupy. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><span style=\"color:#8c9fa7\" class=\"has-inline-color\"><strong>Note:<\/strong> The variable <code>cur_player<\/code>, stores the current player making the move, as in <code>'X'<\/code> or <code>'O'<\/code>.  <\/span><\/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<h2 class=\"wp-block-heading\">Step 3: Game Loop<\/h2>\n\n\n\n<p>Every game, has some kind of game loop, which runs until some player wins or the game ends in a draw. In tic-tac-toe, each loop iteration refers to a single move any player makes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Game Loop for a single game of Tic Tac Toe\nwhile True:\n\tprint_tic_tac_toe(values)\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\">Step 4: Handle player input<\/h2>\n\n\n\n<p>In every game iteration, a player must input his move. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Try exception block for MOVE input\ntry:\n\tprint(&quot;Player &quot;, cur_player, &quot; turn. Which box? : &quot;, end=&quot;&quot;)\n\tmove = int(input())\t\nexcept ValueError:\n\tprint(&quot;Wrong Input!!! Try Again&quot;)\n\tcontinue\n\n# Sanity check for MOVE inout\nif move &lt; 1 or move &gt; 9:\n\tprint(&quot;Wrong Input!!! Try Again&quot;)\n\tcontinue\n\n# Check if the box is not occupied already\nif values&#x5B;move-1] != &#039; &#039;:\n\tprint(&quot;Place already filled. Try again!!&quot;)\n\tcontinue\n<\/pre><\/div>\n\n\n<p>We create a <code>try<\/code> block, in case a player enters some unintended value. Such an event must not stop the game, therefore, we handle the exception of <code>ValueError<\/code> and continue with our game. <\/p>\n\n\n\n<p>We need to perform some sanity checks, like value entered is a valid position and if it is a valid position, is it already occupied?<\/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\">Step 5: Update information<\/h2>\n\n\n\n<p>According to the player input, we need to update the information for the smooth functioning of the game. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Update game information\n\n# Updating grid status \nvalues&#x5B;move-1] = cur_player\n\n# Updating player positions\nplayer_pos&#x5B;cur_player].append(move)\n<\/pre><\/div>\n\n\n<p>The <code>values<\/code> list updates the cell occupied according to the current player. The player position adds the position just taken by the current player.<\/p>\n\n\n\n<p>After updating the <code>values<\/code> list and calling the <code>print_tic_tac_toe()<\/code> function, the grid looks like this: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"328\" height=\"260\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/update_after_turn.png\" alt=\"Update After Turn\" class=\"wp-image-6309\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/update_after_turn.png 328w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/update_after_turn-300x238.png 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><figcaption>Tic-tac-toe after 5 turns.<br> Last move: &#8216;X&#8217; at 2<\/figcaption><\/figure><\/div>\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\">Step 6: Check win or draw<\/h2>\n\n\n\n<p>After each move, we have to check whether any player won the game or the game has been drawn. It can be checked by:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function Calls:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function call for checking win\nif check_win(player_pos, cur_player):\n\tprint_tic_tac_toe(values)\n\tprint(&quot;Player &quot;, cur_player, &quot; has won the game!!&quot;)\t\t\n\tprint(&quot;\\n&quot;)\n\treturn cur_player\n\n# Function call for checking draw game\nif check_draw(player_pos):\n\tprint_tic_tac_toe(values)\n\tprint(&quot;Game Drawn&quot;)\n\tprint(&quot;\\n&quot;)\n\treturn &#039;D&#039;\n<\/pre><\/div>\n\n\n<p>If any player wins, then the <code>single_game()<\/code> function returns the current player, who made the move. In case, the game is drawn, <code>'D'<\/code> is sent back.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Functions:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function to check if any player has won\ndef check_win(player_pos, cur_player):\n\n\t# All possible winning combinations\n\tsoln = &#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;7, 8, 9], &#x5B;1, 4, 7], &#x5B;2, 5, 8], &#x5B;3, 6, 9], &#x5B;1, 5, 9], &#x5B;3, 5, 7]]\n\n\t# Loop to check if any winning combination is satisfied\n\tfor x in soln:\n\t\tif all(y in player_pos&#x5B;cur_player] for y in x):\n\n\t\t\t# Return True if any winning combination satisfies\n\t\t\treturn True\n\t# Return False if no combination is satisfied\t\t\n\treturn False\t\t\n\n# Function to check if the game is drawn\ndef check_draw(player_pos):\n\tif len(player_pos&#x5B;&#039;X&#039;]) + len(player_pos&#x5B;&#039;O&#039;]) == 9:\n\t\treturn True\n\treturn False\t\n<\/pre><\/div>\n\n\n<p><strong>check_win() &#8211; <\/strong>The function has all the winning combinations. All it does is, it checks whether any of the winning combinations is satisfied by the current player&#8217;s positions. If it does, it returns <code>True<\/code>. If none of the combinations is satisfied, then the function returns <code>False<\/code>.<\/p>\n\n\n\n<p><strong>check_draw() &#8211; <\/strong>The draw condition is fairly simple, as the game is drawn when all &#8216;nine&#8217; positions are taken. <\/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\">Step 7: Switch the current player<\/h2>\n\n\n\n<p>Since each player only moves once at a time, therefore after every successful move, we have to swap the current player.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Switch player moves\nif cur_player == &#039;X&#039;:\n\tcur_player = &#039;O&#039;\nelse:\n\tcur_player = &#039;X&#039;\n<\/pre><\/div>\n\n\n<p> As far as a single game is concerned, this is all we need to do. But this article also presents a scoreboard system for keeping track, if the players want to play multiple games.<\/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\">Step 8: Enter player names<\/h2>\n\n\n\n<p>It is mandatory for any scoreboard to display each player names.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif __name__ == &quot;__main__&quot;:\n\n\tprint(&quot;Player 1&quot;)\n\tplayer1 = input(&quot;Enter the name : &quot;)\n\tprint(&quot;\\n&quot;)\n\n\tprint(&quot;Player 2&quot;)\n\tplayer2 = input(&quot;Enter the name : &quot;)\n\tprint(&quot;\\n&quot;)\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\">Step 9: Store game-related information <\/h2>\n\n\n\n<p>The information like the current player, the choice of players (take cross or naught), the available options (cross and naught), and the scoreboard need to be stored.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Stores the player who chooses X and O\ncur_player = player1\n\n# Stores the choice of players\nplayer_choice = {&#039;X&#039; : &quot;&quot;, &#039;O&#039; : &quot;&quot;}\n\n# Stores the options\noptions = &#x5B;&#039;X&#039;, &#039;O&#039;]\n\n# Stores the scoreboard\nscore_board = {player1: 0, player2: 0}\n<\/pre><\/div>\n\n\n<p>By default, the current player is the player who entered the name first. <\/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\">Step 10: Design scoreboard<\/h2>\n\n\n\n<p>The scoreboard is stored as a dictionary, where keys are the player names and values are their win number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function to print the score-board\ndef print_scoreboard(score_board):\n\tprint(&quot;--------------------------------&quot;)\n\tprint(&quot;       \t   SCOREBOARD       &quot;)\n\tprint(&quot;--------------------------------&quot;)\n\n\tplayers = list(score_board.keys())\n\tprint(&quot;   &quot;, players&#x5B;0], &quot;    &quot;, score_board&#x5B;players&#x5B;0]])\n\tprint(&quot;   &quot;, players&#x5B;1], &quot;    &quot;, score_board&#x5B;players&#x5B;1]])\n\n\tprint(&quot;--------------------------------\\n&quot;)\n<\/pre><\/div>\n\n\n<p>To display the scoreboard, we need the player names. The keys are extracted using <code>.keys()<\/code> function and then converted to list, so that it can indexed while displaying the scores.<\/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\">Step 11: Outer Game Loop <\/h2>\n\n\n\n<p>We need another game loop, for managing multiple matches of Tic-tac-toe. Each match, the current player chooses his mark (<code>'X'<\/code>or <code>'O'<\/code>). The menu for choosing must be displayed in every game iteration:  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Game Loop for a series of Tic Tac Toe\n# The loop runs until the players quit \nwhile True:\n\n\t# Player choice Menu\n\tprint(&quot;Turn to choose for&quot;, cur_player)\n\tprint(&quot;Enter 1 for X&quot;)\n\tprint(&quot;Enter 2 for O&quot;)\n\tprint(&quot;Enter 3 to Quit&quot;)\n<\/pre><\/div>\n\n\n<p>The scoreboard and menu look like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"344\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/score_board_menu.png\" alt=\"Score Board Menu\" class=\"wp-image-6311\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/score_board_menu.png 453w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/score_board_menu-300x228.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><figcaption>Scoreboard and Menu<\/figcaption><\/figure><\/div>\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\">Step 12: Handle and Assign Player Choice<\/h2>\n\n\n\n<p>Each iteration, we have to handle and store current player&#8217;s choice.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Try exception for CHOICE input\ntry:\n\tchoice = int(input())\t\nexcept ValueError:\n\tprint(&quot;Wrong Input!!! Try Again\\n&quot;)\n\tcontinue\n\n# Conditions for player choice\t\nif choice == 1:\n\tplayer_choice&#x5B;&#039;X&#039;] = cur_player\n\tif cur_player == player1:\n\t\tplayer_choice&#x5B;&#039;O&#039;] = player2\n\telse:\n\t\tplayer_choice&#x5B;&#039;O&#039;] = player1\n\nelif choice == 2:\n\tplayer_choice&#x5B;&#039;O&#039;] = cur_player\n\tif cur_player == player1:\n\t\tplayer_choice&#x5B;&#039;X&#039;] = player2\n\telse:\n\t\tplayer_choice&#x5B;&#039;X&#039;] = player1\n\t\t\nelif choice == 3:\n\tprint(&quot;Final Scores&quot;)\n\tprint_scoreboard(score_board)\n\tbreak\t\n\nelse:\n\tprint(&quot;Wrong Choice!!!! Try Again\\n&quot;)\n<\/pre><\/div>\n\n\n<p>According to the player&#8217;s choice, the data has been stored. This is important since after each game finishes, it will tell us which player won.  <\/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\">Step 13: Execute the match<\/h2>\n\n\n\n<p>After storing all the necessary information, it is time to execute an independent match and store the winning mark. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Stores the winner in a single game of Tic-tac-toe\nwinner = single_game(options&#x5B;choice-1])\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\">Step 14: Update the scoreboard<\/h2>\n\n\n\n<p>We need to update the scoreboard after each match of Tic-tac-toe.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Updates the scoreboard according to the winner\nif winner != &#039;D&#039; :\n\tplayer_won = player_choice&#x5B;winner]\n\tscore_board&#x5B;player_won] = score_board&#x5B;player_won] + 1\n\nprint_scoreboard(score_board)\n<\/pre><\/div>\n\n\n<p>If the game has not ended in a draw, then we update the scoreboard.<\/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\">Step 15: Switch the choosing player<\/h2>\n\n\n\n<p>It is a generous idea, that each player must have the opportunity to choose which mark they want. To do so, we swap the value in <code>cur_player<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Switch player who chooses X or O\nif cur_player == player1:\n\tcur_player = player2\nelse:\n\tcur_player = player1\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\">Complete Working Code <\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Function to print Tic Tac Toe\ndef print_tic_tac_toe(values):\n\tprint(&quot;\\n&quot;)\n\tprint(&quot;\\t     |     |&quot;)\n\tprint(&quot;\\t  {}  |  {}  |  {}&quot;.format(values&#x5B;0], values&#x5B;1], values&#x5B;2]))\n\tprint(&#039;\\t_____|_____|_____&#039;)\n\n\tprint(&quot;\\t     |     |&quot;)\n\tprint(&quot;\\t  {}  |  {}  |  {}&quot;.format(values&#x5B;3], values&#x5B;4], values&#x5B;5]))\n\tprint(&#039;\\t_____|_____|_____&#039;)\n\n\tprint(&quot;\\t     |     |&quot;)\n\n\tprint(&quot;\\t  {}  |  {}  |  {}&quot;.format(values&#x5B;6], values&#x5B;7], values&#x5B;8]))\n\tprint(&quot;\\t     |     |&quot;)\n\tprint(&quot;\\n&quot;)\n\n\n# Function to print the score-board\ndef print_scoreboard(score_board):\n\tprint(&quot;\\t--------------------------------&quot;)\n\tprint(&quot;\\t       \t   SCOREBOARD       &quot;)\n\tprint(&quot;\\t--------------------------------&quot;)\n\n\tplayers = list(score_board.keys())\n\tprint(&quot;\\t   &quot;, players&#x5B;0], &quot;\\t    &quot;, score_board&#x5B;players&#x5B;0]])\n\tprint(&quot;\\t   &quot;, players&#x5B;1], &quot;\\t    &quot;, score_board&#x5B;players&#x5B;1]])\n\n\tprint(&quot;\\t--------------------------------\\n&quot;)\n\n# Function to check if any player has won\ndef check_win(player_pos, cur_player):\n\n\t# All possible winning combinations\n\tsoln = &#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;7, 8, 9], &#x5B;1, 4, 7], &#x5B;2, 5, 8], &#x5B;3, 6, 9], &#x5B;1, 5, 9], &#x5B;3, 5, 7]]\n\n\t# Loop to check if any winning combination is satisfied\n\tfor x in soln:\n\t\tif all(y in player_pos&#x5B;cur_player] for y in x):\n\n\t\t\t# Return True if any winning combination satisfies\n\t\t\treturn True\n\t# Return False if no combination is satisfied\t\t\n\treturn False\t\t\n\n# Function to check if the game is drawn\ndef check_draw(player_pos):\n\tif len(player_pos&#x5B;&#039;X&#039;]) + len(player_pos&#x5B;&#039;O&#039;]) == 9:\n\t\treturn True\n\treturn False\t\t\n\n# Function for a single game of Tic Tac Toe\ndef single_game(cur_player):\n\n\t# Represents the Tic Tac Toe\n\tvalues = &#x5B;&#039; &#039; for x in range(9)]\n\t\n\t# Stores the positions occupied by X and O\n\tplayer_pos = {&#039;X&#039;:&#x5B;], &#039;O&#039;:&#x5B;]}\n\t\n\t# Game Loop for a single game of Tic Tac Toe\n\twhile True:\n\t\tprint_tic_tac_toe(values)\n\t\t\n\t\t# Try exception block for MOVE input\n\t\ttry:\n\t\t\tprint(&quot;Player &quot;, cur_player, &quot; turn. Which box? : &quot;, end=&quot;&quot;)\n\t\t\tmove = int(input())\t\n\t\texcept ValueError:\n\t\t\tprint(&quot;Wrong Input!!! Try Again&quot;)\n\t\t\tcontinue\n\n\t\t# Sanity check for MOVE inout\n\t\tif move &lt; 1 or move &gt; 9:\n\t\t\tprint(&quot;Wrong Input!!! Try Again&quot;)\n\t\t\tcontinue\n\n\t\t# Check if the box is not occupied already\n\t\tif values&#x5B;move-1] != &#039; &#039;:\n\t\t\tprint(&quot;Place already filled. Try again!!&quot;)\n\t\t\tcontinue\n\n\t\t# Update game information\n\n\t\t# Updating grid status \n\t\tvalues&#x5B;move-1] = cur_player\n\n\t\t# Updating player positions\n\t\tplayer_pos&#x5B;cur_player].append(move)\n\n\t\t# Function call for checking win\n\t\tif check_win(player_pos, cur_player):\n\t\t\tprint_tic_tac_toe(values)\n\t\t\tprint(&quot;Player &quot;, cur_player, &quot; has won the game!!&quot;)\t\t\n\t\t\tprint(&quot;\\n&quot;)\n\t\t\treturn cur_player\n\n\t\t# Function call for checking draw game\n\t\tif check_draw(player_pos):\n\t\t\tprint_tic_tac_toe(values)\n\t\t\tprint(&quot;Game Drawn&quot;)\n\t\t\tprint(&quot;\\n&quot;)\n\t\t\treturn &#039;D&#039;\n\n\t\t# Switch player moves\n\t\tif cur_player == &#039;X&#039;:\n\t\t\tcur_player = &#039;O&#039;\n\t\telse:\n\t\t\tcur_player = &#039;X&#039;\n\nif __name__ == &quot;__main__&quot;:\n\n\tprint(&quot;Player 1&quot;)\n\tplayer1 = input(&quot;Enter the name : &quot;)\n\tprint(&quot;\\n&quot;)\n\n\tprint(&quot;Player 2&quot;)\n\tplayer2 = input(&quot;Enter the name : &quot;)\n\tprint(&quot;\\n&quot;)\n\t\n\t# Stores the player who chooses X and O\n\tcur_player = player1\n\n\t# Stores the choice of players\n\tplayer_choice = {&#039;X&#039; : &quot;&quot;, &#039;O&#039; : &quot;&quot;}\n\n\t# Stores the options\n\toptions = &#x5B;&#039;X&#039;, &#039;O&#039;]\n\n\t# Stores the scoreboard\n\tscore_board = {player1: 0, player2: 0}\n\tprint_scoreboard(score_board)\n\n\t# Game Loop for a series of Tic Tac Toe\n\t# The loop runs until the players quit \n\twhile True:\n\n\t\t# Player choice Menu\n\t\tprint(&quot;Turn to choose for&quot;, cur_player)\n\t\tprint(&quot;Enter 1 for X&quot;)\n\t\tprint(&quot;Enter 2 for O&quot;)\n\t\tprint(&quot;Enter 3 to Quit&quot;)\n\n\t\t# Try exception for CHOICE input\n\t\ttry:\n\t\t\tchoice = int(input())\t\n\t\texcept ValueError:\n\t\t\tprint(&quot;Wrong Input!!! Try Again\\n&quot;)\n\t\t\tcontinue\n\n\t\t# Conditions for player choice\t\n\t\tif choice == 1:\n\t\t\tplayer_choice&#x5B;&#039;X&#039;] = cur_player\n\t\t\tif cur_player == player1:\n\t\t\t\tplayer_choice&#x5B;&#039;O&#039;] = player2\n\t\t\telse:\n\t\t\t\tplayer_choice&#x5B;&#039;O&#039;] = player1\n\n\t\telif choice == 2:\n\t\t\tplayer_choice&#x5B;&#039;O&#039;] = cur_player\n\t\t\tif cur_player == player1:\n\t\t\t\tplayer_choice&#x5B;&#039;X&#039;] = player2\n\t\t\telse:\n\t\t\t\tplayer_choice&#x5B;&#039;X&#039;] = player1\n\t\t\n\t\telif choice == 3:\n\t\t\tprint(&quot;Final Scores&quot;)\n\t\t\tprint_scoreboard(score_board)\n\t\t\tbreak\t\n\n\t\telse:\n\t\t\tprint(&quot;Wrong Choice!!!! Try Again\\n&quot;)\n\n\t\t# Stores the winner in a single game of Tic Tac Toe\n\t\twinner = single_game(options&#x5B;choice-1])\n\t\t\n\t\t# Edits the scoreboard according to the winner\n\t\tif winner != &#039;D&#039; :\n\t\t\tplayer_won = player_choice&#x5B;winner]\n\t\t\tscore_board&#x5B;player_won] = score_board&#x5B;player_won] + 1\n\n\t\tprint_scoreboard(score_board)\n\t\t# Switch player who chooses X or O\n\t\tif cur_player == player1:\n\t\t\tcur_player = player2\n\t\telse:\n\t\t\tcur_player = player1\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\">Time for a Gameplay!<\/h2>\n\n\n\n<p>All the steps to create the game has been finished. Now is the time to play the game. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\nPlayer 1\nEnter the name : Luffy\n\n\nPlayer 2\nEnter the name : Sanji\n\n\n\t--------------------------------\n\t       \t   SCOREBOARD       \n\t--------------------------------\n\t    Luffy \t     0\n\t    Sanji \t     0\n\t--------------------------------\n\nTurn to choose for Luffy\nEnter 1 for X\nEnter 2 for O\nEnter 3 to Quit\n1\n\n\n\t     |     |\n\t     |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  X  turn. Which box? : 5\n\n\n\t     |     |\n\t     |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  O  turn. Which box? : 1\n\n\n\t     |     |\n\t  O  |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  X  turn. Which box? : 9\n\n\n\t     |     |\n\t  O  |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |  X\n\t     |     |\n\n\nPlayer  O  turn. Which box? : 2\n\n\n\t     |     |\n\t  O  |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |  X\n\t     |     |\n\n\nPlayer  X  turn. Which box? : 3\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |  X\n\t     |     |\n\n\nPlayer  O  turn. Which box? : 7\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t_____|_____|_____\n\t     |     |\n\t  O  |     |  X\n\t     |     |\n\n\nPlayer  X  turn. Which box? : 6\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  X  |  X\n\t_____|_____|_____\n\t     |     |\n\t  O  |     |  X\n\t     |     |\n\n\nPlayer  X  has won the game!!\n\n\n\t--------------------------------\n\t       \t   SCOREBOARD       \n\t--------------------------------\n\t    Luffy \t     1\n\t    Sanji \t     0\n\t--------------------------------\n\nTurn to choose for Sanji\nEnter 1 for X\nEnter 2 for O\nEnter 3 to Quit\n2\n\n\n\t     |     |\n\t     |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  O  turn. Which box? : 5\n\n\n\t     |     |\n\t     |     |   \n\t_____|_____|_____\n\t     |     |\n\t     |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  X  turn. Which box? : 3\n\n\n\t     |     |\n\t     |     |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  O  turn. Which box? : 2\n\n\n\t     |     |\n\t     |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |     |   \n\t     |     |\n\n\nPlayer  X  turn. Which box? : 8\n\n\n\t     |     |\n\t     |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t     |     |\n\n\nPlayer  O  turn. Which box? : 1\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |   \n\t     |     |\n\n\nPlayer  X  turn. Which box? : 9\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |   \n\t_____|_____|_____\n\t     |     |\n\t     |  X  |  X\n\t     |     |\n\n\nPlayer  O  turn. Which box? : 6\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |  O\n\t_____|_____|_____\n\t     |     |\n\t     |  X  |  X\n\t     |     |\n\n\nPlayer  X  turn. Which box? : 7\n\n\n\t     |     |\n\t  O  |  O  |  X\n\t_____|_____|_____\n\t     |     |\n\t     |  O  |  O\n\t_____|_____|_____\n\t     |     |\n\t  X  |  X  |  X\n\t     |     |\n\n\nPlayer  X  has won the game!!\n\n\n\t--------------------------------\n\t       \t   SCOREBOARD       \n\t--------------------------------\n\t    Luffy \t     2\n\t    Sanji \t     0\n\t--------------------------------\n\nTurn to choose for Luffy\nEnter 1 for X\nEnter 2 for O\nEnter 3 to Quit\n3\nFinal Scores\n\t--------------------------------\n\t       \t   SCOREBOARD       \n\t--------------------------------\n\t    Luffy \t     2\n\t    Sanji \t     0\n\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<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>We hope that this article was fun as well as informative to the reader. I&#8217;ve also uploaded the code on Github. You can visit <a aria-label=\" (opens in a new tab)\" class=\"rank-math-link rank-math-link\" rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Aprataksh\/Tic-tac-toe\" target=\"_blank\">here<\/a> for the code. If there are any suggestions for the game, feel free to comment.<\/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","protected":false},"excerpt":{"rendered":"<p>In this article, we will be going through the steps of creating Tic-tac-toe using Python Language from scratch. About the game Tic-tac-toe is a two-player game, that is played on a 3&#215;3 square grid. Each player occupies a cell in turns, with the objective of placing three marks in a horizontal, vertical, or diagonal pattern. [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":6329,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-6296","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\/6296","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=6296"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6296\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/6329"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=6296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=6296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=6296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}