necesito ayuda para terminar un juego de ajedrez

Iniciado por xXedilXx, 22 Diciembre 2009, 14:31 PM

0 Miembros y 1 Visitante están viendo este tema.

xXedilXx

me podrian ayudar a validar los movimientos de todas las piezas de ajedrez me falta eso para terminar el juego lo estoy haciendo en vb .net les agradeceria su ayuda.

desde ya muchas gracias..

MCKSys Argentina

Hola!

Pues esto está en C, pero podrías sacar la "idea" de la función:

TChessBoard::VerifyMove(TMove Move)
{
// The purpose is to determine whether the move is legal or not
// by verifying correct movement of the piece and whether the
// resulting position leaves the mover in check.
int i, Piece, diff;
int From, To;
int Checked;
TMoveList List;
From=Move.From;
To=Move.To;
if (From/10<1 || From/10>8 || From%10<1 || From%10>8) return 299;
if (To/10<1 || To/10>8 || To%10<1 || To%10>8) return 300;
Piece=Square[From];
if (From==To) return 301;
if (Piece<EMPTY && Square[MOVER]==BLACK) return 302;
if (Piece>EMPTY && Square[MOVER]==WHITE) return 303;
if (Piece==EMPTY) return 304;
if (Square[MOVER]==WHITE && Square[To]<EMPTY) return 305;
if (Square[MOVER]==BLACK && Square[To]>EMPTY) return 306;
switch(Piece)
{
  case WK : if ((abs(GetX(To)-GetX(From))>1 || abs(GetY(To)-GetY(From))>1) && !(From==51 && (To==71 || To==31))) return 1;
if (From==51 && To==71)
{
  if (Square[61]!=EMPTY || Square[71]!=EMPTY) return 13;
  if (Square[WROOK8MOVED]==TRUE) return 3;
  if (Square[WKINGMOVED]==TRUE) return 4;
  if (IsCheck()) return 22;
  Square[61]=WK; Square[51]=EMPTY;
  if (IsCheck()) {
Square[51]=WK; Square[61]=EMPTY;
return 21;
  }
  Square[51]=WK; Square[61]=EMPTY;
}
if (From==51 && To==31)
{
  if (Square[41]!=EMPTY || Square[31]!=EMPTY || Square[21]!=EMPTY) return 13;
  if (Square[WROOK1MOVED]==TRUE) return 3;
  if (Square[WKINGMOVED]==TRUE) return 4;
  if (IsCheck()) return 22;
  Square[41]=WK; Square[51]=EMPTY;
  if (IsCheck()) {
Square[51]=WK; Square[41]=EMPTY;
return 21;
  }
  Square[51]=WK; Square[41]=EMPTY;
}
break;
  case BK : if ((abs(GetX(To)-GetX(From))>1 || abs(GetY(To)-GetY(From))>1) && !(From==58 && (To==78 || To==38))) return 1;
if (From==58 && To==78)
{
  if (Square[68]!=EMPTY || Square[78]!=EMPTY) return 13;
  if (Square[BROOK8MOVED]==TRUE) return 3;
  if (Square[BKINGMOVED]==TRUE) return 4;
  if (IsCheck()) return 22;
  Square[68]=BK; Square[58]=EMPTY;
  if (IsCheck()) {
Square[58]=BK; Square[68]=EMPTY;
return 21;
  }
  Square[58]=BK; Square[68]=EMPTY;
}
if (From==58 && To==38)
{
  if (Square[48]!=EMPTY || Square[38]!=EMPTY || Square[28]!=EMPTY) return 13;
  if (Square[BROOK1MOVED]==TRUE) return 3;
  if (Square[BKINGMOVED]==TRUE) return 4;
  if (IsCheck()) return 22;
  Square[48]=BK; Square[58]=EMPTY;
  if (IsCheck()) {
Square[58]=BK; Square[48]=EMPTY;
return 21;
  }
  Square[58]=BK; Square[48]=EMPTY;
}
break;
  case WR : if (GetX(From)!=GetX(To) && GetY(From)!=GetY(To)) return 6;
if (GetX(From)==GetX(To)) {
  if (GetY(To)>GetY(From)) {
  for (i=From+1; i<=To; i++) if (Square[i]<10) return 14; else if (Square[i]>100 && i!=To) return 14;
  } else {
  for (i=From-1; i>=To; i--) if (Square[i]<10) return 14; else if (Square[i]>100 && i!=To) return 14;
  }
} else {
  if (GetX(To)>GetX(From)) {
  for (i=From+10; i<=To; i+=10) if (Square[i]<10) return 14; else if (Square[i]>100 && i!=To) return 14;
  } else {
  for (i=From-10; i>=To; i-=10) if (Square[i]<10) return 14; else if (Square[i]>100 && i!=To) return 14;
  }
}
break;
  case BR : if (GetX(From)!=GetX(To) && GetY(From)!=GetY(To)) return 6;
if (GetX(From)==GetX(To)) {
  if (GetY(To)>GetY(From)) {
  for (i=From+1; i<=To; i++) if (Square[i]>100) return 14; else if (Square[i]<10 && i!=To) return 14;
  } else {
  for (i=From-1; i>=To; i--) if (Square[i]>100) return 14; else if (Square[i]<10 && i!=To) return 14;
  }
} else {
  if (GetX(To)>GetX(From)) {
  for (i=From+10; i<=To; i+=10) if (Square[i]>100) return 14; else if (Square[i]<10 && i!=To) return 14;
  } else {
  for (i=From-10; i>=To; i-=10) if (Square[i]>100) return 14; else if (Square[i]<10 && i!=To) return 14;
  }
}
break;
  case WQ : if (!
  (
(From>To && (From-To)%9==0 && GetY(To)>GetY(From)) ||
(From>To && (From-To)%11==0 && GetY(From)>GetY(To)) ||
(To>From && (To-From)%11==0 && GetY(To)>GetY(From)) ||
(To>From && (To-From)%9==0 && GetY(From)>GetY(To)) ||
(GetX(From)==GetX(To)) ||
(GetY(From)==GetY(To))
  )
  ) return 7;
if (GetX(From)==GetX(To) && To>From) for (i=From+1; i<=To; i++) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (GetX(From)==GetX(To) && From>To) for (i=From-1; i>=To; i--) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (GetY(From)==GetY(To) && To>From) for (i=From+10; i<=To; i+=10) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (GetY(From)==GetY(To) && From>To) for (i=From-10; i>=To; i-=10) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (From>To && (From-To)%11==0) for (i=From-11; i>=To; i-=11) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (To>From && (To-From)%11==0) for (i=From+11; i<=To; i+=11) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (From>To && (From-To)%9==0) for (i=From-9; i>=To; i-=9) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
if (To>From && (To-From)%9==0) for (i=From+9; i<=To; i+=9) if (Square[i]<10 || (Square[i]>100 && i!=To)) return 20;
break;
  case BQ : if (!
  (
(From>To && (From-To)%9==0 && GetY(To)>GetY(From)) ||
(From>To && (From-To)%11==0 && GetY(From)>GetY(To)) ||
(To>From && (To-From)%11==0 && GetY(To)>GetY(From)) ||
(To>From && (To-From)%9==0 && GetY(From)>GetY(To)) ||
(GetX(From)==GetX(To)) ||
(GetY(From)==GetY(To))
  )
  ) return 7;
if (GetX(From)==GetX(To) && To>From) for (i=From+1; i<=To; i++) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (GetX(From)==GetX(To) && From>To) for (i=From-1; i>=To; i--) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (GetY(From)==GetY(To) && To>From) for (i=From+10; i<=To; i+=10) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (GetY(From)==GetY(To) && From>To) for (i=From-10; i>=To; i-=10) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (From>To && (From-To)%11==0) for (i=From-11; i>=To; i-=11) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (To>From && (To-From)%11==0) for (i=From+11; i<=To; i+=11) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (From>To && (From-To)%9==0) for (i=From-9; i>=To; i-=9) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
if (To>From && (To-From)%9==0) for (i=From+9; i<=To; i+=9) if (Square[i]>100 || (Square[i]<10 && i!=To)) return 20;
break;
  case WP : if (GetY(From)>GetY(To)) return 16; // moving backwards?
// attempting capture?
if (Square[To]>100)
  if (To-From!=11 && From-To!=9) return 9; else break;
// attempting 2 squares?
if (GetY(To)-GetY(From)==2 && (GetX(To)==GetX(From))) {
if (GetY(From)!=2) return 10; // not on correct rank
else if (Square[From+1]!=EMPTY) return 17; // attempted jump
else break;
}
// attempting to move diagonally (if for capture, would not get this far)
if (GetX(From)!=GetX(To)) {
if (To-From==11 || From-To==9) { // No enemy, en-passant perhaps?
if (GetX(To)==Square[ENPAWN] && GetY(To)==6) break;
else return 18;
} else
return 8;
} else {  // must now be going straight forward, no capturing and no 2-square attempt
if (GetY(To)-GetY(From)>1) return 8; else
if (Square[To]>100) return 11; else // can't capture like this
break;
}
  case BP : if (GetY(From)<GetY(To)) return 16;  // moving backwards?
// attempting capture?
if (Square[To]<10)
  if (From-To!=11 && To-From!=9) return 9; else break;
// attempting 2 squares?
if (GetY(From)-GetY(To)==2 && (GetX(To)==GetX(From))) {
if (GetY(From)!=7) return 10; // not on correct rank
else if (Square[From-1]!=EMPTY) return 17; // attempted jump
else break;
}
// attempting to move diagonally (if for capture, would not get this far)
if (GetX(From)!=GetX(To)) {
if (From-To==11 || To-From==9) { // No enemy, en-passant perhaps?
if (GetX(To)==Square[ENPAWN] && GetY(To)==3) break;
else return 18;
} else
return 8;
} else {  // must now be going straight forward, no capturing and no 2-square attempt
if (GetY(From)-GetY(To)>1) return 8; else
if (Square[To]<10) return 11; else // can't capture like this
break;
}
  case WB : if ((To>From) && GetY(To)>GetY(From)) { // North-East
  if (abs(From-To)%11!=0) return 12;
  for (i=From+11; i<=To; i+=11) if (Square[i]<10) return 15; else if (Square[i]>100 && i!=To) return 15;
} else
if ((To>From) && GetY(To)<GetY(From)) { // South-East
  if (abs(From-To)%9!=0) return 12;
  for (i=From+9; i<=To; i+=9) if (Square[i]<10) return 15; else if (Square[i]>100 && i!=To) return 15;
} else
if ((To<From) && GetY(To)>GetY(From)) { // North-West
  if (abs(From-To)%9!=0) return 12;
  for (i=From-9; i>=To; i-=9) if (Square[i]<10) return 15; else if (Square[i]>100 && i!=To) return 15;
} else
if ((To<From) && GetY(To)<GetY(From)) { // South-West
  if (abs(From-To)%11!=0) return 12;
  for (i=From-11; i>=To; i-=11) if (Square[i]<10) return 15; else if (Square[i]>100 && i!=To) return 15;
} else return 12;
break;
  case BB : if ((To>From) && GetY(To)>GetY(From)) { // North-East
  if (abs(From-To)%11!=0) return 12;
  for (i=From+11; i<=To; i+=11) if (Square[i]>100) return 15; else if (Square[i]<10 && i!=To) return 15;
} else
if ((To>From) && GetY(To)<GetY(From)) { // South-East
  if (abs(From-To)%9!=0) return 12;
  for (i=From+9; i<=To; i+=9) if (Square[i]>100) return 15; else if (Square[i]<10 && i!=To) return 15;
} else
if ((To<From) && GetY(To)>GetY(From)) { // North-West
  if (abs(From-To)%9!=0) return 12;
  for (i=From-9; i>=To; i-=9) if (Square[i]>100) return 15; else if (Square[i]<10 && i!=To) return 15;
} else
if ((To<From) && GetY(To)<GetY(From)) { // South-West
  if (abs(From-To)%11!=0) return 12;
  for (i=From-11; i>=To; i-=11) if (Square[i]>100) return 15; else if (Square[i]<10 && i!=To) return 15;
} else return 12;
break;
  case WN : diff=abs(From-To);
if (diff!=12 && diff!=8 && diff!=21 && diff!=19) return 5;
break;
  case BN : diff=abs(From-To);
if (diff!=12 && diff!=8 && diff!=21 && diff!=19) return 5;
break;
}
// Is the mover in check once this move is made?
check_for_check:
// BackUpBoard(BACKUP);
memcpy(BackUpSquare, Square, sizeof(int)*89);
MakeMove(Move, FALSE);
if (Square[MOVER]==WHITE) Square[MOVER]=BLACK; else Square[MOVER]=WHITE; // Want to see check from point of view on non-Square[MOVER]
Checked=IsCheck();
memcpy(Square, BackUpSquare, sizeof(int)*89);
// BackUpBoard(RESTORE);
if (Checked) return 2;
else return 0;
} // end of TChessBoard::VerifyMove(TMove)


