Home / Linux / Creating the Concentration Game PAIRS with Bash, Part II
Creating the Concentration Game PAIRS with Bash, Part II
Creating the Concentration Game PAIRS with Bash, Part II

Creating the Concentration Game PAIRS with Bash, Part II

Creating the Concentration Game PAIRS with Bash, Part II

Dave finally ends up the PAIRS focus sport, most effective to comprehend it’s too
arduous to unravel!

In my ultimate
article
, I tossed away my PC card and mentioned how I used to be partial to the
British colonial-era author Rudyard Kipling. With that during thoughts, I do
respect that you are nonetheless studying my column.

I used to be discussing the reminiscence sport that the British undercover agent performs with the
orphan boy Kim in the e book of the similar identify. The sport in query comes to
Kim
being proven a tray of stones of quite a lot of shapes, sizes and colours. Then
it is hidden, and he has to recite as many patterns as he can recall.

The card sport Concentration is obviously impressed by way of the similar trend
memorization sport, and it is significantly more straightforward to arrange: shuffle a deck
of playing cards, position them face down in a grid, then turn pairs to seek out fits. In
the starting, it is simply guessing, after all, however as the sport proceeds, it
turns into extra about spatial reminiscence than success. Someone with an eidetic reminiscence
all the time will win.

Using letters makes issues simple, so I urged a row, column, notational
conference like this:


    1   2   Three   four   five   6   7   eight   nine   10  11  12  13
1: [-] [-] [-] [-] [-] [-] [-] [-] [-] [-] [-] [-] [-]
2: [-] [-] [-] [A] [-] [-] [-] [-] [-] [-] [-] [-] [-]
Three: [-] [-] [-] [-] [-] [-] [-] [-] [E] [-] [-] [-] [-]
four: [-] [-] [-] [-] [-] [-] [-] [-] [-] [-] [-] [-] [Z]

You can constitute uppercase letters as a shell array like this:


claim -a letters=(A B C D E F G H I J Ok L M N O P Q R
                    S T U V W X Y Z)

Unfortunately, Bash does not beef up multidimensional arrays, so you are
going to must constitute the grid as a one-dimensional array. It’s no longer
too arduous despite the fact that, as a result of the grid is simple. Here’s an index
method if firstvalue is the first digit and leisure is the rest of the
index worth:


index=$(( ( ( $firstvalue - 1 ) * 13 ) + $leisure ))

The letter “E” in the above grid, at Three,nine, would display up in the array
as ((Three-1)*13)+nine or slot 35.

Shuffle Those Values

The script from my ultimate
article
already initializes the whole lot in sequential order
and defaults to two * 13 slots (for simplicity in debugging). The paintings of the
script is truly in the shuffle, however it seems that there is a lovely
chic little shuffle set of rules (proven in a type of sloppy C for illustrative
functions) floating round the web that may be tapped for this job:


shuffle 

Translating this right into a shell script and the use of higher variable names,
here is what I created:


shuffle ()


Instead of getting a separate serve as for the worth switch, I simply went forward
and dropped that into the serve as itself. It’s quicker and likewise allows you to sidestep
the dereference trouble smartly.

Here’s what occurs while you initialize the
grid, shuffle it, then show it on display screen (and sure, I modified the
“[]” to “<>” to make it extra visually fascinating):


    1   2   Three   four   five   6   7   eight   nine   10  11  12  13
1: <V> <X> <M> <R> <C> <F> <Ok> <O> <U> <H>
<T> <Q> <L>
2: <A> <G> <B> <N> <J> <Y> <P> <W> <Z> <E>
<D> <I> <S>

Of route, 26 grid spots equals precisely the selection of letters in the
alphabet, so
there are precisely 0 pairs. That’s no longer a lot amusing as video games move, however what if
you request
a four-line grid?


    1   2   Three   four   five   6   7   eight   nine   10  11  12  13
1: <G> <J> <A> <Ok> <P> <L> <B> <O> <I> <X>
<Y> <N> <F>
2: <Y> <C> <Z> <O> <G> <D> <T> <N> <V> <D>
<H> <E> <U>
Three: <W> <C> <R> <Q> <M> <B> <E> <Ok> <F> <I>
<T> <Q> <R>
four: <U> <Z> <P> <H> <S> <W> <L> <J> <M> <X>
<V> <S> <A>

A couple of pairs leap out, like 2,13 and four,1 for the “U” values, however
consider, the sport goes to cover all of this, and it is your task to wager
those pairs.

Ah, it is no longer really easy, eh?

Tracking What’s Been Guessed

Now that the grid is being created and shuffled appropriately, the subsequent step is to
differentiate between spots which have been guessed appropriately and those who
are nonetheless unknown. You may do this with a parallel array, however why undergo
the trouble? Instead, initialize each worth to have a touch as its first
persona after which take away it as soon as guessed.

The show serve as now can take a look at a price to look if it is a
“negative” letter. If so, it will show a “-” as an alternative of
the precise worth. Now the preliminary grid seems like this:


    1   2   Three   four   five   6   7   eight   nine   10  11  12  13
1: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
2: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
Three: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
four: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->

What about coming into your wager for the location of a given pair? I am going
to make issues tougher by way of no longer appearing the values in a grid however somewhat simply
showing them immediately.

