Herramientas de usuario

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Próxima revisión
Revisión previa
chessba.sh [2024/04/29 21:10] – creado peronchessba.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.  See the GNU General Public License for more details. # 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.  See the GNU General Public License for more details.
 # 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 >/dev/null 2>&1 && useStty=true || useStty=false type stty >/dev/null 2>&1 && useStty=true || useStty=false
 + 
 # 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://en.wikipedia.org/wiki/Combining_Grapheme_Joiner) and retrieving the cursor position afterwards. # We do this using a trick: printing a special zero-length unicode char (http://en.wikipedia.org/wiki/Combining_Grapheme_Joiner) and retrieving the cursor position afterwards.
Línea 75: Línea 75:
 fi fi
 echo -e "\e[u\e8\e[2K\r\e[0m\nWelcome to \e[1mChessBa.sh\e[0m - a Chess game written in Bash \e[2mby Bernhard Heinloth, 2015\e[0m\n" echo -e "\e[u\e8\e[2K\r\e[0m\nWelcome to \e[1mChessBa.sh\e[0m - a Chess game written in Bash \e[2mby Bernhard Heinloth, 2015\e[0m\n"
 + 
 # Print version information # Print version information
 function version() { function version() {
  echo "ChessBash 0.4"  echo "ChessBash 0.4"
 } }
 + 
 # Wait for key press # Wait for key press
 # no params/return # no params/return
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://stackoverflow.com/a/9221063 # source: http://stackoverflow.com/a/9221063
Línea 153: Línea 153:
  fi  fi
 } }
 + 
 # Named ANSI colors # Named ANSI colors
 declare -a colors=( "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" ) declare -a colors=( "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" )
 + 
 # 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 ":a:A:b:B:c:P:s:t:w:dghilmMnpvVz" options; do while getopts ":a:A:b:B:c:P:s:t:w:dghilmMnpvVz" options; do
Línea 350: Línea 350:
  esac  esac
 done done
 + 
 # get terminal dimension # get terminal dimension
 echo -en '\e[18t' echo -en '\e[18t'
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 >/dev/null 2>&1 ; then  if type gdialog >/dev/null 2>&1 ; then
Línea 382: Línea 382:
  error "The graphical configuration requires gdialog/zenity, dialog or at least whiptail - but none of them was found on your system. You have to use the arguments to configure the game unless you install one of the required tools..."  error "The graphical configuration requires gdialog/zenity, dialog or at least whiptail - but none of them was found on your system. You have to use the arguments to configure the game unless you install one of the required tools..."
  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/error message  # Print a message box with a warning/error message
  # Params:  # Params:
Línea 431: Línea 431:
  dlg --msgbox "$1" $dlgh $dlgw  dlg --msgbox "$1" $dlgh $dlgw
  }  }
 + 
  # 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="ON"  local dlg_on="ON"
  local dlg_off="OFF"  local dlg_off="OFF"
 + 
  declare -a option_player=( "Human" "Computer" "Network" )  declare -a option_player=( "Human" "Computer" "Network" )
  declare -a option_settings=( "Color support" "Unicode support" "Verbose Messages" "Mouse support" "AI Cache" )  declare -a option_settings=( "Color support" "Unicode support" "Verbose Messages" "Mouse support" "AI Cache" )
 + 
  local dlg_main  local dlg_main
  while dlg_main=$(dlg --ok-button "Edit" --cancel-button "Start Game" --menu "New Game" $dlgh $dlgw 0 "$option_mainmenu_playerA" "$(typeOfPlayerA || true)" "$option_mainmenu_playerB" "$(typeOfPlayerB || true )" "$option_mainmenu_settings" "Color, Unicode, Mouse & AI Cache") ; do  while dlg_main=$(dlg --ok-button "Edit" --cancel-button "Start Game" --menu "New Game" $dlgh $dlgw 0 "$option_mainmenu_playerA" "$(typeOfPlayerA || true)" "$option_mainmenu_playerB" "$(typeOfPlayerB || true )" "$option_mainmenu_settings" "Color, Unicode, Mouse & AI Cache") ; do
  case "$dlg_main" in  case "$dlg_main" in
 + 
  # Player A settings  # Player A settings
  "$option_mainmenu_playerA" )  "$option_mainmenu_playerA" )
