Pieters bash scripts
Documentation for the bash scripts I have published
Loading...
Searching...
No Matches
patience.sh File Reference

This script allows someone to play the game patience. More...

Go to the source code of this file.

Functions

 printErrorDescription ($arg1)
 Prints a descriptive error message based on the given error code.
 terminalBlackForeground ()
 Sets the terminal text color to black.
 terminalRedForeground ()
 Sets the terminal text color to red.
 terminalGreenForeground ()
 Sets the terminal text color to green.
 terminalBrownForeground ()
 Sets the terminal text color to brown.
 terminalBlueForeground ()
 Sets the terminal text color to blue.
 terminalPurpleForeground ()
 Sets the terminal text color to purple.
 terminalCyanForeground ()
 Sets the terminal text color to cyan.
 terminalGreyForeground ()
 Sets the terminal text color to gray.
 terminalBlackBackground ()
 Sets the terminal background color to black.
 terminalRedBackground ()
 Sets the terminal background color to red.
 terminalGreenBackground ()
 Sets the terminal background color to green.
 terminalBrownBackground ()
 Sets the terminal background color to brown.
 terminalBlueBackground ()
 Sets the terminal background color to blue.
 terminalPurpleBackground ()
 Sets the terminal background color to purple.
 terminalCyanBackground ()
 Sets the terminal background color to cyan.
 terminalGreyBackground ()
 Sets the terminal background color to gray.
 terminalBold ()
 Sets the terminal to print bold text.
 terminalUnderline ()
 Sets the terminal to print underlined text.
 terminalClearColors ()
 Sets the terminal to reset the colors to the default.
 setBoardBackground ()
 Sets the background of the board patience is played on.
 debugPrintDeck ()
 Print the cards in the deck for debug purposes.
 removeFromDeckOpen ($arg1)
 removeFromDeckFront ($arg1, $arg2)
 Remove cards from the front of the deck.
 removeFromDeck ($arg1)
 reindexDeck ()
 Reindex the deck, i.e. remove gaps in array.
 shuffleDeck ()
 Move the cards in the deck to a random new position.
 populateDeck ()
 Fill the deck with all the cards.
 rotateDeck ($arg1)
 printSuitsStack ()
 Print the suit stacks as a string, if empty print the suit symbol.
 printDeck ($arg1)
 printBoard ($arg1, $arg2)
 printBoardContent ()
 Helper function to print the board. It handles the coloring of the board. arguments same as args to printf.
 printScore ()
 Helper function to print the board. It handles the coloring of the board.
 printHelp ($arg1)
 Print the help function and handles help navigation through the help.
 printCard ($arg1, $arg2)
 printCredits ()
 Print the credits of the game and perform a short "animation".
 isGameWon ()
 dealBlinds ()
 Deal the blind cards on the board.
 fillOpen ()
 turnBlind ($arg1)
 initSuitStack ()
 Initialise the suitstacks.
 getBottomCard ($arg1)
 getTopCard ($arg1)
 moveDeckToSuitStack ()
 removeFromOpenStacks ($arg1, $arg2)
 removeFromSuit ($arg1)
 initOpen ()
 Initialise the open stacks on the board.
 findCard ($arg1)
 getCardValue ($arg1)
 getCardSuit ($arg1)
 getCardColor ($arg1)
 isValidBoardMove ($arg1, $arg2)
 isValidSuitMove ($arg1)
 moveCards ($arg1, $arg2)
 autoMoveToSuitstacks ()
 Try to put all available cards on the suitstacks.
 nameToCard ($arg1)
 cardToName ($arg1)
 calculateLeftMargin ($arg1, $arg2)
 calculateTopMargin ($arg1, $arg2)
 init ()
 Initialise the game.
 cleanup ()
 Cleanup remnants of the old game before starting a new.
 confirmAction ($arg1)
 checkTerminalSize ($arg1, $arg2)
 main ()
 Main function.

Variables

const $C_UNINIT = 2
 Bool is not initialised.
const $C_TRUE = 1
 True value.
const $C_FALSE = 0
 False value.
const $C_SUCCESS = 255
const $C_EMPTY_COLUMN = 254
 Indicates the selected column to take a card from is emtpy.
const $C_MOVE_INVALID = 253
 Indicates the given move is not a valid move.
