Kakuro 0.1

By Firstmate on Jan 28, 2009

Kakuro v0.1

Guess I just like making puzzles, but heres Kakuro.
Like my previous script, Sudoku, this uses a socket to retrieve the puzzle from kakuro.cc

To start kakuro, load the script and type /kakuro or alternatively right click > kakuro
It'll start a window like:

Image

The numbers with the grayish background are given numbers and are un-editable.
For those who have never heard of Kakuro, I'll give my explanation, but another source is:
http://en.wikipedia.org/wiki/Kakuro

To play kakuro, you must make the numbers in a row/column add up to the given number.
For example, in the picture posted above, if you look on the second row, there's a 3 (note the position of the 3) and 2 white boxes next to it. It means, that those two white boxes must equal 3. Same goes for downwards, upwards, and across.

^ Dunno if that helps.

changelog
+: Added feature
-: Removed feature
X: Fixed glitch/bug

v0.1
Released Kakuro

;Kakuro 0.1 made by Firstmate on 01/28/09
;To load: 
;Go to scripts editor: File > new > paste script

alias kakuro {
  window -pdoCk0 +l @kakuro 1 1 300 400
  k.drawlines
  set %p 0
  set %k.level 1
}
menu status,channel,query {
  Kakuro:kakuro
}
on *:keydown:@kakuro:*: {
  if ($keychar isnum && $keychar !== 0 && %k.pos && $inrect($gettok(%k.mouse,1,32),$gettok(%k.mouse,2,32),5,5,285,285)) {
    var %row $gettok(%k.pos,1,32)
    var %col $gettok(%k.pos,2,32)
    drawrect -fr @kakuro $rgb(255,255,255) 1 $calc(((%row - 1) * 40) + 6) $calc(((%col - 1) * 40) + 6) 38 38
    drawtext -r @kakuro $rgb(0,0,0) Tahoma 15 $calc(((%row - 1) * 40) + 20) $calc(((%col - 1) * 40) + 20) $keychar
    k.replace %row %col $keychar
  }
}
menu @kakuro {
  sclick: { 
    if ($inrect($mouse.x,$mouse.y,5,5,285,285) && $getdot(@kakuro,$mouse.x,$mouse.y) == 16777215) {
      set %k.pos $k.checkpos($mouse.x,$mouse.y)
      set %k.mouse $mouse.x $mouse.y
    }
    if ($inrect($mouse.x,$mouse.y,5,330,80,20)) { k.win }
    if ($inrect($mouse.x,$mouse.y,5,300,50,20)) { %p = 0 | k.drawlines }
  }
}
alias k.drawlines {
  clear @kakuro
  drawfill -r @Kakuro $rgb(255,255,255) $rgb(255,255,255) 1 1
  drawline -r @kakuro $rgb(135,125,105) 2 5 5 5 285
  drawline -r @kakuro $rgb(135,125,105) 2 5 5 285 5
  drawline -r @kakuro $rgb(135,125,105) 2 285 285 5 285
  drawline -r @kakuro $rgb(135,125,105) 2 285 5 285 285

  drawrect -r @kakuro $rgb(0,0,0) 1 5 300 50 20
  drawtext -r @kakuro $rgb(0,0,0) tahoma 14 10 300 New? 

  drawrect -r @kakuro $rgb(0,0,0) 1 5 330 80 20
  drawtext -r @kakuro $rgb(0,0,0) tahoma 14 10 330 Did I Win?

  var %x = 7
  while (%x > 0) {
    drawline -r @kakuro $rgb(0,0,0) 1 $calc((%x * 40) + 5) 5 $calc((%x * 40) + 5) 285
    dec %x
  }
  var %y = 7
  while (%y > 0) {
    drawline -r @kakuro $rgb(0,0,0) 1 5 $calc((%y * 40) + 5) 285 $calc((%y * 40) + 5)
    dec %y
  }
  sockopen kakuro kakuro.cc 80
}
alias k.getid {
  var %x = $1, %i = 7, %p
  while (%i > 0) {
    if (%x <= $calc(%i * 7)) {
      %p = $calc(7 - ((%i * 7) - %x)) %i
    }
    dec %i
  }
  return %p
}
alias k.checkpos {
  var %x $1
  var %y $2
  var %col,%row
  var %i = 1, %j = 1
  while (%i < 7) {
    if (%x < $calc((%i * 40) + 5)) { %col = %i | break }
    inc %i
  }
  while (%j < 7) {
    if (%y < $calc((%j * 40) + 5)) { %row = %j | break }
    inc %j
  }
  return %i %j
}
alias k.replace {
  var %row $1, %col $2, %k $3
  if ($read(kakuro.txt,s,$+(%row,.,%col))) { write -ds $+(%row,.,%col) kakuro.txt $+(%row,.,%col) $keychar }
  else { write kakuro.txt $+(%row,$chr(32),%col) %k }
}
alias k.win {
  if ($lines(kakuro.txt) !== 20) { drawtext -r @kakuro $rgb(0,0,0) tahoma 14 100 330 Fill it all out! }
  else { sockopen kakuro_sol kakuro.cc 80 }
}
on *:SOCKOPEN:kakuro: {
  var %date = $replace($asctime($rand($ctime(March 01 2006),$ctime),d m yyyy),$chr(32),-)
  set %k.get $+($replace(%k.level,1,E,2,M,3,H),%date,-kakuro.aspx)
  drawrect -fr @kakuro $rgb(255,255,255) 1 175 355 200 200
  drawtext -r @kakuro $rgb(0,0,0) tahoma 14 175 355 $+($replace(%k.level,1,Easy,2,Medium,3,Hard),$chr(32),$replace(%date,$chr(32),-))
  sockwrite -nt $sockname GET $+(/,%k.get) HTTP/1.1
  sockwrite -nt $sockname Host: kakuro.cc
  sockwrite -nt $sockname $crlf
}
on *:SOCKREad:kakuro: {
  var %x, %id, %tn, %tb, %row, %col
  ;%p is to count which cell they're on
  ;%t # of input cells
  ;%id = X Y for kakuro window
  sockread %x
  if (<td class='black' isin %x) {
    ;not a given or input cell
    inc %p
    %id = $k.getid(%p)
    %row = $gettok(%id,1,32)
    %col = $gettok(%id,2,32)
    drawrect -fr @kakuro $rgb(135,125,105) 1 $calc(((%row - 1) * 40) + 6) $calc(((%col - 1) * 40) + 6) 39 39
  }
  if (<td class='white' isin %x) {
    ;input cell
    inc %p
    %id = $k.getid(%p)
    %row = $gettok(%id,1,32)
    %col = $gettok(%id,2,32)
    inc %k.num
  }
  if (<td class='infocell' isin %x) {
    ;given cell
    inc %p
    %x = $remove(%x,&)
    %id = $k.getid(%p)
    %row = $gettok(%id,1,32)
    %col = $gettok(%id,2,32)
    %tn = $remove($iif($left(%x,4) == <tr>,$gettok($gettok(%x,8,62),1,60),$gettok($gettok(%x,7,62),1,60)),nbsp;,/td,td)
    %tb = $remove($iif($left(%x,4) == <tr>,$gettok($gettok(%x,12,62),1,60),$gettok($gettok(%x,11,62),1,60)),nbsp;,td,/td)
    drawrect -fr @kakuro $rgb(135,125,105) 1 $calc(((%row - 1) * 40) + 6) $calc(((%col - 1) * 40) + 6) 39 39
    drawline -r @kakuro $rgb(0,0,0) 1 $calc(((%row - 1) * 40) + 5) $calc(((%col - 1) * 40) + 5) $calc((%row * 40) + 5) $calc((%col * 40) + 5)
    if (%tn) { drawtext -r @kakuro $rgb(0,0,0) Tahoma 12 $calc(((%row - 1) * 40) + 30) $calc(((%col - 1) * 40) + 10) %tn }
    if (%tb) { drawtext -r @kakuro $rgb(0,0,0) Tahoma 12 $calc(((%row - 1) * 40) + 10) $calc(((%col - 1) * 40) + 30) %tb }
  }
  if (</HTML> isin %x) { sockclose kakuro }
}

