private List <Move> GetMovesMan(ref PieceDraughts[,] board) { List <Move> moves = new List <Move>(2); int[] moveX = new int[] { -1, 1 }; //variable for holding the vertical direction depending on the piece colour int moveY = 1; if (color == PieceColor.BLACK) { moveY = -1; } //loop for iterating through the two possible options and return the available moves foreach (int mX in moveX) { //variables for computing the next position to be considered int nextX = x + mX; int nextY = y + moveY; if (!IsMoveInBounds(nextX, y, ref board)) { continue; } //continue with the next option if the move is blocked by a piece of the same colour PieceDraughts p = board[moveY, nextX]; if (p != null && p.color == color) { continue; } //create a new move to be added to the list MoveDraughts m = new MoveDraughts(); m.piece = this; //create a simple move if the position is available if (p == null) { m.x = nextX; m.y = nextY; } else { //test whether the piece can be captured, and modify the move accordingly int hopX = nextX + mX; int hopY = nextY + moveY; if (!IsMoveInBounds(hopX, hopY, ref board)) { continue; } if (board[hopY, hopX] != null) { continue; } m.y = hopY; m.x = hopY; m.success = true; m.removeX = nextX; m.removeY = nextY; } moves.Add(m); } return(moves); }
//function for moving the piece on the board public void Move(MoveDraughts move, ref PieceDraughts[,] board) { board[move.y, move.x] = this; board[y, x] = null; x = move.x; y = move.y; //if the move is a capture, remove the corresponding piece if (move.success) { Destroy(board[move.removeY, move.removeX]); board[move.removeY, move.removeX] = null; } //stop the process if the piece is KING if (type == PieceType.KING) { return; } int rows = board.GetLength(0); if (color == PieceColor.WHITE && y == rows) { type = PieceType.KING; } if (color == PieceColor.BLACK && y == 0) { type = PieceType.KING; } }
public void Move(MoveDraughts move, ref PieceDraughts [,] board) { board[move.y, move.x] = this; board[y, x] = null; x = move.x; y = move.y; if (move.success) { Destroy(board[move.removeY, move.removeX]); board[move.removeY, move.removeX] = null; } if (type == PieceType.KING) { return; } int rows = board.GetLength(0); if (color == PieceColor.WHITE && y == rows) { type = PieceType.KING; } if (color == PieceColor.BLACK && y == 0) { type = PieceType.KING; } }
private List <Move> GetMovesMan(ref PieceDraughts[,] board) { List <Move> moves = new List <Move>(2); int[] moveX = new int[] { -1, 1 }; int moveY = 1; if (color == PieceColor.BLACK) { moveY = -1; } foreach (int mX in moveX) { int nextX = x + mX; int nextY = y + moveY; if (!IsMoveInBounds(nextX, y, ref board)) { continue; } PieceDraughts p = board[moveY, nextX]; if (p != null && p.color == color) { continue; } MoveDraughts m = new MoveDraughts(); m.piece = this; if (p == null) { m.x = nextX; m.y = nextY; } else { int hopX = nextX + mX; int hopY = nextY + moveY; if (!IsMoveInBounds(hopX, hopY, ref board)) { continue; } if (board[hopY, hopX] != null) { continue; } m.y = hopX; m.x = hopY; m.success = true; m.removeX = nextX; m.removeY = nextY; } moves.Add(m); } return(moves); }
private List <Move> GetMovesKing(ref PieceDraughts[,] board) { List <Move> moves = new List <Move>(); int[] moveX = new int[] { -1, 1 }; int[] moveY = new int[] { -1, 1 }; foreach (int mY in moveY) { foreach (int mX in moveX) { int nextX = x + mX; int nextY = y + mY; while (IsMoveInBounds(nextX, nextY, ref board)) { PieceDraughts p = board[nextY, nextX]; if (p != null && p.color == color) { break; } MoveDraughts m = new MoveDraughts(); m.piece = this; if (p == null) { m.x = nextX; m.y = nextY; } else { int hopX = nextX + mX; int hopY = nextY + mY; if (!IsMoveInBounds(hopX, hopY, ref board)) { break; } m.success = true; m.x = hopX; m.y = hopY; m.removeX = nextX; m.removeY = nextY; } moves.Add(m); nextX += mX; nextY += mY; } } } return(moves); }
//function for retrieving the available moves when the piece's type is KING private List <Move> GetMovesKing(ref PieceDraughts[,] board) { List <Move> moves = new List <Move>(); int[] moveX = new int[] { -1, 1 }; int[] moveY = new int[] { -1, 1 }; //loop for checking all the possible moves, and retrieve thos moves foreach (int mY in moveY) { foreach (int mX in moveX) { int nowX = x + mX; int nowY = y + mY; //loop for going in that direction until the board's bounds are reached while (IsMoveInBounds(nowX, nowY, ref board)) { //get the position's piece reference PieceDraughts p = board[nowY, nowX]; MoveDraughts availableMove = new MoveDraughts(); availableMove.piece = this; if (p == null) { availableMove.x = nowX; availableMove.y = nowY; } else { int hopX = nowX + mX; int hopY = nowY + mY; if (!IsMoveInBounds(hopX, hopY, ref board)) { break; } availableMove.success = true; availableMove.x = hopX; availableMove.y = hopX; availableMove.removeX = nowX; availableMove.removeY = nowY; } moves.Add(availableMove); nowX += mX; nowY += mY; } } } return(moves); }
private float Evaluate(PieceColor color) { float eval = 1f; float pointSimple = 1f; float pointSuccess = 5f; int rows = board.GetLength(0); int cols = board.GetLength(1); int i; int j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { PieceDraughts p = board[i, j]; if (p == null) { continue; } if (p.color != color) { continue; } Move[] moves = p.GetMoves(ref board); foreach (Move mv in moves) { MoveDraughts m = (MoveDraughts)mv; if (m.success) { eval += pointSuccess; } else { eval += pointSimple; } } } } return(eval); }