const $C_CARD_NOT_FOUND = 252
 Indicates the card asked for is not available for a move.
const $C_COLUMN_NOT_EXISTS = 251
 Indicates the given column does not exist.
const $C_INVALID_ARGUMENT = 250
 Indicates the argument given does not exist.
const $C_NULL_CARD = 249
 Indicates the card given is empty.
const $C_NO_BLINDS = 248
 The request to turn a blind card cannot be fulfilled as there are no blind cards.
const $C_NOT_ENOUGH_CARDS = 247
 The card stack is too small to give the requested cards.
const $C_INCORRECT_CARD_NAME = 246
 The given name is not a valid card name.
const $C_TERMINAL_SIZE_ERROR = 245
 The size of the terminal is not large enough to show the patience board.
const $C_DEBUG_STOP = 70
 Returned prematurely for debug purposes.
 $useColoredTerminal = $C_TRUE
 Indicates if terminal colors should be used.
 $deck
 The deck of cards.
 $blindStacks
 The blind cards of each column.
 $openStacks
 The open cards of each column.
 $deckOpen
 The open cards on the unplayed deck.
 $deckFront
 The card on the front of the deck.
 $suitStack
 Stacks of cards having been sorted by suit.
 $user_msg1 = "Welcome"
 Text for the user message, first row.
 $user_msg2 = " press h for help"
 Text for the user message, second row.
 $marginLeft = ""
 How much margin to keep to the left of the terminal to center the board.
 $marginTop = ""
 How much margin to keep to the top of the terminal to center the board.
 $nDeckIterations = 0
 How many times has the deck been rotated (viewed all cards and started over).
 $score = 0
 Current score.

Detailed Description

This script allows someone to play the game patience.

Author: Pieter van der Star (info@.nosp@m.piet.nosp@m.ervan.nosp@m.ders.nosp@m.tar.n.nosp@m.l) Modifications by: (unmodified)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Definition in file patience.sh.

Function Documentation

◆ autoMoveToSuitstacks()

autoMoveToSuitstacks ( )

Try to put all available cards on the suitstacks.

Definition at line 1161 of file patience.sh.

Here is the call graph for this function:

◆ calculateLeftMargin()

calculateLeftMargin ( $arg1,
$arg2 )

Calculate the margin so the board ends up in the middle of the terminal.

Parameters
$arg1terminal width.
$arg2width of the content (board is different to help)
Postcondition
Sets global marginLeft.

Definition at line 1270 of file patience.sh.

◆ calculateTopMargin()

calculateTopMargin ( $arg1,
$arg2 )

Calculate the margin so the board ends up in the middle of the terminal.

Parameters
$arg1terminal height.
$arg2height of the content (board is different to help)
Postcondition
Sets global marginTop.

Definition at line 1287 of file patience.sh.

◆ cardToName()

cardToName ( $arg1)

Convert cardnumber to a human readable card name.

Parameters
$arg1card number.
Returns
Error code according to Return values used in patience.sh..

Prints card name, for use in $(cardToName n)

Definition at line 1239 of file patience.sh.

◆ checkTerminalSize()

checkTerminalSize ( $arg1,
$arg2 )

Check if the terminal has the correct size to display the board.

Parameters
$arg1Width of the terminal (in characters).
$arg2Width of the terminal (in characters).

Definition at line 1370 of file patience.sh.

◆ cleanup()

cleanup ( )

Cleanup remnants of the old game before starting a new.

Definition at line 1341 of file patience.sh.

◆ confirmAction()

confirmAction ( $arg1)

Ask user for confirmation.

Parameters
$arg1Description of the action to perform.
Returns
Bool, user confirmed.

Definition at line 1353 of file patience.sh.

◆ dealBlinds()

dealBlinds ( )

Deal the blind cards on the board.

Definition at line 745 of file patience.sh.

Here is the call graph for this function:

◆ debugPrintDeck()

debugPrintDeck ( )

Print the cards in the deck for debug purposes.

Definition at line 165 of file patience.sh.

◆ fillOpen()

fillOpen ( )

Fill any open columns on the board.

Returns
Error code according to Return values used in patience.sh..

Definition at line 764 of file patience.sh.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findCard()

findCard ( $arg1)

Search the deck, board and suitstacks for the given card.

