// NEWLIB
//alert ('NEWLIB')
var testingmoves = false
var curb=""
var emptyf = "_"
var whiteturn = ">"
var blackturn = "<"
var blackbottom = "bb"
var whitebottom = "wb"
var bottomcolor = whitebottom
var curb=forsythtoplain(initpos())
var themove="*"
var thename=""
var boardnum=0
var boards = new makearray(100)
//boards = initarray(boards,"")
var readingmove = true
var writtenR = "R"
var writtenN = "N"
var writtenB = "B"
var writtenQ = "Q"
var writtenK = "K"
var bshowifwhite = true
var bshowifblack = true
function showifwhite() {
bshowifwhite = true
bshowifblack = false
findquestionstoask()
return true
}
function showifblack() {
bshowifwhite = false
bshowifblack = true
findquestionstoask()
return true
}
function showifany() {
bshowifwhite = true
bshowifblack = true
findquestionstoask()
return true
}
function forsythtoplain(aa) {
// e.g. aa = "r1bqkbnr;pppp1ppp;2n5;1B2p4;4P3;5N2;PPPP1PPP;RNBQKB1R;>"
aa = new String (aa)
res=""
thelen=aa.length
for (i=0;i"
return res
}
function forsythtoplain1char(cc) {
// e.g. cc = "r", "n", "b", "3"
res=""
if (cc=="<") {return cc}
if (cc==">") {return cc}
if (cc==emptyf) {return cc}
if (cc=="r") {return cc}
if (cc=="n") {return cc}
if (cc=="b") {return cc}
if (cc=="q") {return cc}
if (cc=="k") {return cc}
if (cc=="p") {return cc}
if (cc=="R") {return cc}
if (cc=="N") {return cc}
if (cc=="B") {return cc}
if (cc=="Q") {return cc}
if (cc=="K") {return cc}
if (cc=="P") {return cc}
emptyf1 = emptyf
emptyf2 = emptyf1 + emptyf1
emptyf4 = emptyf2 + emptyf2
emptyf8 = emptyf4 + emptyf4
if (cc=="1") {return emptyf1}
if (cc=="2") {return emptyf2}
if (cc=="3") {return emptyf2+emptyf1}
if (cc=="4") {return emptyf4}
if (cc=="5") {return emptyf4+emptyf1}
if (cc=="6") {return emptyf4+emptyf2}
if (cc=="7") {return emptyf4+emptyf2+emptyf1}
if (cc=="8") {return emptyf8}
return ""
}
function putpiece (i,cc) {
// e.g. putpiece (5,"k") {
// alert ( 'putpiece ' + i + ' = ' + cc )
res = ""
if (false) {
} else if (cc==">") { res += "wk"
} else if (cc=="<") { res += "bk"
} else if (cc==emptyf) { res += "mt"
} else if (cc=="r") { res += "br"
} else if (cc=="n") { res += "bn"
} else if (cc=="b") { res += "bb"
} else if (cc=="q") { res += "bq"
} else if (cc=="k") { res += "bk"
} else if (cc=="p") { res += "bp"
} else if (cc=="z") { res += "bq"
} else if (cc=="R") { res += "wr"
} else if (cc=="N") { res += "wn"
} else if (cc=="B") { res += "wb"
} else if (cc=="Q") { res += "wq"
} else if (cc=="K") { res += "wk"
} else if (cc=="P") { res += "wp"
} else if (cc=="Z") { res += "wq"
} else {}
if (false) {
} else if (cc=="z" || cc=="Z") {
res += "_l"
if (false) {
} else if (!iswhite(i)) {
res += "_l"
} else {
res += "_d"
}
} else if (i==64 && cc==">") {
res += "_l"
} else if (i==64 && cc=="<") {
res += "_d"
} else if (iswhite(i)) {
res += "_l"
} else {
res += "_d"
}
document.images[i].src=giffile(res)
return true
}
function iswhite(i) {
rowstart = (mydiv(i,8) % 2)
field = i % 2
return (rowstart == field)
}
function mydiv(i,x) {
iclean = i - (i%x)
idivx = iclean / x
return idivx
}
function domakemove( fromftof , updatecurb ) {
// ---------------------------------------------
// -- curb: the board
// -- fromftof: "d2d4"
// ---------------------------------------------
fromf = ""
fromf += fromftof.charAt(0)
fromf += fromftof.charAt(1)
tof = ""
tof += fromftof.charAt(2)
tof += fromftof.charAt(3)
return makemove(curb,fromf,tof,updatecurb)
}
function makemove( bb, fromf, tof , updatecurb ) {
// ---------------------------------------------
// -- bb: the board
// -- fromf: move from field
// -- tof: move to field
// ---------------------------------------------
if (test){ alert ( 'makemove ' + bb + "\n" + fromf + ", " + tof ) }
// alert ( 'makemove ' + bb + "\n" + fromf + ", " + tof )
var turnnumber = 64
var turn = bb.charAt(turnnumber)
var fromfnum = fieldtonumber(fromf)
var tofnum = fieldtonumber(tof)
var piece = bb.charAt(fromfnum)
if (fromf==tof) { piece=emptyf }
// ---------------------------------------------
// castling
// ---------------------------------------------
if (isking(piece)) {
if (false) {
} else if (fromf=="e1" && tof=="g1"){
bb = makemove(bb,"h1","f1")
} else if (fromf=="e1" && tof=="c1"){
bb = makemove(bb,"a1","d1")
} else if (fromf=="e8" && tof=="g8"){
bb = makemove(bb,"h8","f8")
} else if (fromf=="e8" && tof=="c8"){
bb = makemove(bb,"a8","d8")
}
}
// ---------------------------------------------
// en passant
// ---------------------------------------------
if (ispawn(piece)) {
line1 = fromf.charAt(0)
line2 = tof.charAt(0)
if (line1!=line2) {
passingf = tof.charAt(0) + fromf.charAt(1)
// (line) (row)
if (bb.charAt(tofnum) == emptyf) {
bb = makemove(bb,passingf,passingf)
}
}
}
// ---------------------------------------------
// pawn -> officer
// ---------------------------------------------
if (ispawn(piece)) {
if (tof.charAt(1)=="8" || tof.charAt(1)=="1") {
if (false) {
} else if (piece=="p"){
piece = "z"
} else if (piece=="P"){
piece = "Z"
}
}
}
// ---------------------------------------------
// and then...
// ---------------------------------------------
// alert ( "makemove res < " + fromf + tof
// + "\n" +bb )
bb = replacepiece ( bb, tofnum, piece )
bb = replacepiece ( bb, fromfnum, emptyf )
bb = replacepiece ( bb, turnnumber, switchturn(turn) )
// alert ( "makemove res > " + bb )
if (updatecurb) {curb = bb}
return bb
} // makemove
function fieldtonumber ( ff ) {
// ---------------------------------------------
// -- ff: "e2" (e.g.)
// ---------------------------------------------
//alert ( 'fieldtonumber: ' + ff )
theline = ff.charAt(0)
therow = ff.charAt(1)
// alert ( 'line ' + theline + " row " + therow )
res = 8 * rowtonumber(therow) + linetonumber(theline)
//alert ( 'fieldtonumber-res: ' + res )
return res
}
function numbertofield ( ff ) {
// ---------------------------------------------
// -- ff: 17 (e.g.)
// ---------------------------------------------
//alert ( 'numbertofield: ' + ff )
linenum = ff % 8
rownum = mydiv(ff,8)
res = ""
// alert ( 'line ' + linenum + " row " + rownum )
res += numbertoline(linenum)
res += numbertorow(rownum)
//alert ( 'numbertofield-res: ' + res )
return res
}
function linetonumber(theline) {
if (theline=="a") {return 0}
if (theline=="b") {return 1}
if (theline=="c") {return 2}
if (theline=="d") {return 3}
if (theline=="e") {return 4}
if (theline=="f") {return 5}
if (theline=="g") {return 6}
if (theline=="h") {return 7}
}
function numbertoline(linenumber) {
if (linenumber==0) {return "a"}
if (linenumber==1) {return "b"}
if (linenumber==2) {return "c"}
if (linenumber==3) {return "d"}
if (linenumber==4) {return "e"}
if (linenumber==5) {return "f"}
if (linenumber==6) {return "g"}
if (linenumber==7) {return "h"}
}
function rowtonumber(therow) {
if (therow=="8") {return 0}
if (therow=="7") {return 1}
if (therow=="6") {return 2}
if (therow=="5") {return 3}
if (therow=="4") {return 4}
if (therow=="3") {return 5}
if (therow=="2") {return 6}
if (therow=="1") {return 7}
}
function numbertorow(rownumber) {
if (rownumber==0) {return "8"}
if (rownumber==1) {return "7"}
if (rownumber==2) {return "6"}
if (rownumber==3) {return "5"}
if (rownumber==4) {return "4"}
if (rownumber==5) {return "3"}
if (rownumber==6) {return "2"}
if (rownumber==7) {return "1"}
}
function replacepiece ( bb, ii, piece ) {
// ---------------------------------------------
// -- bb: the board
// -- ff: field (number 0-63)
// -- piece: e.g. "r" = rook
// ---------------------------------------------
// alert ( "replacepiece: " + piece )
res = replacestringpos(bb,ii,ii+1,piece)
// alert ( "replacepiece res = " + res )
return res
}
function switchturn ( turn ) {
// ---------------------------------------------
// -- turn = blackturn/whiteturn
// ---------------------------------------------
if (turn==whiteturn) {
return blackturn
} else {
return whiteturn
}
}
function replacestringpos ( ss, ii, jj, tt ) {
// ---------------------------------------------
// -- bb: the string
// -- ii: start of substring to be replaced
// -- jj: first char after substring to be replaced
// -- tt: replace string
// ---------------------------------------------
// alert ( "replacestringpos: " + ss + "\n" + ii + ", " + jj + ", " + tt )
res = ""
res += ss.substring(0,ii)
res += tt
res += ss.substring(jj,ss.length)
// alert ( 'result: ' + res )
return res
}
function initpos() {
return (
"rnbqkbnr;"
+ "pppppppp;"
+ "8;"
+ "8;"
+ "8;"
+ "8;"
+ "PPPPPPPP;"
+ "RNBQKBNR;>"
)
}
// -----------------------------------------------
// ** Parsing a chess game
// **
// -----------------------------------------------