zoukankan      html  css  js  c++  java
  • [Java] 練習用對戰小遊戲

    繼承、介面自我練習時所建立的小遊戲,一開始輸入名稱來建立對戰腳色,之後以輸入招式號碼的方式互相打鬥,最後沒血的一方就輸了。

    人物種族

    abstract public class Human {
        int hp = 100;
        int atk = 10;
        int def = 4;
        int 自我恢復 = 10;
        int count = 0;
    
        public int 自我恢復() {
            if (自我恢復 < 0) {
                自我恢復 = 0;
                System.err.println("過度使用,自我恢復已失效");
                return 自我恢復;
            } else {
                自我恢復 -= count;
                count++;
                return 自我恢復;
            }
        }
    }

    職業

    public interface 戰士 {
        int ihp = 10;
        int iatk = 4;
        int idef = 3;
        int 捨身攻擊damage = 20;
    
        public int 捨身攻擊();
    
        public void 能力加成();
    
    }

    角色

    public class 人類戰士 extends Human implements 戰士 {
    
        private String 玩家名稱;
        private String 玩家職業 = "人類戰士";
        private int 玩家hp = super.hp;
        private int 玩家atk = super.atk;
        private int 玩家def = super.def;
        int 一般攻擊damage = 10;
        String[] 技能 = { "一般攻擊", Integer.toString(一般攻擊damage), "捨身攻擊", Integer.toString(捨身攻擊damage), "自我恢復",
                Integer.toString(自我恢復) };
    
        人類戰士(String name) {
            玩家名稱 = name;
        }
    
        public int 一般攻擊() {
            System.out.println(玩家名稱 + "使用了一般攻擊");
            return 一般攻擊damage;
        }
    
        @Override
        public int 捨身攻擊() {
            System.out.println(玩家名稱 + "使用了捨身攻擊");
            return 捨身攻擊damage;
    
        }
    
        @Override
        public int 自我恢復() {
            System.out.println(玩家名稱 + "使用了自我恢復,將可恢復" + super.自我恢復 + "血量");
            super.自我恢復();
            return super.自我恢復;
        }
    
        @Override
        public void 能力加成() {
    
            System.out.println();
            System.out.println("玩家名稱	" + this.玩家名稱);
            System.out.println("玩家職業	" + this.玩家職業);
            this.玩家hp += ihp;
            System.out.println("血量	" + this.玩家hp);
            this.玩家atk += iatk;
            System.out.println("攻擊力	" + this.玩家atk);
            this.玩家def += idef;
            System.out.println("防禦力	" + this.玩家def);
        }
    
        public String get玩家名稱() {
            return 玩家名稱;
        }
    
        public int get玩家hp() {
            return 玩家hp;
        }
    
        public void set玩家hp(int 玩家hp) {
            this.玩家hp = 玩家hp;
        }
    
        public int get玩家atk() {
            return 玩家atk;
        }
    
        public void set玩家atk(int 玩家atk) {
            this.玩家atk = 玩家atk;
        }
    
        public int get玩家def() {
            return 玩家def;
        }
    
        public void set玩家def(int 玩家def) {
            this.玩家def = 玩家def;
        }
    }

    主程式

    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Scanner;
    
    public class TestGame {
    
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            System.out.println("請輸入玩家A的名稱");
            System.out.print("=>");
            String 玩家A名稱 = sc.nextLine();
            System.out.println("請輸入玩家B的名稱");
            System.out.print("=>");
            String 玩家B名稱 = sc.nextLine();
    
            人類戰士 玩家A = new 人類戰士(玩家A名稱);
    
            玩家A.能力加成();
            LinkedHashMap<String, String> 玩家A技能 = new LinkedHashMap<>();
            for (int i = 0; i < 玩家A.技能.length; i += 2) {
                玩家A技能.put(玩家A.技能[i], 玩家A.技能[i + 1]);
            }
    
            人類戰士 玩家B = new 人類戰士(玩家B名稱);
            玩家B.能力加成();
            LinkedHashMap<String, String> 玩家B技能 = new LinkedHashMap<>();
            for (int i = 0; i < 玩家B.技能.length; i += 2) {
                玩家B技能.put(玩家B.技能[i], 玩家B.技能[i + 1]);
            }
    
            int 玩家A動作 = 0;
            int 玩家B動作 = 0;
            do {
                do {
                    try {
                        System.out.println();
                        System.out.println(玩家A.get玩家名稱() + "請輸入使用招式的順序號碼 ");
                        int 招式號碼 = 0;
                        Iterator<Entry<String, String>> iiIterator = 玩家A技能.entrySet().iterator();
                        while (iiIterator.hasNext()) {
                            招式號碼++;
                            Map.Entry<String, String> entry = iiIterator.next();
                            System.out.println(招式號碼 + ")" + entry.getKey() + "	預計產生效果(初步)	" + entry.getValue());
                        }
    
                        System.out.print("=>");
                        int 動作 = Integer.parseInt(sc.nextLine());
                        if (動作 > 玩家A技能.size()) {
                            throw new ArithmeticException();
                        } else {
                            玩家A動作 = 動作;
                            break;
                        }
                    } catch (Exception e) {
                        System.err.println("輸入錯誤");
                    }
                } while (true);
    
                switch (玩家A動作) {
                case 1:
                    玩家B.set玩家hp(玩家B.get玩家hp() - (玩家A.一般攻擊() - 玩家B.get玩家def()));
                    System.out.println(玩家B.get玩家名稱() + "受了" + 玩家A.一般攻擊damage + "傷害,血量還剩" + 玩家B.get玩家hp());
                    break;
                case 2:
                    玩家B.set玩家hp(玩家B.get玩家hp() - (玩家A.捨身攻擊() - 玩家B.get玩家def()));
                    System.out.println(玩家B.get玩家名稱() + "受了" + 玩家A.捨身攻擊damage + "傷害,血量還剩" + 玩家B.get玩家hp());
                    玩家A.set玩家hp(玩家A.get玩家hp() - 玩家B.get玩家atk());
                    System.out.println(玩家A.get玩家名稱() + "受到反擊,損失" + 玩家B.get玩家atk() + "滴血,血量還剩" + 玩家A.get玩家hp());
                    break;
                case 3:
                    玩家A.set玩家hp(玩家A.get玩家hp() + 玩家A.自我恢復());
                    System.out.println(玩家A.get玩家名稱() + "血量還剩" + 玩家A.get玩家hp());
                    break;
                default:
                    break;
                }
                System.out.println();
                if (玩家B.get玩家hp() <= 0 && 玩家A.get玩家hp() <= 0) {
                    System.out.println("無人生還");
                    break;
                } else if (玩家B.get玩家hp() <= 0) {
                    System.out.println(玩家A.get玩家名稱() + "獲勝");
                    break;
                } else if (玩家A.get玩家hp() <= 0) {
                    System.out.println(玩家B.get玩家名稱() + "獲勝");
                    break;
                }
    
                do {
                    try {
                        System.out.println(玩家B.get玩家名稱() + "請輸入使用招式的順序號碼 ");
                        int 招式號碼 = 0;
                        Iterator<Entry<String, String>> iiIterator = 玩家A技能.entrySet().iterator();
                        while (iiIterator.hasNext()) {
                            招式號碼++;
                            Map.Entry<String, String> entry = iiIterator.next();
                            System.out.println(招式號碼 + ")" + entry.getKey() + "	預計產生效果(初步)	" + entry.getValue());
                        }
                        System.out.print("=>");
                        int 動作 = Integer.parseInt(sc.nextLine());
                        if (動作 > 玩家B技能.size()) {
                            throw new ArithmeticException();
                        } else {
                            玩家B動作 = 動作;
                            break;
                        }
                    } catch (Exception e) {
                        System.err.println("輸入錯誤");
                    }
                } while (true);
    
                switch (玩家B動作) {
                case 1:
                    玩家A.set玩家hp(玩家A.get玩家hp() - (玩家B.一般攻擊() - 玩家A.get玩家def()));
                    System.out.println(玩家A.get玩家名稱() + "受了" + 玩家B.一般攻擊damage + "傷害,血量還剩" + 玩家A.get玩家hp());
                    break;
                case 2:
                    玩家A.set玩家hp(玩家A.get玩家hp() - (玩家B.捨身攻擊() - 玩家A.get玩家def()));
                    System.out.println(玩家A.get玩家名稱() + "受了" + 玩家B.捨身攻擊damage + "傷害,血量還剩" + 玩家A.get玩家hp());
                    玩家B.set玩家hp(玩家B.get玩家hp() - 玩家A.get玩家atk());
                    System.out.println(玩家B.get玩家名稱() + "受到反擊,損失" + 玩家A.get玩家atk() + "滴血,血量還剩" + 玩家B.get玩家hp());
                    break;
                case 3:
                    玩家B.set玩家hp(玩家B.get玩家hp() + 玩家B.自我恢復());
                    System.out.println(玩家B.get玩家名稱() + "血量還剩" + 玩家B.get玩家hp());
                    break;
                default:
                    break;
                }
                System.out.println();
                if (玩家B.get玩家hp() <= 0 && 玩家A.get玩家hp() <= 0) {
                    System.out.println("無人生還");
                    break;
                } else if (玩家B.get玩家hp() <= 0) {
                    System.out.println(玩家A.get玩家名稱() + "獲勝");
                    break;
                } else if (玩家A.get玩家hp() <= 0) {
                    System.out.println(玩家B.get玩家名稱() + "獲勝");
                    break;
                }
    
            } while (true);
        }
    }
  • 相关阅读:
    【整理】数组面试题集锦
    【整理】二叉树面试题集锦
    【转】C++怎么设计只能在堆或者栈分配空间的类以及定义一个不能被继承的类
    【转】面试题:最长回文子串
    【转】后缀数组求最长重复子串
    【转】linux硬链接与软链接
    【转】随机函数面试题
    【转】C++ 重载、覆盖和隐藏
    分类算法评估指标
    Pandas_对某列的内容分列
  • 原文地址:https://www.cnblogs.com/pyleu1028/p/10343096.html
Copyright © 2011-2022 走看看