This article shares the specific code for Android to implement chess game for your reference. The specific content is as follows
It is mainly to achieve two-person chess battles, but not human-machine battles. It is mainly to not judge which one will be the next move, or to score each next move and select the highest score and then which one will take.
Activity class:
public class ChessActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); //LinearLayout is wrapped with a TextView TextView text = (TextView) findViewById(); ("Chess"); LinearLayout linear = (LinearLayout) findViewById(); ChessView ccView = new ChessView(); //Custom View --- You can also use SurfaceView (requires loop draw) /android_cmos/article/details/68955134 (text); (ccView, new (.MATCH_PARENT, .MATCH_PARENT)); } }
Custom View class:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class ChessView extends View { // TODO Draw the chessboard // TODO draw chess pieces // TODO Touch Interaction --> Flag Walk --->Mobile Rules // TODO determines the winner and lose private int chessWidth = ; //The width and height of the chess piece are the same private Bitmap boardBitmap; //chessboard bitmap private int red = 0; //Red chess piece private int black = 1; //Black chess piece private int currentChessMove = red; //The current chess piece private boolean chooseStatus; //Status Whether to select a chess piece //Bai Fang: 1 car, 2 horses, 3 phases, 4 soldiers, 5 coaches, 6 guns, 7 soldiers //Black side: 14 carts, 13 horses, 12 phases, 11 soldiers, 10 coaches, 9 guns, 8 soldiers private int[] currentPosition = new int[2]; // Used to record the x,y of the previous piece Paint paint = new Paint(); Paint redPaint = new Paint(); Paint blackPaint = new Paint(); public ChessView(Context context) { this(context, null); } public ChessView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ChessView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } //Initialize something private void init() { (); (); //Set desertion (true); (45); (3); (); (true); (60); (); (); (); (true); (60); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //Set your own width and height equal int width = (widthMeasureSpec); if (chessWidth == 0) chessWidth = width / 10; ("tag", "onMeasure:width=" + chessWidth); heightMeasureSpec = (width + chessWidth, ); setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); } float touchX, touchY; // Used to record the touch point @Override public boolean onTouchEvent(MotionEvent event) { ("tag", "onTouchEvent:x=" + () + " y=" + () + " 10 * chessWidth=" + 10 * chessWidth); if (() > 10.5 * chessWidth||()!=Rules.non_win) { //No point beyond the chess board range is needed -- because there are 10 rows, and the chess pieces also occupy half of the row. return false; // Someone won't allow moving } int action = (); switch (action) { case MotionEvent.ACTION_DOWN: touchX = (); touchY = (); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: ("tag", "Move chess pieces:x=" + () + " y=" + () + " chessWidth=" + chessWidth + " touchX=" + touchX + " touchY=" + touchY); if ((() - touchX) > chessWidth || (() - touchY) > chessWidth) { ("tag", "Moving chess pieces should not be too large"); return (event); //The chess piece you want to move cannot be confirmed --- down and up coordinates are large } else { int x = (int) (() / chessWidth - 0.5f); int y = (int) (() / chessWidth - 0.5f); ("tag", "Move chess pieces:x=" + x + " y=" + y); if (y > 9 || x > 8) { return (event); } if (currentChessMove == red) { //The red chess goes if (chooseStatus == false) { //No chess piece was selected -- Start selecting if ((x, y) > 0 && (x, y) < 8) { chooseStatus = true; currentPosition[0] = x; currentPosition[1] = y; invalidate(); //Re-draw } } else { //The chess piece has been selected --- Move if ((currentPosition[0], currentPosition[1], x, y)) {//Can move the chess pieces chooseStatus = false; (currentPosition[0], currentPosition[1], x, y); currentChessMove = black; invalidate(); //Re-draw } else if ((x, y) > 0 && (x, y) < 8) { currentPosition[0] = x; //Select other chess pieces currentPosition[1] = y; invalidate(); } else { (getContext(), "Not compliant with the rules", Toast.LENGTH_SHORT).show(); } } } else { //Black Chess Goes if (chooseStatus == false) { //No chess piece selected if ((x, y) > 7 && (x, y) < 15) { chooseStatus = true; currentPosition[0] = x; currentPosition[1] = y; invalidate(); //Re-draw } } else { //The chess piece has been selected if ((currentPosition[0], currentPosition[1], x, y)) {//Can move the chess pieces chooseStatus = false; (currentPosition[0], currentPosition[1], x, y); currentChessMove = red; invalidate(); } else if ((x, y) > 7 && (x, y) < 15) { currentPosition[0] = x; //Select other chess pieces currentPosition[1] = y; invalidate(); } else { (getContext(), "Not compliant with the rules", Toast.LENGTH_SHORT).show(); } } } } break; } return true; } TextView tv; public void setText(TextView tv) { //Used to prompt information -- Show who wins and then = tv; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { //This can also obtain width and height (w, h, oldw, oldh); if (chessWidth == 0) chessWidth = getWidth() / 10; ("tag", "onSizeChanged:width=" + chessWidth); } @Override public void onDraw(Canvas canvas) { (canvas); long time = (); (("#5500ff00"); //Draw a white background canvasBoard(canvas); //Draw the chessboard --- You can optimize it, save a bitmap, and drawBitmap directly if (chooseStatus) { (); (chessWidth * (currentPosition[0] + 1), chessWidth * (currentPosition[1] + 1), chessWidth / 2 + 5, paint); } canvasChess(canvas); //Draw chess pieces int win = (); if (win != Rules.non_win) { if (tv == null) { return; } if (win == Rules.up_win) { ("The Red Side Wins"); } else { ("Black wins"); } getParent().requestDisallowInterceptTouchEvent(false); // } ("tag", "Time to use:" + (() - time) ); //Generally not more than 20ms } int widthCenter = 1; //The middle of the Chu River and Han boundary is blank, so some adjustments are needed to make the chessboard look bad int space = chessWidth / 10; // It is 0 at the beginning, and the chessWidth cannot be obtained. // int line = 40; int line = chessWidth / 3; private void canvasBoard(Canvas canvas) { if (boardBitmap == null) { space = chessWidth / 10; //Determine the interval of the lines line = chessWidth / 3; //Length of line boardBitmap = (getWidth(), getHeight(), .ARGB_8888); ("tag", "boardBitmap==null" + getWidth() + "height:" + getHeight()); Canvas bb = new Canvas(boardBitmap); (); for (int i = 0; i < 10; i++) { //Draw lines (chessWidth, chessWidth * (i + 1), chessWidth * 9, chessWidth * (i + 1), paint);//Draw horizontal lines -- 9 lines } for (int i = 0; i < 9; i++) { (chessWidth * (i + 1), chessWidth, chessWidth * (i + 1), chessWidth * 10, paint);//Draw vertical lines -- 10 columns } //The diagonal line of the painter (4 * chessWidth, 1 * chessWidth, 6 * chessWidth, chessWidth * 3, paint); (4 * chessWidth, 3 * chessWidth, 6 * chessWidth, chessWidth * 1, paint); (4 * chessWidth, 8 * chessWidth, 6 * chessWidth, chessWidth * 10, paint); (4 * chessWidth, 10 * chessWidth, 6 * chessWidth, chessWidth * 8, paint); //Draw the soldiers line -- cannon line --- The miscellaneous calculation code written can also be ignored drawDetails(bb); //Draw the Chu River and Han ---- Here is just a simple drawing (); (chessWidth + widthCenter, 5 * chessWidth + widthCenter, 9 * chessWidth - widthCenter, 6 * chessWidth - widthCenter, paint); (("#D1BD92")); String text = "Chuhe �; Rect rect = new Rect(); (text, 0, (), rect); (text, 6 * chessWidth - chessWidth - () / 2, 6 * chessWidth - chessWidth / 2 + () / 2 - 5, paint); (Canvas.ALL_SAVE_FLAG); (); (boardBitmap, 0, 0, paint); } else { ("tag", "boardBitmap is not empty"); (boardBitmap, 0, 0, paint); } } private void drawDetails(Canvas bb) { int x1, x2, y1, y2; for (int i = 0; i < 4; i++) { x1 = chessWidth + space; x2 = chessWidth + space; y1 = 4 * chessWidth - line - space; y2 = 4 * chessWidth - space; (i * 2 * chessWidth + x1, y1, i * 2 * chessWidth + x2, y2, paint); //Perpendicular (i * 2 * chessWidth + x2, y2, i * 2 * chessWidth + x2 + line, y2, paint); //Horizontal line (i * 2 * chessWidth + x2, y2 + 2 * space, i * 2 * chessWidth + x2 + line, y2 + 2 * space, paint); //Horizontal line (i * 2 * chessWidth + x1, y2 + 2 * space, i * 2 * chessWidth + x2, y2 + 2 * space + line, paint); //Perpendicular (i * 2 * chessWidth + x1, y1 + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 3 * chessWidth, paint);//Transize 3*chessWidth Y-axis plus translation value (i * 2 * chessWidth + x2, y2 + 3 * chessWidth, i * 2 * chessWidth + x2 + line, y2 + 3 * chessWidth, paint); (i * 2 * chessWidth + x2, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2 + line, y2 + 2 * space + 3 * chessWidth, paint); (i * 2 * chessWidth + x1, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 2 * space + line + 3 * chessWidth, paint); x1 = 3 * chessWidth - space; x2 = 3 * chessWidth - space; y1 = 4 * chessWidth - line - space; y2 = 4 * chessWidth - space; (i * 2 * chessWidth + x1, y1, i * 2 * chessWidth + x2, y2, paint); //Perpendicular (i * 2 * chessWidth + x2, y2, i * 2 * chessWidth + x2 - line, y2, paint); //Horizontal line (i * 2 * chessWidth + x2, y2 + 2 * space, i * 2 * chessWidth + x2 - line, y2 + 2 * space, paint); //Horizontal line (i * 2 * chessWidth + x1, y2 + 2 * space, i * 2 * chessWidth + x2, y2 + 2 * space + line, paint); //Perpendicular (i * 2 * chessWidth + x1, y1 + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 3 * chessWidth, paint); //Perpendicular (i * 2 * chessWidth + x2, y2 + 3 * chessWidth, i * 2 * chessWidth + x2 - line, y2 + 3 * chessWidth, paint); //Horizontal line (i * 2 * chessWidth + x2, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2 - line, y2 + 2 * space + 3 * chessWidth, paint); //Horizontal line (i * 2 * chessWidth + x1, y2 + 2 * space + 3 * chessWidth, i * 2 * chessWidth + x2, y2 + 2 * space + line + 3 * chessWidth, paint); //Perpendicular } //Draw the cannon line x1 = 2 * chessWidth + space; x2 = 2 * chessWidth + space; y1 = 3 * chessWidth - line - space; y2 = 3 * chessWidth - space; (x1, y1, x2, y2, paint); //Perpendicular (x2, y2, x2 + line, y2, paint); //Horizontal line (x2, y2 + 2 * space, x2 + line, y2 + 2 * space, paint); //Horizontal line (x1, y2 + 2 * space, x2, y2 + 2 * space + line, paint); //Perpendicular (x1 - 2 * space, y1, x2 - 2 * space, y2, paint);//Perpendicular (x2 - 2 * space - line, y2, x2 + line - 2 * space - line, y2, paint); //Horizontal line (x2 - 2 * space - line, y2 + 2 * space, x2 + line - 2 * space - line, y2 + 2 * space, paint); //Horizontal line (x1 - 2 * space, y2 + 2 * space, x2 - 2 * space, y2 + 2 * space + line, paint); //Perpendicular (x1, y1 + 5 * chessWidth, x2, y2 + 5 * chessWidth, paint); //Vertical line ---Unitly move downwards 5*chessWidth (x2, y2 + 5 * chessWidth, x2 + line, y2 + 5 * chessWidth, paint); //Horizontal line (x2, y2 + 2 * space + 5 * chessWidth, x2 + line, y2 + 2 * space + 5 * chessWidth, paint); //Horizontal line (x1, y2 + 2 * space + 5 * chessWidth, x2, y2 + 2 * space + line + 5 * chessWidth, paint); //Perpendicular (x1 - 2 * space, y1 + 5 * chessWidth, x2 - 2 * space, y2 + 5 * chessWidth, paint);//Perpendicular (x2 - 2 * space - line, y2 + 5 * chessWidth, x2 + line - 2 * space - line, y2 + 5 * chessWidth, paint); //Horizontal line (x2 - 2 * space - line, y2 + 2 * space + 5 * chessWidth, x2 + line - 2 * space - line, y2 + 2 * space + 5 * chessWidth, paint); //Horizontal line (x1 - 2 * space, y2 + 2 * space + 5 * chessWidth, x2 - 2 * space, y2 + 2 * space + line + 5 * chessWidth, paint); //Perpendicular (6 * chessWidth + x1, y1, 6 * chessWidth + x2, y2, paint); //Vertical line --- Move to the right uniformly 6*chessWidth+ (6 * chessWidth + x2, y2, 6 * chessWidth + x2 + line, y2, paint); //Horizontal line (6 * chessWidth + x2, y2 + 2 * space, 6 * chessWidth + x2 + line, y2 + 2 * space, paint); //Horizontal line (6 * chessWidth + x1, y2 + 2 * space, 6 * chessWidth + x2, y2 + 2 * space + line, paint); //Perpendicular (6 * chessWidth + x1 - 2 * space, y1, 6 * chessWidth + x2 - 2 * space, y2, paint);//Perpendicular (6 * chessWidth + x2 - 2 * space - line, y2, 6 * chessWidth + x2 + line - 2 * space - line, y2, paint); //Horizontal line (6 * chessWidth + x2 - 2 * space - line, y2 + 2 * space, 6 * chessWidth + x2 + line - 2 * space - line, y2 + 2 * space, paint); //Horizontal line (6 * chessWidth + x1 - 2 * space, y2 + 2 * space, 6 * chessWidth + x2 - 2 * space, y2 + 2 * space + line, paint); //Perpendicular (6 * chessWidth + x1, y1 + 5 * chessWidth, 6 * chessWidth + x2, y2 + 5 * chessWidth, paint); //Vertical line ---Unitly move 6*chessWidth to the right (6 * chessWidth + x2, y2 + 5 * chessWidth, 6 * chessWidth + x2 + line, y2 + 5 * chessWidth, paint); //Horizontal line (6 * chessWidth + x2, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2 + line, y2 + 2 * space + 5 * chessWidth, paint); //Horizontal line (6 * chessWidth + x1, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2, y2 + 2 * space + line + 5 * chessWidth, paint); //Perpendicular (6 * chessWidth + x1 - 2 * space, y1 + 5 * chessWidth, 6 * chessWidth + x2 - 2 * space, y2 + 5 * chessWidth, paint);//Perpendicular (6 * chessWidth + x2 - 2 * space - line, y2 + 5 * chessWidth, 6 * chessWidth + x2 + line - 2 * space - line, y2 + 5 * chessWidth, paint); //Horizontal line (6 * chessWidth + x2 - 2 * space - line, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2 + line - 2 * space - line, y2 + 2 * space + 5 * chessWidth, paint); //Horizontal line (6 * chessWidth + x1 - 2 * space, y2 + 2 * space + 5 * chessWidth, 6 * chessWidth + x2 - 2 * space, y2 + 2 * space + line + 5 * chessWidth, paint); //Perpendicular } private void canvasChess(Canvas canvas) { for (int y = 0; y < 10; y++) { for (int x = 0; x < 9; x++) { if ((x, y) <= 0 || (x, y) > 14) { if ((x, y) < 0) ("tag", "hava a bug"); } else { drawChess(canvas, x, y); } } } } int textWidth; int textHeight; private void drawChess(Canvas canvas, int x, int y) { //This is the place to draw chess pieces --- You can change it yourself, for example: change the appearance --> icon //Red side: 1 car 2 horses 3 phases 4 soldiers 5 coaches 6 guns 7 soldiers //Black side: 14 carts, 13 horses, 12 phases, 11 soldiers, 10 coaches, 9 guns, 8 soldiers String text = "car"; Paint chessPaint = null; if ((x, y) < 8) {//Red Square switch ((x, y)) { case 1: text = "car"; break; case 2: text = "horse"; break; case 3: text = "Mutually"; break; case 4: text = "scholar"; break; case 5: text = "handsome"; break; case 6: text = "gun"; break; case 7: text = "Soldier"; break; } chessPaint = redPaint; } else { switch (15 - (x, y)) { //Black Square case 1: text = "car"; break; case 2: text = "horse"; break; case 3: text = "Mutually"; break; case 4: text = "scholar"; break; case 5: text = "handsome"; break; case 6: text = "gun"; break; case 7: text = "Soldier"; break; } chessPaint = blackPaint; } if (textHeight == 0 || textWidth == 0) { Rect rect = new Rect(); (text, 0, (), rect); textWidth = ();//Wide text textHeight = () - 10;//The text height --- The height is relatively unmatched, and minor adjustments are needed } x += 1; y += 1; (("#D1BD92")); (chessWidth * x, chessWidth * y, chessWidth / 2, paint); // (chessWidth*j-textWidth/2,chessWidth*i-textHeight/2,chessWidth*j+textWidth/2,chessWidth*i+textHeight/2,paint); (text, chessWidth * x - textWidth / 2, chessWidth * y + textHeight / 2, chessPaint); } }
Chess moveable rules:
public class Rules { /** * Chessboard (9,8), current position (x,y), next position (x,y) * return 0 is beyond the board 1 is in place 2 The next step is on your own chess piece 3 The movement does not comply with the rules * 4 Can move to the next step */ public static class Config { public static int chessWidth = 0; //Class piece width setting } public static int[][] chessBoard = //Distribution of chessboards and chess pieces {9}[8] 10 horizontal and 9 columns { {1, 2, 3, 4, 5, 4, 3, 2, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 6, 0, 0, 0, 0, 0, 6, 0}, {7, 0, 7, 0, 7, 0, 7, 0, 7}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, //Distribution of red chess pieces //There should not be a red or black. If there is a function of changing chess, both sides may change colors, but the value of this chessboard should not change, otherwise there will be a series of problems. {0, 0, 0, 0, 0, 0, 0, 0, 0}, //Distribution of black chess pieces {8, 0, 8, 0, 8, 0, 8, 0, 8}, {0, 9, 0, 0, 0, 0, 0, 9, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {14, 13, 12, 11, 10, 11, 12, 13, 14}, }; public static int non_win = 0; public static int up_win = 1; //There should not be a distinction between red and black. There should be only a better distinction between upper and lower ones. public static int down_win = 2; public static int win() { //Judge the win or lose int x = 0, y = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 9; j++) { if (chessBoard[i][j] == 5) { x = 1; } else if (chessBoard[i][j] == 10) { y = 1; } } } if (x == y) { return non_win; } else if (x == 1 && y == 0) { return up_win; } else { return down_win; } } // Sequence// {1, 2, 3, 4, 5, 4, 3, 2, 1}, // {0, 0, 0, 0, 0, 0, 0, 0, 0}, // {0, 6, 0, 0, 0, 0, 0, 6, 0}, // {7, 0, 7, 0, 7, 0, 7, 0, 7}, // {0, 0, 0, 0, 0, 0, 0, 0, 0}, // {0, 0, 0, 0, 0, 0, 0, 0, 0}, // {8, 0, 8, 0, 8, 0, 8, 0, 8}, // {0, 9, 0, 0, 0, 0, 0, 9, 0}, // {0, 0, 0, 0, 0, 0, 0, 0, 0}, // {14, 13, 12, 11, 10, 11, 12, 13, 14}, public static int chessValue(int positionX, int positionY) { //To coordinates to array value return chessBoard[positionY][positionX]; } public static void moveChess(int fromX, int fromY, int toX, int toY) { chessBoard[toY][toX] = chessValue(fromX,fromY); chessBoard[fromY][fromX] = 0; } public static boolean canMove(int fromX, int fromY, int toX, int toY) {//0,3 0,4 //TODO Start position to destination position fromX, fromY, toX, toY This is the coordinate position ----Note that the array corresponding to the conversion to the chess piece should be paid attention to, for example: coordinate position x,y --> chessBoard[y][x] if (toX > 8 || toY > 9 || toX < 0 || toY < 0) { //Exceed the board range return false; } if (fromX == toX && fromY == toY) { //They are walking in place return false; } if (chessBoard[fromY][fromX] > 7) { //The position of a chess piece is the position of your chess piece if (chessBoard[toY][toX] > 7) { return false; } } else if (chessBoard[fromY][fromX] > 0) { if (chessBoard[toY][toX] < 8 &&chessValue(toX,toY)!=0) { return false; } } switch (chessBoard[fromY][fromX]) { case 1: //car case 14: if ((toY - fromY) > 0 && (toX - fromX) == 0) {//The vertical line if (toY > fromY) { for (int i = fromY + 1; i < toY; i++) { if (chessBoard[i][fromX] != 0) { return false; } } } else { for (int i = toY + 1; i < fromY; i++) { if (chessBoard[i][fromX] != 0) { return false; } } } return true; } else if ((toX - fromX) > 0 && (toY - fromY) == 0) { //The horizontal line if (toX > fromX) { for (int i = fromX + 1; i < toX; i++) { if (chessBoard[fromY][i] != 0) { return false; } } } else { for (int i = toX + 1; i < fromX; i++) { if (chessBoard[fromY][i] != 0) { return false; } } } return true; } break; case 2: //horse case 13: if ((toY - fromY) == 2 && (toX - fromX) == 1) { int centerY = (toY + fromY) / 2; if (chessBoard[centerY][fromX] != 0) {//There are chess pieces at the horse's hooves return false; } return true; } else if ((toY - fromY) == 1 && (toX - fromX) == 2) { int centerX = (toX + fromX) / 2; if (chessBoard[fromY][centerX] != 0) {//There are chess pieces at the horse's hooves return false; } return true; } break; case 3: //Mutually if (toY > 4) {//Cross the river return false; } else if ((toY - fromY) == 2 && (toX - fromX) == 2) { //Walk the word "field" int centerY = (toY + fromY) / 2; int centerX = (toX + fromX) / 2; if (chessBoard[centerY][centerX] != 0) {// There are chess pieces in the elephant's eye return false; } return true; } break; case 12: if (toY < 5) {//Cross the river return false; } else if ((toY - fromY) == 2 && (toX - fromX) == 2) { //Walk the word "field" int centerY = (toY + fromY) / 2; int centerX = (toX + fromX) / 2; if (chessBoard[centerY][centerX] != 0) {// There are chess pieces in the elephant's eye return false; } return true; } break; case 4: //scholar if (toY > 2 || toX < 3 || toX > 5) { //The nine-grid grid is released return false; } else if ((toX - fromX) == 1 && (toY - fromY) == 1) {//Take a diagonal line and go straight to one square return true; } break; case 11: if (toY < 7 || toX < 3 || toX > 5) { //The nine-grid grid is released return false; } else if ((toX - fromX) == 1 && (toY - fromY) == 1) {//Take a diagonal line and go straight to one square return true; } break; //handsome case 5: if (toY > 2 || toX < 3 || toX > 5) {//The nine-grid grid is released return false; } else if (((toX - fromX) + (toY - fromY)) == 1) {//Only one square return true; } break; case 10: if (toY < 7 || toX < 3 || toX > 5) {//The nine-grid grid is released return false; } else if (((toX - fromX) + (toY - fromY)) == 1) {//Only one square return true; } break; case 6: //gun case 9: int count = 0; if (chessBoard[toY][toX] == 0) { //The location you reach is an empty location if ((toY - fromY) > 0 && (toX - fromX) == 0) {//The vertical line if (toY > fromY) { for (int i = fromY + 1; i < toY; i++) { if (chessBoard[i][fromX] != 0) { return false; } } } else { for (int i = toY + 1; i < fromY; i++) { if (chessBoard[i][fromX] != 0) { return false; } } } } else if ((toX - fromX) > 0 && (toY - fromY) == 0) {//The horizontal line if (toX > fromX) { for (int i = fromX + 1; i < toX; i++) { if (chessBoard[fromY][i] != 0) { return false; } } } else { for (int i = toX + 1; i < fromX; i++) { if (chessBoard[fromY][i] != 0) { return false; } } } } return true; }else { //There is a child at the location if ((toY - fromY) > 0 && (toX - fromX) == 0) {//The vertical line if (toY > fromY) { for (int i = fromY + 1; i < toY; i++) { if (chessBoard[i][fromX] != 0) { count++; } } } else { for (int i = toY + 1; i < fromY; i++) { if (chessBoard[i][fromX] != 0) { count++; } } } } else if ((toX - fromX) > 0 && (toY - fromY) == 0) {//The horizontal line if (toX > fromX) { for (int i = fromX + 1; i < toX; i++) { if (chessBoard[fromY][i] != 0) { count++; } } } else { for (int i = toX - 1; i > fromX; i--) { if (chessBoard[fromY][i] != 0) { count++; } } } } if (count == 1) { //If there is only one chess piece interval in the middle, it's OK return true; } } break; case 7: //Soldier if ((toY - fromY) < 0) {//Back return false; } else if (fromY > 4) {//Crossing the river if (((toX - fromX) + (toY - fromY)) == 1) { //Only one square return true; } } else {//Not crossing the river if ((toY - fromY) == 1 && fromX == toX) { //Only go forward return true; } } break; case 8: if ((toY - fromY) > 0) {//Back return false; } else if (fromY <= 4) {//Crossing the river if (((toX - fromX) + (toY - fromY)) == 1) { //Only one square return true; } } else {//Not crossing the river if ((toY - fromY) == 1 && fromX == toX) { //Only go forward return true; } } break; default: //If it is other values, it is not allowed to move break; } return false; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.