⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamer.java

📁 23种java设计模式
💻 JAVA
字号:
package game;
import java.util.Vector;
import java.util.Iterator;
import java.util.Random;

public class Gamer {
    private int money;                          // 強帩嬥
    private Vector fruits = new Vector();       // 僼儖乕僣
    private Random random = new Random();       // 棎悢敪惗婍
    private static String[] fruitsname = {      // 僼儖乕僣柤偺昞
        "儕儞僑", "傇偳偆", "僶僫僫", "傒偐傫",
    };
    public Gamer(int money) {                   // 僐儞僗僩儔僋僞
        this.money = money;
    }
    public int getMoney() {                     // 尰嵼偺強帩嬥傪摼傞
        return money;
    }
    public void bet() {                         // 搎偗傞乧僎乕儉偺恑峴
        int dice = random.nextInt(6) + 1;           // 僒僀僐儘傪怳傞
        if (dice == 1) {                            // 1偺栚乧強帩嬥偑憹偊傞
            money += 100;
            System.out.println("強帩嬥偑憹偊傑偟偨丅");
        } else if (dice == 2) {                     // 2偺栚乧強帩嬥偑敿暘偵側傞
            money /= 2;
            System.out.println("強帩嬥偑敿暘偵側傝傑偟偨丅");
        } else if (dice == 6) {                     // 6偺栚乧僼儖乕僣傪傕傜偆
            String f = getFruit();
            System.out.println("僼儖乕僣(" + f + ")傪傕傜偄傑偟偨丅");
            fruits.add(f);
        } else {                                    // 偦傟埲奜乧壗傕婲偒側偄
            System.out.println("壗傕婲偙傝傑偣傫偱偟偨丅");
        }
    }
    public Memento createMemento() {                // 僗僫僢僾僔儑僢僩傪嶣傞
        Memento m = new Memento(money);
        Iterator it = fruits.iterator();
        while (it.hasNext()) {
            String f = (String)it.next();
            if (f.startsWith("偍偄偟偄")) {         // 僼儖乕僣偼偍偄偟偄傕偺偩偗曐懚
                m.addFruit(f);
            }
        }
        return m;
    }
    public void restoreMemento(Memento memento) {       // 傾儞僪僁傪峴偆
        this.money = memento.money;
        this.fruits = memento.fruits;
    }
    public String toString() {                      // 暥帤楍昞尰
        return "[money = " + money + ", fruits = " + fruits + "]";
    }
    private String getFruit() {                     // 僼儖乕僣傪1屄摼傞
        String prefix = "";
        if (random.nextBoolean()) {
            prefix = "偍偄偟偄";
        }
        return prefix + fruitsname[random.nextInt(fruitsname.length)];
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -