-4

Я в джава-скриптах недавно. Решил попробовать подредактировать под себя уже готовую шахматную ходилку. Заранее скажу, что всякая шахматная позиция имеет свой EPD (например, 1r4rk/6pp/7b/p1p3N1/PpB5/1P6/1Pq2PPP/5RK1 w - -). На шахматной доске можно двигать фигуры и вместе с позицией, соответственно меняется EPD позиции. Через EPD планирую проверять позицию. Т.е. я знаю EPD верной позиции. И если EPD позиции, поставленной на доске совпала с моей уже заранее прописанной EPD, то решение верное. Возникла проблема с проверкой, не понимаю, в чём дело. Заранее скажу, что ходилку делаю для себя. Вот код уже мной правленый:

<script type="text/javascript">var WHITE = 0;
var BLACK = 1;

var WHITE_KING_CASTLE = 0x01;
var WHITE_QUEEN_CASTLE = 0x02;
var BLACK_KING_CASTLE = 0x04;
var BLACK_QUEEN_CASTLE = 0x08;

var letterA = 'a';
var digit0 = '0';

function Inc(n) { return (++n); }
function trim(string) { return string.replace (/(^\s+)|(\s+$)/g, ""); }

function BuildPieceNoticeName (piece) {
 switch(piece) {
  case 'p': 
  case 'P': return '';
  case 'n':
  case 'N': return 'К';
  case 'b':
  case 'B': return 'С';
  case 'r':
  case 'R': return 'Л';
  case 'q':
  case 'Q': return 'Ф';
  case 'k':
  case 'K': return 'Кр';
  default:  return null;
 }
}

function GetColor(piece)
{
  switch(piece)
  {
      case 'k':
      case 'q':
      case 'r':
      case 'b':
      case 'n':
      case 'p':
        return BLACK;
  }
  return WHITE;
}

function BuildNotation (exInfo) {
 if (typeof(exInfo.castling)!='undefined' && exInfo.castling!='') return exInfo.castling;
 var n=exInfo.pieceName + exInfo.fieldFrom + exInfo.delimiter + exInfo.fieldTo;
 if (typeof(exInfo.promotionName)!= 'undefined') n+=exInfo.promotionName;
 if (exInfo.check) n+='+';
 return n;
}

function BuildFen(position)
{
  var fen = '';
  var skipped = 0;
  for (rank=7; rank>=0; --rank)
  {
    for (file=0; file<8; ++file)
    {
      var piece = position.board[file + 16*rank];
      if (piece == '.')
      {
        ++skipped;
      }
      else {
        if (skipped > 0) fen = fen + skipped;
        skipped = 0;
        fen = fen + piece;
      }
    }
    if (skipped > 0) fen = fen + skipped;
    skipped = 0;
    if (rank != 0) fen = fen + '/';
  }

  fen += ' ' + (position.activeSide == WHITE ? 'w' : 'b') + ' ';

  if (position.castle == 0)
    fen += '- '
  else  
    fen += ''
      + (position.castle & WHITE_KING_CASTLE ? 'K' : '')  
      + (position.castle & WHITE_QUEEN_CASTLE ? 'Q' : '')  
      + (position.castle & BLACK_KING_CASTLE ? 'k' : '')  
      + (position.castle & BLACK_QUEEN_CASTLE ? 'q' : '')  
      + ' ';

  if (position.passant == -1)
    fen += '- '
  else
    fen += ''
      + String.fromCharCode(letterA.charCodeAt(0) + (position.passant % 8))
      + String.fromCharCode(digit0.charCodeAt(0) + 1 + ((position.passant >> 4) % 8))
      + ' ';

  fen = fen + '0 1';
  return fen;
}

function BuildPosition(fen)
{
  var retValue = new Object();
  retValue.board = Array(0x88);

  var fenIndex = 0;
  var file = 0;
  var rank = 7;
  var kingCount = 0;

  var ch;

  var fen = trim(fen);
  for(;;)
  {
    if (fenIndex >= fen.length) break;
    ch = fen.charAt(fenIndex);

    if (ch == ' ') break;
    switch(ch)
    {
      case 'k': case 'K':
        ++kingCount;
        if (ch == 'k')
          retValue.blackKing = file + 16*rank;
        else  
          retValue.whiteKing = file + 16*rank;
      case 'q': case 'Q':
      case 'r': case 'R':
      case 'b': case 'B': 
      case 'n': case 'N':
      case 'p': case 'P':
        if (file >= 8) return 'Переполнение в строке FEN, номер символа = ' + Inc(fenIndex);
        retValue.board[file + 16*rank] = ch;
        file = file + 1;
        break;
      case '/':
        if (file != 8) return 'Обнаружен разделитель, но линия не определена полностью в строке FEN, номер символа = ' + Inc(fenIndex);
        if (rank == 0) return 'Обнаружен разделитель, но вся позиция прочитана в строке FEN, номер символа = ' + Inc(fenIndex);
        file = 0;
        rank = rank - 1;
        break;
      case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': 
        for (counterCh = '1'; counterCh <= ch; ++counterCh)
        {
          if (file >= 8) return 'Переполнение в строке FEN, номер символа = ' + Inc(fenIndex);
          retValue.board[file + 16*rank] = '.';
          file = file + 1;
        }  
        break;
      default: 
        return 'Неверный символ "' + ch + '" в строке FEN, номер символа = ' + Inc(fenIndex);
    }

    ++fenIndex;
  }

  if (typeof(retValue.whiteKing) == 'undefined') 
    return 'Неверная позиция. На доске нет белого короля';
  if (typeof(retValue.blackKing) == 'undefined') 
    return 'Неверная позиция. На доске нет черного короля';
  if (kingCount > 2)
    return 'Неверная позиция. На доске более 2 королей';

  for(;;)
  {
    ++fenIndex;
    if (fenIndex >= fen.length)
    {
      retValue.isValid = false;
      return 'Не указана очередность хода! Добавьте пробел и w (белые) или b (чёрные) к нотации';
    }  
    ch = fen.charAt(fenIndex);
    if (ch == 'w' || ch == 'b') break;
    if (ch != ' ')
      return 'Неверный символ "' + ch + '" в строке FEN, номер символа = ' + Inc(fenIndex);
  }

  retValue.activeSide = ch == 'w' ? WHITE : BLACK;
  retValue.isValid = true;

  retValue.castle = 0;
  retValue.passant = -1;

  for(;;)
  {
    ++fenIndex;
    if (fenIndex >= fen.length)
    {
      retValue.castle = 0x0F;
      return retValue;
    }

    ch = fen.charAt(fenIndex);
    if (ch != ' ') break;
  }

  if (ch != '-')
    for(;;)
    {
      switch(ch)
      {
        case 'K':
          retValue.castle |= WHITE_KING_CASTLE;
          break;            
        case 'k':
          retValue.castle |= BLACK_KING_CASTLE;
          break;            
        case 'Q':
          retValue.castle |= WHITE_QUEEN_CASTLE;
          break;            
        case 'q':
          retValue.castle |= BLACK_QUEEN_CASTLE;
          break;            
        default:  
          return 'Неверный символ "' + ch + '" в строке FEN, номер символа = ' + Inc(fenIndex);
      }

      ++fenIndex;
      if (fenIndex >= fen.length) return retValue;
      ch = fen.charAt(fenIndex);
      if (ch == ' ') break;
    }

  for(;;)
  {
    ++fenIndex;
    if (fenIndex >= fen.length)
    {
      retValue.castle = 0x0F;
      return retValue;
    }

    ch = fen.charAt(fenIndex);
    if (ch != ' ') break;
  }

  if (ch != '-')
  {
    var passantFile;
    var passantRank;

    switch(ch)
    {
      case 'a': passantFile = 0; break;
      case 'b': passantFile = 1; break;
      case 'c': passantFile = 2; break;
      case 'd': passantFile = 3; break;
      case 'e': passantFile = 4; break;
      case 'f': passantFile = 5; break;
      case 'g': passantFile = 6; break;
      case 'h': passantFile = 7; break;
      default: return 'Неверный символ "' + ch + '" в строке FEN, номер символа = ' + Inc(fenIndex);
    }

    ++fenIndex;
    if (fenIndex < fen.length)
    {
      ch = fen.charAt(fenIndex);
      switch(ch)
      {
        case '1': passantRank = 0; break;
        case '2': passantRank = 1; break;
        case '3': passantRank = 2; break;
        case '4': passantRank = 3; break;
        case '5': passantRank = 4; break;
        case '6': passantRank = 5; break;
        case '7': passantRank = 6; break;
        case '8': passantRank = 7; break;
        default: return 'Неверный символ "' + ch + '" в строке FEN, номер символа = ' + Inc(fenIndex);
      }

      retValue.passant = passantFile + 16 * passantRank;
    }
  }

  return retValue;
}

var steps = new Array(0x01, 0x10, -0x01, -0x10, 0x0f, 0x11, -0x0f, -0x11);
var stepPiece = new Array('r', 'r', 'r', 'r', 'b', 'b', 'b', 'b');
var knightSteps = new Array(+14, +18, +31, +33, -14, -18, -31, -33);

function TestCheck(position, color, point)
{
  if (typeof(point) == 'undefined') color = position.activeSide;
  if (typeof(point) == 'undefined') point = color == WHITE ? position.whiteKing : position.blackKing;

  var opponentKing = color == WHITE ? 'k' : 'K';
  var opponentQueen = color == WHITE ? 'q' : 'Q';
  var opponentKnight = color == WHITE ? 'n' : 'N';
  var opponentPawn = color == WHITE ? 'p' : 'P';

  var tempPoint;
  for (i=0; i<8; ++i)
  {
    var opponentRookOrBishop = color == WHITE ? stepPiece[i] : stepPiece[i].toUpperCase();
    var firstTime = true;

    var tempPoint = point;
    for(;;)
    {
      tempPoint += steps[i];
      if (tempPoint & 0x88) break;
      if (position.board[tempPoint] == opponentQueen) return true;
      if (position.board[tempPoint] == opponentRookOrBishop) return true;
      if (firstTime && position.board[tempPoint] == opponentKing) return true;
      if (position.board[tempPoint] != '.') break;
      firstTime = false;
    }
  }

  for (i=0; i<8; ++i)
  {
    var tempPoint = point + knightSteps[i];
    if (tempPoint & 0x88) continue;
    if (position.board[tempPoint] == opponentKnight) return true;
  }

  var tempPoint;
  tempPoint = point + (color == WHITE ? + 1 : -1 ) * 15;
  if (!(tempPoint & 0x88))
    if (position.board[tempPoint] == opponentPawn)
      return true;
  tempPoint = point + (color == WHITE ? + 1 : -1 ) * 17;
  if (!(tempPoint & 0x88))
    if (position.board[tempPoint] == opponentPawn)
      return true;

  return false;
}

function CheckPawnMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
{
  var delta = color == WHITE ? +1 : -1;
  var startRank = color == WHITE ? 1 : 6;

  if (!isTaking)
  if (position.passant == fileTo + 16*rankTo)
  if (Math.abs(fileFrom - fileTo) == 1)
  if (rankTo - rankFrom == delta)
  {
    exInfo.isSimple = false;
    var myPawn = position.board[fileFrom + 16 * rankFrom];
    var opponentPawn = position.board[position.passant - 16*delta];

    position.board[fileTo + 16 * rankTo] = myPawn;
    position.board[fileFrom + 16 * rankFrom] = '.';
    position.board[position.passant - 16*delta] = '.';

    if (!TestCheck(position, color)) return true;

    position.board[position.passant - 16*delta] = opponentPawn;
    position.board[fileFrom + 16 * rankFrom] = myPawn;
    position.board[fileTo + 16 * rankTo] = '.';

    return false;
  }

  if (!isTaking)
  {
    if (fileTo != fileFrom) return false;
    if (rankTo - rankFrom != delta) 
    {
      if (rankTo - rankFrom != 2 * delta) return false;
      if (rankFrom != startRank) return false;
      if (position.board[fileFrom + 16*rankFrom + 16*delta] != '.') return false;
      exInfo.passant = fileFrom + 16*rankFrom + 16*delta;
    }
  }
  else {
    if (rankTo - rankFrom != delta) return false;
    if (Math.abs(fileFrom - fileTo) != 1) return false;
  }

  exInfo.isPromotion = rankTo == 0 || rankTo == 7;
  if (exInfo.isPromotion) exInfo.promotionField = fileTo+16*rankTo;
  else exInfo.promotionField = -1;
  return true;
}

function CheckKnightMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
{
  var delta1 = Math.abs(fileFrom-fileTo);
  var delta2 = Math.abs(rankFrom-rankTo);
  return (delta1 == 1 && delta2 == 2) || (delta1 == 2 && delta2 == 1);
}

function CheckStepMove(position, from, to, step)
{
  for(;;)
  {
    from += step;
    if (from & 0x88) return false;
    if (from == to) return true;
    if (position.board[from] != '.') return false;
  }
}

function CheckBishopMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
{
  var step;

  if (false) return false;
  else if (fileTo > fileFrom && rankTo > rankFrom) step = +0x11; 
  else if (fileTo < fileFrom && rankTo > rankFrom) step = +0x0f; 
  else if (fileTo > fileFrom && rankTo < rankFrom) step = -0x0f; 
  else if (fileTo < fileFrom && rankTo < rankFrom) step = -0x11
  else return false;

  return CheckStepMove(position, fileFrom + 16*rankFrom, fileTo + 16*rankTo, step);
}

function CheckRookMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
{
  var step;

  if (false) return false;
  else if (fileTo == fileFrom && rankTo > rankFrom) step = +0x10; 
  else if (fileTo == fileFrom && rankTo < rankFrom) step = -0x10; 
  else if (rankTo == rankFrom && fileTo > fileFrom) step = +0x01;
  else if (rankTo == rankFrom && fileTo < fileFrom) step = -0x01;
  else return false;

  var retValue = CheckStepMove(position, fileFrom + 16*rankFrom, fileTo + 16*rankTo, step);
  if (color == WHITE && fileFrom == 0 && rankFrom == 0)
    position.castle &= ~WHITE_QUEEN_CASTLE;
  if (color == WHITE && fileFrom == 7 && rankFrom == 0)
    position.castle &= ~WHITE_KING_CASTLE;
  if (color == BLACK && fileFrom == 0 && rankFrom == 7)
    position.castle &= ~BLACK_QUEEN_CASTLE;
  if (color == BLACK && fileFrom == 7 && rankFrom == 7)
    position.castle &= ~BLACK_KING_CASTLE;
  return retValue;
}

function CheckQueenMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
{
  return false
    || CheckBishopMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
    || CheckRookMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
    ;
}

