/// <summary>Checks if Bishop move is legal.</summary> public bool bishopLegal(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { if (Math.Abs (x1 - x2) == Math.Abs (y1 - y2)) { if (x1 < x2) { if (y1 < y2) { int j = y1 + 1; for (int i = x1 + 1; i < x2; i++) { if (chessBoard [i, j].piece != chessman.empty) { return false; } j++; } return true; } else if (y2 < y1) { int j = y1 - 1; for (int i = x1 + 1; i < x2; i++) { if (chessBoard [i, j].piece != chessman.empty) { return false; } j--; } return true; } } else if (x2 < x1) { if (y1 < y2) { int j = y1 + 1; for (int i = x1 - 1; i > x2; i--) { if (chessBoard [i, j].piece != chessman.empty) { return false; } j++; } return true; } else if (y2 < y1) { int j = y1 - 1; for (int i = x1 - 1; i > x2; i--) { if (chessBoard [i, j].piece != chessman.empty) { return false; } j--; } return true; } } } return false; }
/// <summary>Get last move made by other player, /// false is returned if no move yet made.</summary> public bool getLastMove(gameSquare[,] chessBoard) { try { string toSend = "GETMOVE"; socket.getOut().WriteLine(toSend); socket.getOut().Flush(); string recieve; recieve = socket.getIn().ReadLine(); string[] recieveSplit; recieveSplit = recieve.Split(' '); if (recieveSplit [0] == "MOVE") { int x1 = Convert.ToInt32(recieveSplit [1]); int y1 = Convert.ToInt32(recieveSplit [2]); int x2 = Convert.ToInt32(recieveSplit [3]); int y2 = Convert.ToInt32(recieveSplit [4]); // the move made by player 2 if (chessBoard[x2, y2].piece == chessman.King) { endGame = true; } if (chessBoard[x1, y1].piece == chessman.Pawn && (x2 == 0 || x2 == 7)) { chessBoard[x1, y1].piece = chessman.Queen; } chessBoard [x2, y2] = chessBoard [x1, y1]; chessBoard [x1, y1] = new gameSquare { colour = chessmanColour.empty, piece = chessman.empty }; return(true); } else { return(false); } } catch (Exception e) { ChessActivity.OnServerFail(); return(false); } }
/// <summary>Moves a game piece.</summary> /// <remarks><para>(x1,y1) - from postion, (x2,y2) - to position</para></remarks> public bool move(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { try { // check parameters are legal if (x1 >= 0 && x1 <= 7 && x2 >= 0 && x2 <= 7 && y1 >= 0 && y1 <= 7 && y2 >= 0 && y2 <= 7) { // make sure it's your piece to move if ((chessBoard [x1, y1].colour == chessmanColour.white && isWhite) || chessBoard [x1, y1].colour == chessmanColour.black && !isWhite) { // check if the move is legal if ((chessBoard [x1, y1].colour == chessmanColour.white && chessBoard [x2, y2].colour == chessmanColour.white) || (chessBoard [x1, y1].colour == chessmanColour.black && chessBoard [x2, y2].colour == chessmanColour.black)) { return(false); } bool isLegal = false; switch (chessBoard [x1, y1].piece) { case chessman.Bishop: isLegal = bishopLegal(chessBoard, x1, y1, x2, y2); break; case chessman.King: isLegal = kingLegal(chessBoard, x1, y1, x2, y2); break; case chessman.Knight: isLegal = knightLegal(chessBoard, x1, y1, x2, y2); break; case chessman.Pawn: isLegal = pawnLegal(chessBoard, x1, y1, x2, y2); if (isLegal && (x2 == 7 || x2 == 0)) { chessBoard [x1, y1].piece = chessman.Queen; } break; case chessman.Queen: isLegal = queenLegal(chessBoard, x1, y1, x2, y2); break; case chessman.Rook: isLegal = rookLegal(chessBoard, x1, y1, x2, y2); break; } /* Android.Widget.Toast.MakeText (Android.App.Application.Context, isLegal.ToString (), * Android.Widget.ToastLength.Long).Show (); */ if (isLegal) { string toSend = "MOVE " + x1.ToString() + " " + y1.ToString() + " " + x2.ToString() + " " + y2.ToString(); if (chessBoard[x2, y2].piece == chessman.King) { endGame = true; } chessBoard [x2, y2] = chessBoard [x1, y1]; chessBoard [x1, y1] = new gameSquare { colour = chessmanColour.empty, piece = chessman.empty }; socket.getOut().WriteLine(toSend); socket.getOut().Flush(); return(true); } else { return(false); } } else { return(false); } } else { return(false); } } catch (Exception e) { ChessActivity.OnServerFail(); return(false); } }
/// <summary>Checks if Rook move is legal.</summary> public bool rookLegal(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { if (x1 == x2) { if (y2 > y1) { for (int i = y1 + 1; i < y2; i++) { if (chessBoard [x1, i].piece != chessman.empty) return false; } return true; } else { for (int i = y1 - 1; i > y2; i--) { if (chessBoard [x1, i].piece != chessman.empty) return false; } return true; } } else if (y1 == y2) { if (x2 > x1) { for (int i = x1 + 1; i < x2; i++) { if (chessBoard [i, y1].piece != chessman.empty) { return false; } } return true; } else { for (int i = x1 - 1; i > x2; i--) { if (chessBoard [i, y1].piece != chessman.empty) { return false; } } return true; } } return false; }
/// <summary>Prints and ASCI board. (Not used in current version)</summary> public string printBoard(gameSquare[,] square) { string board = ""; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { switch (square[i, j].piece) { case chessman.Bishop: board = board + "B "; break; case chessman.King: board = board + "K "; break; case chessman.Knight: board = board + "N "; break; case chessman.Pawn: board = board + "P "; break; case chessman.Queen: board = board + "Q "; break; case chessman.Rook: board = board + "R "; break; default: board = board + " "; break; } } board = board + "\n"; } return board; }
/// <summary>Checks if Queen move is legal.</summary> public bool queenLegal(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { // queen is a combination of these moves being legal return bishopLegal(chessBoard, x1, y1, x2, y2) || rookLegal(chessBoard, x1, y1, x2, y2); }
/// <summary>Checks if Pawn move is legal.</summary> public bool pawnLegal(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { if (chessBoard [x1, y1].colour == chessmanColour.white) { if (x1 == 6) { if (x1 == x2 + 2 && y1 == y2) return true; } if (chessBoard [x2, y2].piece == chessman.empty) { if (x1 == x2 + 1 && y1 == y2) return true; } else if (chessBoard [x2, y2].piece != chessman.empty && chessBoard [x2, y2].colour == chessmanColour.black) { if (x1 == x2 + 1 && ((y1 == y2 - 1) || (y1 == y2 + 1))) return true; } } else if (chessBoard [x1, y1].colour == chessmanColour.black) { if (x1 == 1) { if (x1 == x2 - 2 && y1 == y2) return true; } if (chessBoard [x2, y2].piece == chessman.empty) { if (x1 == x2 - 1 && y1 == y2) return true; } else if (chessBoard [x2, y2].piece != chessman.empty && chessBoard[x2, y2].colour == chessmanColour.white) { if (x1 == x2 - 1 && ((y1 == y2 - 1) || (y1 == y2 + 1))) return true; } } return false; }
/// <summary>Moves a game piece.</summary> /// <remarks><para>(x1,y1) - from postion, (x2,y2) - to position</para></remarks> public bool move(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { try { // check parameters are legal if (x1 >= 0 && x1 <= 7 && x2 >= 0 && x2 <= 7 && y1 >= 0 && y1 <= 7 && y2 >= 0 && y2 <= 7) { // make sure it's your piece to move if ((chessBoard [x1, y1].colour == chessmanColour.white && isWhite) || chessBoard [x1, y1].colour == chessmanColour.black && !isWhite) { // check if the move is legal if ((chessBoard [x1, y1].colour == chessmanColour.white && chessBoard [x2, y2].colour == chessmanColour.white) || (chessBoard [x1, y1].colour == chessmanColour.black && chessBoard [x2, y2].colour == chessmanColour.black)) return false; bool isLegal = false; switch (chessBoard [x1, y1].piece) { case chessman.Bishop: isLegal = bishopLegal (chessBoard, x1, y1, x2, y2); break; case chessman.King: isLegal = kingLegal (chessBoard, x1, y1, x2, y2); break; case chessman.Knight: isLegal = knightLegal (chessBoard, x1, y1, x2, y2); break; case chessman.Pawn: isLegal = pawnLegal (chessBoard, x1, y1, x2, y2); if(isLegal && (x2 == 7 || x2 == 0)) chessBoard [x1, y1].piece = chessman.Queen; break; case chessman.Queen: isLegal = queenLegal (chessBoard, x1, y1, x2, y2); break; case chessman.Rook: isLegal = rookLegal (chessBoard, x1, y1, x2, y2); break; } /* Android.Widget.Toast.MakeText (Android.App.Application.Context, isLegal.ToString (), Android.Widget.ToastLength.Long).Show (); */ if (isLegal) { string toSend = "MOVE " + x1.ToString () + " " + y1.ToString () + " " + x2.ToString () + " " + y2.ToString (); if (chessBoard[x2, y2].piece == chessman.King) endGame = true; chessBoard [x2, y2] = chessBoard [x1, y1]; chessBoard [x1, y1] = new gameSquare { colour = chessmanColour.empty, piece = chessman.empty }; socket.getOut ().WriteLine (toSend); socket.getOut ().Flush (); return true; } else { return false; } } else { return false; } } else { return false; } } catch (Exception e) { ChessActivity.OnServerFail (); return false; } }
/// <summary>Checks if Knight move is legal.</summary> public bool knightLegal(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { if (x1 == x2 - 1 || x1 == x2 + 1) { if (y1 == y2 - 2 || y1 == y2 + 2) { return true; } } else if (y1 == y2 - 1 || y1 == y2 + 1) { if (x1 == x2 - 2 || x1 == x2 + 2) { return true; } } return false; }
/// <summary>Checks if King move is legal.</summary> public bool kingLegal(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2) { if (x1 == x2) { if (y1 == y2 + 1 || y1 == y2 - 1) return true; } else if (y1 == y2) { if (x1 == x2 + 1 || x1 == x2 - 1) return true; } else { if (x1 == x2 + 1 || x1 == x2 - 1) { if (y1 == y2 + 1 || y1 == y2 - 1) return true; } } return false; }
/// <summary>Get last move made by other player, /// false is returned if no move yet made.</summary> public bool getLastMove(gameSquare[,] chessBoard) { try { string toSend = "GETMOVE"; socket.getOut ().WriteLine (toSend); socket.getOut ().Flush (); string recieve; recieve = socket.getIn ().ReadLine (); string[] recieveSplit; recieveSplit = recieve.Split (' '); if (recieveSplit [0] == "MOVE") { int x1 = Convert.ToInt32 (recieveSplit [1]); int y1 = Convert.ToInt32 (recieveSplit [2]); int x2 = Convert.ToInt32 (recieveSplit [3]); int y2 = Convert.ToInt32 (recieveSplit [4]); // the move made by player 2 if (chessBoard[x2, y2].piece == chessman.King) endGame = true; if(chessBoard[x1, y1].piece == chessman.Pawn && (x2 == 0 || x2 == 7)) chessBoard[x1, y1].piece = chessman.Queen; chessBoard [x2, y2] = chessBoard [x1, y1]; chessBoard [x1, y1] = new gameSquare { colour = chessmanColour.empty, piece = chessman.empty }; return true; } else { return false; } } catch (Exception e) { ChessActivity.OnServerFail (); return false; } }