alias tictactoe dialog $iif($dialog(TicTac),-v,-md) TicTac TicTac
dialog TicTac {
  title "Two Player Tic Tac Toe"
  size -1 -1 120 110
  option dbu
  button "", 1, 29 25 20 20
  button "", 2, 50 25 20 20
  button "", 3, 71 25 20 20
  button "", 4, 29 46 20 20
  button "", 5, 50 46 20 20
  button "", 6, 71 46 20 20
  button "", 7, 29 67 20 20
  button "", 8, 50 67 20 20
  button "", 9, 71 67 20 20
  text "Tic Tac Toe by Olliegod", 10, 32 1 56 8, disable
  text "", 15, 2 10 116 14, center multi
  text "", 16, 2 90 116 14, center multi
  button "", 17, 1 1 1 1, hide ok
  menu "Game", 11
  item "New game", 12, 11
  item "End Game", 13, 11
  item "Reset", 14, 11
  menu "Help", 18
  item "Help", 19, 18
}
dialog TicTacHelp {
  title "Tic Tac Toe Help"
  size -1 -1 110 105
  option dbu
  text "To begin a new game, make sure the other player has this script and then select New Game from the Game menu. After you have started a game, to make a move you simply click the button of the box you want you make your move in. To end the game select End Game from the Games menu. After the game has ended, press Reset to play another round, or press End game to stop playing. For more help/support, contact either Poop or Buttsecks in #olliegod on irc.swiftirc.net.", 1, 4 8 104 78
  box "Tic Tac Toe Help", 2, 1 1 108 89
  button "Close", 3, 40 91 30 12, ok
}
dialog TicTacAcceptDecline {
  title "I like pie"
  size -1 -1 90 65
  option dbu
  button "Accept", 1, 13 51 30 12, ok
  button "Decline", 2, 47 51 30 12, cancel
  text "", 3, 1 1 86 47, multi
}
on *:DIALOG:TicTacAcceptDecline:sclick:1: {
  if (requesting isin $did($dname,3)) {
    set %ttt.nick $gettok($did($dname,3),1,32)
    set %ttt.mynick $me
    set %ttt.squares -10 -10 -10 -10 -10 -10 -10 -10 -10
    set %ttt.turn 2
    set %ttt.firstmove 1
    .ctcp %ttt.nick tictactoe acceptgame
    dialog -x TicTacAcceptDecline TicTacAcceptDecline
    dialog $iif($dialog(TicTac),-v,-md) TicTac TicTac
    did -ra TicTac 15 Current player: %ttt.nick
    did -ra TicTac 16 Waiting for %ttt.nick to make their move...
  }
  elseif (Are you sure you want isin $did($dname,3)) {
    .ctcp %ttt.nick tictactoe endgame 
    unset %ttt.*
    dialog -x TicTac TicTac
  }
}
on *:DIALOG:TicTacAcceptDecline:sclick:2: if (requesting isin $did($dname,3)) .ctcp $gettok($did($dname,3),1,32) tictactoe declinegame
on *:DIALOG:TicTac:menu:12: { .ctcp $$?="Enter nick of player to invite here $crlf $+ Note: Other player must have this script also" tictactoe newgame | did -ra $dname 15 Awaiting response from $! }
on *:DIALOG:TicTac:menu:13: if (%ttt.nick) { dialog $iif($dialog(TicTacAcceptDecline),-v,-md) TicTacAcceptDecline TicTacAcceptDecline | did -ra TicTacAcceptDecline 3 Are you sure you want to end the current game with %ttt.nick $+ ? | did -ra TicTacAcceptDecline 1 Yes | did -ra TicTacAcceptDecline 2 No }
on *:DIALOG:TicTac:menu:14: {
  if (%ttt.nick) {
    inc %ttt.firstmove
    did -re TicTac 1-9
    did -ra TicTac 15 Current player: %ttt.nick
    did -ra TicTac 16 $iif(2 // %ttt.firstmove,It is your turn to make a move.,Waiting for %ttt.nick to make their move...)
    set %ttt.moves 0
    set %ttt.squares -10 -10 -10 -10 -10 -10 -10 -10 -10
    set %ttt.turn $iif(2 // %ttt.firstmove,1,2)
    .ctcp %ttt.nick tictactoe reset
  }
}
on *:DIALOG:TicTac:menu:19: dialog $iif($dialog(TicTacHelp),-v,-m) TicTacHelp TicTacHelp
on *:DIALOG:TicTac:close:0: if (%ttt.nick) { .ctcp %ttt.nick tictactoe endgame | unset %ttt.* }
on *:DIALOG:TicTac:sclick:*: {
  if ($did isnum 1-9) && (%ttt.nick) && (%ttt.turn == 1) {
    set %ttt.squares $puttok(%ttt.squares,1,$did,32)
    set %ttt.turn 2
    inc %ttt.moves 
    did -rab $dname $did X
    did -ra TicTac 15 Current player: %ttt.nick
    did -ra $dname 16 Waiting for %ttt.nick to make their move...
    if (%ttt.moves >= 5) tictac.checkwinner
    .ctcp %ttt.nick tictactoe move $did
  }
}
on *:NICK: if ($nick == %ttt.mynick) { .ctcp %ttt.nick tictactoe newnick $nick | set %ttt.mynick $newnick }
ctcp *:tictactoe*:?: {
  if ($2 == newgame) {
    if (!%ttt.nick) {
      dialog $iif($dialog(TicTacAcceptDecline),-v,-md) TicTacAcceptDecline TicTacAcceptDecline
      did -ra TicTacAcceptDecline 3 $nick is requesting a game of Tic Tac Toe with you. Do you accept or decline?
    }
    else .ctcp $nick tictactoe declinegame alreadyplaying
    haltdef
  }
  elseif ($2 == acceptgame) && ($dialog(TicTac)) && ($nick == $gettok($did(TicTac,15),4,32)) {
    set %ttt.nick $nick
    set %ttt.mynick $me
    set %ttt.turn 1
    set %ttt.squares -10 -10 -10 -10 -10 -10 -10 -10 -10
    set %ttt.firstmove 2
    did -ra TicTac 15 Current player: %ttt.nick
    did -ra TicTac 16 It is your turn to make a move.
    haltdef
  }
  elseif ($2 == declinegame) && ($dialog(TicTac)) && ($nick == $gettok($did(TicTac,15),4,32)) {
    if ($3 == alreadyplaying) did -ra TicTac 15 $nick is already playing a game of Tic Tac Toe.
    else did -ra TicTac 15 $nick has declined your request to play Tic Tac Toe.
    haltdef
  }
  elseif ($2 == move) && ($dialog(TicTac)) && ($nick == %ttt.nick) {
    set %ttt.squares $puttok(%ttt.squares,2,$3,32)
    set %ttt.turn 1
    inc %ttt.moves
    did -rab TicTac $3 O
    did -ra TicTac 15 Current player: %ttt.nick
    did -ra TicTac 16 It is your turn to make a move.
    if (%ttt.moves >= 5) tictac.checkwinner
    haltdef
  }
  elseif ($2 == endgame) && ($dialog(TicTac)) && ($nick == %ttt.nick) {
    did -re TicTac 1-9,16
    did -ra TicTac 15 %ttt.nick has ended the current game of Tic Tac Toe
    unset %ttt.*
    haltdef
  }
  elseif ($2 == reset) && ($dialog(TicTac)) && ($nick == %ttt.nick) {
    inc %ttt.firstmove
    did -re TicTac 1-9
    did -ra TicTac 15 %ttt.nick has reset the game
    did -ra TicTac 16 $iif(2 // %ttt.firstmove,It is your turn to make a move.,Waiting for %ttt.nick to make their move...)
    set %ttt.moves 0
    set %ttt.squares -10 -10 -10 -10 -10 -10 -10 -10 -10
    set %ttt.turn $iif(2 // %ttt.firstmove,1,2)
    haltdef
  }
  elseif ($2 == newnick) && ($dialog(TicTac)) && ($3 == %ttt.nick) { set %ttt.nick $nick | haltdef }
}
alias tictac.addsquares return $calc($gettok(%ttt.squares,$1,32) + $gettok(%ttt.squares,$2,32) + $gettok(%ttt.squares,$3,32))
alias tictac.checkwinner {
  var %possiblewins = 1.2.3 4.5.6 7.8.9 1.4.7 2.5.8 3.6.9 1.5.9 3.5.7, %x = 1
  while (%x <= 8) {
    tokenize 46 $gettok(%possiblewins,%x,32)
    if (3 // $tictac.addsquares($1,$2,$3)) && ($tictac.addsquares($1,$2,$3) > 0) {
      did -ra TicTac 16 $iif($tictac.addsquares($1,$2,$3) == 3,You win!,You lose!) Press Reset to play again!
      set %ttt.turn 0
      return
    }
    inc %x
  }
  if (-10 !isin %ttt.squares) did -ra TicTac 16 Its a draw! Press Reset to play again!
}