function CheckKingMove(position, fileFrom, rankFrom, fileTo, rankTo, color, isTaking, exInfo)
{
  exInfo.castling = '';
  if (fileFrom == 4)
    switch(fileTo)
    {
      case 6:
        if (color == WHITE)
        {
          if (!(position.castle & WHITE_KING_CASTLE)) return false;
          if (rankFrom != 0) return false;
          if (rankTo != 0) return false;
          if (TestCheck(position, color, 0x04)) return false;
          if (TestCheck(position, color, 0x05)) return false;
          if (TestCheck(position, color, 0x06)) return false;
          if (position.board[0x07] != 'R') return false;
          if (position.board[0x06] != '.') return false;
          if (position.board[0x05] != '.') return false;
          position.castle &= ~WHITE_KING_CASTLE & ~WHITE_QUEEN_CASTLE;
          position.board[0x04] = '.';
          position.board[0x05] = 'R';
          position.board[0x06] = 'K';
          position.board[0x07] = '.';
          exInfo.isSimple = false;
          exInfo.castling = '0-0';
          return true;
        }
        else {
          if (!(position.castle & BLACK_KING_CASTLE)) return false;
          if (rankFrom != 7) return false;
          if (rankTo != 7) return false;
          if (TestCheck(position, color, 0x74)) return false;
          if (TestCheck(position, color, 0x75)) return false;
          if (TestCheck(position, color, 0x76)) return false;
          if (position.board[0x77] != 'r') return false;
          if (position.board[0x76] != '.') return false;
          if (position.board[0x75] != '.') return false;
          position.castle &= ~BLACK_KING_CASTLE & ~BLACK_QUEEN_CASTLE;
          position.board[0x74] = '.';
          position.board[0x75] = 'r';
          position.board[0x76] = 'k';
          position.board[0x77] = '.';
          exInfo.isSimple = false;
          exInfo.castling = '0-0';
          return true;
        }
        break;
      case 2:
        if (color == WHITE)
        {
          if (!(position.castle & WHITE_QUEEN_CASTLE)) return false;
          if (rankFrom != 0) return false;
          if (rankTo != 0) return false;
          if (TestCheck(position, color, 0x04)) return false;
          if (TestCheck(position, color, 0x03)) return false;
          if (TestCheck(position, color, 0x02)) return false;
          if (position.board[0x00] != 'R') return false;
          if (position.board[0x01] != '.') return false;
          if (position.board[0x02] != '.') return false;
          if (position.board[0x03] != '.') return false;
          position.castle &= ~WHITE_KING_CASTLE & ~WHITE_QUEEN_CASTLE;
          position.board[0x04] = '.';
          position.board[0x03] = 'R';
          position.board[0x02] = 'K';
          position.board[0x01] = '.';
          position.board[0x00] = '.';
          exInfo.isSimple = false;
          exInfo.castling = '0-0-0';
          return true;
        }
        else {
          if (!(position.castle & BLACK_QUEEN_CASTLE)) return false;
          if (rankFrom != 7) return false;

          if (rankTo != 7) return false;
          if (TestCheck(position, color, 0x74)) return false;
          if (TestCheck(position, color, 0x73)) return false;
          if (TestCheck(position, color, 0x72)) return false;
          if (position.board[0x70] != 'r') return false;
          if (position.board[0x71] != '.') return false;
          if (position.board[0x72] != '.') return false;
          if (position.board[0x73] != '.') return false;
          position.castle &= ~BLACK_KING_CASTLE & ~BLACK_QUEEN_CASTLE;
          position.board[0x74] = '.';
          position.board[0x73] = 'r';
          position.board[0x72] = 'k';
          position.board[0x71] = '.';
          position.board[0x70] = '.';
          exInfo.castling = '0-0-0';
          exInfo.isSimple = false;
          return true;
        }
        break;  
    }

  var delta1 = fileFrom - fileTo;
  var delta2 = rankFrom - rankTo;
  if (delta1 == 0 && delta2 == 0) return false;
  if (delta1 != -1 && delta1 != 0 && delta1 != +1) return false;
  if (delta2 != -1 && delta2 != 0 && delta2 != +1) return false;

  if (color == WHITE)
    position.castle &= ~WHITE_KING_CASTLE & ~WHITE_QUEEN_CASTLE;
  else  
    position.castle &= ~BLACK_KING_CASTLE & ~BLACK_QUEEN_CASTLE;

  return true;
}

function DoMove(position, fileFrom, rankFrom, fileTo, rankTo, exInfo)
{
  if (!position.isValid) return false;

  if (fileFrom < 0 || fileFrom >= 8) return false;
  if (rankFrom < 0 || rankFrom >= 8) return false;
  if (fileTo < 0 || fileTo >= 8) return false;
  if (rankTo < 0 || rankTo >= 8) return false;

  var piece = position.board[fileFrom + 16*rankFrom];
  var destPiece = position.board[fileTo + 16*rankTo];
  var isTaking = destPiece != '.';
  if (isTaking && GetColor(destPiece) == GetColor(piece)) return false;

  exInfo.isSimple = true;
  exInfo.isPromotion = false;
  exInfo.passant = -1;

  var isWhiteKing = false;
  var isBlackKing = false;

  var isValidMove;
  switch(piece)
  {
    case 'P': 
      isValidMove = CheckPawnMove(position, fileFrom, rankFrom, fileTo, rankTo, WHITE, isTaking, exInfo);
      break;
    case 'p': 
      isValidMove = CheckPawnMove(position, fileFrom, rankFrom, fileTo, rankTo, BLACK, isTaking, exInfo);
      break;
    case 'N': 
      isValidMove = CheckKnightMove(position, fileFrom, rankFrom, fileTo, rankTo, WHITE, isTaking, exInfo);
      break;
    case 'n': 
      isValidMove = CheckKnightMove(position, fileFrom, rankFrom, fileTo, rankTo, BLACK, isTaking, exInfo);
      break;
    case 'B': 
      isValidMove = CheckBishopMove(position, fileFrom, rankFrom, fileTo, rankTo, WHITE, isTaking, exInfo);
      break;
    case 'b': 
      isValidMove = CheckBishopMove(position, fileFrom, rankFrom, fileTo, rankTo, BLACK, isTaking, exInfo);
      break;
    case 'R': 
      isValidMove = CheckRookMove(position, fileFrom, rankFrom, fileTo, rankTo, WHITE, isTaking, exInfo);
      break;
    case 'r': 
      isValidMove = CheckRookMove(position, fileFrom, rankFrom, fileTo, rankTo, BLACK, isTaking, exInfo);
      break;
    case 'Q': 
      isValidMove = CheckQueenMove(position, fileFrom, rankFrom, fileTo, rankTo, WHITE, isTaking, exInfo);
      break;
    case 'q': 
      isValidMove = CheckQueenMove(position, fileFrom, rankFrom, fileTo, rankTo, BLACK, isTaking, exInfo);
      break;
    case 'K': 
      isValidMove = CheckKingMove(position, fileFrom, rankFrom, fileTo, rankTo, WHITE, isTaking, exInfo);
      isWhiteKing = true;
      break;
    case 'k': 
      isValidMove = CheckKingMove(position, fileFrom, rankFrom, fileTo, rankTo, BLACK, isTaking, exInfo);
      isBlackKing = true;
      break;
    default:
      return false;
  }

  if (isValidMove == false) return false;

  var killed = position.board[fileTo + 16*rankTo];  
  if (exInfo.isSimple)
  {
   position.board[fileTo + 16*rankTo] = position.board[fileFrom + 16*rankFrom];
   position.board[fileFrom + 16*rankFrom] = '.';
  }
  var whiteKing = isWhiteKing ? fileTo + 16*rankTo : position.whiteKing;
  var blackKing = isBlackKing ? fileTo + 16*rankTo : position.blackKing;

  if (TestCheck(position, position.activeSide, position.activeSide == WHITE ? whiteKing : blackKing))
  {
   position.board[fileFrom + 16*rankFrom] = position.board[fileTo + 16*rankTo];
   position.board[fileTo + 16*rankTo] = killed;
   return false;
  }

  position.whiteKing = whiteKing;
  position.blackKing = blackKing;

  exInfo.pieceName=BuildPieceNoticeName (position.board[fileTo + 16*rankTo]);
  var s='ABCDEFGH';
  exInfo.fieldFrom=s.substring(fileFrom,fileFrom+1)+(++rankFrom);
  exInfo.fieldTo=s.substring(fileTo,fileTo+1)+(++rankTo);
  if (killed=='.') exInfo.delimiter='-';
  else exInfo.delimiter=':';

  position.activeSide ^= 1;
  position.passant = exInfo.passant;

  return true;
}
</script>
<script type="text/javascript">function get_Id(id)
{
  return document.getElementById(id);
}

var config =
{
  pieceDir: 'pieces/',
  pieceExt: '.png',
  bitmapWidth: 41,
  bitmapHeight: 41,
  fieldColorWhite: '#EEEEEE',
  fieldColorBlack: '#888888'
};

function CalculateAbsoluteOffset(ctrl)
{
  var elem = ctrl;

  var left = 0;
  var top = 0;

  while (elem)
  {
    left += elem.offsetLeft;
    top += elem.offsetTop;
    elem = elem.offsetParent;
  }

  var retValue = new Object();
  retValue.left = left;
  retValue.top = top;
  return retValue;
}

function AddHandler(object, event, handler)
{
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof object.attachEvent != 'undefined')
    object.attachEvent('on' + event, handler);
  else
    throw "Несовместимый браузер (Incompatible browser)";
}

function RemoveHandler(object, event, handler)
{
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else
    throw "Несовместимый браузер (Incompatible browser)";
}

function PreventDefault(event)
{
  if (typeof(event.preventDefault) != 'undefined')
    event.preventDefault();
}

function GetTarget(e)
{
  return typeof(e.target) != 'undefined' ? e.target : e.srcElement;
}

function ParentNode(node)
{
  return typeof(node.parentNode) != 'undefined' ? node.parentNode : node.parentElement;
}

function FindParent(ctrl, tagName)
{
  var node = ctrl; 
  for(;;)
  {
    if (typeof(node.tagName) != 'string') return null;
    if (node.tagName.toUpperCase() == tagName.toUpperCase()) return node;

    node = ParentNode(node);
    if (node == null) return null;
  }  
}

function GetFirstChild(ctrl, tagName)
{
  var children = ctrl.getElementsByTagName(tagName);
  return children.length == 0 ? null : children[0];
}

function FindTd(div, file, rank)
{
  if (file < 0 || file >= 8) return null;
  if (rank < 0 || rank >= 8) return null;

  var table = GetFirstChild(div, 'TABLE');
  var tbody = GetFirstChild(table, 'TBODY');

  var trArray = tbody.getElementsByTagName('TR');
  if (trArray.length < 8) return null;
  var tr = trArray[7-rank];

  var tdArray = tr.getElementsByTagName('TD');
  if (tdArray.length < 8) return null;
  return tdArray[file];
}

function ExtractPixelValue(s)
{
  var suffix = s.length >= 2 ? s.substr(s.length-2, 2) : '';
  if (suffix.toLowerCase() == 'px')
    s = s.substr(0, s.length - 2);
  return parseInt(s);  
}

function Move(ctrl, deltaX, deltaY)
{
  var left = ExtractPixelValue(ctrl.style.left);
  var top = ExtractPixelValue(ctrl.style.top);
  ctrl.style.left = left + deltaX + 'px';
  ctrl.style.top = top + deltaY + 'px';
}

function TdMouseDown(e)
{
  var td = FindParent(GetTarget(e), 'TD'); 
  if (td == null) return;

  PreventDefault(e);

  var div = get_Id(td.divId);
  if (div==null) { return; }
  var position = div.position;

  if (div.isPromotion) return;

  var piece = position.board[td.file + 16*td.rank];
  if (piece == '.') return;

  if (GetColor(piece) != position.activeSide) return;

  var imgDiv = document.createElement('div');
  var offset = CalculateAbsoluteOffset(td);
  imgDiv.style.position = 'absolute';
  imgDiv.style.left = offset.left + 'px';
  imgDiv.style.top = offset.top + 'px';
  imgDiv.style.width = config.bitmapWidth + 'px';
  imgDiv.style.height = config.bitmapHeight + 'px';
  imgDiv.style.backgroundImage = td.style.backgroundImage;
  imgDiv.zIndex = 10;

  td.style.backgroundImage = '';
  div.appendChild(imgDiv);

  div.dragTd = td;
  div.dragDiv = imgDiv;
  div.dragLastX = e.clientX;
  div.dragLastY = e.clientY;

  AddHandler(div, 'mousemove', TdMouseMove);
  AddHandler(div, 'mouseup', TdMouseUp);
}

function TdMouseMove(e)
{
  PreventDefault(e);

  var node = GetTarget(e);
  for(;;)
  {
    node = FindParent(node, 'DIV');
    if (typeof(node) == 'indefined') return;
    if (typeof(node.dragTd) != 'undefined') break;
    node = ParentNode(node);
  }

  var div = node;
  Move(div.dragDiv, e.clientX - div.dragLastX, e.clientY - div.dragLastY);
  div.dragLastX = e.clientX;
  div.dragLastY = e.clientY;
}

function PieceClick(e)
{
  var img = GetTarget(e);
  var div = img.div;
  if (div.position.activeSide == WHITE)
  {
    for (point=0x00; point<0x08; ++point)
      if (div.position.board[point] == 'p')
      {
        div.position.board[point] = img.piece;
        break;
      }
  }
  else {
    for (point=0x70; point<0x78; ++point)
      if (div.position.board[point] == 'P')
      {
        div.position.board[point] = img.piece.toUpperCase();
        break;
      }
  }

  div.removeChild(div.promotionDiv);  
  div.isPromotion = false;
  div.promotionDiv = null;
  RefreshPositionDiv(div);
  div.history.push(BuildFen(div.position));
  if (div.exInfo.promotionField>-1) {
   div.exInfo.promotionName = BuildPieceNoticeName (div.position.board[point]);
   if (TestCheck(div.position,div.position.activeSide)) div.exInfo.check=true;
   else div.exInfo.check=false;
  }
  div.notation.push(BuildNotation(div.exInfo));
  ShowNotation (get_Id('notId'), div.notation, div.notationvisibility);
  div.undo.disabled = false;
}

function CreatePromotionTd(div, piece, color)
{
  var td = document.createElement('td');
  var img = document.createElement('img');
  img.src = config.pieceDir + (color == WHITE ? 'w' : 'b') + piece + config.pieceExt;
  img.style.cursor = 'pointer';
  img.piece = piece;
  img.div = div;
  AddHandler(img, 'click', PieceClick);
  td.appendChild(img);
  return td;
}

function RunPromotion(div)
{
  div.isPromotion = true;

  var offset = CalculateAbsoluteOffset(div);

  var promotionDiv = document.createElement('div');
  promotionDiv.parentDiv = div;

  promotionDiv.style.border = '1px solid #000000';
  promotionDiv.style.backgroundColor = 'White';

  promotionDiv.style.position = 'absolute';
  promotionDiv.style.left = offset.left + config.bitmapWidth/2 + 'px';
  promotionDiv.style.top = offset.top + config.bitmapHeight/2 + 'px';

  promotionDiv.innerHTML = 'Выберите фигуру:<BR>';

  var table = document.createElement('table');
  table.cellPadding = '0px';
  table.cellSpacing = '0px';

  var tbody = document.createElement('tbody');
  var tr = document.createElement('tr');

  tr.appendChild(CreatePromotionTd(div, 'q', div.position.activeSide ^ 1));
  tr.appendChild(CreatePromotionTd(div, 'r', div.position.activeSide ^ 1));
  tr.appendChild(CreatePromotionTd(div, 'b', div.position.activeSide ^ 1));
  tr.appendChild(CreatePromotionTd(div, 'n', div.position.activeSide ^ 1));

  tbody.appendChild(tr);
  table.appendChild(tbody);
  promotionDiv.appendChild(table);
  div.appendChild(promotionDiv);

  var tableOffset = CalculateAbsoluteOffset(table);
  var offset = CalculateAbsoluteOffset(promotionDiv);    
  promotionDiv.style.width = tableOffset.left - offset.left + table.offsetWidth + 'px';
  promotionDiv.style.height = tableOffset.top - offset.top + table.offsetHeight + 'px';
  div.promotionDiv = promotionDiv;
}

function TdMouseUp(e)
{
  PreventDefault(e);

  var node = GetTarget(e);
  for(;;)
  {
    node = FindParent(node, 'DIV');
    if (typeof(node) == 'undefined') return;
    if (typeof(node.dragTd) != 'undefined') break;
    node = ParentNode(node);
  }

  var div = node;
  Move(div.dragDiv, e.clientX - div.dragLastX, e.clientY - div.dragLastY);
  div.dragLastX = e.clientX;
  div.dragLastY = e.clientY;

  var divOffset = CalculateAbsoluteOffset(div);
  var dragDivOffset = CalculateAbsoluteOffset(div.dragDiv);
  var divMouseLeft = dragDivOffset.left - divOffset.left;
  var divMouseTop = dragDivOffset.top - divOffset.top;
  var x = Math.round(divMouseLeft / config.bitmapWidth);
  var y = Math.round(divMouseTop / config.bitmapHeight);
  var file = x;
  var rank = 7 - y;

  var td;
  var exInfo = new Object();
  var isOk = DoMove(div.position, div.dragTd.file, div.dragTd.rank, file, rank, exInfo);
  if (isOk)
  {
    if (TestCheck(div.position,div.position.activeSide^1)) exInfo.check=true;
    else exInfo.check=false;
    td = FindTd(div, file, rank);  
    ShowFen(get_Id(div.id + 'Span'), div.position);
  }  
  else 
    td = div.dragTd;

  td.style.backgroundImage = div.dragDiv.style.backgroundImage;
  div.removeChild(div.dragDiv);

  RemoveHandler(div, 'mousemove', TdMouseMove);
  RemoveHandler(div, 'mouseup', TdMouseUp);

  div.dragTd = null;
  div.dragDiv = null;
  div.dragLastX = null;
  div.dragLastY = null;

  if (isOk)
  {
    if (exInfo.isPromotion) {
      div.exInfo = exInfo;
      RunPromotion(div);
    }
    else
    {
      div.history.push(BuildFen(div.position));
      div.undo.disabled = false;
      div.notation.push(BuildNotation(exInfo));
      ShowNotation (get_Id('notId'), div.notation, div.notationvisibility);
    }
  }

  if (!exInfo.isSimple)
    RefreshPositionDiv(div);
}