on *:sockopen:kakuro_sol: {
  var %get $remove(%k.get,-kakuro.aspx)
  sockwrite -nt $sockname GET $+(/printerfriendly.aspx?Solution=Yes&,%get) HTTP/1.1
  sockwrite -nt $sockname Host: kakuro.cc
  sockwrite -nt $sockname $crlf
}
on *:sockread:kakuro_sol: {
  var %x, %solved, %row, %col, %i, %t
  sockread %x
  %x = $remove(%x,$chr(40),$chr(41))
  if (var iGridSolved isin %x) { 
    %x = $remove(%x,var iGridSolved= new Array,;)
    sockclose kakuro_sol
    %i = 49
    while (%i > 0) {
      if ($gettok(%x,%i,44) == -1) { }
      else {
        %t = $k.getid(%i)
        %row = $gettok(%t,1,32) | %col = $gettok(%t,2,32)
        if ($read(kakuro.txt,s,$+(%row,.,%col)) == $gettok(%x,%i,44)) { %t = 1 }
        else { %t = 0 | break }
      }
      dec %i
    }
    if (%t == 1) { drawtext -r @kakuro $rgb(0,0,0) tahoma 14 100 330 Good job! You win! }
    if (%t == 0) { drawtext -r @kakuro $rgb(0,0,0) tahoma 14 100 330 Try Again :( }
  }
  if (</html> isin %x) { sockclose kakuro_sol }
}
on 1:CLOSE:@kakuro: {
  .remove kakuro.txt
  unset %k.num
}

Comments

Sign in to comment.
PuNkTuReD   -  Jan 28, 2009

niice work here Firstmate
8/10

 Respond  
Firstmate   -  Jan 28, 2009

lol Sorry for double-post, but I fixed that bug.

 Respond  
Firstmate   -  Jan 28, 2009

Yeah it's pretty fun once you get the hang of it. I found the first few times playing it frustrating as hell. lol

ugh, I found a bug btw. If you click new, it doesn't actually get a new puzzle D:
I'll fix it right away.

 Respond  
Aucun50   -  Jan 28, 2009

Never seen that game before, looks 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.