{"id":21676761,"url":"https://github.com/anandman03/smart-tic-tac-toe","last_synced_at":"2025-07-30T00:36:05.137Z","repository":{"id":162457194,"uuid":"332847023","full_name":"anandman03/Smart-Tic-Tac-Toe","owner":"anandman03","description":"Tic-Tac-Toe using minimax + alpha-beta pruning in C++ ","archived":false,"fork":false,"pushed_at":"2021-01-30T16:29:51.000Z","size":38,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-19T16:48:52.069Z","etag":null,"topics":["alpha-beta-pruning","cpp","minimax-algorithm","oops-in-cpp","tic-tac-toe"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anandman03.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2021-01-25T18:40:30.000Z","updated_at":"2021-01-30T16:32:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"821be642-a0d2-40f3-8fc1-09e03fa30bd3","html_url":"https://github.com/anandman03/Smart-Tic-Tac-Toe","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anandman03/Smart-Tic-Tac-Toe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anandman03%2FSmart-Tic-Tac-Toe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anandman03%2FSmart-Tic-Tac-Toe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anandman03%2FSmart-Tic-Tac-Toe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anandman03%2FSmart-Tic-Tac-Toe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anandman03","download_url":"https://codeload.github.com/anandman03/Smart-Tic-Tac-Toe/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anandman03%2FSmart-Tic-Tac-Toe/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267785895,"owners_count":24144124,"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","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["alpha-beta-pruning","cpp","minimax-algorithm","oops-in-cpp","tic-tac-toe"],"created_at":"2024-11-25T14:16:14.269Z","updated_at":"2025-07-30T00:36:05.101Z","avatar_url":"https://github.com/anandman03.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Smart Tic-Tac-Toe\n\nThe \"Tic Tac Toe\" is a very classic puzzle game with simple operation and great entertainment. Two players, one in a circle (O), one in a fork (X), take turns to play their own symbols on a 3 by 3 grid, the first to be horizontal, straight, and oblique to form a line to win.\n\nIf both parties are correct, they will get a tie. This game is actually controlled by the first player, the first player is the attack and the second player is the defender. This game has only 765 possible games and 26,830 games.\n\n## Visualisation\n\n![minimax](https://raw.githubusercontent.com/anandman03/Smart-Tic-Tac-Toe/main/Public/minimax.jpg)\n\n## Installation\n\n```\n$ git clone https://github.com/anandman03/Smart-Tic-Tac-Toe.git\n$ cd Smart-Tic-Tac-Toe\n$ g++ Source.cpp\n$ ./a.out\n```\n\n## Code Snippets\n\n#### Cell Class\n\nThe `Cell class` contains x, y attributes representing ith row and jth column in grid.\n\n```c++\nclass Cell {\nprivate:\n\tint x, y;\n\npublic:\n\tCell() {\n\t\tthis-\u003ex = this-\u003ey = 0;\n\t}\n\n\tvoid setCoordinates(const vector\u003cint\u003e \u0026p) {\n\t\tthis-\u003ex = p[0];\n\t\tthis-\u003ey = p[1];\n\t}\n\n\tvector\u003cint\u003e getCoordinates() {\n\t\treturn {x, y};\n\t}\n};\n```\n\n#### Game Class\n\nThe `constructor` takes the board, symbol of current player and symbol of opponent.\n\n```c++\nGame(vector\u003cvector\u003cchar\u003e\u003e board, char player, char opponent) {\n\tthis-\u003en = 3;\n\tthis-\u003eboard.resize(n, vector\u003cchar\u003e(n, 0));\n\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\tfor(int j = 0 ; j \u003c n ; ++j) {\n\t\t\tthis-\u003eboard[i][j] = board[i][j];\n\t\t}\n\t}\n\tthis-\u003eplayer = player;\n\tthis-\u003eopponent = opponent;\n}\n```\n\nThe `evaluate` function calculates if the current player wins/loses/draws based on checking of symbols in row, column and diagonal of board.\n\n```c++\nint evaluate() {\n\t// check status for rows.\n\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\tif(board[i][0] == board[i][1] \u0026\u0026 board[i][1] == board[i][2]) {\n\t\t\tif(board[i][0] == player) {\n\t\t\t\treturn 10;\n\t\t\t}\n\t\t\tif(board[i][0] == player) {\n\t\t\t\treturn -10;\n\t\t\t}\n\t\t}\n\t}\n\t// check status for columns.\n\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\tif(board[0][i] == board[1][i] \u0026\u0026 board[1][i] == board[2][i]) {\n\t\t\tif(board[0][i] == player) {\n\t\t\t\treturn 10;\n\t\t\t}\n\t\t\tif(board[0][i] == player) {\n\t\t\t\treturn -10;\n\t\t\t}\n\t\t}\n\t}\n\t// check for top-left to bottom-right diagonal.\n\tif(board[0][0] == board[1][1] \u0026\u0026 board[1][1] == board[2][2]) {\n\t\tif(board[1][1] == player) {\n\t\t\treturn +10;\n\t\t}\n\t\telse {\n\t\t\treturn -10;\n\t\t}\n\t}\n\t// check for top-right to bottom-left diagonal.\n\tif(board[0][2] == board[1][1] \u0026\u0026 board[1][1] == board[2][0]) {\n\t\tif(board[1][1] == player) {\n\t\t\treturn +10;\n\t\t}\n\t\telse {\n\t\t\treturn -10;\n\t\t}\n\t}\n\t// in case of DRAW.\n\treturn 0;\n}\n```\n\nThe `isMovesleft` check if the any moves can be taken or not.\n\n```c++\nbool isMovesLeft() {\n\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\tfor(int j = 0 ; j \u003c n ; ++j) {\n\t\t\tif(board[i][j] == '.') return true;\n\t\t}\n\t}\n\treturn false;\n}\n```\n\nThe `miniMax` function considers all the possible ways the game can go and returns the value of the board.\n\n```c++\nint miniMax(bool isMaximizer) {\n\t// evaluate score based on current board.\n\tint score = evaluate();\n\n\t// if maximizer has won/lost return evaluated score.\n\tif(score == 10 || score == -10) {\n\t\treturn score;\n\t}\n\t// if no moves are left then there is draw return 0 score.\n\tif(!isMovesLeft()) {\n\t\treturn 0;\n\t}\n\n\tint bestScore;\n\t// if this is maximizer's move.\n\tif(isMaximizer) {\n\t\tbestScore = -INF;\n\t\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\t\tfor(int j = 0 ; j \u003c n ; ++j) {\n\t\t\t\tif(board[i][j] == '.') {\n\t\t\t\t\tboard[i][j] = player;\n\t\t\t\t\tbestScore = max(bestScore, miniMax(!isMaximizer));\n\t\t\t\t\tboard[i][j] = '.';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t// if this is not maximizer's move.\n\telse {\n\t\tbestScore = INF;\n\t\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\t\tfor(int j = 0 ; j \u003c n ; ++j) {\n\t\t\t\tif(board[i][j] == '.') {\n\t\t\t\t\tboard[i][j] = opponent;\n\t\t\t\t\tbestScore = min(bestScore, miniMax(!isMaximizer));\n\t\t\t\t\tboard[i][j] = '.';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn bestScore;\n}\n```\n\nThe `getBestMove` returns the best possible move for the current player.\n\n```c++\nvoid getBestMove() {\n\tint bestValue = -INF;\n\tCell bestMove;\n\n\t// Traverse all cells, evaluate minimax function for\n\t// all empty cells. And return the cell with optimal\n\t// value.\n\tfor(int i = 0 ; i \u003c n ; ++i) {\n\t\tfor(int j = 0 ; j \u003c n ; ++j) {\n\t\t\t// if cell is empty.\n\t\t\tif(board[i][j] == '.') {\n\t\t\t\tboard[i][j] = player;\n\t\t\t\tint currValue = miniMax(false);\n\t\t\t\tboard[i][j] = '.';\n\t\t\t\t// update best move.\n\t\t\t\tif(bestValue \u003c currValue) {\n\t\t\t\t\tbestValue = currValue;\n\t\t\t\t\tbestMove.setCoordinates({i, j});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tvector\u003cint\u003e currCoordinates = bestMove.getCoordinates();\n\tboard[currCoordinates[0]][currCoordinates[1]] = player;\n}\n```\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanandman03%2Fsmart-tic-tac-toe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanandman03%2Fsmart-tic-tac-toe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanandman03%2Fsmart-tic-tac-toe/lists"}