private bool PathGen(Position fromPos, Position toPos) { bool isValid; int xchange = fromPos.x - toPos.x, ychange = fromPos.y - toPos.y; int xsinglechange, ysinglechange; xsinglechange = (xchange == 0) ? 0 : ((xchange < 0) ? 1 : -1); ysinglechange = (ychange == 0) ? 0 : ((ychange < 0) ? 1 : -1); Position looppos = PosTools.getPos(fromPos.x + xsinglechange, fromPos.y + ysinglechange); isValid = true; while ((looppos.x != toPos.x || looppos.y != toPos.y) && isValid) { if (chessBoard[looppos.x, looppos.y] != null) { isValid = false; } looppos.x += xsinglechange; looppos.y += ysinglechange; } return(isValid); }
void PlayChess(Game chessGame) { Position fromPos = null, toPos = null; chessGame.Init(); while (chessGame.winner == ChessPieceColor.None) { chessGame.Display(); Console.WriteLine($"{chessGame.whoIsActive}, you're up!\n"); do { fromPos = PosTools.ReadPosition("From: "); toPos = PosTools.ReadPosition("To: "); } while (!chessGame.CheckMove(fromPos, toPos)); chessGame.MovePawn(fromPos, toPos); Console.WriteLine("Press any key to swap turns"); Console.ReadKey(); } chessGame.Display(); Console.WriteLine($"{chessGame.winner} won the game!"); }