Código fuente del juego completo:
http://www.redhotpawn.com/rival/download/downloader.php?file=rival-source-1.8.28.zip

Saludos!

MCKSys Argentina

"Si piensas que algo está bien sólo porque todo el mundo lo cree, no estás pensando."


seba123neo

busca en PSC hay un monton de "motorcitos" de ajedrez y algunos basicos como para empezar, hay algunos que ni siquiera tienen motor sino que validan los movimientos de las piezas, eso te va a servir para empezar, esto es visual basic 6 te lo muevo a .NET.

PD:los ejemplos que te dije de PSC estan en visual basic 6, pero proba buscar en .NET seguro hay alguno.
La característica extraordinaria de las leyes de la física es que se aplican en todos lados, sea que tú elijas o no creer en ellas. Lo bueno de las ciencias es que siempre tienen la verdad, quieras creerla o no.

Neil deGrasse Tyson

Skeletron

NO debe ser dificil, pero si tendrias que organizarte bien..

Crea una matriz de [ x ] [ y ]

Y valida la posicion anterior, con la final.

Si la piesa movida es CABALLO, y estaba en [2][3], y el movimiento final queda en: [x+2][y+1] o [x+2][y-1] o [x+1][y+2] o [x+1][y-2].. etc... ACEPTAR el movimiento, sino, avisar de movimiento invalido.