Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AHK Chess


  • Please log in to reply
12 replies to this topic
floodindahood
  • Members
  • 383 posts
  • Last active: Apr 20 2018 01:45 PM
  • Joined: 21 Oct 2011

output_Ucl6V1.gif

 

Felt like having some r&r this weekend so figured I'll try making Chess and well here it is. (The ascii chess pieces are from http://ascii.co.uk/art/chess)

It's for 2 players sitting at the same computer so maybe someone will have some nice family/friend moments with it :)

global sBoard,pBoard
yLoc := 0, cell := 30, sFEN := "rnbqkbnr/pppppppp/eeeeeeee/eeeeeeee/eeeeeeee/eeeeeeee/PPPPPPPP/RNBQKBNR"
stringsplit, pFenY, sFEN,`/
gui,font, % "s" cell/26, Lucida Console
loop, 8 {
	stringSplit, pFenX, pFenY%A_Index%
	yIndex := A_Index, xLoc := 0
	loop, 8 {
		sBoard .= pFenX%A_Index%
		name := yIndex A_Index
		gui,add,text, % ((mod(A_Index+yIndex,2) == 0) ? "0x4" : "0x5") " x" xLoc " y" yLoc " w" cell " h" cell
		gui,font, % (regexMatch(GetItem(yIndex,A_Index),"[PRNBQKBNR]")) ? "cwhite" : "cblack"
		gui,add,text, x%xLoc% y%yLoc% w%cell% h%cell% +center +BackgroundTrans v%name% gAction, % GetAscii(GetItem(yIndex,A_Index))
		xLoc += cell
	}
	yLoc += cell
}
pBoard := sBoard
active := (active == "W") ? "b" : "W"
gui, show, w%xLoc% h%yLoc%, Chess
return

Action:
y := substr(A_GuiControl,1,1), x := substr(A_GuiControl,2,1)
if (selected == "" && GetItem(y,x) != "e" && (regexMatch(GetItem(y,x),"[PRNBQKBNR]") == regexMatch(active,"[W]"))) { 
	gui, font, cYellow
	GuiControl, font, %A_GuiControl%
	selected := A_GuiControl
}
else if (selected != "") {
	if (CheckLegal(selected,A_GuiControl) && selected != A_GuiControl) {
		SetItem(GetItem(substr(selected,1,1),substr(selected,2,1)),y,x)
		SetItem("e",substr(selected,1,1),substr(selected,2,1))
		active := (active == "W") ? "b" : "W"
	}
	UpdateBoard(selected)
	selected := ""
	
	pBoard := sBoard
}
return

SetItem(i,y,x) {
	loop,parse,sBoard
	{
		if (A_Index == ((y-1)*8)+x)
			ret .= i
		else
			ret .= A_LoopField
	}
	sBoard := ret
}

GetItem(y,x,b = 0) {
	return % substr((b == 0) ? sBoard : pBoard,((y-1)*8)+x,1)
}

UpdateBoard(selected) {
	loop, 8 {
		yIndex := A_Index
		loop, 8 {
			name := yIndex A_Index, kingCheck .= (GetItem(yIndex,A_Index) == "k" || GetItem(yIndex,A_Index) == "K") ? GetItem(yIndex,A_Index) : ""
			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)) {
				gui, font, % (regexMatch(GetItem(yIndex,A_Index),"[PRNBQKBNR]")) ? "cwhite" : "cblack"
				GuiControl, Font, %name%
				Guicontrol,,%name%, % GetAscii(GetItem(yIndex,A_Index))
			}
		}
	}
	if (kingCheck == "K" || kingCheck == "k")
		MsgBox % ((kingCheck == "K") ? "Black" : "White") " king died!`nWell played " ((kingCheck == "K") ? "white!" : "black!")
}

CheckLegal(i1,i2) {
	stringsplit,a,i1
	stringsplit,b,i2
	y := b1 - a1, x := b2 - a2
	piece := GetItem(a1,a2)
	col := regexMatch(piece,"[PRNBQKBNR]")
	StringUpper,piece,piece
	if (regexMatch(piece,"[PRBQ]")) {
		h := (abs(x) > abs(y)) ? abs(x) : abs(y)
		loop % h
			if (!Step(col,piece,(x > 0) ? x/h : (x < 0) ? x/h : 0,(y > 0) ? y/h : (y < 0) ? y/h : 0,i1,h))
				illegal := true
		return % (illegal) ? false : true
	}
	else
		return % Step(col,piece,x,y,i1)
}

