Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tic-tac-toe in the shell

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Hi all..
I decided I wanted a little ttt program in the shell
and wrote one.
Unfortunately the algo for searching for winning
combo's is linear and is limited to three consecutive
moves, when a winning combo like:

Code:
                        #               #
                   X    #      X        #   X
                        #               #
                 ###############################
                        #               #
                        #      O        #   O
                        #               #
                 ###############################
                        #               #
                   O    #               #   X
                        #               #
Is ignored.

Does anyone have a cool function to search a list of
moves for all winning combos given a list of player
moves in the format: "15798" ??? With the playing
squares ordered by row of course.
eg:
Code:
           123
           456
           789
Thanks
 
any 3 consecutive numbers (123),
any 3 numbers with 3 between them (147),
or any set of 3 numbers (with 5 in them) that add up to 15 (159).

will that do?

little nawk script showing implementation
Code:
#!/bin/sh
echo $1 | nawk '
{
  len=length($1);
  for (n=1;n<len+1;n++)
  {
    nums[n]=(substr($1,n,1)+0);
  }
  if (len > 2)
  {
    for (a=len;a>2;a--) {
    for (b=a-1;b>1;b--) {
    for (c=b-1;c>0;c--) {
      if (nums[a]-1==nums[b] && nums[b]-1==nums[c])
      {
        print nums[c]&quot; - &quot;nums[b]&quot; - &quot;nums[a];
        print &quot;you have won&quot;; exit
      }
      if (nums[a]+3==nums[b] && nums[b]+3==nums[c])
      {
        print nums[c]&quot; - &quot;nums[b]&quot; - &quot;nums[a];
        print &quot;you have won&quot;; exit
      }
      if (nums[a]==5 || nums[b]==5 || nums[c]==5)
      {
        if (nums[a]+nums[b]+nums[c]==15)
        {
          print nums[c]&quot; - &quot;nums[b]&quot; - &quot;nums[a];
          print &quot;you have won&quot;; exit
        }
      }
    }}}
    print &quot;You have not won yet&quot;;
  } else {
    print &quot;not enough moves yet&quot;;
  }
}'
 
actually i got that a little wrong ... the numbers need to be in the correct order 132 wouldn't work ... and the second if should have -'s instead of +'s ...

but the first should be easy enough to sort out ... you can order the nums array before you start, or do something like:
Code:
pos=0; for(n=1;n<10;n++){if (index($0,n) > 0) {nums[++pos]=n;}};
instead of:
Code:
for (n=1;n<=length($1);n++){nums[n]=(substr($1,n,1)+0);}

which might work :)
 
Thanks, I settled on this:
Code:
function check_win() {
list=$1

     ifo=`echo $list | awk ' {
          p=1
          for (x=1 ; x < (length($0) - 2) ; x++) {
            for (p=(x+p) ; p < length($0) ; p++) {
                if ((p + 1) > length($0)) {
                    break
                }
                printf &quot;%s%s%s\n&quot;, substr($0,x,1), substr($0,p,1), substr($0,(p + 1),1)
             }
            p=1
           }
        }'`

 if [ ! -z &quot;$ifo&quot; ] ; then 
  for all in $(echo $ifo)
  do
      for mm in ${wins[@]}
      do
         if [ &quot;$all&quot; = &quot;$mm&quot; ] ; then
            printf &quot;1\n&quot; ; return 
         fi
      done
   done
 fi
printf &quot;0\n&quot;
return 
}

Does anybody know where I could find another tt prog
written in awk/shell?
I'd like to see another approach.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top