Parameters
$arg1Cardnumber of the card to find.
Postcondition
updates inherited: $fromFromDeck to indicate if the card is the first card on the deck. $fromFromSuit to indicate if the card is the top of the suit stack. $inColumn to indicate which column the card is in.
Returns
Error code according to Return values used in patience.sh..

Definition at line 911 of file patience.sh.

◆ getBottomCard()

getBottomCard ( $arg1)

Get the card value at the bottom of the given column

Parameters
$arg1column
Returns
Error code according to Return values used in patience.sh..

Definition at line 808 of file patience.sh.

Here is the caller graph for this function:

◆ getCardColor()

getCardColor ( $arg1)

Get the color of the card based on the cardnumber.

Parameters
$arg1Cardnumber.
Returns
Number indicating card value.

Definition at line 966 of file patience.sh.

◆ getCardSuit()

getCardSuit ( $arg1)

Get the suit of the card based on the cardnumber.

Parameters
$arg1Cardnumber.
Returns
Number indicating card suit.

Definition at line 958 of file patience.sh.

◆ getCardValue()

getCardValue ( $arg1)

Get the value of the card based on the cardnumber.

Parameters
$arg1Cardnumber.
Returns
Card value.

Definition at line 950 of file patience.sh.

◆ getTopCard()

getTopCard ( $arg1)

Get the card value at the top of the given column.

Parameters
$arg1column.
Returns
Error code according to Return values used in patience.sh..

Definition at line 824 of file patience.sh.

◆ init()

init ( )

Initialise the game.

Definition at line 1325 of file patience.sh.

Here is the call graph for this function:

◆ initOpen()

initOpen ( )

Initialise the open stacks on the board.

Definition at line 896 of file patience.sh.

◆ initSuitStack()

initSuitStack ( )

Initialise the suitstacks.

Definition at line 798 of file patience.sh.

◆ isGameWon()

isGameWon ( )

Check if the game is won.

Returns
Boolean true if game won, false if not (yet).

Definition at line 733 of file patience.sh.

◆ isValidBoardMove()

isValidBoardMove ( $arg1,
$arg2 )

Check if the move is a valid move on the board.

Parameters
$arg1Cardnumber of card to move.
$arg2cardnumber of card to move the arg1 card on top of.
Returns
Error code according to Return values used in patience.sh..

Definition at line 975 of file patience.sh.

◆ isValidSuitMove()

isValidSuitMove ( $arg1)

Check if the move is a valid move to the suitstacks.

Parameters
$arg1cardnumber of card to move.
Returns
Error code according to Return values used in patience.sh..

Definition at line 1009 of file patience.sh.

◆ main()

main ( )

Main function.

Definition at line 1382 of file patience.sh.

◆ moveCards()

moveCards ( $arg1,
$arg2 )

Move card from A to B.

Parameters
$arg1card to move.
$arg2location to move to.
Returns
Error code according to Return values used in patience.sh..

Definition at line 1025 of file patience.sh.

Here is the caller graph for this function:

◆ moveDeckToSuitStack()

moveDeckToSuitStack ( )

Move a card from the deck to the corresponding suitstack.

Returns
Error code according to Return values used in patience.sh..

Definition at line 839 of file patience.sh.

◆ nameToCard()

nameToCard ( $arg1)

Convert a human card name to a cardnumber.

Parameters
$arg1abbrieviated human name for the card.
Returns
Card number.

Definition at line 1210 of file patience.sh.

◆ populateDeck()

populateDeck ( )

Fill the deck with all the cards.

Definition at line 306 of file patience.sh.

Here is the caller graph for this function:

◆ printBoard()

printBoard ( $arg1,
$arg2 )

Print the board.

Parameters
$arg1(optional) has player won the game.
$arg2(optional) end of deck iterations.

Definition at line 414 of file patience.sh.

Here is the call graph for this function:

◆ printBoardContent()

printBoardContent ( )

Helper function to print the board. It handles the coloring of the board. arguments same as args to printf.

Definition at line 496 of file patience.sh.

◆ printCard()

printCard ( $arg1,
$arg2 )

Output a card character.

Parameters
$arg1cardnumber of card to print.
$arg2(optional) print the card closed.
Returns
Error code according to Return values used in patience.sh..

Definition at line 667 of file patience.sh.

