{"id":19155564,"url":"https://github.com/duot/ttt","last_synced_at":"2026-04-28T12:01:51.522Z","repository":{"id":72931364,"uuid":"238332196","full_name":"duot/ttt","owner":"duot","description":"This is a customizable terminal based Tic Tac Toe game","archived":false,"fork":false,"pushed_at":"2020-02-05T22:34:01.000Z","size":66,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-04T12:02:07.944Z","etag":null,"topics":["exercise","ruby","small-project","terminal-game"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/duot.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-02-04T23:59:55.000Z","updated_at":"2021-09-29T15:25:16.000Z","dependencies_parsed_at":"2023-07-22T23:47:38.159Z","dependency_job_id":null,"html_url":"https://github.com/duot/ttt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/duot/ttt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duot%2Fttt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duot%2Fttt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duot%2Fttt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duot%2Fttt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/duot","download_url":"https://codeload.github.com/duot/ttt/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duot%2Fttt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32379629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T11:25:28.583Z","status":"ssl_error","status_checked_at":"2026-04-28T11:25:05.435Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["exercise","ruby","small-project","terminal-game"],"created_at":"2024-11-09T08:31:04.565Z","updated_at":"2026-04-28T12:01:51.507Z","avatar_url":"https://github.com/duot.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TTT\n\nThis is a terminal based Tic-Tac-Toe game with different playable modes:\n\n\t* Human vs Computer\n\t* Human vs Human\n\t* Computer vs Computer\n\t* Custom\n\nThe custom modes allows:\n\n\t* Board sizes 3, 5, 7, 9, 11, 13, and 15\n\t* Players of up 15 (with any combination of Humans and Computers)\n\t* Human Players can choose any valid symbols\n\nThe computer players are implemented with minimax algorithm for game modes with 2 players. For games with more than 2 players, maximax is used.\n\n## Usage\n\nTo play, run `ruby ttt.rb`. Press \u003ckbd\u003eCtrl\u003c/kbd\u003e/\u003ckbd\u003eCommand\u003c/kbd\u003e + \u003ckbd\u003ec\u003c/kbd\u003e to quit.\n\n## Implementation Details\n\n### Game class\n\nThe `Game` class is the starting point. It generates presets game modes or prompt the user for custom values. These values are used to initialize `TTTGame` .\n\n### TTTGame class\nGame engine. \n\n​\tOther game logic are delegated to `Board`, ie, full?, line_formed?, etc\n\n- takes a new `Board`\n\n- takes Array of `Players` ordered by first to move\n\n- protects the board from being modified more than once per turn\n\n- protects board from `Players` using other symbols\n\n- TODO suggest early draw, when no win is possible\n\n\n  #### score\n\n  Hash of player index to score value\n\n#### TTTGame.new{players:}\n\nArray of `Player` objects ordered 0..last.\n\n#### Game rules and dynamics\n\nSee https://www.gamedev.net/forums/topic/646788-tic-tac-toe-on-5x5-board/\n\n- Game dynamics changes if playing 3-x-row with larger sized boards. I.e. first player likely wins.\n- Playing edge to edge results in more draws than wins. Eg for 5-x-row with 5x5 board, all the winning moves are fewer and easy to block.\n- Game dynamics and fairness to be explored.\n\n* Temporary solution: Set winning \"line\" length at `Board#initialize`\n\n### Board class\n\nGame state. It's an n x n matrix of `Squares` numbered 1..n*n. Supports (odd) n x n size from 3 to 15. Supports multiple players.\n\nUses `Square` class to represent a square. Uses `Line` class to abstract a 'line' . Uses `Grid` class to display the TTT board.\n\n- passed between players, and game engine\n  - `TTTGame` protects the board from illegal moves by any `Player` class\n- `Players` can inspect the board, changes are discarded by `TTTGame`. Although, `Player` can modify a it's own `Board.copy` e.g. using minimax\n\n| name            | purpose                                                   |\n| --------------- | --------------------------------------------------------- |\n| `Line`s         | collections of square numbers in a line                   |\n| `Square#symbol` | a marker or an empty space; used for displaying the board |\n| `Player#marker` | a Player marker. e.g. 'X' or 'O'                          |\n\n### Line class\n\nA group of squares, used to abstract a collection of squares.\n\n### Square class\n\nHolds the  Player's marker on the board.\n\n- a String\n- one symbol per player\n\n### Grid class\n\nDisplays the board. It scales with the size of the board.\n\nNOTE: Currently unable to detect the user's terminal size to set maximum board size.\n\n### Player class\n\nPlayer inspects the board using Board#unmarked_squares.\n\n- `TTTGame` modifies board, `Player` is passed a deep copy\n- 'owns' the symbol\n- Can choose any character symbol\n- ensures unique symbols for each\n- `Player#choose` returns the move\n\n### Human class\n\nChild of Player class. Displays and prompts users for name, choice of symbol, and moves.\n\n### Computer class\n\nInherits Player. Set's it's own name and symbol. Able to defend from imminent loss. Able to make a winning move. All other moves are random.\n\n### Analyzing1Computer class\n\nInherits Computer. Looks ahead only one move using [maximax strategy](https://cs.stanford.edu/people/eroberts/courses/soco/projects/1998-99/game-theory/Minimax.html). Using the following table to evaluate moves.\n\n#### Points\n\n| point                 | function              | notes                                                        |\n| --------------------- | --------------------- | ------------------------------------------------------------ |\n|                       | `Board#win_length`    | Maximum x-in-a-row. Length of the `side` of the `Board`      |\n|                       | `@defend_score`       | `win_length ** 3`. Arbitrary choice, but it must be *lower* than capture score |\n|                       | `@capture_score`      | `defend_score * 2`. Highest value to ensure win. We *prioritize immediate win* over defense. |\n| 1                     | line claimed          | all squares in that line were previously empty               |\n| 3 x (lines blocked)   | blocks                | max of 4(planes) * win_length, where \"planes\" are the line directions. Denys opponents ability to make x-in-a-row. |\n| 2 x (per lines built) | builds                | reinforcing lines that are claimed and not blocked           |\n| `defend_score`        | deny win              |                                                              |\n| `capture_score`       | win                   |                                                              |\n| 0                     | lines already blocked | no use to take that move                                     |\n\n### Max2 class\n\nImplements minimax algorithm on 2 player matches. For more players, it reverts back to Analyzing1Computer#choose method\n\n#### Minimax\n\nMinimax is tricky in many ways.\n\n​\t In more than 2 players, it's more difficult to calculate winning moves.\n\n​\tThe depth plays a huge part in determining the best move. Too deep and the game hangs, and too shallow and the move is weak. #increase_magnitude allows the minimax to prefer winning move at shallower depth.\n\n​\tTODO: add depth arg\n\n​\tHeuristic: Currently considers only winning, loosing, neutral moves with scores 1,  -1, and 0.\n\n### Utility\n\nImplemented Array#joinor\n\nImplemented String#parenthesize\n\n## License\n\n[MIT](https://opensource.org/licenses/MIT)\n\nCopyright 2020 [James Duot](https:github.com/duot)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduot%2Fttt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fduot%2Fttt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduot%2Fttt/lists"}