Multiplayer Tic Tac Toe

By sunslayer on Jan 03, 2010

This is a Multiplayer Tic Tac Toe game, it uses the Notice event to send the info so you and your opponent have to be connected to the same IRC network

to request a game type /ttt [nick you want to challenge]
but obviously they have to have this script loaded also

if someone else requests a game to you, a small $tip window will appear at the bottom right of your screen telling you who(with their hostmask) is requesting a game. to accept the game double click the window, if you don't want to play you can either ignore it or close it by clicking the X button at the top

once you start a game, a Tic Tac Toe board will pop up, if you got the request you are the first player (X), otherwise your the second (O)

there shouldn't be any false wins/ties/loses as i tested the win checker pretty thoroughly but if there are tell me

on *:load: {
  if (!$isfile(ttt.ini)) {
    var %a = writeini ttt.ini ttt
    %a power 1
    %a wins 0
    %a loses 0
    %a ties 0
    %a moves 0
  }
}
on ^*:notice:ttt*:*:{
  if ($readini(ttt.ini,ttt,power)) {
    if ($2 == request) {
      if (!$hget(ttt,ingame)) {
        .noop $tip(ttt,Tic Tac Toe Request,A Tic Tac Toe game has been requested from $+($nick,$chr(40),$ial($nick),$chr(41),$chr(44)) double click to start $crlf $&
          $nick $+ 's STATS: $crlf Wins: $3 $crlf Loses: $4 $crlf Ties: $5 $crlf Total Moves: $6,10,,,ttt.start $nick)
        .timerTTT 1 15 .notice $nick ttt TO
      }
      else .notice $nick ttt ingame Sorry $nick $+ , but I am already playing a game of Tic Tac Toe.
    }
    elseif ($2 == ingame) {
      .noop $tip(ttt,Tic Tac Toe,$nick is already playing a game of Tic Tac Toe $+ $chr(44) try again later,10)
      .hfree ttt
    }
    elseif ($2 == accepted) ttt.start
    elseif ($2 == TO) { .noop $tip(ttt,Tic Tac Toe,Your Tic Tac Toe request for $nick timed out,10) | .hfree ttt }
    elseif ($2 == move) drawsign $3-4
    elseif ($2 == end && !$chkwin && $hget(ttt)) { drawtext @ttt 11 arial 40 70 120 You Win }
  }
  halt
}
alias ttt {
  if ($hget(ttt,ingame)) { echo -a Your already playing a game of Tic Tac Toe | halt }
  elseif ($hget(ttt)) .hfree ttt
  var %a = $regsubex(wins loses ties moves,/(.+?)\b/g,$readini(ttt.ini,ttt,\t) $chr(32))
  .notice $1 ttt request %a
  .hadd -m ttt p2 $1
}
alias ttt.start {
  if ($hget(ttt,ingame)) halt
  elseif ($1) { .hadd -m ttt p2 $1 | .notice $1 ttt accepted }
  .timerTTT off
  .hadd -m ttt player $iif($1,1,0)
  .hadd ttt p1 $iif($1,1,0)
  .hadd ttt sign $iif($1,x,o)
  .hadd ttt ingame 1
  window -pdk0o @ttt -1 -1 318 337
  dboard
}
alias -l dboard {
  var %a = 1
  while (%a <= 2) {
    drawline @ttt 0 5 0 $calc(%a *100) 300 $calc(%a *100)
    drawline @ttt 0 5 $calc(%a *100) 300 $calc(%a *100) 0
    inc %a
  }
  drawline @ttt 0 5 0 0 300 0 300 300 0 300 0 0 | %a = 9
  while (%a) {
    hadd ttt %a 0
    dec %a
  }
}
alias -l getgrid {
  var %x = $iif($len($1) < 3,1,$calc($left($1,1) +1)),%y = $iif($len($2) < 3,1,$calc($left($2,1) +1))
  return $calc(%x + %y $iif(%y == 1,-1,$iif(%y == 2,+1,+3)))
}
alias -l drawsign {
  if ($hget(ttt,$2)) halt
  elseif ($1 == X) {
    var %a = $iif($2 isin 147,100,$iif($2 isin 258,200,300)),%b = $iif($2 isin 123,0,$iif($2 isin 456,100,200)),%c = $calc(%a -100),%d = $calc(%b +100)
    drawline @ttt 0 5 %a %b %c %d %c %b %a %d
  }
  elseif ($1 == O) {
    var %a = $iif($2 isin 147,50,$iif($2 isin 258,150,250)),%b = $iif($2 isin 123,50,$iif($2 isin 456,150,250))
    drawdot @ttt 0 40 %a %b | drawdot @ttt 1 35 %a %b
  }
  hadd ttt $2 $1
  var %nplayer = $iif($hget(ttt,player),0,1),%nsign = $iif($1 == x,o,x)
  hadd ttt player %nplayer
  hadd ttt sign %nsign
  if ($chkwin == 1) { 
    drawtext @ttt 11 arial 40 90 120 $hget(ttt,winner) won! 
    var %a = $iif($hget(ttt,sign) == o && $hget(ttt,p1),loses,wins)
    writeini ttt.ini ttt %a $calc($readini(ttt.ini,ttt,%a) +1)
  }
  elseif ($chkwin == 2) { drawtext @ttt 11 arial 50 95 120 TIE! | writeini ttt.ini ttt ties $calc($readini(ttt.ini,ttt,ties) +1) }
  writeini ttt.ini ttt moves $calc($readini(ttt.ini,ttt,moves) +1)
}
alias -l chkwin {
  var %a = 123 456 789 147 258 369 159 357,%b = 1,%board = $regsubex(123456789,/\d/g,$hget(ttt,\n) $chr(32))
  while ($gettok(%a,%b,32)) {
    if ($regex($regsubex($v1,/(.)/g,$hget(ttt,\t)),/(x|o)(?(1)\1{2}|)/)) { var %iswin = 1 | .hadd ttt winner $regml(1) | break }
    inc %b
  }
  if (!%iswin && 0 !isin %board) { var %iswin = 2 }
  return $iif(%iswin,$v1,0)
}
menu channel {
  Tic Tac Toe
  .$style($iif($status == connected && $readini(ttt.ini,ttt,power),0,2)) Challenge a friend:ttt $$?="Enter the Nickname of the person you want to Challenge."
  .Power
  ..$style($iif($readini(ttt.ini,ttt,power),1,0)) On:writeini ttt.ini ttt power 1
  ..$style($iif($readini(ttt.ini,ttt,power),0,1)) Off:writeini ttt.ini ttt power 0
}
menu @ttt {
  sclick: {
    if ($hget(ttt,player) && !$hget(ttt,winner)) {
      var %a = $hget(ttt,sign),%b = $getgrid($mouse.x,$mouse.y)
      drawsign %a %b
      .notice $hget(ttt,p2) ttt MOVE %a %b
    }
  }
}
on *:close:@ttt:if (!$hget(ttt,winner) && $hget(ttt,p2)) { .notice $hget(ttt,p2) ttt end | .writeini ttt.ini ttt loses $calc($readini(ttt.ini,ttt,loses) +1) } | .hfree ttt

Comments

Sign in to comment.
Meow   -  Apr 24, 2010

This is pretty chill. :D

 Respond  
sunslayer   -  Jan 05, 2010

thanks Jethro_
fixed a few bugs and the problem with the winner/loser counter

 Respond  
Jethro   -  Jan 05, 2010

yeah STFU is what sunslayer represents in his avatar. lol By the way, nice work, Sunslayer.

 Respond  
DarkCoder   -  Jan 05, 2010

nou, chu is kirby :) you tell me to stfu. XD, accept my challange :<, my score pwnz0rs.

 Respond  
sunslayer   -  Jan 04, 2010

lol, im sunslayer not kirby. im usually on irc.swiftirc.net

 Respond  
DarkCoder   -  Jan 04, 2010

I cant find anyone to play with :<, were can i find you kirby?

 Respond  
God425   -  Jan 04, 2010

oh kool and i was trying o say /ttt

 Respond  
sunslayer   -  Jan 03, 2010

its /ttt not /tt or, you can just right click in a channel and click 'challenge and friend'

if that still doesn't work type

/writeini ttt.ini ttt power 1
 Respond  
God425   -  Jan 03, 2010

lol but when i use it cant cant play
i type /tt nothing happen

 Respond  
sunslayer   -  Jan 03, 2010

added a tracker for wins/loses/ties/total moves
and also added a power option

 Respond  
sunslayer   -  Jan 03, 2010

thanks :p

 Respond  
God425   -  Jan 03, 2010

nice

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.