Skip to Content
CoursesCSC102จำลองเกมการผจญภัย

จำลองเกมการผจญภัย

โจทย์

Write an adventure game program according to the given the UML diagram below.

The Player class represents a player in the game. The Player class has an attribute named playerCount which will increase every time, if a new player is created (the playerCount value start from 0). Each player can own many characters. From the start, each player will earn 10 bahts and start at level 1 and player id will be generate using the number of playerCount.

A Player can choose character that he/she owns to adventure using the adventure method. The logic of method checks if whether the selected character is in the characters list as the order of following conditions:

  1. If the player does not own any character it should print (println) “You do not own any character”.

  2. If the player does not own the character it should print (println) “You do not own this character”.

  3. If the player owns the character, the player’s level and character’s level increases +5 points and money increases +10.

A Player can buy new character using the method buyCharacter. The price for each character is based on rank (i.e., rank : 3 cost 15 baht, 2 cost 10 baht, and 1 cost 5 baht). The buying logic is based on the order of following conditions:

  1. If the character rank are not 1, 2, and 3 it should print (println) “The rank of this character are not supported”.

  2. If a player do not have enough money it should print (println) “You do not have enough money”.

  3. Otherwise, a character can be bought. The money must be reduced properly and the character must be added to the list of owned characters.

The method getAllCharacter lists all characters’ name the player has own. Print name of characters line by line. Hint: Use the loop to iterate through the ArrayList.

The Character class is an abstract class which provides template for each character when a character is created, its level will start at 1. The method increaseLevel will increase the level by adding up the current level points to the input level points.

The Geo, Anemo, and Electro class inherit properties from Character class. Each of them can perform different kind of attack to an enemy (i.e., stoneStatue, windFlow, electricCurrent). Attack skill will decrease an enemy healthPoint consider by character rank, character level, and class. The formula for calculating attack are:

Geo=(Level×0.2)+(rank×2)Geo = (Level \times 0.2) + (rank \times 2)

Anemo=(Level×0.5)+(rank×1.5)Anemo = (Level \times 0.5) + (rank \times 1.5)

Electro=(Level×0.1)+(rank×2)+5Electro = (Level \times 0.1) + (rank \times 2) + 5

When a character defeats the enemy (i.e., healthPoint <= 0), it should print (println) “You defeated an enemy” and its level will be increase by +1 by using the method increaseLevel.

The driver class of Tester is provided in the answer box for testing the game system. Please do not modify the driver class since your output may be incorrect.

โค้ดเริ่มต้น

import java.util.ArrayList; public class AdventureGame { public static void main(String[] args) { Player p1 = new Player("Float"); Player p2 = new Player("BABA"); System.out.println("--------------------------------"); System.out.println("Player: "+p1.getName()); System.out.println("Player Id: "+p1.getId()); System.out.println("Level: "+p1.getLevel() + " Money: " + p1.getMoney()); System.out.println("--------------------------------"); System.out.println("Player: "+p2.getName()); System.out.println("Player Id: "+p2.getId()); System.out.println("Level: "+p2.getLevel() + " Money: " + p2.getMoney()); System.out.println("--------------------------------"); Geo ZhongLi = new Geo("Zhong Li", 3); Anemo Venti = new Anemo("Venti", 2); Electro RaidenShogun = new Electro("Raiden Shogun", 3); Electro Beidou = new Electro("Beidou", 1); p1.adventure(Beidou); p1.buyCharacter(RaidenShogun); p1.buyCharacter(Beidou); System.out.println("--------------------------------"); System.out.println("Player: "+p1.getName()); System.out.println("Player Id: "+p1.getId()); System.out.println("Level: "+p1.getLevel() + " Money: " + p1.getMoney()); System.out.println("--------------------------------"); System.out.println("Character: "+ Beidou.getName() + " Level: " +Beidou.getLevel()); p1.adventure(Beidou); System.out.println("--------------------------------"); System.out.println("Player: "+p1.getName()); System.out.println("Player Id: "+p1.getId()); System.out.println("Level: "+p1.getLevel() + " Money: " + p1.getMoney()); System.out.println("--------------------------------"); Enemy e1 = new Enemy(10); System.out.println(e1.getHealthPoint()); Beidou.electricCurrent(e1); System.out.println(e1.getHealthPoint()); Beidou.electricCurrent(e1); System.out.println("--------------------------------"); p1.adventure(RaidenShogun); p1.buyCharacter(ZhongLi); p1.getAllCharacter(); p1.adventure(ZhongLi); p1.adventure(ZhongLi); p1.buyCharacter(RaidenShogun); System.out.println("--------------------------------"); Enemy e2 = new Enemy(5); System.out.println("Enemy Health: "+e2.getHealthPoint()); System.out.println("Character: "+ ZhongLi.getName() + " Level: " +ZhongLi.getLevel()); ZhongLi.stoneStatue(e2); System.out.println("Character: "+ ZhongLi.getName() + " Level: " +ZhongLi.getLevel()); System.out.println("--------------------------------"); Anemo RiceShower = new Anemo("Rice Shower",500); p1.buyCharacter(RiceShower); } }

