游戏设定
游戏的目的是扮演传说中的勇者,打倒妄图控制世界的龙王,顺道救出被掳走的公主。完成游戏。
游戏会随机存在四种操作:勇者攻击,恶龙攻击,勇者吃掉血药生命值恢复,勇者吃掉毒药攻击力提升,如果勇者在限定的回合内击败恶龙且不被恶龙击败,讨伐成功则可成功救出公主。讨伐失败则公主被恶龙掳走。
代码实现
DragonFightGame类
1. public class DragonFightGame { 2. public static final int MAX_ROUND = 10; 3. public static final int MAX_HP = 100; 4. public static final int MAX_ATTACK = 20; 5. public static final int HEAL_HP = 30; 6. public static final int INCREASE_ATTACK = 5; 7. 8. 9. public int round = 1; 10. public int dragonHp = MAX_HP; 11. public int heroHp = MAX_HP; 12. public int heroAttack = MAX_ATTACK; 13. public String dragonName; 14. public String heroName; 15. 16. public DragonFightGame(String dragonName, String heroName) { 17. this.dragonName = dragonName; 18. this.heroName = heroName; 19. } 20. 21. public synchronized void dragonAttack() { 22. if (round > MAX_ROUND) { 23. return; 24. } 25. int damage = (int) (Math.random() * MAX_ATTACK); 26. heroHp -= damage; 27. System.out.println(dragonName + "对" + heroName + "造成了" + damage + "点伤害"); 28. if (heroHp <= 0) { 29. System.out.println(heroName + "被" + dragonName + "击败了"); 30. return; 31. } 32. round++; 33. } 34. 35. public synchronized void heroAttack() { 36. if (round > MAX_ROUND) { 37. return; 38. } 39. int damage = heroAttack; 40. dragonHp -= damage; 41. System.out.println(heroName + "对" + dragonName + "造成了" + damage + "点伤害"); 42. if (dragonHp <= 0) { 43. System.out.println(dragonName + "被" + heroName + "击败了"); 44. return; 45. } 46. round++; 47. } 48. 49. public synchronized void useHeal() { 50. if (round > MAX_ROUND) { 51. return; 52. } 53. heroHp += HEAL_HP; 54. if (heroHp > MAX_HP) { 55. heroHp = MAX_HP; 56. } 57. System.out.println(heroName + "使用了恢复道具,恢复了" + HEAL_HP + "点生命值"); 58. round++; 59. } 60. 61. public synchronized void useAttackBoost() { 62. if (round > MAX_ROUND) { 63. return; 64. } 65. heroAttack += INCREASE_ATTACK; 66. System.out.println(heroName + "使用了攻击力提升道具,攻击力增加了" + INCREASE_ATTACK); 67. round++; 68. } 69. }
DragonFightGameDemo类
1. public class DragonFightGameDemo { 2. public static void main(String[] args) { 3. DragonFightGame game = new DragonFightGame("黑龙", "勇者"); 4. DragonFightGameThread dragonThread = new DragonFightGameThread(game, "黑龙线程"); 5. DragonFightGameThread heroThread = new DragonFightGameThread(game, "勇者线程"); 6. dragonThread.start(); 7. heroThread.start(); 8. while (game.round <= DragonFightGame.MAX_ROUND && game.dragonHp > 0 && game.heroHp > 0) { 9. int action = (int) (Math.random() * 4); 10. switch (action) { 11. case 0: 12. game.dragonAttack(); 13. break; 14. case 1: 15. game.heroAttack(); 16. break; 17. case 2: 18. game.useHeal(); 19. break; 20. case 3: 21. game.useAttackBoost(); 22. break; 23. } 24. } 25. if(game.dragonHp<=0){ 26. System.out.println(); 27. System.out.println("勇者讨伐成功\n"); 28. } 29. else{ 30. System.out.println(); 31. System.out.println("勇者讨伐失败\n"); 32. } 33. } 34. 35. }
DragonFightGameThread类
1. public class DragonFightGameThread extends Thread { 2. private DragonFightGame game; 3. private String name; 4. 5. public DragonFightGameThread(DragonFightGame game, String name) { 6. this.game = game; 7. this.name = name; 8. } 9. 10. @Override 11. public void run() { 12. while (game.round <= DragonFightGame.MAX_ROUND && game.dragonHp > 0 && game.heroHp > 0) { 13. int action = (int) (Math.random() * 4); 14. switch (action) { 15. case 0: 16. game.dragonAttack(); 17. break; 18. case 1: 19. game.heroAttack(); 20. break; 21. case 2: 22. game.useHeal(); 23. break; 24. case 3: 25. game.useAttackBoost(); 26. break; 27. } 28. } 29. System.out.println(name + "结束了游戏"); 30. } 31. }
实机演示