A round combat game made with aspectj

Source: Internet
Author: User
As the AOP part of spring 2 has been greatly adjusted, the use of many places has introduced the content in aspectj, in order to complete the in-depth spring 2: the Spring AOP application chapter in the principles and practices of Lightweight J2EE development framework is written. Therefore, I have studied aspectj over the past two days and re-read "aspectj in action", which has benefited a lot. Here is an example of a round combat scene today: In this example, a soldier class is designed, which has some random functions, including launching attacks, treatments, escaping, and moving. In addition, there is a simple main program maintest, which is used to allow the two soldiers to attack each other in turn. Another aspect is used to observe and input the detailed combat process until a battle is knocked down: first look at the soldier code:

Package springroad. Demo. chap5.aspectj;

Public class soldier {
Private string name;
Private int health = 100;
Private int damage = 10;
Private int x = 10;
Private int y = 10;
Public Boolean attack (Soldier target ){
Boolean ret = false;
If (! Target. Dodge ())
{
Target. sethealth (target. gethealth ()-This. Damage );
Ret = true;
}
Move ();
Treat ();
Return ret;
}
Public void move ()
{
This. x + = getrandom (5 );
This. Y + = getrandom (5 );
}
// Avoids random changes of X and Y. The success rate is 50%.
Public Boolean Dodge ()
{
Return getrandom (10) % 2 = 0;
}
// Treatment, has a certain chance of success, can increase the life value 0-20 points
Public void treat ()
{
If (cantreat ())
This. Health + = getrandom (20 );
}
Public Boolean cantreat ()
{
Return getrandom (10)/2 = 0;
}

Private int getrandom (INT seed)
{
Return randomutil. getrandomvalue (SEED );
}

// Getter and setter Methods
Public int gethealth (){
Return health;
}
Public void sethealth (INT health ){
This. Health = health;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public int getx (){
Return X;
}
Public void setx (int x ){
This. x = X;
}
Public int Gety (){
Return y;
}
Public void sety (INT y ){
This. Y = y;
}
Public int getdamage (){
Return damage;
}
Public void setdamage (INT damage ){
This. Damage = damage;
}

}

Let's take a look at the battlefield simulation code (the main program) in maintest. Java: Package springroad. Demo. chap5.aspectj;
Public class maintest {
Public static void main (string [] ARGs ){
Soldier p1 = new soldier ();
P1.setname ("role 1 ");
Soldier P2 = new soldier ();
P2.setname ("role 2 ");
Int I = 0;
While (p1.gethealth ()> 0 & p2.gethealth ()> 0)
{
P2.attack (P1 );
P1.attack (P2 );
I + = 2;
}
System. Out. println ("combat times:" + I );
If (p1.gethealth ()> 0) system. Out. println (p1.getname () + "Beat! ");
Else system. Out. println (p2.getname () + "Beat! ");
}
} Random number generator (probability generation) package springroad. Demo. chap5.aspectj; public class randomutil {
Private Static java. util. Random random = new java. util. Random ();
Public static int getrandomvalue (INT seed)
{
Return random. nextint (SEED );
}
} The above three programs are complete. You can directly compile them using javac and execute maintest. We can see that after a short battle, the number of battles and the outcome are displayed. To observe the detailed process of the entire battle, aspectj is used to output attacks without modifying the program code above. The code for recordgame. AJ is as follows: Package springroad. Demo. chap5.aspectj;
Public aspect recordgame {
Private Static java. Text. simpledateformat df = new java. Text. simpledateformat ("yyyy-mm-dd H: M: s ");
Pointcut dorecord (): Execution (Boolean soldier. Attack (soldier ));
Pointcut supperrole (soldier s): Target (s) & execution (Boolean soldier. cantreat ());
After () Returning (Boolean value): dorecord ()
{
Soldier S = (soldier) thisjoinpoint. gettarget ();
Soldier t = (soldier) thisjoinpoint. getargs () [0];
System. out. println (DF. format (New Java. util. date () + ":" + S. getname () + "" + T. getname () + "launched an attack! -- Result: "+ (value? "Successful": "failed "));
System. out. println (S. getname () + "Life Value:" + S. gethealth () + ";" + T. getname () + "Life Value:" + T. gethealth ());
}
After (soldier s) Returning (Boolean value): Target (s) & call (Boolean soldier. cantreat ())
{
If (value) system. Out. println (S. getname () + "get treatment! ");
}
Boolean around (soldier s): supperrole (s)
{
If ("super". Equals (S. getname () return true;
Else return proceed (s );
}
} Use ajt to compile the above four files, and enter the following command in the command line (requires aspectj to be correctly installed and configured): AJC springroad/demo/chap5 /*. * j * Then run the Java command to execute maintest. This shows very interesting results. As shown in:

 

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.