โค้ด

import java.util.ArrayList; public class AdventureGame { public static void main(String[] args) { Player p1 = new Player("Float"); Player p2 = new Player("BABA"); System.out.println("--------------------------------"); System.out.println("Player: "+p1.getName()); System.out.println("Player Id: "+p1.getId()); System.out.println("Level: "+p1.getLevel() + " Money: " + p1.getMoney()); System.out.println("--------------------------------"); System.out.println("Player: "+p2.getName()); System.out.println("Player Id: "+p2.getId()); System.out.println("Level: "+p2.getLevel() + " Money: " + p2.getMoney()); System.out.println("--------------------------------"); Geo ZhongLi = new Geo("Zhong Li", 3); Anemo Venti = new Anemo("Venti", 2); Electro RaidenShogun = new Electro("Raiden Shogun", 3); Electro Beidou = new Electro("Beidou", 1); p1.adventure(Beidou); p1.buyCharacter(RaidenShogun); p1.buyCharacter(Beidou); System.out.println("--------------------------------"); System.out.println("Player: "+p1.getName()); System.out.println("Player Id: "+p1.getId()); System.out.println("Level: "+p1.getLevel() + " Money: " + p1.getMoney()); System.out.println("--------------------------------"); System.out.println("Character: "+ Beidou.getName() + " Level: " +Beidou.getLevel()); p1.adventure(Beidou); System.out.println("--------------------------------"); System.out.println("Player: "+p1.getName()); System.out.println("Player Id: "+p1.getId()); System.out.println("Level: "+p1.getLevel() + " Money: " + p1.getMoney()); System.out.println("--------------------------------"); Enemy e1 = new Enemy(10); System.out.println(e1.getHealthPoint()); Beidou.electricCurrent(e1); System.out.println(e1.getHealthPoint()); Beidou.electricCurrent(e1); System.out.println("--------------------------------"); p1.adventure(RaidenShogun); p1.buyCharacter(ZhongLi); p1.getAllCharacter(); p1.adventure(ZhongLi); p1.adventure(ZhongLi); p1.buyCharacter(RaidenShogun); System.out.println("--------------------------------"); Enemy e2 = new Enemy(5); System.out.println("Enemy Health: "+e2.getHealthPoint()); System.out.println("Character: "+ ZhongLi.getName() + " Level: " +ZhongLi.getLevel()); ZhongLi.stoneStatue(e2); System.out.println("Character: "+ ZhongLi.getName() + " Level: " +ZhongLi.getLevel()); System.out.println("--------------------------------"); Anemo RiceShower = new Anemo("Rice Shower",500); p1.buyCharacter(RiceShower); } } abstract class Character { private String name; private int level; private int rank; public Character(String name, int rank) { this.name = name; this.level = 1; this.rank = rank; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void increaseLevel(int level) { this.level += level; } public int getRank() { return rank; } } class Enemy { private double healthPoint; public Enemy(double healthPoint) { this.healthPoint = healthPoint; } public double getHealthPoint() { return healthPoint; } public void setHealthPoint(double healthPoint) { this.healthPoint = healthPoint; } } class Player { private int id; private String name; private int level; private int money; private ArrayList<Character> characters; private static int playerCount = 0; public Player(String name) { this.id = playerCount++; this.name = name; this.level = 1; this.money = 10; this.characters = new ArrayList<>(); } public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public int getMoney() { return money; } public void adventure(Character character) { if (characters.isEmpty()) { System.out.println("You do not own any character"); } else if (!characters.contains(character)) { System.out.println("You do not own this character"); } else { this.level += 5; this.money += 10; character.increaseLevel(5); } } public void buyCharacter(Character character) { int rank = character.getRank(); if (rank < 1 || rank > 3) { System.out.println("The rank of this character are not supported"); } else { int pay = 5 * rank; if (this.money >= pay) { this.money -= pay; characters.add(character); } else { System.out.println("You do not have enough money"); } } } public void getAllCharacter() { for (Character c : characters) { System.out.println(c.getName()); } } } class Geo extends Character { public Geo(String name, int level) { super(name, level); } public void stoneStatue(Enemy enemy) { double attack = enemy.getHealthPoint() - ((this.getLevel() * 0.2) + (this.getRank() * 2)); enemy.setHealthPoint(Math.max(0, attack)); if (enemy.getHealthPoint() <= 0) { System.out.println("You defeated an enemy"); this.increaseLevel(1); } } } class Anemo extends Character { public Anemo(String name, int level) { super(name, level); } public void windFlow(Enemy enemy) { double attack = enemy.getHealthPoint() - ((this.getLevel() * 0.5) + (this.getRank() * 1.5)); enemy.setHealthPoint(Math.max(0, attack)); if (enemy.getHealthPoint() <= 0) { System.out.println("You defeated an enemy"); this.increaseLevel(1); } } } class Electro extends Character { public Electro(String name, int level) { super(name, level); } public void electricCurrent(Enemy enemy) { double attack = enemy.getHealthPoint() - ((this.getLevel() * 0.1) + (this.getRank() * 2) + 5); enemy.setHealthPoint(Math.max(0, attack)); if (enemy.getHealthPoint() <= 0) { System.out.println("You defeated an enemy"); this.increaseLevel(1); } } }

คำอธิบาย

การเพิ่มคลาส Character

abstract class Character { private String name; private int level; private int rank; public Character(String name, int rank) { this.name = name; this.level = 1; this.rank = rank; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void increaseLevel(int level) { this.level += level; } public int getRank() { return rank; } }
  • คลาส Character เป็นคลาสนามธรรม (abstract class) ที่ใช้เป็นแม่แบบสำหรับตัวละครประเภทต่างๆ โดยในคลาสนี้:
    • มีการกำหนด ชื่อ (name), ระดับ (level), และ อันดับ (rank) ของตัวละคร
    • ระดับเริ่มต้นคือ 1
    • มีเมธอด increaseLevel เพื่อเพิ่มระดับตัวละครตามค่าที่ส่งเข้ามา

การเพิ่มคลาส Player

class Player { private int id; private String name; private int level; private int money; private ArrayList<Character> characters; private static int playerCount = 0; public Player(String name) { this.id = playerCount++; this.name = name; this.level = 1; this.money = 10; this.characters = new ArrayList<>(); } public int getId() { return id; } public String getName() { return name; } ... }
  • คลาส Player สร้างขึ้นเพื่อแสดงข้อมูลของผู้เล่น เช่น:
    • id ของผู้เล่น จะเพิ่มขึ้นอัตโนมัติตาม playerCount
    • เริ่มต้นด้วยระดับ (level) เท่ากับ 1 และเงิน (money) 10 บาท
    • characters เป็น ArrayList ที่ใช้เก็บตัวละครที่ผู้เล่นเป็นเจ้าของ

การเพิ่มคลาส Enemy

class Enemy { private double healthPoint; public Enemy(double healthPoint) { this.healthPoint = healthPoint; } public double getHealthPoint() { return healthPoint; } public void setHealthPoint(double healthPoint) { this.healthPoint = healthPoint; } }
  • คลาส Enemy ใช้สำหรับสร้างศัตรู โดยเก็บค่าพลังชีวิต (healthPoint) และมีเมธอดสำหรับ อ่านและแก้ไขค่าพลังชีวิต
ปรับปรุงล่าสุด