{"id":22513917,"url":"https://github.com/emahtab/design-tic-tac-toe","last_synced_at":"2026-03-19T23:03:04.800Z","repository":{"id":79525434,"uuid":"277149153","full_name":"eMahtab/design-tic-tac-toe","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2022-01-10T16:54:06.000Z","size":54,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:07.514Z","etag":null,"topics":["leetcode","low-level-design","problem-solving","tic-tac-toe"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-04T16:45:11.000Z","updated_at":"2022-05-18T18:44:15.000Z","dependencies_parsed_at":"2023-05-10T17:15:32.639Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/design-tic-tac-toe","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdesign-tic-tac-toe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdesign-tic-tac-toe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdesign-tic-tac-toe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fdesign-tic-tac-toe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/design-tic-tac-toe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952211,"owners_count":20699446,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","low-level-design","problem-solving","tic-tac-toe"],"created_at":"2024-12-07T03:15:00.756Z","updated_at":"2026-01-07T07:17:32.632Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Design Tic Tac Toe\n## https://leetcode.com/problems/design-tic-tac-toe\n\nAssume the following rules are for the tic-tac-toe game on an n x n board between two players:\n\n1. A move is guaranteed to be valid and is placed on an empty block.\n2. Once a winning condition is reached, no more moves are allowed.\n3. A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.\n\n```\nImplement the TicTacToe class:\n\nTicTacToe(int n) Initializes the object the size of the board n.\n\nint move(int row, int col, int player) Indicates that the player with id player plays at the cell (row, col) of the board.\nThe move is guaranteed to be a valid move.\n```\n# Implementation 1 : Check the row, check column, check main diagonal, check opposite diagonal\nThe `player` can only win when he makes the move. Because player 1 will only make 1 and player 2 will only make 2. \nSo player 1 will never mark a 2 on the tic-tac-toe board and similalry player 2 will never mark a 1 on the board.\n\n```java\nclass TicTacToe {\n    int[][] board;\n    public TicTacToe(int n) {\n        board = new int[n][n];\n    }\n    \n    public int move(int row, int col, int player) {\n        board[row][col] = player;\n        int win = checkBoard(row,col,player);\n        return win;\n    }\n    \n    private int checkBoard(int row, int col, int player) {\n        //check the row\n        int rowCount = 0;\n        for(int j = 0; j \u003c board[0].length; j++) {\n            if(board[row][j] != player)\n                break;\n            else\n                rowCount++;\n        }\n        if(rowCount == board.length)\n            return player;\n        \n        //check the column\n        int colCount = 0;\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][col] != player)\n                break;\n            else\n                colCount++;\n        }\n        if(colCount == board.length)\n            return player;\n        \n        //check the main diagonal\n        int mainDiagonalCount = 0;\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][i] != player)\n                break;\n            else\n               mainDiagonalCount++; \n        }\n        if(mainDiagonalCount == board.length)\n            return player;\n        \n        //check opposite diagonal\n        int oppositeDiagonalCount = 0;\n        int n = board.length;\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][n-1-i] != player)\n                break;\n            else\n               oppositeDiagonalCount++; \n        }\n        if(oppositeDiagonalCount == board.length)\n            return player;\n        \n        return 0;\n    }\n}\n```\n\n# Implementation 2 : Refactoring of implementation 1\n```java\nclass TicTacToe {\n    int[][] board;\n    public TicTacToe(int n) {\n        board = new int[n][n];\n    }\n    \n    public int move(int row, int col, int player) {\n        board[row][col] = player;\n        \n        if(checkRow(row,player) || checkColumn(col,player) ||\n           checkMainDiagonal(player) || checkOppositeDiagonal(player))\n            return player;\n        \n        return 0;\n    }\n        \n    private boolean checkRow(int row, int player) {\n        for(int j = 0; j \u003c board[0].length; j++) {\n            if(board[row][j] != player)\n                return false;\n        }\n        return true;\n    }\n    \n    private boolean checkColumn(int col, int player) {\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][col] != player)\n                return false;\n        }\n        return true;\n    }\n    \n    private boolean checkMainDiagonal(int player) {\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][i] != player)\n                return false;\n        }\n        return true;\n    }\n    \n    private boolean checkOppositeDiagonal(int player) {\n        int n = board.length;\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][n-1-i] != player)\n                return false;\n        }\n        return true;\n    }\n}\n\n```\n\n# Implementation 3 : Check Main and Opposite diagonals based on player move(row,col)\n\nWe should only check main diagonal if the move was made on main diagonal, for any cell on main diagonal `row == col`\n\nSimilarly we should only check opposite diagonal if the move was made on opposite diagonal, for any cell on opposite diagonal `row+col == board.length - 1`\n\n![Tic Tac Toe](example.JPG?raw=true)\n\n```java\nclass TicTacToe {\n    int[][] board;\n    public TicTacToe(int n) {\n        board = new int[n][n];\n    }\n    \n    public int move(int row, int col, int player) {\n        board[row][col] = player;\n        \n        if(checkRow(row,player) || checkColumn(col,player) ||\n           // we should only check main diagonal if the move was made on main diagonal\n           (row == col \u0026\u0026 checkMainDiagonal(player)) || \n           // we should only check opposite diagonal if the move was made on opposite diagonal\n           ((row+col == board.length-1) \u0026\u0026 checkOppositeDiagonal(player)) )\n            return player;\n        \n        return 0;\n    }\n        \n    private boolean checkRow(int row, int player) {\n        for(int j = 0; j \u003c board[0].length; j++) {\n            if(board[row][j] != player)\n                return false;\n        }\n        return true;\n    }\n    \n    private boolean checkColumn(int col, int player) {\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][col] != player)\n                return false;\n        }\n        return true;\n    }\n    \n    private boolean checkMainDiagonal(int player) {\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][i] != player)\n                return false;\n        }\n        return true;\n    }\n    \n    private boolean checkOppositeDiagonal(int player) {\n        int n = board.length;\n        for(int i = 0; i \u003c board.length; i++) {\n            if(board[i][n-1-i] != player)\n                return false;\n        }\n        return true;\n    }\n}\n\n```\n\n# References :\n1. https://leetcode.com/problems/design-tic-tac-toe/solution\n2. https://leetcode.com/problems/design-tic-tac-toe/discuss/163279/Java-naive-o(4n)-solution-by-checking-row-col-and-two-diagonals-each-time\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fdesign-tic-tac-toe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fdesign-tic-tac-toe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fdesign-tic-tac-toe/lists"}