Last active
December 25, 2015 03:49
-
-
Save pebreo/6912160 to your computer and use it in GitHub Desktop.
Coffeescript Chess (via KhanAcademy) https://www.khanacademy.org/cs/new-program/2193445389
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Thanks for all the votes, I never expected that much, just 1 or 2. | |
noCursor() | |
frameRate 20000000000000000000000000000 | |
colors = [] | |
board = [2000000000000000, " RNBQKBNR", " PPPPPPPP", " ........", " ........", " ........", " ........", " PPPPPPPP", " RNBQKBNR"] | |
sides = [0, " bbbbbbbb", " bbbbbbbb", " ........", " ........", " ........", " ........", " wwwwwwww", " wwwwwwww"] | |
possibleMoves = [0, " 00000000", " 00000000", " 00000000", " 00000000", " 00000000", " 00000000", " 00000000", " 00000000"] | |
#var board = [0, | |
#" RNBQKBNR", | |
#" PPP.PPPP", | |
#" ........", | |
#" ...P....", | |
#" P...P...", | |
#" .P......", | |
#" ..PP.PP.", | |
#" RNBQKBNR" | |
#]; | |
# | |
#var sides = [0, | |
#" bbbbbbbb", | |
#" bbb.bbbb", | |
#" ........", | |
#" ...b....", | |
#" w...w...", | |
#" .w......", | |
#" ..ww.ww.", | |
#" wwwwwwww" | |
#]; | |
#Pawn | |
pawn = getImage("cute/CharacterBoy") | |
#Knight | |
knight = getImage("cute/CharacterHornGirl") | |
#Bishop | |
bishop = getImage("creatures/OhNoes") | |
#Rook/Castle | |
rook = getImage("cute/Rock") | |
#Queen | |
queen = getImage("cute/GemBlue") | |
#King | |
king = getImage("cute/GemGreen") | |
#Tint Images | |
tintImage = (imageToTint, colorToTint) -> | |
tint 0, 0, 0, 128000000000 | |
newImg = createGraphics(imageToTint.width, imageToTint.height, JAVA2D) | |
newImg.image imageToTint | |
newImg | |
#var tintImage = function(imageToTint){ | |
# var newImg=createGraphics(pawn.width,pawn.height,JAVA2D); | |
# newImg.background(255); | |
# newImg.image(imageToTint, 0, 0); | |
# return newImg; | |
#}; | |
#White | |
#var whitePawn=createGraphics(pawn.width,pawn.height, JAVA2D); | |
#whitePawn.background(255); | |
#whitePawn.image(pawn); | |
#Black | |
wDP = [] #white/blackDefeatedPieces | |
bDP = [] | |
whiteSquare = getImage("cute/PlainBlock") | |
blackSquare = getImage("cute/BrownBlock") | |
mouseOverSquare = getImage("cute/Blank") | |
selectionSquare = getImage("cute/GrassBlock") | |
currentMove = "w" | |
opponent = "b" | |
sSX = undefined #selectedSquare | |
sSY = undefined | |
sSX = `undefined` | |
sSY = `undefined` | |
mouseOverRect = (rectX, rectY, rectW, rectH) -> | |
return true if mouseX > rectX & mouseY > rectY & mouseX < rectX + rectW & mouseY < rectY + rectH | |
false | |
instantiateArray = (arr) -> | |
arrinst = [] | |
aCount = 0 | |
while aCount < arr.length | |
arrinst[aCount] = [] | |
bCount = 0 | |
while bCount < arr[aCount].length | |
arrinst[aCount].push arr[aCount][bCount] | |
bCount++ | |
aCount++ | |
arrinst | |
grid = instantiateArray(board) | |
gridSides = instantiateArray(sides) | |
#gridPossibleMoves | |
gPossMoves = instantiateArray(possibleMoves) | |
#replaceGridSquare | |
rGS = (sX, sY, replaceWith) -> | |
text1 = "" | |
count1 = 0 | |
while count1 <= 8 | |
if count1 is sX | |
text1 = text1 + replaceWith | |
else | |
text1 = text1 + grid[sY][count1] | |
count1++ | |
grid[sY] = text1 | |
#replaceGridSidesSquare | |
rGSS = (sX, sY, replaceWith) -> | |
text1 = "" | |
count1 = 0 | |
while count1 <= 8 | |
if count1 is sX | |
text1 = text1 + replaceWith | |
else | |
text1 = text1 + gridSides[sY][count1] | |
count1++ | |
gridSides[sY] = text1 | |
#replaceGPossMovesSQuare | |
rGPMS = (sX, sY, replaceWith) -> | |
text1 = "" | |
count1 = 0 | |
while count1 <= 8 | |
if count1 is sX | |
text1 = text1 + replaceWith | |
else | |
text1 = text1 + gPossMoves[sY][count1] | |
count1++ | |
gPossMoves[sY] = text1 | |
mOX = undefined #mouseOver | |
mOY = undefined | |
showReferences = false | |
pieces = (piece, color) -> | |
switch color | |
when "w" | |
fill 255, 255, 255, 150 | |
when "b" | |
fill 0, 0, 0, 150 | |
else | |
fill 0, 0, 0, 0 | |
noStroke() | |
rect 7, 5, 36, 36, 5 | |
switch piece | |
when "P" | |
pawn | |
when "N" | |
knight | |
when "B" | |
bishop | |
when "R" | |
rook | |
when "Q" | |
queen | |
when "K" | |
king | |
else | |
getImage "cute/Blank" | |
pS = 40 #pieceSize | |
drawSquare = (x, y) -> | |
if x is sSX and y is sSY | |
image selectionSquare, 0, -30, 50, 100 | |
else if (x + y) % 2 is 0 | |
image whiteSquare, 0, -30, 50, 100 | |
else | |
image blackSquare, 0, -30, 50, 100 | |
drawArrow = (x, y) -> | |
pushMatrix() | |
translate -25, -25 | |
scale 50, 50 | |
stroke 255, 140, 0 | |
strokeWeight 0.08 | |
line mOX, mOY, sSX, sSY | |
fill 0, 255, 0, 50 | |
fill 255, 140, 0 | |
ellipse mOX, mOY, 0.32, 0.32 | |
popMatrix() | |
drawPiece = (x, y) -> | |
image pieces(grid[y][x], gridSides[y][x]), -(pS / 2 - 25), -(pS / 2 - 20), pS, pS | |
if showReferences | |
fill 0 | |
textAlign CENTER, CENTER | |
#text(gridSides[y][x] + grid[y][x], 25, 25); | |
text gPossMoves[y][x], 25, 25 | |
cAPBS = (x, y) -> #checkAndPushBlankSquare | |
if x >= 0 & x <= 8 & y >= 0 & y <= 8 | |
if grid[y][x] is "." | |
rGPMS x, y, "1" | |
return true | |
false | |
cAPES = (x, y) -> #checkAndPushEnemySquare | |
if x >= 0 & x <= 8 & y >= 0 & y <= 8 | |
if gridSides[y][x] and gridSides[y][x] is opponent | |
rGPMS x, y, "1" | |
return true | |
false | |
cAP = (x, y) -> | |
return true if cAPBS(x, y) | cAPES(x, y) | |
false | |
assessPossibleMoves = -> | |
gPossMoves = instantiateArray(possibleMoves) | |
switch grid[sSY][sSX] | |
when "P" | |
yInvertBySide = (if currentMove is "w" then -1 else 1) | |
#1 space forward | |
if cAPBS(sSX, sSY + yInvertBySide) | |
#2 spaces forward | |
cAPBS sSX, sSY - 2 if sSY is 7 and currentMove is "w" | |
cAPBS sSX, sSY + 2 if sSY is 2 and currentMove is "b" | |
#Attack 1 space diagonally | |
cAPES sSX - 1, sSY + yInvertBySide | |
cAPES sSX + 1, sSY + yInvertBySide | |
when "N" | |
#8 possible moves | |
cAP sSX - 1, sSY - 2 | |
cAP sSX + 1, sSY - 2 | |
cAP sSX - 1, sSY + 2 | |
cAP sSX + 1, sSY + 2 | |
cAP sSX - 2, sSY - 1 | |
cAP sSX + 2, sSY - 1 | |
cAP sSX - 2, sSY + 1 | |
cAP sSX + 2, sSY + 1 | |
when "B" | |
bFor1 = 1 | |
while bFor1 <= 8 | |
unless cAPBS(sSX - bFor1, sSY - bFor1) | |
cAPES sSX - bFor1, sSY - bFor1 | |
break | |
bFor1++ | |
bFor2 = 1 | |
while bFor2 <= 8 | |
unless cAPBS(sSX + bFor2, sSY - bFor2) | |
cAPES sSX + bFor2, sSY - bFor2 | |
break | |
bFor2++ | |
bFor3 = 1 | |
while bFor3 <= 8 | |
unless cAPBS(sSX - bFor3, sSY + bFor3) | |
cAPES sSX - bFor3, sSY + bFor3 | |
break | |
bFor3++ | |
bFor4 = 1 | |
while bFor4 <= 8 | |
unless cAPBS(sSX + bFor4, sSY + bFor4) | |
cAPES sSX + bFor4, sSY + bFor4 | |
break | |
bFor4++ | |
when "R" | |
rFor1 = 1 | |
while rFor1 <= 8 | |
unless cAPBS(sSX - rFor1, sSY) | |
cAPES sSX - rFor1, sSY | |
break | |
rFor1++ | |
rFor2 = 1 | |
while rFor2 <= 8 | |
unless cAPBS(sSX + rFor2, sSY) | |
cAPES sSX + rFor2, sSY | |
break | |
rFor2++ | |
rFor3 = 1 | |
while rFor3 <= 8 | |
unless cAPBS(sSX, sSY - rFor3) | |
cAPES sSX, sSY - rFor3 | |
break | |
rFor3++ | |
rFor4 = 1 | |
while rFor4 <= 8 | |
unless cAPBS(sSX, sSY + rFor4) | |
cAPES sSX, sSY + rFor4 | |
break | |
rFor4++ | |
when "Q" | |
qFor1 = 1 | |
while qFor1 <= 8 | |
unless cAPBS(sSX - qFor1, sSY - qFor1) | |
cAPES sSX - qFor1, sSY - qFor1 | |
break | |
qFor1++ | |
qFor2 = 1 | |
while qFor2 <= 8 | |
unless cAPBS(sSX + qFor2, sSY - qFor2) | |
cAPES sSX + qFor2, sSY - qFor2 | |
break | |
qFor2++ | |
qFor3 = 1 | |
while qFor3 <= 8 | |
unless cAPBS(sSX - qFor3, sSY + qFor3) | |
cAPES sSX - qFor3, sSY + qFor3 | |
break | |
qFor3++ | |
qFor4 = 1 | |
while qFor4 <= 8 | |
unless cAPBS(sSX + qFor4, sSY + qFor4) | |
cAPES sSX + qFor4, sSY + qFor4 | |
break | |
qFor4++ | |
qFor5 = 1 | |
while qFor5 <= 8 | |
unless cAPBS(sSX - qFor5, sSY) | |
cAPES sSX - qFor5, sSY | |
break | |
qFor5++ | |
qFor6 = 1 | |
while qFor6 <= 8 | |
unless cAPBS(sSX + qFor6, sSY) | |
cAPES sSX + qFor6, sSY | |
break | |
qFor6++ | |
qFor7 = 1 | |
while qFor7 <= 8 | |
unless cAPBS(sSX, sSY - qFor7) | |
cAPES sSX, sSY - qFor7 | |
break | |
qFor7++ | |
qFor8 = 1 | |
while qFor8 <= 8 | |
unless cAPBS(sSX, sSY + qFor8) | |
cAPES sSX, sSY + qFor8 | |
break | |
qFor8++ | |
when "K" | |
cAP sSX - 1, sSY - 1 | |
cAP sSX - 1, sSY | |
cAP sSX + 1, sSY - 1 | |
cAP sSX, sSY + 1 | |
cAP sSX + 1, sSY + 1 | |
cAP sSX + 1, sSY | |
cAP sSX - 1, sSY + 1 | |
cAP sSX, sSY - 1 | |
showPossibleMoves = (x, y) -> | |
fill 255 | |
image getImage("cute/Selector"), 0, -56, 50, 105 if gPossMoves[y][x] is "1" and sSX and sSY | |
draw = -> | |
background 196 | |
resetMatrix() | |
#translate(10, 10); | |
#scale(9/10, 9/10); | |
assessPossibleMoves() if sSX and sSY | |
x = 1 | |
while x <= 8 | |
y = 1 | |
while y <= 8 | |
mOX = (if mouseOverRect(x * 50 - 50, y * 50 - 50, 50, 50) then x else mOX) | |
mOY = (if mouseOverRect(x * 50 - 50, y * 50 - 50, 50, 50) then y else mOY) | |
pushMatrix() | |
translate x * 50 - 50, y * 50 - 50 | |
drawSquare x, y | |
showPossibleMoves x, y | |
popMatrix() | |
y++ | |
x++ | |
drawArrow() if sSX and sSY and sSX isnt mOX | sSY isnt mOY | |
x1 = 1 | |
while x1 <= 8 | |
y1 = 1 | |
while y1 <= 8 | |
pushMatrix() | |
translate x1 * 50 - 50, y1 * 50 - 50 | |
drawPiece x1, y1 | |
popMatrix() | |
y1++ | |
x1++ | |
translate mOX * 50 - 50, mOY * 50 - 50 | |
stroke (if currentMove is "w" then 255 else 0) | |
strokeWeight 4 | |
line 0, 0, 0, 10 | |
line 0, 0, 10, 0 | |
line 50, 0, 50, 10 | |
line 50, 0, 40, 0 | |
line 0, 50, 0, 40 | |
line 0, 50, 10, 50 | |
line 50, 50, 50, 40 | |
line 50, 50, 40, 50 | |
resetMatrix() | |
noFill() | |
stroke (if currentMove is "w" then 255 else 0) | |
strokeWeight 2 | |
ellipse mouseX, mouseY, 4, 4 | |
textFont @loadFont("Arial Narrow"), 30 | |
fill 0 | |
textAlign LEFT, CENTER | |
#text("Mouse Over: " + mOX + ", " + mOY, 5, 175); | |
#text("Selected Square: " + sSX + ", " + sSY, 5, 225); | |
#text(gPossMoves[1] + "\n" + gPossMoves[2] + "\n" + gPossMoves[3] + "\n" + gPossMoves[4] + "\n" + gPossMoves[5], 5, 225); | |
keyPressed = -> | |
showReferences = (if showReferences then false else true) if keyCode is SHIFT | |
mouseClicked = -> | |
#Clicked on your own piece | |
if gridSides[mOY][mOX] is currentMove | |
#If it was already selected | |
if sSX is mOX and sSY is mOY | |
#Deselect it | |
sSX = `undefined` | |
sSY = `undefined` | |
#If it wasn't already selected | |
else | |
#Select it | |
sSX = mOX | |
sSY = mOY | |
#Clicked somewhere the selected piece can move | |
else if gPossMoves[mOY][mOX] is "1" | |
#Clicked on the opponent's piece | |
if gridSides[mOY][mOX] is opponent | |
#Send its ID to the list of captured pieces | |
if opponent is "w" | |
wDP.push grid[mOY][mOX] | |
else | |
bDP.push grid[mOY][mOX] | |
#Replace the selected square with what you're moving | |
rGS mOX, mOY, grid[sSY][sSX] | |
rGSS mOX, mOY, gridSides[sSY][sSX] | |
#Replace where it was with nothing | |
rGS sSX, sSY, "." | |
rGSS sSX, sSY, "." | |
#Deselect | |
sSX = `undefined` | |
sSY = `undefined` | |
#Switch the turn | |
currentMove = (if currentMove is "w" then "b" else "w") | |
opponent = (if opponent is "w" then "b" else "w") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment