private static void ChessMove(ChessGame game, BoardInternal game2, string move, Validation validate) { MoveModel mm = validate.GetMoveCoordsAndPiece(game, move); if (mm.srcval == " ") { Message.Append("ERROR No piece to move from that square! :" + move + " "); } else if (!validate.IsWhiteOrBlackToMove(game, move)) { if (game.Moves.Count % 2 == 0) { Message.Append("ERROR White's move! :" + move + " "); } else { Message.Append("ERROR Black's move! :" + move + " "); } } else if (validate.IsPieceMoveValid(game, move) && validate.IsClearPath(game, game2, move) && !validate.DoesMovePutSelfInCheck(move)) { if (validate.StorePieceMovePiece(game, move)) { game.Moves.Add(move); } } else { Message.Append("ERROR Invalid move :" + move + " "); } }
////// /// public static void DisplayBoard(BoardInternal intBoard) { if (intBoard.Moves.Count % 2 == 0) //then its even or zero { Console.WriteLine("╔══╦══╦══╦══╦══╦══╦══╦══╗ WHITE to move..."); } else { Console.WriteLine("╔══╦══╦══╦══╦══╦══╦══╦══╗ BLACK to move..."); } for (int R = 0; R < 8; R++) { for (int F = 0; F < 8; F++) { if (!intBoard.FLIPPED) { Console.Write("║" + GetPiece(intBoard.Board[R, F])); } else { Console.Write("║" + GetPiece(intBoard.Board[7 - R, 7 - F])); } if (F == 7) { if (!intBoard.FLIPPED) { Console.Write("║ " + (8 - R).ToString()); } else { Console.Write("║ " + (R + 1).ToString()); } Console.Write("\n"); } } if (R < 7) { Console.WriteLine("╠══╬══╬══╬══╬══╬══╬══╬══╣"); } } Console.WriteLine("╚══╩══╩══╩══╩══╩══╩══╩══╝"); if (!intBoard.FLIPPED) { Console.WriteLine(" a b c d e f g h Pieces taken: " + intBoard.PiecesTaken); } else { Console.WriteLine(" h g f e d c b a Pieces taken: " + intBoard.PiecesTaken); } Console.WriteLine(); }
private static void ChessMove2(BoardInternal game, string move, Validation validate) { //if (validate.IsPieceMoveValid(game, move) // && validate.IsClearPath(game, move) // && validate.DoesMovePutSelfInCheck(move)) //{ // if (validate.StorePieceMovePiece(game, move)) // { // game.Moves.Add(move); // } //} //else //{ // Message.Append("Invalid move :" + move + " "); //} }
private static void CommandMove2(BoardInternal game, string move) { if (move.StartsWith("q")) //quit the game { PLAYING = false; } else if (move.StartsWith("r")) //reset { game.FLIPPED = false; game.Board = game.InitialiseGame(); game.Moves.Clear(); game.PiecesTaken.Clear(); } else if (move.StartsWith("f")) //flip the board over { game.FLIPPED = !game.FLIPPED; } else if (move.StartsWith("s")) //save { // save the game - TODO } }
public static void Main() { string LowerCaseMove; Validation validate = new Validation(); ChessGame Game1 = new ChessGame(); BoardInternal Game2 = new BoardInternal(); //test comment while (PLAYING) { CommonUtils.DisplayBoard(Game2); WriteBoard(Game1); Message.Clear(); LowerCaseMove = Console.ReadLine().ToLower(); if (validate.IsFormatValid(LowerCaseMove)) { if (LowerCaseMove == "r" || LowerCaseMove == "f" || LowerCaseMove == "s" || LowerCaseMove == "q") { CommandMove(Game1, LowerCaseMove); CommandMove2(Game2, LowerCaseMove); } else { ChessMove(Game1, Game2, LowerCaseMove, validate); ChessMove2(Game2, LowerCaseMove, validate); } } else { Message.Append("Invalid move :" + LowerCaseMove + " "); } } }
public bool IsClearPath(ChessGame game, BoardInternal game2, string move) //is the path clear? { var src = move.Substring(0, 2); var dst = move.Substring(2, 2); //real in memory coordinates.. int srank = GetRank(src); //source rank int sfile = GetFile(src); //source file int drank = GetRank(dst); //dest rank int dfile = GetFile(dst); //dest file //if its any piece moving just one square then no spaces in between to check if ((Math.Abs(srank - drank) <= 1) && (Math.Abs(sfile - dfile) <= 1)) { return(true); } //if a knight is moving return true as they just jump over things so dont need to check spaces if (" N n".Contains(GetVal(game.Board, move.Substring(0, 2)))) { return(true); } //check spaces in between all other moves ///////// horizontal? if (Math.Abs(srank - drank) == 0) // y(rank) coordinate of src to dst not varying so moving horizontally { if (sfile < dfile) //x(file) moving right { for (int i = sfile + 1; i < dfile; i++) //so move along to the rank to the right by incrementing i { if (GetVal(game.Board, srank, i) != " ") //checking each square for blank { return(false); //if its not blank then something is in the way } } return(true); } else //moving to left { for (int i = sfile - 1; i > dfile; i--) //so move along to the rank to the left by decrementing i { if (GetVal(game.Board, srank, i) != " ") //checking each square for blank { return(false); //if its not blank then something is in the way } } return(true); } } ////////////////////// ///////// vertical? if (Math.Abs(sfile - dfile) == 0) // x(file) coordinate of src to dst not varying so moving vertically { if (srank > drank) //y(rank) moving down { for (int i = srank - 1; i > drank; i--) //so move up the file by decrementing i { if (GetVal(game.Board, i, sfile) != " ") //checking each square for blank { return(false); //if its not blank then something is in the way } } return(true); } else //moving up the file { for (int i = srank + 1; i < drank; i++) //so move down the file by decrementing i { if (GetVal(game.Board, i, sfile) != " ") //checking each square for blank { return(false); //if its not blank then something is in the way } } return(true); } } //diagonal ? if the absolute difference between the source rank and the destination rank is the same // as the file source and destination difference then it is diagonal movement int j; if (Math.Abs(srank - drank) == Math.Abs(sfile - dfile)) //this means it is diagonal movement { if (srank < drank) //moving down the board (from the black side to the white side - source rank < destination rank) { if (sfile < dfile) //so, moving generally from 0,0 towards 7,7 (in memory coordinates) top left to bottom right { j = sfile + 1; //start checks on the second square as the start square is where the piece is coming from for (int i = srank + 1; i < drank; i++) //move along the diagonal with increasing rank, dont need to check the last square.. that has already been done in a previous method { if (GetVal(game.Board, i, j) != " ") //checking for non blank square { return(false); } j++; //increment the file coordinate } } else { //so, moving generally from 0,7 towards 7,0 (in memory coordinates) top right to bottom left j = sfile - 1; for (int i = srank + 1; i < drank; i++) //move along the diagonal with increasing rank { if (GetVal(game.Board, i, j) != " ") //checking for non blank square { return(false); } j--; //decrement the file coordinate } } return(true); } if (srank > drank) //moving up the board (from the white side to the black side - source rank > destination rank) { if (sfile < dfile) //so, moving generally from 7,0 towards 0,7 (in memory coordinates) bottom left to top right { j = sfile + 1; for (int i = srank - 1; i > drank; i--) //move along the diagonal with decreasing rank { if (GetVal(game.Board, i, j) != " ") //checking for non blank square { return(false); } j++; //increment the file coordinate } } else { //so, moving generally from 7,7 towards 0,0 (in memory coordinates) bottom right to top left j = sfile - 1; for (int i = srank - 1; i > drank; i--) //move down along the diagonal with decreasing rank { if (GetVal(game.Board, i, j) != " ") //checking for non blank square { return(false); } j--; //decrement the file coordinate } } return(true); } } return(true); }