Línea 484: Línea 484:
  if dlg_networkport=$(dlg --inputbox "Server Port (non privileged)" $dlgh $dlgw "$port") ; then  if dlg_networkport=$(dlg --inputbox "Server Port (non privileged)" $dlgh $dlgw "$port") ; then
  if validPort "$dlg_networkport" ; then  if validPort "$dlg_networkport" ; then
- port=$dlg_networkport+ port=$dlg_networkport
  else  else
- dlgerror "Your input '$dlg_remoteip' is not a valid Port!"+ dlgerror "Your input '$dlg_remoteip' is not a valid Port!"
  fi  fi
  fi  fi
Línea 509: Línea 509:
  fi  fi
  ;;  ;;
 + 
  # Player B settings  # Player B settings
  "$option_mainmenu_playerB" )  "$option_mainmenu_playerB" )
Línea 562: Línea 562:
  fi  fi
  ;;  ;;
 + 
  # Game settings  # Game settings
  "$option_mainmenu_settings" )  "$option_mainmenu_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 "\e7\e[s\e[?47h\e[?25l\e[2J\e[H"  echo -e "\e7\e[s\e[?47h\e[?25l\e[2J\e[H"
 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=( "(empty)" "pawn" "knight" "bishop" "rook" "queen" "king" ) declare -a figNames=( "(empty)" "pawn" "knight" "bishop" "rook" "queen" "king" )
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 "\x$((48-$1))$(($2+1))"  echo -en "\x$((48-$1))$(($2+1))"
 } }
 + 
 # 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 "\x$1"  echo -en "\x$1"
 } }
 + 
 # Get ascii code number of character # Get ascii code number of character
 # Params: # Params:
Línea 1094: Línea 1094:
  LC_CTYPE=C printf '%d' "'$1"  LC_CTYPE=C printf '%d' "'$1"
 } }
 + 
 # 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 "\e[0m\e[8m"  echo -en "\e[0m\e[8m"
 } }
 + 
 # Draw the battlefield # Draw the battlefield
 # (no params / return value) # (no params / return value)
Línea 1300: Línea 1300:
  echo -en "\e[0m\e[$(( originY + 10 ));0H\e[2K\n\e[2K$message\e[8m"  echo -en "\e[0m\e[$(( originY + 10 ));0H\e[2K\n\e[2K$message\e[8m"
 } }
 + 
 # 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/return value) # (no params/return value)
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 "\r\n\e[2mYou've wasted $seconds,$(( duration -( seconds * 1000000000 ))) seconds of your lifetime playing with a Bash script.\e[0m\n"  echo -e "\r\n\e[2mYou've wasted $seconds,$(( duration -( seconds * 1000000000 ))) seconds of your lifetime playing with a Bash script.\e[0m\n"
 } }
 + 
 # Exit trap # Exit trap
 trap "end" 0 trap "end" 0
 + 
 # setting up requirements for network # setting up requirements for network
 piper="cat" piper="cat"
Línea 1727: Línea 1727:
  fi  fi
 fi fi
 + 
 # print welcome title # print welcome title
 title="Welcome to ChessBa.sh" title="Welcome to ChessBa.sh"
Línea 1733: Línea 1733:
  title="$title - your room heater tool!"  title="$title - your room heater tool!"
 fi fi
 + 
 # permanent cache: import # permanent cache: import
 if [[ -n "$cache" && -f "$cache" ]] ; then if [[ -n "$cache" && -f "$cache" ]] ; then
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 > "$fifopipe"  done | $piper > "$fifopipe"
 + 
  # check exit code  # check exit code
  netcatExit=$?  netcatExit=$?
Línea 1805: Línea 1805:
  fi  fi
 } 3>&1 } 3>&1
-</code>+</file>

Este sitio web utiliza cookies para guardar datos esenciales de su actividad, como su autenticación. Al entrar acepta el uso de cookies.

Más información