Step(col,piece,x,y,i1,cycle = 1){
	x := round(x-mod(x,1)), y := round(y-mod(y,1))
	coll := CollisionCheck(i1,substr(i1,1,1)+y substr(i1,2,1)+x)
	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
		   : (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
		   : (piece == "R" && (y == 0 || x == 0)) ? true
		   : (piece == "N" && ((abs(y) == 1 && abs(x) == abs(y)+1) || (abs(x) == 1 && abs(y) == abs(x)+1))) ? true
		   : (piece == "B" && (abs(y) == abs(x))) ? true
		   : (piece == "Q" && ((y == 0 || x == 0) || (abs(y) == abs(x)))) ? true
		   : (piece == "K" && (abs(y) <= 1 && abs(x) <= 1)) ? true : false
	return (legal && (coll == "foe" || coll == "e")) ? true : false
}

CollisionCheck(i1,i2) {
	stringsplit,a,i1
	stringsplit,b,i2
	return % (GetItem(b1,b2) == "e") ? "e" : (regexMatch(GetItem(a1,a2),"[PRNBQKBNR]") == regexMatch(GetItem(b1,b2),"[PRNBQKBNR]")) ? "friend" : "foe"
}

GetAscii(str) {
	StringUpper,str,str
	return % (str == "P") ? "`n`n`n`n`n___`n/   \`n\___/`n/____\`n|  |`n|__|`n(====)`n}===={`n(______)`n"
		   : (str == "R") ? "`n`n`n`n`n|'-'-'|`n|_____|`n|   |`n|   |`n|   |`n)___(`n(     )`n}     {`n(_______)`n"
		   : (str == "N") ? "`n`n`n`n(\=,`n//  .\`n(( \_  \`n))    `\_)`n(/    \`n|  _.-'|`n)_____(`n(=====)`n}====={`n(_______)`n"
		   : (str == "B") ? "`n`n`n()`n/\`n//\\`n(    )`n)__(`n/____\`n|  |`n|  |`n/____\`n(======)`n}======{`n(________)`n"
		   : (str == "Q") ? "`n`n()`n.-:--:-.`n\____/`n{====}`n)__(`n/____\`n|  |`n|  |`n|  |`n|  |`n/____\`n(======)`n}======{`n(________)`n"
		   : (str == "K") ? "`n_:_`n'-.-'`n__.'.__`n|_______|`n\=====/`n)___(`n/_____\`n|   |`n|   |`n|   |`n|   |`n|   |`n/_____\`n(=======)`n}======={`n(_________)`n" : ""
}

GuiEscape:
GuiClose:
Exitapp 

To note: there is no castling nor en passant but, otherwise should work perfectly.



Lv2
  • Members
  • 23 posts
  • Last active: Dec 18 2014 05:05 PM
  • Joined: 10 Jun 2014

That's really cool.

Cheers



garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005

very nice floodindahood , didn't realized how to move ( maybe XP netbook problem (?)  )



floodindahood
  • Members
  • 383 posts
  • Last active: Apr 20 2018 01:45 PM
  • Joined: 21 Oct 2011

Thanks :)

 

I noticed there is some version compatibility issues though.

On this computer works fine (1.1.14.02) but, at work for whatever reason failed to function properly so gonna take a look at what's going wrong.



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011

WOW, thanks for posting.

you should feel like having some r&r every weekend.



floodindahood
  • Members
  • 383 posts
  • Last active: Apr 20 2018 01:45 PM
  • Joined: 21 Oct 2011

Updated the first post.

Removed the array and replaced with string to make it more compatible with different ahk versions.

Also made it so that it only updates the changed controls instead of the whole board which caused annoying flickering.

 

I have loads of (crappy) weekend projects, this one just happened to turn out decent :)

 

Btw Garry, to move you click on a piece which then turns yellow, then moving the piece according to chess rules.

Though I have a strong feeling that the problem of moving you're having is caused by some weirdness I've made rather than your deductive skills.



Edd
  • Members
  • 212 posts
  • Last active: Jan 06 2016 07:36 AM
  • Joined: 24 Dec 2011

Very cool ! ;)



garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005

sorry floodindahood, first and second  example works fine , was my failure



fiveseven808
  • Members
  • 1 posts
  • Last active: Nov 28 2014 11:47 PM
  • Joined: 13 Sep 2014

Holy cow! This is amazing! I'm amazed the chess pieces look really good, even though they're made from ASCII!



floodindahood
  • Members
  • 383 posts
  • Last active: Apr 20 2018 01:45 PM
  • Joined: 21 Oct 2011

fiveseven,

Check this one out, it lacks the chess logics and such, but it just looks and works gloriously.

http://www.autohotke...trols/?p=490368

 

and Garry don't worry about it, I actually noticed a couple pretty big issues when I was going over it so all good in the end :)



Godric
  • Members
  • 19 posts
  • Last active: Jun 02 2016 09:11 PM
  • Joined: 11 Nov 2013

That was awesome mate.

I love it.



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

this is extremely well done



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

Impressive.. and so short. You have my commendation, sir. Although, I noticed a few bugs in the logic.

 

1) Getting your pawn to the opposite end deems it a dead-piece.

2) If you move a pawn 2-spaces forward, then try to move one of your pawns in a touching column to that exact spot, it will, and the pawn will cease to exist.

 

I only ran it for about a minute or so, I'm just really good at breaking software  :)


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++