From fetchpop Thu Apr  8 13:43:31 1999
Received: from spock.linux.it (postfix@spock.linux.it [151.99.137.11])
	by pisoft.it (8.8.5/8.8.5) with ESMTP id KAA13433
	for <andreoli@pisoft.it>; Thu, 8 Apr 1999 10:18:38 +0200
Received: by spock.linux.it (Postfix, from userid 38)
	id 795AE1C696; Thu,  8 Apr 1999 08:58:38 +0200 (CEST)
Old-Return-Path: <linux@netbusiness.it>
Delivered-To: mulinux@spock.linux.it
Message-ID: <370CE451.503F@netbusiness.it>
Date: Thu, 08 Apr 1999 10:16:01 -0700
From: Andrea Manzini <linux@netbusiness.it>
X-Mailer: Mozilla 3.01 (Win95; I; 16bit)
MIME-Version: 1.0
To: mulinux@lists.linux.it
Subject: [mu release] mu-TTT (fixed)
Content-Type: multipart/mixed; boundary="------------C30E1D3B8"
Resent-Message-ID: <"2p0CRD.A.wrF.dOFD3"@spock>
Resent-From: mulinux@lists.linux.it
Reply-To: mulinux@lists.linux.it
X-Mailing-List: <mulinux@lists.linux.it> archive/latest/424
X-Loop: mulinux@lists.linux.it
Precedence: list
Resent-Sender: mulinux-request@lists.linux.it
Resent-Date: Thu,  8 Apr 1999 08:58:38 +0200 (CEST)
X-UIDL: 86a819e2da84084eb45ea182019f76c0
Status: RO
Content-Length: 2145
Lines: 98

This is a multi-part message in MIME format.

--------------C30E1D3B8
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi folks...

It seems that my previuos msg went corrupted, so I repost my little game
as attachment with some fixes...
- now the first player is randomly choosen;
- the CPU is somehow smarter (do you remember the movie "Wargames") 

I just edited with msdos, so if there are CRLF at end of lines, 
issue a tr -d "\r" 

Have fun! ;-)

--------------C30E1D3B8
Content-Type: text/plain; charset=us-ascii; name="TTT.AWK"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="TTT.AWK"

#!/usr/bin/nawk -f
BEGIN {
print "\nmu-TicTacToe (8 apr 1999) by Andrea Manzini <linux@netbusiness.it>"
print "You have naughts, I have crosses. To move, type a number 1-9"
  split("000000000",A,"")
  split("147258369123456789159357",C,"")
  split("X O",B,"")
  SEP="\n+---+---+---+\n"
  ORS=""
  turn=(2*int(rand()+0.5))-1
  while(1) {
    refresh_check()
    if (turn==1) {
      do { k=prompt() } while(k==0)
    } else {
      if (moves==0) k=5; else k=cpumove()
      print "My move: " k "\n"
    }
    moves++
    A[k]=turn
    turn=-turn
  }
}

function tris(dir)
{
 sum=0
 for(j=0;j<3;j++) sum+=A[C[j+dir]]
 return sum
}

function refresh_check()
{
  print SEP
  for(j=1;j<=9;j+=3) {
    for(i=0; i<3; i++) {
     k=B[A[i+j]+2];
     if (k==" ") print "| " (i+j) " " ; else print "| " k " "
    }
    print "|"SEP
  }
  for(i=1;i<=24;i+=3) {
    k=tris(i)
    if (k==-3) { print "\nYOU LOSE !\n" ; exit }
    if (k==3) { print "\nYOU WIN !\n" ; exit }
  }
  j=0
  for(i=1;i<=9;i++) if ( A[i]=="0" ) j=1
  if (j==0) {print "\nIT'S A DRAW !!\n"; exit}
}

function prompt() {
  i=0
  print "Enter your move: "
  getline i
  if (i<1) i=0
  if (i>9) i=0
  if (A[i]!="0") i=0
  return i
}

function cpumove() {
  for(i=1;i<=24;i+=3) if (tris(i)==-2) return (gap(i))
  for(i=1;i<=24;i+=3) if (tris(i)==2) return (gap(i))
  do { move=2*int(rand()*4)+1 } while (A[move]!=0);
  return move
}

function gap(dir) {
  for(j=0;j<3;j++) if ( A[C[j+dir]]==0 ) return (C[j+dir])
}
--------------C30E1D3B8--




