move() { case $key in "w") if [ $row != 1 ] then ((row--)) fi ;;"a") if [ $col != 1 ] then ((col--)) fi ;;"s") if [ $row != $1 ] then ((row++)) fi ;;"d") if [ $col != $1 ] then ((col++)) fi ;;*) if [ "${matrix[$(($row * $1 + $col - $1 - 1))]}" = " " ] && ! ([ $output = 8 ] && [ $row = 1 ]) then matrix[$(($row * $1 + $col - $1 - 1))]=$turn if [ $turn == "X" ] then turn="O" else if [ $output = 6 ] || [ $output = 7 ] && [ $turn == "O" ] then turn="I" else turn="X" fi fi fi esac } detect() { tie=1 for ((i=1; i<=$2; i++)) do for ((start=1; start<$(($2 - $1 + 2)); start++)) do xrow=0 orow=0 irow=0 xcol=0 ocol=0 icol=0 for ((n=1; n<=$1; n++)) do case ${matrix[$(($(($n + $start - 2)) * $2 + $i - 1))]} in "X") ((xrow++));; "O") ((orow++));; "I") ((irow++));; *) if ! ([ $output = 8 ] && [ $(($n + $start)) = 2 ]) then tie=0 fi esac case ${matrix[$(($i * $2 + $n - $2 + $start - 2))]} in "X") ((xcol++));; "O") ((ocol++));; "I") ((icol++));; *) if ! ([ $output = 8 ] && [ $i = 1 ]) then tie=0 fi esac done if ! [ $output -eq 2 ] then if [ $xrow -ge $1 ] || [ $xcol -ge $1 ] then echo "X wins!!" return fi if [ $orow -ge $1 ] || [ $ocol -ge $1 ] then echo "O wins!!" return fi if [ $irow -ge $1 ] || [ $icol -ge $1 ] then echo "I wins!!" return fi fi if [ $i -lt $(($2 - $1 + 2)) ] then xrow=0 orow=0 irow=0 xcol=0 ocol=0 icol=0 for ((n=1; n<=$1; n++)) do case ${matrix[$(($(($i + $n - 2)) * $2 + $start + $n - 2))]} in "X") ((xrow++));; "O") ((orow++));; "I") ((irow++));; esac case ${matrix[$(($(($i + $n - 1)) * $2 - $start - $n + 1))]} in "X") ((xcol++));; "O") ((ocol++));; "I") ((icol++));; esac done if [ $xrow -ge $1 ] || [ $xcol -ge $1 ] then echo "X wins!!" return fi if [ $orow -ge $1 ] || [ $ocol -ge $1 ] then echo "O wins!!" return fi if [ $irow -ge $1 ] || [ $icol -ge $1 ] then echo "I wins!!" return fi fi done done if [ $tie = 1 ] then echo "Tie Game" return fi } matrix() { if [ "$1" = "set" ] then case $2 in 1) for i in "${!matrix1[@]}" do matrix[i]=${matrix1[i]} done;; 2) for i in "${!matrix2[@]}" do matrix[i]=${matrix2[i]} done;; 3) for i in "${!matrix3[@]}" do matrix[i]=${matrix3[i]} done;; 4) for i in "${!matrix4[@]}" do matrix[i]=${matrix4[i]} done;; 5) for i in "${!matrix5[@]}" do matrix[i]=${matrix5[i]} done;; 6) for i in "${!matrix6[@]}" do matrix[i]=${matrix6[i]} done;; 7) for i in "${!matrix7[@]}" do matrix[i]=${matrix7[i]} done;; 8) for i in "${!matrix8[@]}" do matrix[i]=${matrix8[i]} done;; 9) for i in "${!matrix9[@]}" do matrix[i]=${matrix9[i]} done;; esac elif [ "$1" = "get" ] then case $2 in 1) for i in "${!matrix[@]}" do matrix1[i]=${matrix[i]} done;; 2) for i in "${!matrix[@]}" do matrix2[i]=${matrix[i]} done;; 3) for i in "${!matrix[@]}" do matrix3[i]=${matrix[i]} done;; 4) for i in "${!matrix[@]}" do matrix4[i]=${matrix[i]} done;; 5) for i in "${!matrix[@]}" do matrix5[i]=${matrix[i]} done;; 6) for i in "${!matrix[@]}" do matrix6[i]=${matrix[i]} done;; 7) for i in "${!matrix[@]}" do matrix7[i]=${matrix[i]} done;; 8) for i in "${!matrix[@]}" do matrix8[i]=${matrix[i]} done;; 9) for i in "${!matrix[@]}" do matrix9[i]=${matrix[i]} done;; esac fi } game1() { echo "Get three in a row to win" echo echo "Press enter to continue" read -s for i in {0..8} do matrix+=(" ") done while true do clear echo " Tic Tac Toe $turn's turn │ │ ${matrix[0]} │ ${matrix[1]} │ ${matrix[2]} │ │ ───────┼───────┼─────── │ │ ${matrix[3]} │ ${matrix[4]} │ ${matrix[5]} │ │ ───────┼───────┼─────── │ │ ${matrix[6]} │ ${matrix[7]} │ ${matrix[8]} │ │" win=$(detect 3 3) if [ "$win" != "" ] then tput cup 14 0 echo $win tput cup 14 8 stty -echo read start fi tput cup $((4 * $row)) $((8 * $col - 5)) read -sn 1 key move 3 done } game2() { echo "Get three in a diagonal row to win" echo echo "Press enter to continue" read -s for i in {0..15} do matrix+=(" ") done while true do clear echo " Tic Tac Toe 4x4 $turn's turn │ │ │ ${matrix[0]} │ ${matrix[1]} │ ${matrix[2]} │ ${matrix[3]} │ │ │ ───────┼───────┼───────┼─────── │ │ │ ${matrix[4]} │ ${matrix[5]} │ ${matrix[6]} │ ${matrix[7]} │ │ │ ───────┼───────┼───────┼─────── │ │ │ ${matrix[8]} │ ${matrix[9]} │ ${matrix[10]} │ ${matrix[11]} │ │ │ ───────┼───────┼───────┼─────── │ │ │ ${matrix[12]} │ ${matrix[13]} │ ${matrix[14]} │ ${matrix[15]} │ │ │" win=$(detect 3 4) if [ "$win" != "" ] then tput cup 18 0 echo $win tput cup 18 8 stty -echo read start fi tput cup $((4 * $row)) $((8 * $col - 5)) read -sn 1 key move 4 done } game3() { if [ $output = 3 ] then echo "Win by getting four in a row or four in a box" else echo "Win by getting four in a row" fi echo "Nobody can place in the center square" echo echo "Press enter to continue" read -s for i in {0..24} do matrix+=(" ") done matrix[12]="*" while true do clear if [ $output = 3 ] then echo " Toe Tac BOX!" else echo " Toe Tac Tic" fi echo " $turn's turn │ │ │ │ ${matrix[0]} │ ${matrix[1]} │ ${matrix[2]} │ ${matrix[3]} │ ${matrix[4]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[5]} │ ${matrix[6]} │ ${matrix[7]} │ ${matrix[8]} │ ${matrix[9]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ \ | / │ │ ${matrix[10]} │ ${matrix[11]} │ - * - │ ${matrix[13]} │ ${matrix[14]} │ │ / | \ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[15]} │ ${matrix[16]} │ ${matrix[17]} │ ${matrix[18]} │ ${matrix[19]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[20]} │ ${matrix[21]} │ ${matrix[22]} │ ${matrix[23]} │ ${matrix[24]} │ │ │ │" win=$(detect 4 5) if [ $output = 3 ] then for ((i=1; i<=4; i++)) do for ((n=1; n<=4; n++)) do if [ "${matrix[$((5 * $i + $n - 6))]}" = "X" ] && [ "${matrix[$((5 * $i + $n - 5))]}" = "X" ] && [ "${matrix[$((5 * $i + $n - 1))]}" = "X" ] && [ "${matrix[$((5 * $i + $n))]}" = "X" ] then tput cup $((4 * $i)) $((8 * $n - 5)) echo "\033[1;31mX\033[0m" tput cup $((4 * $i + 4)) $((8 * $n - 5)) echo "\033[1;31mX\033[0m" tput cup $((4 * $i + 4)) $((8 * $n + 3)) echo "\033[1;31mX\033[0m" tput cup $((4 * $i)) $((8 * $n + 3)) echo "\033[1;31mX\033[0m" win="X wins!!" fi if [ "${matrix[$((5 * $i + $n - 6))]}" = "O" ] && [ "${matrix[$((5 * $i + $n - 5))]}" = "O" ] && [ "${matrix[$((5 * $i + $n - 1))]}" = "O" ] && [ "${matrix[$((5 * $i + $n))]}" = "O" ] then tput cup $((4 * $i)) $((8 * $n - 5)) echo "\033[1;31mO\033[0m" tput cup $((4 * $i + 4)) $((8 * $n - 5)) echo "\033[1;31mO\033[0m" tput cup $((4 * $i + 4)) $((8 * $n + 3)) echo "\033[1;31mO\033[0m" tput cup $((4 * $i)) $((8 * $n + 3)) echo "\033[1;31mO\033[0m" win="O wins!!" fi done done fi if [ "$win" != "" ] then tput cup 22 0 echo $win tput cup 22 8 stty -echo read start fi tput cup $((4 * $row)) $((8 * $col - 5)) read -sn 1 key move 5 done } game4() { echo "Get 3 in a row, but send the other player to other boxes" echo echo "Press enter to continue" read -s for i in {0..8} do matrix+=(" ") matrix1+=(" ") matrix2+=(" ") matrix3+=(" ") matrix4+=(" ") matrix5+=(" ") matrix6+=(" ") matrix7+=(" ") matrix8+=(" ") matrix9+=(" ") done while true do clear echo " Super Tic Tac Toe $turn's turn │ │ │ │ │ │ ${matrix1[0]} │ ${matrix1[1]} │ ${matrix1[2]} ${matrix2[0]} │ ${matrix2[1]} │ ${matrix2[2]} ${matrix3[0]} │ ${matrix3[1]} │ ${matrix3[2]} │ │ │ │ │ │ ───────┼───────┼─────── ───────┼───────┼─────── ───────┼───────┼─────── │ │ │ │ │ │ ${matrix1[3]} │ ${matrix1[4]} │ ${matrix1[5]} ${matrix2[3]} │ ${matrix2[4]} │ ${matrix2[5]} ${matrix3[3]} │ ${matrix3[4]} │ ${matrix3[5]} │ │ │ │ │ │ ───────┼───────┼─────── ───────┼───────┼─────── ───────┼───────┼─────── │ │ │ │ │ │ ${matrix1[6]} │ ${matrix1[7]} │ ${matrix1[8]} ${matrix2[6]} │ ${matrix2[7]} │ ${matrix2[8]} ${matrix3[6]} │ ${matrix3[7]} │ ${matrix3[8]} │ │ │ │ │ │ │ │ │ │ │ │ ${matrix4[0]} │ ${matrix4[1]} │ ${matrix4[2]} ${matrix5[0]} │ ${matrix5[1]} │ ${matrix5[2]} ${matrix6[0]} │ ${matrix6[1]} │ ${matrix6[2]} │ │ │ │ │ │ ───────┼───────┼─────── ───────┼───────┼─────── ───────┼───────┼─────── │ │ │ │ │ │ ${matrix4[3]} │ ${matrix4[4]} │ ${matrix4[5]} ${matrix5[3]} │ ${matrix5[4]} │ ${matrix5[5]} ${matrix6[3]} │ ${matrix6[4]} │ ${matrix6[5]} │ │ │ │ │ │ ───────┼───────┼─────── ───────┼───────┼─────── ───────┼───────┼─────── │ │ │ │ │ │ ${matrix4[6]} │ ${matrix4[7]} │ ${matrix4[8]} ${matrix5[6]} │ ${matrix5[7]} │ ${matrix5[8]} ${matrix6[6]} │ ${matrix6[7]} │ ${matrix6[8]} │ │ │ │ │ │ │ │ │ │ │ │ ${matrix7[0]} │ ${matrix7[1]} │ ${matrix7[2]} ${matrix8[0]} │ ${matrix8[1]} │ ${matrix8[2]} ${matrix9[0]} │ ${matrix9[1]} │ ${matrix9[2]} │ │ │ │ │ │ ───────┼───────┼─────── ───────┼───────┼─────── ───────┼───────┼─────── │ │ │ │ │ │ ${matrix7[3]} │ ${matrix7[4]} │ ${matrix7[5]} ${matrix8[3]} │ ${matrix8[4]} │ ${matrix8[5]} ${matrix9[3]} │ ${matrix9[4]} │ ${matrix9[5]} │ │ │ │ │ │ ───────┼───────┼─────── ───────┼───────┼─────── ───────┼───────┼─────── │ │ │ │ │ │ ${matrix7[6]} │ ${matrix7[7]} │ ${matrix7[8]} ${matrix8[6]} │ ${matrix8[7]} │ ${matrix8[8]} ${matrix9[6]} │ ${matrix9[7]} │ ${matrix9[8]} │ │ │ │ │ │" win=$(detect 3 3) if [ "$win" != "" ] then tput cup 40 0 echo $win tput cup 40 8 stty -echo read start fi tput cup $((4 * $row + 13 * $boardrow)) $((8 * $col - 5 + 28 * $boardcol)) read -sn1 key matrix "set" $(($boardrow * 3 + $boardcol + 1)) move 3 matrix "get" $(($boardrow * 3 + $boardcol + 1)) if [ "$key" != "w" ] && [ "$key" != "a" ] && [ "$key" != "s" ] && [ "$key" != "d" ] then boardrow=$(($row - 1)) boardcol=$(($col - 1)) row=2 col=2 fi done } game5() { echo "Get three in a row to win" echo echo "Press enter to continue" read -s for i in {0..24} do matrix+=(" ") done while true do clear echo " 3 Player Tic Tac Toe $turn's turn │ │ │ │ ${matrix[0]} │ ${matrix[1]} │ ${matrix[2]} │ ${matrix[3]} │ ${matrix[4]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[5]} │ ${matrix[6]} │ ${matrix[7]} │ ${matrix[8]} │ ${matrix[9]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[10]} │ ${matrix[11]} │ ${matrix[12]} │ ${matrix[13]} │ ${matrix[14]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[15]} │ ${matrix[16]} │ ${matrix[17]} │ ${matrix[18]} │ ${matrix[19]} │ │ │ │ ───────┼───────┼───────┼───────┼─────── │ │ │ │ ${matrix[20]} │ ${matrix[21]} │ ${matrix[22]} │ ${matrix[23]} │ ${matrix[24]} │ │ │ │" win=$(detect 3 5) if [ "$win" != "" ] then tput cup 22 0 echo $win tput cup 22 8 stty -echo read start fi tput cup $((4 * $row)) $((8 * $col - 5)) read -sn 1 key move 5 done } game6() { echo "Get four in a row or four in a box to win" echo echo "Press enter to continue" read -s for i in {0..35} do matrix+=(" ") done while true do clear echo " 3 Player Tic Tac BOX! $turn's turn │ │ │ │ │ ${matrix[0]} │ ${matrix[1]} │ ${matrix[2]} │ ${matrix[3]} │ ${matrix[4]} │ ${matrix[5]} │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ ${matrix[6]} │ ${matrix[7]} │ ${matrix[8]} │ ${matrix[9]} │ ${matrix[10]} │ ${matrix[11]} │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ ${matrix[12]} │ ${matrix[13]} │ ${matrix[14]} │ ${matrix[15]} │ ${matrix[16]} │ ${matrix[17]} │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ ${matrix[18]} │ ${matrix[19]} │ ${matrix[20]} │ ${matrix[21]} │ ${matrix[22]} │ ${matrix[23]} │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ ${matrix[24]} │ ${matrix[25]} │ ${matrix[26]} │ ${matrix[27]} │ ${matrix[28]} │ ${matrix[29]} │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ ${matrix[30]} │ ${matrix[31]} │ ${matrix[32]} │ ${matrix[33]} │ ${matrix[34]} │ ${matrix[35]} │ │ │ │ │" win=$(detect 4 6) for ((i=1; i<=5; i++)) do for ((n=1; n<=5; n++)) do if [ "${matrix[$((6 * $i + $n - 7))]}" = "X" ] && [ "${matrix[$((6 * $i + $n - 6))]}" = "X" ] && [ "${matrix[$((6 * $i + $n - 1))]}" = "X" ] && [ "${matrix[$((6 * $i + $n))]}" = "X" ] then tput cup $((4 * $i)) $((8 * $n - 5)) echo "\033[1;31mX\033[0m" tput cup $((4 * $i + 4)) $((8 * $n - 5)) echo "\033[1;31mX\033[0m" tput cup $((4 * $i + 4)) $((8 * $n + 3)) echo "\033[1;31mX\033[0m" tput cup $((4 * $i)) $((8 * $n + 3)) echo "\033[1;31mX\033[0m" win="X wins!!" fi if [ "${matrix[$((6 * $i + $n - 7))]}" = "O" ] && [ "${matrix[$((6 * $i + $n - 6))]}" = "O" ] && [ "${matrix[$((6 * $i + $n - 1))]}" = "O" ] && [ "${matrix[$((6 * $i + $n))]}" = "O" ] then tput cup $((4 * $i)) $((8 * $n - 5)) echo "\033[1;31mO\033[0m" tput cup $((4 * $i + 4)) $((8 * $n - 5)) echo "\033[1;31mO\033[0m" tput cup $((4 * $i + 4)) $((8 * $n + 3)) echo "\033[1;31mO\033[0m" tput cup $((4 * $i)) $((8 * $n + 3)) echo "\033[1;31mO\033[0m" win="O wins!!" fi if [ "${matrix[$((6 * $i + $n - 7))]}" = "I" ] && [ "${matrix[$((6 * $i + $n - 6))]}" = "I" ] && [ "${matrix[$((6 * $i + $n - 1))]}" = "I" ] && [ "${matrix[$((6 * $i + $n))]}" = "I" ] then tput cup $((4 * $i)) $((8 * $n - 5)) echo "\033[1;31mI\033[0m" tput cup $((4 * $i + 4)) $((8 * $n - 5)) echo "\033[1;31mI\033[0m" tput cup $((4 * $i + 4)) $((8 * $n + 3)) echo "\033[1;31mI\033[0m" tput cup $((4 * $i)) $((8 * $n + 3)) echo "\033[1;31mI\033[0m" win="I wins!!" fi done done if [ "$win" != "" ] then tput cup 26 0 echo $win tput cup 26 8 stty -echo read start fi tput cup $((4 * $row)) $((8 * $col - 5)) read -sn 1 key move 6 done } game7() { echo "Get four in a row to win" echo echo "Press enter to continue" read -s for i in {0..48} do matrix+=(" ") done row=7 while true do clear echo " Gravity $turn's turn │ │ │ │ │ │ ${matrix[7]} │ ${matrix[8]} │ ${matrix[9]} │ ${matrix[10]} │ ${matrix[11]} │ ${matrix[12]} │ ${matrix[13]} │ │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ │ ${matrix[14]} │ ${matrix[15]} │ ${matrix[16]} │ ${matrix[17]} │ ${matrix[18]} │ ${matrix[19]} │ ${matrix[20]} │ │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ │ ${matrix[21]} │ ${matrix[22]} │ ${matrix[23]} │ ${matrix[24]} │ ${matrix[25]} │ ${matrix[26]} │ ${matrix[27]} │ │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ │ ${matrix[28]} │ ${matrix[29]} │ ${matrix[30]} │ ${matrix[31]} │ ${matrix[32]} │ ${matrix[33]} │ ${matrix[34]} │ │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ │ ${matrix[35]} │ ${matrix[36]} │ ${matrix[37]} │ ${matrix[38]} │ ${matrix[39]} │ ${matrix[40]} │ ${matrix[41]} │ │ │ │ │ │ ───────┼───────┼───────┼───────┼───────┼───────┼─────── │ │ │ │ │ │ ${matrix[42]} │ ${matrix[43]} │ ${matrix[44]} │ ${matrix[45]} │ ${matrix[46]} │ ${matrix[47]} │ ${matrix[48]} │ │ │ │ │ │" win=$(detect 4 7) if [ "$win" != "" ] then tput cup 26 0 echo $win tput cup 26 8 stty -echo read start fi if [ $row -lt 2 ] then tput cup 2 $((8 * $col - 5)) else tput cup $((4 * $row - 4)) $((8 * $col - 5)) fi read -sn 1 key move 7 row=7 for ((i=0; i<=5; i++)) do if [ "${matrix[$(($row * 7 + $col - 8))]}" != " " ] then ((row--)) fi done done } start() { stty echo menu "Choose a game:" "Tic-Tac-Toe" "Tic-Tac-Toe_4x4" "Toe-Tac-Tic" "Toe-Tac-BOX!" "Super_Tic-Tac-Toe" "3-Player_Tic-Tac-Toe" "3-Player_Tic-Tac-BOX!" "Gravity" "Exit_Game" clear row=1 col=1 box=1 matrix=() matrix1=() matrix2=() matrix3=() matrix4=() matrix5=() matrix6=() matrix7=() matrix8=() matrix9=() turn="X" boardrow=1 boardcol=1 case $output in 1) game1;; 2) game2;; 3) game3;; 4) game3;; 5) game4;; 6) game5;; 7) game6;; 8) game7;; 9) exit esac } cd ~ if [ ! -f "dtar_functions" ] then cd "$(dirname "$0")" if [ ! -f "dtar_functions" ] then curl "https://raw.githubusercontent.com/dtar-github/bash/refs/heads/main/dtar_functions" > dtar_functions chmod +x dtar_functions fi fi source "dtar_functions" cd "$(dirname "$0")" tput smcup while [ $(tput cols) -lt 100 ] && [ $(tput lines) -lt 40 ] do clear echo "Please fullscreen the terminal" echo "Status: Window" sleep 0.1 done clear echo "Status: Fullscreen" echo "Press enter to continue" read start