Here is the caller graph for this function:

◆ printCredits()

printCredits ( )

Print the credits of the game and perform a short "animation".

Definition at line 706 of file patience.sh.

Here is the call graph for this function:

◆ printDeck()

printDeck ( $arg1)

Print the deck, in two parts, first the closed, then the front.

Parameters
$arg1end of deck iterations.

Definition at line 379 of file patience.sh.

◆ printHelp()

printHelp ( $arg1)

Print the help function and handles help navigation through the help.

Parameters
$arg1(optional) pagenumber to show.
Returns
Error code according to Return values used in patience.sh..

Definition at line 512 of file patience.sh.

Here is the call graph for this function:

◆ printScore()

printScore ( )

Helper function to print the board. It handles the coloring of the board.

Definition at line 501 of file patience.sh.

◆ printSuitsStack()

printSuitsStack ( )

Print the suit stacks as a string, if empty print the suit symbol.

Definition at line 356 of file patience.sh.

Here is the call graph for this function:

◆ reindexDeck()

reindexDeck ( )

Reindex the deck, i.e. remove gaps in array.

Definition at line 268 of file patience.sh.

Here is the caller graph for this function:

◆ removeFromDeck()

removeFromDeck ( $arg1)

Remove cards from the closed part of the deck at given index and fill the gap.

Parameters
$arg1index of the card to be removed.

Definition at line 250 of file patience.sh.

Here is the caller graph for this function:

◆ removeFromDeckFront()

removeFromDeckFront ( $arg1,
$arg2 )

Remove cards from the front of the deck.

Parameters
$arg1Index of the card to be removed.
$arg2should it backfill from deckOpen.
Returns
Error code according to Return values used in patience.sh..

Remove cards from the open part of the deck at given index and fill the gap. If backfill is set take one card from the open hand of the deck and move it to the front.

Definition at line 218 of file patience.sh.

◆ removeFromDeckOpen()

removeFromDeckOpen ( $arg1)

Remove cards from the open part of the deck at given index and fill the gap.

Parameters
$arg1Index of the card to be removed.

Definition at line 193 of file patience.sh.

◆ removeFromOpenStacks()

removeFromOpenStacks ( $arg1,
$arg2 )

Remove a card from one of the open stacks on the board.

Parameters
$arg1column index.
$arg2number of cards.
Returns
Error code according to Return values used in patience.sh..

Definition at line 866 of file patience.sh.

◆ removeFromSuit()

removeFromSuit ( $arg1)

Remove a card from the suitstack.

Parameters
$arg1the suit to remove the card from.
Returns
Error code according to Return values used in patience.sh..

Definition at line 886 of file patience.sh.

◆ rotateDeck()

rotateDeck ( $arg1)

Move cards from the front to the open deck and then nFront from the closed to the front.

Parameters
$arg1how many cards should be in a hand.
Postcondition
Sets the global variable nDeckIterations.

Definition at line 317 of file patience.sh.

◆ setBoardBackground()

setBoardBackground ( )

Sets the background of the board patience is played on.

Definition at line 161 of file patience.sh.

Here is the call graph for this function:

◆ shuffleDeck()

shuffleDeck ( )

Move the cards in the deck to a random new position.

Definition at line 288 of file patience.sh.

Here is the call graph for this function:

◆ terminalBlackBackground()

terminalBlackBackground ( )

Sets the terminal background color to black.

Definition at line 138 of file patience.sh.

◆ terminalBlackForeground()

terminalBlackForeground ( )

Sets the terminal text color to black.

Definition at line 121 of file patience.sh.

Here is the caller graph for this function:

◆ terminalBlueBackground()

terminalBlueBackground ( )

Sets the terminal background color to blue.

Definition at line 146 of file patience.sh.

Here is the caller graph for this function:

◆ terminalBlueForeground()

terminalBlueForeground ( )

Sets the terminal text color to blue.

Definition at line 129 of file patience.sh.

◆ terminalBold()

terminalBold ( )

Sets the terminal to print bold text.

Definition at line 155 of file patience.sh.

◆ terminalBrownBackground()

terminalBrownBackground ( )

Sets the terminal background color to brown.

Definition at line 144 of file patience.sh.

◆ terminalBrownForeground()

terminalBrownForeground ( )