Enter a grid worth as row, col, then break up it into the ones values and
multiply
it out to a novel array index. It’s sophisticated, however when you learn
$slot1 and $slot2
as the enter values from the consumer, the research loop is that this:


row1=$( echo $slot1 | minimize -c1 )
col1=$( echo $slot1 | minimize -d, -f2 )
row2=$( echo $slot2 | minimize -c1 )
col2=$( echo $slot2 | minimize -d, -f2 )

index1=$(( ( $row1 - 1) * 13 + $col1 ))
index2=$(( ( $row2 - 1) * 13 + $col2 ))

val1=$
val2=$

There’s a woeful loss of error-checking right here, however that is one thing I
like so as to add in a while, as soon as I am getting the core set of rules useful.

Armed with $val1 and $val2 above, checking out to look you probably have a fit is simple:


if [ $val1 = $val2 ] ; then
  echo "You've got a match. Nicely done!"
  board[$index1]=$
  board[$index1]=$
  unsolved=$(( $unsolved - 2 ))
else
  echo "No fit. $row1,$col1 = $ and 
       $row2,$col2 = $."
fi

Did you understand $unsolved in the matching conditional code?
That’s the way you
can stay observe of whether or not the grid has been solved.

So Let’s Give It a Try!

With all this code in position, let’s give it a whirl:


Welcome to PAIRS. Your project is to spot matching letters
in the grid. Good success. If you surrender at any level, simply use
q to give up.


Enter a couple of values in row,col layout : 1,1 four,1
No fit, however 1,1 = P and four,1 = A.

    1   2   Three   four   five   6   7   eight   nine   10  11  12  13
1: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
2: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
Three: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
four: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->

Enter a couple of values in row,col layout : 2,1 Three,1
No fit, however 2,1 = Z and three,1 = B.


    1   2   Three   four   five   6   7   eight   nine   10  11  12  13
1: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
2: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
Three: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->
four: <-> <-> <-> <-> <-> <-> <-> <-> <-> <->
<-> <-> <->

I am principally performed with the program at this level, and I am knowing
one thing. This is truly arduous to unravel as a sport.

Hacks and Mods

Here’s an workout for you, pricey reader: that is producing 26 imaginable values,
the letters A–Z, which calls for a minimal grid of 52 slots. That’s so much!
Modify it to paintings with unmarried digits, after which regulate the grid dimensions
correctly. For instance, 20 slots can also be portrayed in a four x five grid. For
positive, 19 chances for the fit of a published worth is so much more straightforward than
51 chances.

Have amusing with this, and take hold of the complete script underneath or from right here.
Let me know the way you adjust this to make it extra
entertaining, fascinating or simply make it more straightforward!

The Full Script


#!/bin/sh

# PAIR - a easy matching sport, implmemented as a linear array

# Usage: PAIR rowcount
#    notice: rowcount should be even and specifies what number of 13-slot
#    rows are created
#    the default worth is two rows of 13 values

claim -a board
claim -a letters=(A B C D E F G H I J Ok L M N O P Q R S T U V W X Y Z)
rows=four                  # default # of 13 slot rows

initialize ()


shuffle ()


showgrid ()


##################################

if [ $# -gt 0 ] ; then
  rows=$1
fi

if [ $(( $rows % 4 )) -ne 0 ] ; then
  echo "Ooops! Please most effective specify a more than one of four as the quantity
of rows (four, eight, 12, and many others)"
  go out 1
fi

slot1=slot2="X"                 # get started with a non-empty worth
unsolved=$(( $rows * 13 ))
maxvalues=$unsolved             # parameter checking out

echo "Welcome to PAIRS. Your project is to spot matching letters
in the grid."
echo "Good luck. If you give up at any point, just use q to quit."
echo ""

initialize $rows

shuffle $rows

showgrid

whilst [ $unsolved -gt 0 ] ; do
  echo ""
  /bin/echo -n "Enter a pair of values in row,col format : "
  learn slot1 slot2

  if [ "$slot1" = "" -o "$slot2" = "" ] ; then
    echo "bye."
    go out 1
  fi

  row1=$( echo $slot1 | minimize -c1 )
  col1=$( echo $slot1 | minimize -d, -f2 )
  row2=$( echo $slot2 | minimize -c1 )
  col2=$( echo $slot2 | minimize -d, -f2 )

  index1=$(( ( $row1 - 1) * 13 + $col1 ))
  index2=$(( ( $row2 - 1) * 13 + $col2 ))

  if [ $index1 -lt 0 -o $index1 -gt $maxvalues -o $index2 -lt
 ↪0 -o $index2 -gt $maxvalues ] ; then
    echo "bad input, not a valid value"
    go out 1
  fi

  val1=$
  val2=$

  if [ $val1 = $val2 ] ; then
    echo "You've got a match. Nicely done!"
    board[$index1]=$
    board[$index1]=$
    unsolved=$(( $unsolved - 2 ))
  else
    echo "No fit, however $row1,$col1 = $ and $row2,$col2 =
 ↪$."
  fi

  echo ""
  showgrid

performed

go out zero

Check Also

A Look at Fundamental Linux sed Commands | Linux.com

A Look at Fundamental Linux sed Commands | Linux.com

A Look at Fundamental Linux sed Commands | Linux.com Linux directors who wish to alter …

Leave a Reply

Your email address will not be published. Required fields are marked *