Diferencias
Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| chessba.sh [2024/04/29 21:10] – creado peron | chessba.sh [2026/04/21 16:05] (actual) – editor externo 127.0.0.1 | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| - | <code bash> | + | <file bash chessba.sh> |
| - | #/bin/bash | + | #!/usr/local/bin/bash |
| # | # | ||
| # Chess Bash | # Chess Bash | ||
| Línea 10: | Línea 10: | ||
| # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| # You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | # You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| + | |||
| # Default values | # Default values | ||
| strength=3 | strength=3 | ||
| Línea 32: | Línea 32: | ||
| unicodelabels=true | unicodelabels=true | ||
| port=12433 | port=12433 | ||
| + | |||
| # internal values | # internal values | ||
| timestamp=$( date +%s%N ) | timestamp=$( date +%s%N ) | ||
| Línea 57: | Línea 57: | ||
| labelY=9 | labelY=9 | ||
| type stty >/ | type stty >/ | ||
| + | |||
| # Choose unused color for hover | # Choose unused color for hover | ||
| while (( colorHover == colorPlayerA || colorHover == colorPlayerB )) ; do | while (( colorHover == colorPlayerA || colorHover == colorPlayerB )) ; do | ||
| (( colorHover++ )) | (( colorHover++ )) | ||
| done | done | ||
| + | |||
| # Check Unicode availbility | # Check Unicode availbility | ||
| # We do this using a trick: printing a special zero-length unicode char (http:// | # We do this using a trick: printing a special zero-length unicode char (http:// | ||
| Línea 75: | Línea 75: | ||
| fi | fi | ||
| echo -e " | echo -e " | ||
| + | |||
| # Print version information | # Print version information | ||
| function version() { | function version() { | ||
| echo " | echo " | ||
| } | } | ||
| + | |||
| # Wait for key press | # Wait for key press | ||
| # no params/ | # no params/ | ||
| Línea 89: | Línea 89: | ||
| $useStty && stty -echo | $useStty && stty -echo | ||
| } | } | ||
| + | |||
| # Error message, p.a. on bugs | # Error message, p.a. on bugs | ||
| # Params: | # Params: | ||
| Línea 103: | Línea 103: | ||
| exit 1 | exit 1 | ||
| } | } | ||
| + | |||
| # Check prerequisits (additional executables) | # Check prerequisits (additional executables) | ||
| # taken from an old script of mine (undertaker-tailor) | # taken from an old script of mine (undertaker-tailor) | ||
| Línea 115: | Línea 115: | ||
| } | } | ||
| } | } | ||
| + | |||
| # Validate a number string | # Validate a number string | ||
| # Params: | # Params: | ||
| Línea 127: | Línea 127: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Validate a port string | # Validate a port string | ||
| # Must be non privileged (>1023) | # Must be non privileged (>1023) | ||
| Línea 140: | Línea 140: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Validate an IP v4 or v6 address | # Validate an IP v4 or v6 address | ||
| # source: http:// | # source: http:// | ||
| Línea 153: | Línea 153: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Named ANSI colors | # Named ANSI colors | ||
| declare -a colors=( " | declare -a colors=( " | ||
| + | |||
| # Retrieve ANSI color code from string | # Retrieve ANSI color code from string | ||
| # Black and white are ignored! | # Black and white are ignored! | ||
| Línea 173: | Línea 173: | ||
| return 0 | return 0 | ||
| } | } | ||
| + | |||
| # Check if ai player | # Check if ai player | ||
| # Params: | # Params: | ||
| Línea 193: | Línea 193: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Help message | # Help message | ||
| # Writes text to stdout | # Writes text to stdout | ||
| Línea 244: | Línea 244: | ||
| echo | echo | ||
| } | } | ||
| + | |||
| # Parse command line arguments | # Parse command line arguments | ||
| while getopts ": | while getopts ": | ||
| Línea 350: | Línea 350: | ||
| esac | esac | ||
| done | done | ||
| + | |||
| # get terminal dimension | # get terminal dimension | ||
| echo -en ' | echo -en ' | ||
| Línea 361: | Línea 361: | ||
| termWidth=80 | termWidth=80 | ||
| fi | fi | ||
| + | |||
| # gui config | # gui config | ||
| if $guiconfig ; then | if $guiconfig ; then | ||
| + | |||
| # find a dialog system | # find a dialog system | ||
| if type gdialog >/ | if type gdialog >/ | ||
| Línea 382: | Línea 382: | ||
| error "The graphical configuration requires gdialog/ | error "The graphical configuration requires gdialog/ | ||
| fi | fi | ||
| + | |||
| # Output the type of the first player in a readable string | # Output the type of the first player in a readable string | ||
| function typeOfPlayerA() { | function typeOfPlayerA() { | ||
| Línea 396: | Línea 396: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Output the type of the second player in a readable string | # Output the type of the second player in a readable string | ||
| function typeOfPlayerB() { | function typeOfPlayerB() { | ||
| Línea 410: | Línea 410: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Execute a dialog | # Execute a dialog | ||
| # Params: Dialog params (variable length) | # Params: Dialog params (variable length) | ||
| Línea 423: | Línea 423: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Print a message box with a warning/ | # Print a message box with a warning/ | ||
| # Params: | # Params: | ||
| Línea 431: | Línea 431: | ||
| dlg --msgbox " | dlg --msgbox " | ||
| } | } | ||
| + | |||
| # Start the dialog configuration | # Start the dialog configuration | ||
| # Neither params nor return, this is just a function for hiding local variables! | # Neither params nor return, this is just a function for hiding local variables! | ||
| Línea 440: | Línea 440: | ||
| local dlg_on=" | local dlg_on=" | ||
| local dlg_off=" | local dlg_off=" | ||
| + | |||
| declare -a option_player=( " | declare -a option_player=( " | ||
| declare -a option_settings=( "Color support" | declare -a option_settings=( "Color support" | ||
| + | |||
| local dlg_main | local dlg_main | ||
| while dlg_main=$(dlg --ok-button " | while dlg_main=$(dlg --ok-button " | ||
| case " | case " | ||
| + | |||
| # Player A settings | # Player A settings | ||
| " | " | ||
| Línea 484: | Línea 484: | ||
| if dlg_networkport=$(dlg --inputbox " | if dlg_networkport=$(dlg --inputbox " | ||
| if validPort " | if validPort " | ||
| - | port=$dlg_networkport | + | port=$dlg_networkport |
| else | else | ||
| - | dlgerror "Your input ' | + | dlgerror "Your input ' |
| fi | fi | ||
| fi | fi | ||
| Línea 509: | Línea 509: | ||
| fi | fi | ||
| ;; | ;; | ||
| + | |||
| # Player B settings | # Player B settings | ||
| " | " | ||
| Línea 562: | Línea 562: | ||
| fi | fi | ||
| ;; | ;; | ||
| + | |||
| # Game settings | # Game settings | ||
| " | " | ||
| Línea 607: | Línea 607: | ||
| fi | fi | ||
| ;; | ;; | ||
| + | |||
| # Other --> exit (gdialog) | # Other --> exit (gdialog) | ||
| * ) | * ) | ||
| Línea 615: | Línea 615: | ||
| done | done | ||
| } | } | ||
| + | |||
| # start config dialog | # start config dialog | ||
| dlgconfig | dlgconfig | ||
| fi | fi | ||
| + | |||
| # Save screen | # Save screen | ||
| if $cursor ; then | if $cursor ; then | ||
| echo -e " | echo -e " | ||
| fi | fi | ||
| + | |||
| # lookup tables | # lookup tables | ||
| declare -A cacheLookup | declare -A cacheLookup | ||
| declare -A cacheFlag | declare -A cacheFlag | ||
| declare -A cacheDepth | declare -A cacheDepth | ||
| + | |||
| # associative arrays are faster than numeric ones and way more readable | # associative arrays are faster than numeric ones and way more readable | ||
| declare -A redraw | declare -A redraw | ||
| Línea 639: | Línea 639: | ||
| done | done | ||
| fi | fi | ||
| + | |||
| declare -A field | declare -A field | ||
| + | |||
| # initialize setting - first row | # initialize setting - first row | ||
| declare -a initline=( 4 2 3 6 5 3 2 4 ) | declare -a initline=( 4 2 3 6 5 3 2 4 ) | ||
| Línea 659: | Línea 659: | ||
| done | done | ||
| done | done | ||
| + | |||
| # readable figure names | # readable figure names | ||
| declare -a figNames=( " | declare -a figNames=( " | ||
| Línea 666: | Línea 666: | ||
| # figure weight (for heuristic) | # figure weight (for heuristic) | ||
| declare -a figValues=( 0 1 5 5 6 17 42 ) | declare -a figValues=( 0 1 5 5 6 17 42 ) | ||
| + | |||
| # Warning message on invalid moves (Helper) | # Warning message on invalid moves (Helper) | ||
| # Params: | # Params: | ||
| Línea 675: | Línea 675: | ||
| draw | draw | ||
| } | } | ||
| + | |||
| # Readable coordinates | # Readable coordinates | ||
| # Params: | # Params: | ||
| Línea 684: | Línea 684: | ||
| echo -en " | echo -en " | ||
| } | } | ||
| + | |||
| # Get name of player | # Get name of player | ||
| # Params: | # Params: | ||
| Línea 713: | Línea 713: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Get name of figure | # Get name of figure | ||
| # Params: | # Params: | ||
| Línea 725: | Línea 725: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Check win/loose position | # Check win/loose position | ||
| # (player has king?) | # (player has king?) | ||
| Línea 744: | Línea 744: | ||
| return 1 | return 1 | ||
| } | } | ||
| + | |||
| # Check validity of a concrete single movement | # Check validity of a concrete single movement | ||
| # Params: | # Params: | ||
| Línea 759: | Línea 759: | ||
| local toX=$4 | local toX=$4 | ||
| local player=$5 | local player=$5 | ||
| + | |||
| local i | local i | ||
| if (( fromY < 0 || fromY >= 8 || fromX < 0 || fromX >= 8 || toY < 0 || toY >= 8 || toX < 0 || toX >= 8 || ( fromY == toY && fromX == toX ) )) ; then | if (( fromY < 0 || fromY >= 8 || fromX < 0 || fromX >= 8 || toY < 0 || toY >= 8 || toX < 0 || toX >= 8 || ( fromY == toY && fromX == toX ) )) ; then | ||
| Línea 822: | Línea 822: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # minimax (game theory) algorithm for evaluate possible movements | # minimax (game theory) algorithm for evaluate possible movements | ||
| # (the heart of your computer enemy) | # (the heart of your computer enemy) | ||
| Línea 1041: | Línea 1041: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Perform a concrete single movement | # Perform a concrete single movement | ||
| # Params: | # Params: | ||
| Línea 1065: | Línea 1065: | ||
| return 1 | return 1 | ||
| } | } | ||
| + | |||
| # Unicode helper function (for draw) | # Unicode helper function (for draw) | ||
| # Params: | # Params: | ||
| Línea 1078: | Línea 1078: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Ascii helper function (for draw) | # Ascii helper function (for draw) | ||
| # Params: | # Params: | ||
| Línea 1086: | Línea 1086: | ||
| echo -en " | echo -en " | ||
| } | } | ||
| + | |||
| # Get ascii code number of character | # Get ascii code number of character | ||
| # Params: | # Params: | ||
| Línea 1094: | Línea 1094: | ||
| LC_CTYPE=C printf ' | LC_CTYPE=C printf ' | ||
| } | } | ||
| + | |||
| # Audio and visual bell | # Audio and visual bell | ||
| # No params or return | # No params or return | ||
| Línea 1105: | Línea 1105: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Draw one field (of the gameboard) | # Draw one field (of the gameboard) | ||
| # Params: | # Params: | ||
| Línea 1271: | Línea 1271: | ||
| echo -en " | echo -en " | ||
| } | } | ||
| + | |||
| # Draw the battlefield | # Draw the battlefield | ||
| # (no params / return value) | # (no params / return value) | ||
| Línea 1300: | Línea 1300: | ||
| echo -en " | echo -en " | ||
| } | } | ||
| + | |||
| # Read the next move coordinates | # Read the next move coordinates | ||
| # from keyboard (direct access or cursor keypad) | # from keyboard (direct access or cursor keypad) | ||
| Línea 1462: | Línea 1462: | ||
| return $ret | return $ret | ||
| } | } | ||
| + | |||
| # Player input | # Player input | ||
| # (reads a valid user movement) | # (reads a valid user movement) | ||
| Línea 1510: | Línea 1510: | ||
| done | done | ||
| } | } | ||
| + | |||
| # AI interaction | # AI interaction | ||
| # (calculating movement) | # (calculating movement) | ||
| Línea 1541: | Línea 1541: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Read row from remote | # Read row from remote | ||
| # Returns row (0-7) as status code | # Returns row (0-7) as status code | ||
| Línea 1564: | Línea 1564: | ||
| done | done | ||
| } | } | ||
| + | |||
| # Read column from remote | # Read column from remote | ||
| # Returns column (0-7) as status code | # Returns column (0-7) as status code | ||
| Línea 1580: | Línea 1580: | ||
| done | done | ||
| } | } | ||
| + | |||
| # receive movement from connected player | # receive movement from connected player | ||
| # (no params/ | # (no params/ | ||
| Línea 1622: | Línea 1622: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Write coordinates to network | # Write coordinates to network | ||
| # Params: | # Params: | ||
| Línea 1640: | Línea 1640: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Import transposition tables | # Import transposition tables | ||
| # by reading serialised cache from stdin | # by reading serialised cache from stdin | ||
| Línea 1651: | Línea 1651: | ||
| done | done | ||
| } | } | ||
| + | |||
| # Export transposition tables | # Export transposition tables | ||
| # Outputs serialised cache (to stdout) | # Outputs serialised cache (to stdout) | ||
| Línea 1660: | Línea 1660: | ||
| done | done | ||
| } | } | ||
| + | |||
| # Trap function for exporting cache | # Trap function for exporting cache | ||
| # (no params / return value) | # (no params / return value) | ||
| Línea 1675: | Línea 1675: | ||
| fi | fi | ||
| } | } | ||
| + | |||
| # Perform necessary tasks for exit | # Perform necessary tasks for exit | ||
| # like deleting files and measuring runtime | # like deleting files and measuring runtime | ||
| Línea 1699: | Línea 1699: | ||
| echo -e " | echo -e " | ||
| } | } | ||
| + | |||
| # Exit trap | # Exit trap | ||
| trap " | trap " | ||
| + | |||
| # setting up requirements for network | # setting up requirements for network | ||
| piper=" | piper=" | ||
| Línea 1727: | Línea 1727: | ||
| fi | fi | ||
| fi | fi | ||
| + | |||
| # print welcome title | # print welcome title | ||
| title=" | title=" | ||
| Línea 1733: | Línea 1733: | ||
| title=" | title=" | ||
| fi | fi | ||
| + | |||
| # permanent cache: import | # permanent cache: import | ||
| if [[ -n " | if [[ -n " | ||
| Línea 1744: | Línea 1744: | ||
| echo -e " done\e[0m" | echo -e " done\e[0m" | ||
| fi | fi | ||
| + | |||
| # main game loop | # main game loop | ||
| { | { | ||
| Línea 1795: | Línea 1795: | ||
| fi | fi | ||
| done | $piper > " | done | $piper > " | ||
| + | |||
| # check exit code | # check exit code | ||
| netcatExit=$? | netcatExit=$? | ||
| Línea 1805: | Línea 1805: | ||
| fi | fi | ||
| } 3>&1 | } 3>&1 | ||
| - | </code> | + | </file> |
