In the ontology using object-oriented methods designed to achieve a simple games, the game has a warrior as Soldier and wizards Master two types of players, each player can specify name name, every player has given life value of HP, def damage atk and defense force, by defining the attack () method to let each player has the ability to fight,
Each player can also be equipped with a Weapon equipped Weapon, equipment can change the player's weapons and armor, Weapon by implementing Tradable interface can also realize the buying and selling,
Write GameTest class test running the game, write a fight in the class (Player, the Player) method to simulate the two players, playing against the process of randomly selected a Player to attack, so repeated, until one side of the HP value less than zero, in the main () method, constructs two players, a Solder a Master, calls to fight against () method, and the output against as a result,
Needed to bear on the design of the details of the classes and interfaces as follows,
1) the Tradable interface (10)
Two void declaration in the Tradable interface methods: but (int price) and sell (int price)
2) Weapon class (10)
Weapon class implements the Tradable interface,
Weapon class has two private properties of type int: atk says firepower and def said defense force, for the two attributes provide public accessor methods, as a Weapon class provides a belt and constructor Weapon (int atk, int def),
3) Player class (10)
Player class has five protected attributes:
String name said the player's name
Int HP said the player's life value, the initial value of 100
Int atk says players hit
Int def said the player's defense force
Weapon Weapon said players equipped Weapon
As the attribute name, HP, weapon method provides accessor methods and modifier,
For attributes atk, def only provides accessor methods, pay attention to the player's attack is composed of its own weapons and weapon damage, defense def is also so,
In the Player class rewrite the toString () method, return to the Player's namt and HP values,
Provides a void in the Player class attack (Player Player) method, this method can change the HP value of the Player object being attacked, and the attacker's life value is not affected,
Void attack (Player Player) {
The Random Random=new Random ();
Int realAtk=random. NextInt (getAtk ());//the actual damage
Int realDef=random. NextInt (player. GetDef ());//actual defense
Int realHp=realAtk - realDef & gt; 0? RealAtk - realDef: 0;//the life value of the actual loss
Player. SetHp (player. GetHp () - realHp);//modify attack player's life value
}
4) as class (10)
As a class is a subclass of Player class, provide the class with a belt and constructor as Soldier (String name, Weapon Weapon), is used to initialize as object name, Weapon, HP, atk, def five attribute's value, initial value of 100 HP, atk of 8, def is 4,
5) Master class (10)
Master class is also the Player class subclass, provide the class with a belt and constructor Master (String name, Weapon Weapon), is used to initialize the Master object name, Weapon, HP, atk, def five attribute's value, and initial value of 100 HP, atk is 4, def for 10,
Master class add a cure cure Player (p), is used to specify the Player object of HP value increases 10,
6) is used to test the GameTest class that contains the main () method and fight () method, (10)
Fight () signature of the method to void fight (p1 Player, the Player p2), in this method, two players in the random (i.e., call its attack () method), until one Player's HP value less than zero, the output of two players who is winning and losing,
In the main () method, the statement of two Player object p1 and p2, p1 to new as Soldier (" warrior ", the new Weapon (5, 5)), p2 point to the new Master (" mage ", the new Weapon (5, 5)), calls to fight (p1, p2) test against the situation,
CodePudding user response:
The console games?CodePudding user response:
You can, can describe so clear