;---------------------------------------
; Tick Tack Toe mIRC Script
; use /ttt to star
; QuickStep © mknobbout@gmail.com
;---------------------------------------
alias ttt {
if (!$window($ww)) .window -pCBdok +t $ww 0 0 305 330
.clear $ww
if ($hget(ttt)) hfree ttt
hmake ttt 100
; Default settings, do not change this
hadd ttt user_turn 1
hadd ttt moves 0
hadd ttt user_shape 1
; Decide who's turn it is
if (!%yourturn) {
echo -s Computer's turn to start
set %yourturn 1
hadd ttt user_turn 1
computer_AI 0 0
}
else {
echo -s Your turn to start
unset %yourturn
}
drawfield
}
menu @Tick* {
sclick:{
if ($hget(ttt,user_turn)) {
var %x = $int($calc(($mouse.x / 100) + 1)), %y = $int($calc(($mouse.y / 100) + 1))
if (!$getmove(%x,%y)) {
drawmove %x %y $hget(ttt,user_shape)
hadd ttt user_turn 0
;User made a move, now its time for the computer
computer_AI %x %y
}
}
}
Restart:{ .hfree ttt | ttt }
Exit:{ .hfree ttt | window -c $ww }
-
Copyright:echo -s QuickStep © mknobbout@gmail.com
}
alias endgame {
echo -s $iif($$1 = $hget(ttt,user_shape), You, $iif($$1 = 0, Nobody, The Computer))) = winner!
halt
}
;-- LOCAL FUNCTIONS --
alias -l computer_AI {
; The computer must now check what the best move is (users last move is $1,$2 , this is not used in this event atm though)
;-------------------------------------------------
; NOTE: THE AI CAN BE EASILY IMPROVED/CHANGED HERE
;-------------------------------------------------
var %shape = $iif($hget(ttt,user_shape) = 1,2,1)
; This smalll block of code will do the following: first check if computer can make a win,
; then check if a user can make a win, and make the propper (counter) move
var %d = 2
while (%d >= 1) {
var %a = 1
while (%a <= 8) {
if (($countmove(%a,%d) = 2) && ($countmove(%a,$iif(%d = 2,1,2)) != 1)) {
var %b = $suggestmove(%a), %x = $gettok(%b,1,46), %y = $gettok(%b,2,46)
drawmove %x %y %shape
goto end
}
inc %a
}
dec %d
}
; The following small block of code will do the following: check if the computer can make an offensive move
var %a = 1
while (%a <= 8) {
if (($countmove(%a,2) = 1) && ($countmove(%a,1) = $null)) {
var %b = $suggestmove(%a), %x = $gettok(%b,1,46), %y = $gettok(%b,2,46)
drawmove %x %y %shape
goto end
}
inc %a
}
;Make a random move finally, if nothing worked, the use of goto because there is no DO ... WHILE loop in mIRC
:again
var %x = $rand(1,3), %y = $rand(1,3)
if ($getmove(%x,%y)) goto again
drawmove %x %y %shape
:end | hadd ttt user_turn 1
}
alias -l drawfield {
drawline $ww 1 1 0 100 300 100
drawline $ww 1 1 0 200 300 200
drawline $ww 1 1 100 0 100 300
drawline $ww 1 1 200 0 200 300
}
alias -l drawmove {
var %w = $calc(($1 - 1) * 100), %h = $calc(($2 - 1) * 100), %type = $$3
if (%type = 1) {
; Draw a square
drawrect $ww 2 2 $calc(10 + %w) $calc(10 + %h) 80 80
}
if (%type = 2) {
; Draw a triangle
drawline $ww 4 2 $calc(10 + %w) $calc(90 + %h) $calc(50 + %w) $calc(10 + %h) $&
$calc(90 + %w) $calc(90 + %h) $calc(10 + %w) $calc(90 + %h)
}
; Toevoegen aan hash
hadd ttt $+($1,$chr(46),$2) $3
; Check if there is a winner
var %a = 1
while (%a <= 8) {
if (($countmove(%a,1) = 3) || ($countmove(%a,2) = 3)) {
; And we have a winner
hadd ttt user_turn 0
var %x = $gettok($gettok($getline(%a),1,59),1,58), %y = $gettok($gettok($getline(%a),3,59),1,58)
drawline $ww 1 5 $calc(($gettok(%x,1,46) * 100) - 50) $calc(($gettok(%x,2,46) * 100) - 50) $&
$calc(($gettok(%y,1,46) * 100) - 50) $calc(($gettok(%y,2,46) * 100) - 50)
endgame $iif($countmove(%a,1) = 3, 1, 2)
}
inc %a
}
hinc ttt moves | if ($hget(ttt,moves) >= 9) endgame 0
}
alias -l getline {
; Returns line info in the following format: "x.y:input;x.y:input;x.y:input" (because mIRC does not support arrays)
if ($$1 <= 3) { var %x = 1, %y = $$1, %c1 = 1, %c2 = 0 }
elseif ($1 <= 6) { var %x = $iif(!$calc($$1 % 3), 3, $calc($$1 % 3)), %y = 1, %c1 = 0, %c2 = 1 }
elseif ($1 == 7) { var %x = 1, %y = 1, %c1 = 1, %c2 = 1 }
elseif ($1 == 8) { var %x = 1, %y = 3, %c1 = 1, %c2 = 2 }
var %a = 1, %b
while (%a <= 3) {
%b = $+(%b,%x,$chr(46),%y,$chr(58),$hget(ttt,$+(%x,$chr(46),%y)),$chr(59))
if (%c1) $iif(%c1 = 1,inc %x, dec %x) | if (%c2) $iif(%c2 = 1,inc %y, dec %y)
inc %a
}
return %b
}
alias -l getmove return $hget(ttt,$+($1,$chr(46),$$2))
alias countmove {
; This functions counts the instances of a move from the given line
var %a = $getline($$1), %b = 1, %c
while (%b <= 3) {
if ($gettok($gettok(%a,%b,59),2,58) = $$2) inc %c
inc %b
}
return %c
}
alias suggestmove {
; This function returns a suggested random move from the given line, in the format x.y
var %a = $getline($$1), %b = 1, %c, %d
while (%b <= 3) {
%d = $gettok($gettok(%a,%b,59),2,58)
if (!%d) %c = $+(%c,$chr(59),$gettok($gettok(%a,%b,59),1,58))
inc %b
}
return $gettok(%c,$rand(1,$numtok(%c,59)),59)
}
alias -l ww return $replace(@Tick Tack Toe,$chr(32),$chr(160))
;---------------------