[
  {
    "path": ".gitattributes",
    "content": "# Auto detect text files and perform LF normalization\n* text=auto\n"
  },
  {
    "path": ".gitignore",
    "content": ".idea/*\n*.pyc\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2020 Aqeel Anwar\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# Tic-Tac-Toe\n\nThis repository contains python based interactive Tic-Tac-Toe game.\n\n## Running Tic-Tac-Toe:\n\n```\ngit clone https://github.com/aqeelanwar/Tic-Tac-Toe.git\ncd Tic-Tac-Toe\npython main.py\n```\n\n<p align=\"center\">\n<img src=\"/images/preview.gif\">\n</p>\n\n## Screenshots\n<p align=\"center\">\n<img width=1000 src=\"/images/screenshot.png\">\n\n</p>\n\n## Controls\n1. Player X starts the game\n2. Click on each grid to place symbol\n3. The result of the game is displayed at the end of the game\n4. A track of player scores is maintained\n5. Click anywhere on the result screen to play again\n\n\n\n## Author\n[Aqeel Anwar](https://www.prism.gatech.edu/~manwar8)\n\n"
  },
  {
    "path": "docs/_config.yml",
    "content": "theme: jekyll-theme-cayman"
  },
  {
    "path": "main.py",
    "content": "# Author: aqeelanwar\n# Created: 12 March,2020, 7:06 PM\n# Email: aqeel.anwar@gatech.edu\n\nfrom tkinter import *\nimport numpy as np\n\nsize_of_board = 600\nsymbol_size = (size_of_board / 3 - size_of_board / 8) / 2\nsymbol_thickness = 50\nsymbol_X_color = '#EE4035'\nsymbol_O_color = '#0492CF'\nGreen_color = '#7BC043'\n\n\nclass Tic_Tac_Toe():\n    # ------------------------------------------------------------------\n    # Initialization Functions:\n    # ------------------------------------------------------------------\n    def __init__(self):\n        self.window = Tk()\n        self.window.title('Tic-Tac-Toe')\n        self.canvas = Canvas(self.window, width=size_of_board, height=size_of_board)\n        self.canvas.pack()\n        # Input from user in form of clicks\n        self.window.bind('<Button-1>', self.click)\n\n        self.initialize_board()\n        self.player_X_turns = True\n        self.board_status = np.zeros(shape=(3, 3))\n\n        self.player_X_starts = True\n        self.reset_board = False\n        self.gameover = False\n        self.tie = False\n        self.X_wins = False\n        self.O_wins = False\n\n        self.X_score = 0\n        self.O_score = 0\n        self.tie_score = 0\n\n    def mainloop(self):\n        self.window.mainloop()\n\n    def initialize_board(self):\n        for i in range(2):\n            self.canvas.create_line((i + 1) * size_of_board / 3, 0, (i + 1) * size_of_board / 3, size_of_board)\n\n        for i in range(2):\n            self.canvas.create_line(0, (i + 1) * size_of_board / 3, size_of_board, (i + 1) * size_of_board / 3)\n\n    def play_again(self):\n        self.initialize_board()\n        self.player_X_starts = not self.player_X_starts\n        self.player_X_turns = self.player_X_starts\n        self.board_status = np.zeros(shape=(3, 3))\n\n    # ------------------------------------------------------------------\n    # Drawing Functions:\n    # The modules required to draw required game based object on canvas\n    # ------------------------------------------------------------------\n\n    def draw_O(self, logical_position):\n        logical_position = np.array(logical_position)\n        # logical_position = grid value on the board\n        # grid_position = actual pixel values of the center of the grid\n        grid_position = self.convert_logical_to_grid_position(logical_position)\n        self.canvas.create_oval(grid_position[0] - symbol_size, grid_position[1] - symbol_size,\n                                grid_position[0] + symbol_size, grid_position[1] + symbol_size, width=symbol_thickness,\n                                outline=symbol_O_color)\n\n    def draw_X(self, logical_position):\n        grid_position = self.convert_logical_to_grid_position(logical_position)\n        self.canvas.create_line(grid_position[0] - symbol_size, grid_position[1] - symbol_size,\n                                grid_position[0] + symbol_size, grid_position[1] + symbol_size, width=symbol_thickness,\n                                fill=symbol_X_color)\n        self.canvas.create_line(grid_position[0] - symbol_size, grid_position[1] + symbol_size,\n                                grid_position[0] + symbol_size, grid_position[1] - symbol_size, width=symbol_thickness,\n                                fill=symbol_X_color)\n\n    def display_gameover(self):\n\n        if self.X_wins:\n            self.X_score += 1\n            text = 'Winner: Player 1 (X)'\n            color = symbol_X_color\n        elif self.O_wins:\n            self.O_score += 1\n            text = 'Winner: Player 2 (O)'\n            color = symbol_O_color\n        else:\n            self.tie_score += 1\n            text = 'Its a tie'\n            color = 'gray'\n\n        self.canvas.delete(\"all\")\n        self.canvas.create_text(size_of_board / 2, size_of_board / 3, font=\"cmr 60 bold\", fill=color, text=text)\n\n        score_text = 'Scores \\n'\n        self.canvas.create_text(size_of_board / 2, 5 * size_of_board / 8, font=\"cmr 40 bold\", fill=Green_color,\n                                text=score_text)\n\n        score_text = 'Player 1 (X) : ' + str(self.X_score) + '\\n'\n        score_text += 'Player 2 (O): ' + str(self.O_score) + '\\n'\n        score_text += 'Tie                    : ' + str(self.tie_score)\n        self.canvas.create_text(size_of_board / 2, 3 * size_of_board / 4, font=\"cmr 30 bold\", fill=Green_color,\n                                text=score_text)\n        self.reset_board = True\n\n        score_text = 'Click to play again \\n'\n        self.canvas.create_text(size_of_board / 2, 15 * size_of_board / 16, font=\"cmr 20 bold\", fill=\"gray\",\n                                text=score_text)\n\n    # ------------------------------------------------------------------\n    # Logical Functions:\n    # The modules required to carry out game logic\n    # ------------------------------------------------------------------\n\n    def convert_logical_to_grid_position(self, logical_position):\n        logical_position = np.array(logical_position, dtype=int)\n        return (size_of_board / 3) * logical_position + size_of_board / 6\n\n    def convert_grid_to_logical_position(self, grid_position):\n        grid_position = np.array(grid_position)\n        return np.array(grid_position // (size_of_board / 3), dtype=int)\n\n    def is_grid_occupied(self, logical_position):\n        if self.board_status[logical_position[0]][logical_position[1]] == 0:\n            return False\n        else:\n            return True\n\n    def is_winner(self, player):\n\n        player = -1 if player == 'X' else 1\n\n        # Three in a row\n        for i in range(3):\n            if self.board_status[i][0] == self.board_status[i][1] == self.board_status[i][2] == player:\n                return True\n            if self.board_status[0][i] == self.board_status[1][i] == self.board_status[2][i] == player:\n                return True\n\n        # Diagonals\n        if self.board_status[0][0] == self.board_status[1][1] == self.board_status[2][2] == player:\n            return True\n\n        if self.board_status[0][2] == self.board_status[1][1] == self.board_status[2][0] == player:\n            return True\n\n        return False\n\n    def is_tie(self):\n\n        r, c = np.where(self.board_status == 0)\n        tie = False\n        if len(r) == 0:\n            tie = True\n\n        return tie\n\n    def is_gameover(self):\n        # Either someone wins or all grid occupied\n        self.X_wins = self.is_winner('X')\n        if not self.X_wins:\n            self.O_wins = self.is_winner('O')\n\n        if not self.O_wins:\n            self.tie = self.is_tie()\n\n        gameover = self.X_wins or self.O_wins or self.tie\n\n        if self.X_wins:\n            print('X wins')\n        if self.O_wins:\n            print('O wins')\n        if self.tie:\n            print('Its a tie')\n\n        return gameover\n\n\n\n\n\n    def click(self, event):\n        grid_position = [event.x, event.y]\n        logical_position = self.convert_grid_to_logical_position(grid_position)\n\n        if not self.reset_board:\n            if self.player_X_turns:\n                if not self.is_grid_occupied(logical_position):\n                    self.draw_X(logical_position)\n                    self.board_status[logical_position[0]][logical_position[1]] = -1\n                    self.player_X_turns = not self.player_X_turns\n            else:\n                if not self.is_grid_occupied(logical_position):\n                    self.draw_O(logical_position)\n                    self.board_status[logical_position[0]][logical_position[1]] = 1\n                    self.player_X_turns = not self.player_X_turns\n\n            # Check if game is concluded\n            if self.is_gameover():\n                self.display_gameover()\n                # print('Done')\n        else:  # Play Again\n            self.canvas.delete(\"all\")\n            self.play_again()\n            self.reset_board = False\n\n\ngame_instance = Tic_Tac_Toe()\ngame_instance.mainloop()"
  }
]