function BuildPieceFileName(ch)
{
  switch(ch)
  {
    case 'p':
    case 'n':
    case 'b':
    case 'r':
    case 'q':
    case 'k':
      return 'b' + ch;
    case 'P':
    case 'N':
    case 'B':
    case 'R':
    case 'Q':
    case 'K':
      return 'w' + ch.toLowerCase();
    default:
      return null;
  }
}

function ClearElement(elem)
{
  while (elem.hasChildNodes())
    elem.removeChild(elem.firstChild);
}

function RefreshPositionDiv(div)
{
  var position = div.position;
  ClearElement(div);
  div.isPromotion = false;

  var table = document.createElement('table');
  table.cellPadding = '0px';
  table.cellSpacing = '0px';

  table.style.border = 'solid 1px #000000';

  var tbody = document.createElement('tbody');
  var lineLetter = new Array ('A','B','C','D','E','F','G','H');
  for(y=0; y<8; ++y)
  {
    var tr = document.createElement('tr');

    for (x=0; x<8; ++x)
    {
      var td = document.createElement('td');
      td.divId = div.id;
      td.style.width = config.bitmapWidth + 'px';
      td.style.height = config.bitmapHeight + 'px';

      var rank = 7 - y;
      var file = x;

      td.rank = rank;
      td.file = file;

      if ((file ^ rank) & 1)
      {
        td.style.backgroundColor = config.fieldColorWhite;
      }
      else {
        td.style.backgroundColor = config.fieldColorBlack;
      }

      var ch = position.board[file + 16*rank]
      var filename = BuildPieceFileName(ch);
      if (filename != null)
        td.style.backgroundImage = 'url(' + config.pieceDir + filename + config.pieceExt + ')';
      td.title=lineLetter[x]+''+(8-y);
      tr.appendChild(td);
    }

    tbody.appendChild(tr);
  }

  tr.appendChild(td);
  tbody.appendChild(tr);
  table.appendChild(tbody);
  div.appendChild(table);

  ShowFen(get_Id(div.id + 'Span'), position);  
  if (!div.isMouseDownHandler) {
   AddHandler(div, 'mousedown', TdMouseDown);      
   div.isMouseDownHandler = true;
  }  
}

function UndoClick(e)
{
  var button = GetTarget(e);
  var div = button.div;
  if (div.history.length <= 1) return;
  div.history.pop();
  div.notation.pop();
  var fen = div.history[div.history.length-1];
  div.position = BuildPosition(fen);
  RefreshPositionDiv(div);
  ShowNotation (get_Id('notId'), div.notation, div.notationvisibility);
  button.disabled = div.history.length <= 1;
}

function SetDiagram(divId, fen, undoId, notId)
{
  var div = get_Id(divId);
  var undo = get_Id(undoId);
  var notid;
  if (typeof(notId)!='string') {
   notid = document.createElement('p');
   notid.style.visibility = 'hidden';
   div.appendChild(notid);   
  }
  else notid = get_Id(notId);
  var position = BuildPosition(fen);
  if (typeof(position) == 'string')
  {
    alert('Ошибка: ' + position);
    return;
  }

  ClearElement(div);  
  div.position = position;
  RefreshPositionDiv(div);
  div.history = [fen];
  div.notation = new Array ();
  div.notationvisibility = notid.style.visibility;
  notid.innerHTML='';
  if (position.activeSide==BLACK) {
   div.notation.push ('...');
   ShowNotation(notid, div.notation, div.notationvisibility);
  }
  div.notid = notid;
  notid.div = div;

  undo.disabled = true;
  div.undo = undo;
  undo.div = div;

  if (!undo.isClickHandler)  
  {
    AddHandler(undo, 'click', UndoClick);
    undo.isClickHandler = true;
  }  
}

function SetFen(div, undo, notId) {
 var fen = prompt('Введите нотацию FEN:', '');
 if (typeof(fen) == 'string' && fen != '') {
  SetDiagram(div, fen, undo, notId);
 }
}

function ShowFen(span, position) {
 span.innerHTML = BuildFen(position);
}

function ShowNotation(area, notation, visibility) {
 if (visibility=='hidden') return;
 var n=String(notation);
 var h=n.split(',');
 if (h.length<2 && h[0]=='') { 
  area.innerHTML=''; return; 
 }
/* //Просто нотация без таблицы
 var s='';
 for (var i=0; i<h.length; i++) {
  if (i%2==0) {
   if (i>0) s+='<br>'; 
   var j=Math.round((i+1)/2);
   s+=j+'. ';
  }
  else s+=' ';
  s+=h[i];
 }
 area.innerHTML=s;
*/
 ClearElement(area);
 var table = document.createElement('table');
 table.cellPadding = '2px';
 table.cellSpacing = '0px';
 table.border = '0px';
 var tbody = document.createElement('tbody');
 var tr,td1,td2,td3;
 for (var i=0; i<h.length; i+=2) {
  tr = document.createElement('tr');
  var td1 = document.createElement('td');
  td1.innerHTML=Math.round((i+1)/2);
  var td2 = document.createElement('td');
  td2.innerHTML=h[i];
  var td3 = document.createElement('td');
  if (i+1<h.length) td3.innerHTML=h[i+1];
  else td3.innerHTML='&nbsp;';
  for (var k=1; k<4; k++) tr.appendChild (eval('td'+k));  
  tbody.appendChild(tr);
 }
 table.appendChild(tbody);
 area.appendChild(table);
}</script>
<script type="text/javascript">
 config.pieceDir = "http://chessinvin.ucoz.ru/jscript/chessJs/" + config.pieceDir;