Sets the terminal text color to brown.

Definition at line 127 of file patience.sh.

Here is the caller graph for this function:

◆ terminalClearColors()

terminalClearColors ( )

Sets the terminal to reset the colors to the default.

Definition at line 159 of file patience.sh.

◆ terminalCyanBackground()

terminalCyanBackground ( )

Sets the terminal background color to cyan.

Definition at line 150 of file patience.sh.

◆ terminalCyanForeground()

terminalCyanForeground ( )

Sets the terminal text color to cyan.

Definition at line 133 of file patience.sh.

◆ terminalGreenBackground()

terminalGreenBackground ( )

Sets the terminal background color to green.

Definition at line 142 of file patience.sh.

Here is the caller graph for this function:

◆ terminalGreenForeground()

terminalGreenForeground ( )

Sets the terminal text color to green.

Definition at line 125 of file patience.sh.

Here is the caller graph for this function:

◆ terminalGreyBackground()

terminalGreyBackground ( )

Sets the terminal background color to gray.

Definition at line 152 of file patience.sh.

◆ terminalGreyForeground()

terminalGreyForeground ( )

Sets the terminal text color to gray.

Definition at line 135 of file patience.sh.

Here is the caller graph for this function:

◆ terminalPurpleBackground()

terminalPurpleBackground ( )

Sets the terminal background color to purple.

Definition at line 148 of file patience.sh.

◆ terminalPurpleForeground()

terminalPurpleForeground ( )

Sets the terminal text color to purple.

Definition at line 131 of file patience.sh.

◆ terminalRedBackground()

terminalRedBackground ( )

Sets the terminal background color to red.

Definition at line 140 of file patience.sh.

◆ terminalRedForeground()

terminalRedForeground ( )

Sets the terminal text color to red.

Definition at line 123 of file patience.sh.

Here is the caller graph for this function:

◆ terminalUnderline()

terminalUnderline ( )

Sets the terminal to print underlined text.

Definition at line 157 of file patience.sh.

◆ turnBlind()

turnBlind ( $arg1)

Turn a blind card from a given column.

Parameters
$arg1column to turn the frontmost blind card.
Returns
Error code according to Return values used in patience.sh..

Definition at line 784 of file patience.sh.

Here is the caller graph for this function:

Variable Documentation

◆ $blindStacks

$blindStacks

The blind cards of each column.

Definition at line 1306 of file patience.sh.

◆ $C_FALSE

const $C_FALSE = 0

False value.

Definition at line 60 of file patience.sh.

◆ $C_TRUE

const $C_TRUE = 1

True value.

Definition at line 58 of file patience.sh.

◆ $C_UNINIT

const $C_UNINIT = 2

Bool is not initialised.

Definition at line 56 of file patience.sh.

◆ $deck

$deck

The deck of cards.

Definition at line 1305 of file patience.sh.

◆ $deckFront

$deckFront

The card on the front of the deck.

Definition at line 1309 of file patience.sh.

◆ $deckOpen

$deckOpen

The open cards on the unplayed deck.

Definition at line 1308 of file patience.sh.

◆ $marginLeft

$marginLeft = ""

How much margin to keep to the left of the terminal to center the board.

Definition at line 1315 of file patience.sh.

◆ $marginTop

$marginTop = ""

How much margin to keep to the top of the terminal to center the board.

Definition at line 1317 of file patience.sh.

◆ $nDeckIterations

$nDeckIterations = 0

How many times has the deck been rotated (viewed all cards and started over).

Definition at line 1320 of file patience.sh.

◆ $openStacks

$openStacks

The open cards of each column.

Definition at line 1307 of file patience.sh.

◆ $score

$score = 0

Current score.

Definition at line 1322 of file patience.sh.

◆ $suitStack

$suitStack

Stacks of cards having been sorted by suit.

Definition at line 1310 of file patience.sh.

◆ $useColoredTerminal

$useColoredTerminal = $C_TRUE

Indicates if terminal colors should be used.

Definition at line 119 of file patience.sh.

◆ $user_msg1

$user_msg1 = "Welcome"

Text for the user message, first row.

Definition at line 1311 of file patience.sh.

◆ $user_msg2

$user_msg2 = " press h for help"

Text for the user message, second row.

Definition at line 1313 of file patience.sh.