Snow_Flake

ahk chess

Sep 18th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global sBoard,pBoard
  2. yLoc := 0, cell := 30, sFEN := "rnbqkbnr/pppppppp/eeeeeeee/eeeeeeee/eeeeeeee/eeeeeeee/PPPPPPPP/RNBQKBNR"
  3. stringsplit, pFenY, sFEN,`/
  4. gui,font, % "s" cell/26, Lucida Console
  5. loop, 8 {
  6.     stringSplit, pFenX, pFenY%A_Index%
  7.     yIndex := A_Index, xLoc := 0
  8.     loop, 8 {
  9.         sBoard .= pFenX%A_Index%
  10.         name := yIndex A_Index
  11.         gui,add,text, % ((mod(A_Index+yIndex,2) == 0) ? "0x4" : "0x5") " x" xLoc " y" yLoc " w" cell " h" cell
  12.         gui,font, % (regexMatch(GetItem(yIndex,A_Index),"[PRNBQKBNR]")) ? "cwhite" : "cblack"
  13.         gui,add,text, x%xLoc% y%yLoc% w%cell% h%cell% +center +BackgroundTrans v%name% gAction, % GetAscii(GetItem(yIndex,A_Index))
  14.         xLoc += cell
  15.     }
  16.     yLoc += cell
  17. }
  18. pBoard := sBoard
  19. active := (active == "W") ? "b" : "W"
  20. gui, show, w%xLoc% h%yLoc%, Chess
  21. return
  22.  
  23. Action:
  24. y := substr(A_GuiControl,1,1), x := substr(A_GuiControl,2,1)
  25. if (selected == "" && GetItem(y,x) != "e" && (regexMatch(GetItem(y,x),"[PRNBQKBNR]") == regexMatch(active,"[W]"))) {
  26.     gui, font, cYellow
  27.     GuiControl, font, %A_GuiControl%
  28.     selected := A_GuiControl
  29. }
  30. else if (selected != "") {
  31.     if (CheckLegal(selected,A_GuiControl) && selected != A_GuiControl) {
  32.         SetItem(GetItem(substr(selected,1,1),substr(selected,2,1)),y,x)
  33.         SetItem("e",substr(selected,1,1),substr(selected,2,1))
  34.         active := (active == "W") ? "b" : "W"
  35.     }
  36.     UpdateBoard(selected)
  37.     selected := ""
  38.    
  39.     pBoard := sBoard
  40. }
  41. return
  42.  
  43. SetItem(i,y,x) {
  44.     loop,parse,sBoard
  45.     {
  46.         if (A_Index == ((y-1)*8)+x)
  47.             ret .= i
  48.         else
  49.             ret .= A_LoopField
  50.     }
  51.     sBoard := ret
  52. }
  53.  
  54. GetItem(y,x,b = 0) {
  55.     return % substr((b == 0) ? sBoard : pBoard,((y-1)*8)+x,1)
  56. }
  57.  
  58. UpdateBoard(selected) {
  59.     loop, 8 {
  60.         yIndex := A_Index
  61.         loop, 8 {
  62.             name := yIndex A_Index, kingCheck .= (GetItem(yIndex,A_Index) == "k" || GetItem(yIndex,A_Index) == "K") ? GetItem(yIndex,A_Index) : ""
  63.             if ((GetItem(yIndex,A_Index) != GetItem(yIndex,A_Index,1)) || (regexMatch(GetItem(yIndex,A_Index),"[PRNBQKBNR]") != regexmatch(GetItem(yIndex,A_Index,1),"[PRNBQKBNR]")) || (yIndex A_Index == selected)) {
  64.                 gui, font, % (regexMatch(GetItem(yIndex,A_Index),"[PRNBQKBNR]")) ? "cwhite" : "cblack"
  65.                 GuiControl, Font, %name%
  66.                 Guicontrol,,%name%, % GetAscii(GetItem(yIndex,A_Index))
  67.             }
  68.         }
  69.     }
  70.     if (kingCheck == "K" || kingCheck == "k")
  71.         MsgBox % ((kingCheck == "K") ? "Black" : "White") " king died!`nWell played " ((kingCheck == "K") ? "white!" : "black!")
  72. }
  73.  
  74. CheckLegal(i1,i2) {
  75.     stringsplit,a,i1
  76.     stringsplit,b,i2
  77.     y := b1 - a1, x := b2 - a2
  78.     piece := GetItem(a1,a2)
  79.     col := regexMatch(piece,"[PRNBQKBNR]")
  80.     StringUpper,piece,piece
  81.     if (regexMatch(piece,"[PRBQ]")) {
  82.         h := (abs(x) > abs(y)) ? abs(x) : abs(y)
  83.         loop % h
  84.             if (!Step(col,piece,(x > 0) ? x/h : (x < 0) ? x/h : 0,(y > 0) ? y/h : (y < 0) ? y/h : 0,i1,h))
  85.                 illegal := true
  86.         return % (illegal) ? false : true
  87.     }
  88.     else
  89.         return % Step(col,piece,x,y,i1)
  90. }
  91.  
  92. Step(col,piece,x,y,i1,cycle = 1){
  93.     x := round(x-mod(x,1)), y := round(y-mod(y,1))
  94.     coll := CollisionCheck(i1,substr(i1,1,1)+y substr(i1,2,1)+x)
  95.     legal := (!col && piece == "P" && ((substr(i1,1,1) == 2) ? (y*cycle == 2 || y*cycle == 1) : y*cycle == 1) && (((coll == "foe") && (x == -1 || x == 1)) || ((coll == "e") && (x == 0)))) ? true
  96.            : (col && piece == "P" && ((substr(i1,1,1) == 7) ? (y*cycle == -2 || y*cycle == -1) : y*cycle == -1) && (((coll == "foe") && (x == -1 || x == 1)) || ((coll == "e") && (x == 0)))) ? true
  97.            : (piece == "R" && (y == 0 || x == 0)) ? true
  98.            : (piece == "N" && ((abs(y) == 1 && abs(x) == abs(y)+1) || (abs(x) == 1 && abs(y) == abs(x)+1))) ? true
  99.            : (piece == "B" && (abs(y) == abs(x))) ? true
  100.            : (piece == "Q" && ((y == 0 || x == 0) || (abs(y) == abs(x)))) ? true
  101.            : (piece == "K" && (abs(y) <= 1 && abs(x) <= 1)) ? true : false
  102.     return (legal && (coll == "foe" || coll == "e")) ? true : false
  103. }
  104.  
  105. CollisionCheck(i1,i2) {
  106.     stringsplit,a,i1
  107.     stringsplit,b,i2
  108.     return % (GetItem(b1,b2) == "e") ? "e" : (regexMatch(GetItem(a1,a2),"[PRNBQKBNR]") == regexMatch(GetItem(b1,b2),"[PRNBQKBNR]")) ? "friend" : "foe"
  109. }
  110.  
  111. GetAscii(str) {
  112.     StringUpper,str,str
  113.     return % (str == "P") ? "`n`n`n`n`n___`n/   \`n\___/`n/____\`n|  |`n|__|`n(====)`n}===={`n(______)`n"
  114.            : (str == "R") ? "`n`n`n`n`n|'-'-'|`n|_____|`n|   |`n|   |`n|   |`n)___(`n(     )`n}     {`n(_______)`n"
  115.            : (str == "N") ? "`n`n`n`n(\=,`n//  .\`n(( \_  \`n))    `\_)`n(/    \`n|  _.-'|`n)_____(`n(=====)`n}====={`n(_______)`n"
  116.            : (str == "B") ? "`n`n`n()`n/\`n//\\`n(    )`n)__(`n/____\`n|  |`n|  |`n/____\`n(======)`n}======{`n(________)`n"
  117.            : (str == "Q") ? "`n`n()`n.-:--:-.`n\____/`n{====}`n)__(`n/____\`n|  |`n|  |`n|  |`n|  |`n/____\`n(======)`n}======{`n(________)`n"
  118.            : (str == "K") ? "`n_:_`n'-.-'`n__.'.__`n|_______|`n\=====/`n)___(`n/_____\`n|   |`n|   |`n|   |`n|   |`n|   |`n/_____\`n(=======)`n}======={`n(_________)`n" : ""
  119. }
  120.  
  121. GuiEscape:
  122. GuiClose:
  123. Exitapp
Advertisement
Add Comment
Please, Sign In to add comment