</script>
</head><body><form name="chessFormId" id="chessFormId">
 <div align="center">
  <div id="chessDivId" style="width:328px"><table cellpadding="0px" cellspacing="0px" style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-image: initial; "><tbody><tr><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/br.png); " title="A8"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bn.png); " title="B8"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bb.png); " title="C8"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bq.png); " title="D8"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bk.png); " title="E8"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bb.png); " title="F8"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bn.png); " title="G8"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/br.png); " title="H8"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="A7"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="B7"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="C7"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="D7"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="E7"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="F7"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="G7"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/bp.png); " title="H7"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="A6"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="B6"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="C6"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="D6"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="E6"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="F6"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="G6"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="H6"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="A5"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="B5"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="C5"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="D5"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="E5"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="F5"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="G5"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="H5"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="A4"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="B4"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="C4"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="D4"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="E4"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="F4"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="G4"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="H4"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="A3"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="B3"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="C3"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="D3"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="E3"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="F3"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); " title="G3"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); " title="H3"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="A2"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="B2"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="C2"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png) width="30" height="34"; " title="D2"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="E2"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="F2"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="G2"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wp.png); " title="H2"></td></tr><tr><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wr.png); " title="A1"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wn.png); " title="B1"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wb.png); " title="C1"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wq.png); " title="D1"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wk.png); " title="E1"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wb.png); " title="F1"></td><td style="width: 41px; height: 41px; background-color: rgb(136, 136, 136); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wn.png); " title="G1"></td><td style="width: 41px; height: 41px; background-color: rgb(238, 238, 238); background-image: url(http://chessinvin.ucoz.ru/jscript/chessJs/pieces/wr.png); " title="H1"></td></tr></tbody></table></div>
  <p><span id="chessDivIdSpan" style="color:green">rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1</span></p>
  <input id="chessUndoId" type="button" value="Отменить ход" disabled="">
  <input name="btnSetFen" type="button" value="Задать FEN" onClick="SetFen(&#39;chessDivId&#39;, &#39;chessUndoId&#39;, &#39;notId&#39;)">
  <input type="submit" name="proberka" value="Проверить" onClick="Proverka()">
  <p id="notId"></p>
 </div>
</form>
<script type="text/javascript">
 SetDiagram("chessDivId", 
  "1r3r1k/6pp/7b/p1pQ2N1/PpB5/1P6/1Pq2PPP/5RK1 w - -", 
  "chessUndoId","notId");
</script>
<script type="text/javascript">
function Proverka()
var pozitcia = 1r4rk/5Npp/7b/p1p5/PpB5/1P6/1Pq2PPP/5RK1 b - -
if (parseInt (chessDivIdSpan=pozitcia)) 
{document.write ('Решение верное'}
else {document.write ('Решение неверное'}
</script>
</body>
8
  • 3
    @ChessMen, попробуйте конкретизировать проблему. Столько кода вряд ли кто-то будет разбирать.
    – angry
    29 июн 2012 в 16:11
  • 5
    O_0 @ChessMen, я ж надеюсь, что это только первая часть кода? Будут еще несколько?
    – Deonis
    29 июн 2012 в 16:20
  • Я думаю, если разбить на части, смысл потеряется. Проблема в следующем. Мне надо сверить EPD получившийся с EPD верным (заранее прописанным). Я попытался это сделать, но не вышло <script type="text/javascript"> function Proverka() var pozitcia = 1r4rk/5Npp/7b/p1p5/PpB5/1P6/1Pq2PPP/5RK1 b - - if (parseInt (chessDivIdSpan=pozitcia)) {document.write ('Решение верное'} else {document.write ('Решение неверное'} </script> Проблема исключительно в сравнении, совпадает ли EPD полученный с EPD перным
    – ChessMen
    29 июн 2012 в 16:24
  • 5
    у меня одного так?
    – Spectre
    29 июн 2012 в 16:45
  • @Spectre, да, наверно у одного. Потому как у меня совсем другая картина вырисовывается.
    – Deonis
    29 июн 2012 в 19:05

1 ответ 1

6

Не разбираясь во всем коде, а посмотрев только функцию проверки в конце можно обнаружить кучу ошибок - остальной код даже не хочется смотреть после этого :)

function Proverka()
var pozitcia = 1r4rk/5Npp/7b/p1p5/PpB5/1P6/1Pq2PPP/5RK1 b - -
if (parseInt (chessDivIdSpan=pozitcia)) 
{document.write ('Решение верное'}
else {document.write ('Решение неверное'}

Вам надо сравнить одну строку со второй (EPD в вашем случае простая текстовая строка).

var pozitcia = 1r4rk/5Npp/7b/p1p5/PpB5/1P6/1Pq2PPP/5RK1 b - - //ошибка, значение должно быть в кавычках.

chessDivIdSpan=pozitcia //ошибка, вы не сравниваете, а присваиваете новое значение - должно быть "==" вместо "="

parseInt () // получает число, то есть из строки будет только первый символ (вернее обрежется до первой не цифры) - думаю тоже ошибка

Это не говоря уже об отсутствующих {} в объявлении функции.

Совет - начните изучать javascript, а потом уже что-то правьте "под себя".

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.