Java Chinese chess game program exploring [3] -- legal legitimacy judgment

Source: Internet
Author: User
Legal Validity judgment


Reprinted by the author:

By: 88250

Blog: http:/blog.csdn.net/dl88250

MSN & Gmail & QQ: DL88250@gmail.com

 

This time, we should begin to determine the code of legitimacy in addition to the chessboard. These codes actually describe the rules of Chinese chess. People who have played chess should be familiar with them. Although complicated, they must be written. In addition, this piece of code has a great impact on the performance, and is the basis for the method generation and situation search described later.


Okay, I don't want to talk about it anymore. I directly paste the code, and the comments in it are clear:

/**
* This method be used to determine the specified move is valid or not.
* Firstly, let's glance at the <em> Chinese chess move rules </em>:
* <P>
* <H2> Jiang </H2>
* <Ol>
* <Li>
* It can only move straight, the distance of one
* Motion must equals to 1
* </LI>
* <Li>
* It can only move within the "base ".
* </LI>
* <Li>
* It can eat opponent's Jiang while nothing between them,
* And the "eatways" must be a line.
* </LI>
* </OL>
* </P>
* <P>
* <H2> Shi </H2>
* <Ol>
* <Li>
* It can only move to catercorner point of a square,
* That square's width equals to 1
* </LI>
* <Li>
* It can only move within the "base". refers
* {@ Link Jiang # isvalid (Java. AWT. Point )}
* </LI>
* </OL>
* </P>
* <P>
* <H2> Xiang </H2>
* <Ol>
* <Li>
* It can only move to catercorner point of a rectangle,
* That square's width equals to 2
* </LI>
* <Li>
* The aspect of this motion must has no chessman on the center
* The square, No matter ours nor opponent's. The following example
* Is invalid: <br>
* # --------- <Br>
* | & Nbsp; |
* & Nbsp; | <br>
* ---- <B> & nbsp; X & nbsp; ---- </B> <br>
* | & Nbsp; |
* & Nbsp; | <br>
* --------- Xiang
* </LI>
* <Li>
* A Xiang can't move beyond our "He Jie"
* </LI>
* </OL>
* </P>
* <P>
* <H2> MA </H2>
* <Ol>
* <Li>
* It can only move to catercorner point of a rectangle,
* That rectangle's width equals to 1, and height equals
* 2
* </LI>
* <Li>
* The aspect (the longger distance direction) of this motion
* Must has no Chessman, No matter ours nor opponent's. The
* Following example is invalid: <br>
* # ---- <Br>
* | & Nbsp; | <br>
* ----- <B> x </B> <br>
* | & Nbsp; | <br>
* ----- Ma
* </LI>
* </OL>
* </P>
* <P>
* <H2> che </H2>
* <Ol>
* <Li> it can only move straight </LI>
* </OL>
* </P>
* <P>
* <H2> Pao </H2>
* <Ol>
* <Li>
* It can only move straight
* </LI>
* <Li>
* It can eat opponent chessman between a chessman along a line,
* Like this: <br>
* Pao ---- # ------ opponentchessman <br>
* <B> Note: </B> the motion only occur when eating
* </LI>
* </OL>
* </P>
* <P>
* <H2> Bing </H2>
* <Ol>
* <Li> it can move forward, no backward </LI>
* <Li> only when it passed the "Chu he hang Jie ",
* It can turn left/right forward </LI>
* <Li> motion distance equals 1 </LI>
* </OL>
* </P>
* @ Param fromx X coordinate of a motion's start
* @ Param fromy y coordinate of a motion's start
* @ Param tox X coordinate of a motion's destination
* @ Param toy y coordinate of a motion's destination
* @ Return if is valid, returns <code> true </code>, otherwise,
* Returns <code> false </code>
*/
Final public Boolean isvalidmove (INT fromx, int fromy, int tox, int toy ){
If (basicmoveisvalid (fromx, fromy, tox, toy )){
Int moveid = chessboard [fromx] [fromy];
Int targetid = chessboard [TOX] [Toy];

Switch (moveid ){
Case 7:
// <Editor-fold defaultstate = "Collapsed" DESC = "Hong Jiang">
If (Tox <3 | tox> 5 ){
// Violate the first rule
Return false;
}

If (targetid = 14 ){
Int counter tcount = calcchessmancountalongline (fromx, fromy,
Tox, toy );
If (distinct tcount = 0 ){
// The thrid rule
Return true;
}
}

If (toy> 2 ){
// Violate the first rule
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 1 ){
// Violate the first rule
Return false;
}
/// </Editor-fold>
Break;
Case 14:
// <Editor-fold defaultstate = "Collapsed" DESC = "Hei Jiang">
If (Tox <3 | tox> 5 ){
// Violate the first rule
Return false;
}

If (targetid = 7 ){
Int counter tcount = calcchessmancountalongline (fromx, fromy,
Tox, toy );
If (distinct tcount = 0 ){
// The thrid rule
Return true;
}
}

If (toy <7 ){
// Violate the first rule
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 1 ){
// Violate the first rule
Return false;
}
/// </Editor-fold>
Break;
Case 6:
// <Editor-fold defaultstate = "Collapsed" DESC = "Hong Shi">
If (Tox <3 | tox> 5 | toy> 3 ){
// Violate the first rule
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 2 ){
// Violate the first rule
Return false;
}
/// </Editor-fold>
Break;
Case 13:
// <Editor-fold defaultstate = "Collapsed" DESC = "Hei Shi">
If (Tox <3 | tox> 5 | toy <8 ){
// Violate the first rule
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 2 ){
// Violate the first rule
Return false;
}

/// </Editor-fold>
Break;
Case 5:
// <Editor-fold defaultstate = "Collapsed" DESC = "Hong Xiang">
If (toy> 4 ){
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 8 ){
// Violate the first rule
Return false;
}

If (chessboard [(fromx + TOX)/2] [(fromy + toy)/2]! = 0 ){
// Can't move widthways, violates the second rule
Return false;
}
/// </Editor-fold>
Break;
Case 12:
// <Editor-fold defaultstate = "Collapsed" DESC = "Hei Xiang">
If (toy <6 ){
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 8 ){
// Violate the first rule
Return false;
}

If (chessboard [(fromx + TOX)/2] [(fromy + toy)/2]! = 0 ){
// Can't move widthways, violates the second rule
Return false;
}
/// </Editor-fold>
Break;
Case 2:
Case 9:
// <Editor-fold defaultstate = "Collapsed" DESC = "Ma">
If (distancesq (fromx, fromy, tox, toy )! = 5 ){
// Violate the first rule
Return false;
}

If (chessboard [(fromx + TOX)/2] [fromy]! = 0 &&
ABS (fromy-toy) = 1 ){
// Can't move widthways, violates the second rule
Return false;
}

If (chessboard [fromx] [(fromy + toy)/2]! = 0 &&
ABS (fromx-Tox) = 1 ){
// Can't move lengthways violates the second rule
Return false;
}
/// </Editor-fold>
Break;
Case 1:
Case 8:
// <Editor-fold defaultstate = "Collapsed" DESC = "Che">
If (fromx! = Tox & fromy! = Toy ){
// Must move straight
Return false;
}

Int counter tcount = calcchessmancountalongline (fromx, fromy,
Tox, toy );
// Move eat motion!
If (distinct tcount! = 0 ){
Return false;
}
/// </Editor-fold>
Break;
Case 3:
Case 10:
// <Editor-fold defaultstate = "Collapsed" DESC = "pao">
If (fromx! = Tox & fromy! = Toy ){
// Must move straight, violates the first rule
Return false;
}

Repeated tcount = calcchessmancountalongline (fromx, fromy,
Tox, toy );

If (targetid! = 0 ){
// Move eat motion!
If (distinct tcount! = 1 ){
Return false;
}
} Else {
If (distinct tcount! = 0 ){
Return false;
}
}
/// </Editor-fold>
Break;
Case 4:
Case 11:
// <Editor-fold defaultstate = "Collapsed" DESC = "bing">
If (fromx! = Tox & fromy! = Toy ){
// Must move straight
Return false;
}

If (distancesq (fromx, fromy, tox, toy )! = 1 ){
// Move forware only one step
Return false;
}

If (isblack (moveid )){
If (fromy> 4 & fromx! = TOX ){
// Has nor yet passed the "He Jie"
// Cannot turn left/right
Return false;
}
If (fromy <toy ){
// "Bing" cannot backward!
Return false;
}
} Else {
If (fromy <5 & fromx! = TOX ){
// Has nor yet passed the "He Jie"
// Cannot turn left/right
Return false;
}
If (fromy> toy & fromx = TOX ){
// "Bing" cannot backward!
Return false;
}
}
/// </Editor-fold>
Break;
}
} Else {
Return false;
}

Return true;
}

You can paste the code to the IDE with javadoc to view the code, which is clear :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.