+</span></code></pre><p>As an alternative to manually managing such virtual environments, you can use <a href=https://github.com/astral-sh/uv>uv</a> or <a href=https://github.com/pypa/pipx>pipx</a> instead.<p>As yet another alternative, you can install <code>textual==0.85.2</code> (see <a href=https://textual.textualize.io/getting_started/>Textual documentation</a> for more details), clone <a href=https://github.com/learnbyexample/TUI-apps>this repository</a> and run the <code>connect_square.py</code> file.</p><br><h2 id=screenshots>Screenshots<a aria-label="Anchor link for: screenshots" class=zola-anchor href=#screenshots>🔗</a></h2><p>Adjust your terminal's dimension for the game widgets to appear properly, for example 80x30 (characters x lines). Sample screenshots are shown below:<p align=center><img alt="Sample screenshot for Connect Four game" src=https://raw.githubusercontent.com/learnbyexample/TUI-apps/main/ConnectSquare/connect_four.png><p align=center><img alt="Sample screenshot for Connect Four or Square game" src=https://raw.githubusercontent.com/learnbyexample/TUI-apps/main/ConnectSquare/connect_both.png></p><br><h2 id=guide>Guide<a aria-label="Anchor link for: guide" class=zola-anchor href=#guide>🔗</a></h2><ul><li>Press the <strong>n</strong> key to start a new game. Existing game, if any, will be abandoned<li>You can choose between <strong>Connect Four</strong>, <strong>Connect Square</strong> (default) and <strong>Both</strong> types of game<li>You can choose between <strong>Easy</strong> (default), <strong>Medium</strong> and <strong>Hard</strong> difficulty modes: <ul><li>In the <em>Easy</em> mode, the AI will make a random move<li>In the <em>Medium</em> mode, the AI will make a random move based on certain weight calculations<li>In the <em>Hard</em> mode, the AI will make the best move based on the weight calculations (the algorithm is based only on the current board state and thus it is not impossible for the user to win)</ul><li>The first move is based on the <strong>User first</strong> (default) and <strong>AI first</strong> choices<li>Only the bottom most empty cell of each column will be considered as a valid move<li>Press the <strong>t</strong> key to toggle between light and dark themes<li>Press the <strong>q</strong> key to quit the app</ul><p>User moves are denoted by the ⭕️ character and AI moves are denoted by the ✖️ character.<p>The text panel under the game board displays the current status of the game. If the game ends with one of the players forming a valid line or square, the cells forming the winning move will be highlighted.<h2 id=square-tic-tac-toe>Square tic tac toe<a aria-label="Anchor link for: square-tic-tac-toe" class=zola-anchor href=#square-tic-tac-toe>🔗</a></h2><p>If you liked this game, you might also enjoy <a href=https://github.com/learnbyexample/TUI-apps/tree/main/SquareTicTacToe>Square tic tac toe</a>.<p>If you are interested in learning more about the AI algorithm for the Connect Square game, check out my <a href=https://learnbyexample.github.io/practice_python_projects/square_tic_tac_toe/square_tic_tac_toe_ai.html>explanation here for Square tic tac toe</a> — while there are a few differences between the two, the foundation is the same.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/python/>#python</a><a href=https://learnbyexample.github.io/tags/tui/>#TUI</a><a href=https://learnbyexample.github.io/tags/textual/>#textual</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=previous href=https://learnbyexample.github.io/programming-deals-2025/>← Festive offers for books on Python, Linux, Regular Expressions, Vim and more!</a><br><p><a class=next href=https://learnbyexample.github.io/python-regex-cheatsheet/>Python regular expression cheatsheet and examples →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
0 commit comments