package virtualpet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
public class VirPet {
//前三个为全局变量
public static VirPet p = new VirPet(10, 20, 30, ""); // 数据初始化
public static int count = 0;
public static boolean judgeStop = false;
public String name = "Tommy";
public int Happiness;
public int Hungry;
public int Health;
public String status;
// 构造方法
public VirPet(int Happiness,int Hungry,int Health,String status) {
this.Happiness = Happiness;
this.Hungry = Hungry;
this.Health = Health;
this.status = status;
}
private void printInfo(int data) {
for(int i = 1;i <= (data / 2);i++) {
System.out.print("*");
}
for(int i = (data / 2 + 1);i <= 50;i++) {
System.out.print("-");
}
}
public void sleep() {
this.status = "我在睡觉...";
// System.out.println(this.status);
}
public void wake() {
this.status = "我醒着但我很无聊...";
// System.out.println(this.status);
}
public void walk() {
this.status = "我在散步...";
System.out.println(this.status);
}
public void play() {
this.status = "我在玩耍...";
System.out.println(this.status);
}
public void feed() {
this.status = "我在吃饭...";
System.out.println(this.status);
}
public void seeDoctor() {
this.status = "我在看医生...";
System.out.println(this.status);
}
public void curStatus() {
System.out.println("n当前时间:" + count);
System.out.println("我当前的状态:" + this.status);
System.out.print("Happiness : Sad");
printInfo(this.Happiness);
System.out.println("Happy(" + this.Happiness + ")");
System.out.print("Hungry : Full");
printInfo(this.Hungry);
System.out.println("Hungry(" + this.Hungry + ")");
System.out.print("Health : Sick");
printInfo(this.Health);
System.out.println("Health(" + this.Health + ")");
}
public void orderWakePet(String order) {
if(order.equals("walk")) {
walk();
}else if(order.equals("play")) {
play();
}else if(order.equals("feed")) {
feed();
}else if(order.equals("seedoctor")) {
seeDoctor();
}else if(order.equals("letalone")) {
wake();
System.out.println(this.status);
}else {
System.out.println("我不懂你在说什么");
}
}
public void orderSleepPet(String order) {
if(order.equals("letalone")) {
sleep();
System.out.println(this.status);
}else {
System.out.println("你确定要吵醒我吗?我在睡觉,你要是坚持吵醒我,我会不高兴的!(y表示是/其他表示不是)");
Scanner s = new Scanner(System.in);
String ans = s.nextLine();
if(ans.equals("y")) {
if(order.equals("walk")) {
walk();
}else if(order.equals("play")) {
play();
}else if(order.equals("feed")) {
feed();
}else if(order.equals("seedoctor")) {
seeDoctor();
}else if(order.equals("letalone")) {
sleep();
System.out.println(this.status);
}else {
System.out.println("我不懂你在说什么");
}
}
}
}
// 从文件中读取数据
public void readFromPath() throws Exception {
FileReader r = new FileReader("D:\java\eclipse\demo\src\virtualpet\Data.txt");
BufferedReader br = new BufferedReader(r);
char[] c = new char[50];
int len = 0;
String s = null;
if((len = br.read(c)) != -1) {
s= new String(c, 0, len);
}
String[] str = s.split(" ");
int[] a = new int[4];
for(int i = 0;i < 4;i++) {
a[i] = Integer.parseInt(str[i]);
}
count = a[0];
Happiness = a[1];
Hungry = a[2];
Health = a[3];
status = str[4];
br.close();
r.close();
}
// 保存宠物数据到文件中
public void writeToPath() throws Exception {
FileWriter w = new FileWriter("D:\java\eclipse\demo\src\virtualpet\Data.txt");
BufferedWriter bw = new BufferedWriter(w);
bw.write(count + " ");
bw.write(Happiness + " ");
bw.write(Hungry + " ");
bw.write(Health + " ");
bw.write(status + " ");
bw.flush();
bw.close();
w.close();
}
public static void main(String[] args) {
// 先读取数据
try {
p.readFromPath();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("我的名字叫Tommy,一只可爱的猫咪....");
System.out.println("你可以和我一起散步玩耍,你也可以给我好吃的东西,带我去看病,也可以让我发呆....");
System.out.println("Commands:");
System.out.println("1.walk:散步");
System.out.println("2.play:玩耍");
System.out.println("3.feed:喂我");
System.out.println("4.see doctor:看医生");
System.out.println("5.letalone:让我独自一人");
System.out.println("6.status:查看我的状态");
System.out.println("7.bye:不想看到我n");
// 时钟的线程
new Thread(new Runnable() {
private int rangeData(int num) {
if(num > 100) {
num = 100;
}else if(num < 0) {
num = 0;
}
return num;
}
@Override
public void run() {
if(count == 23) {
p.sleep();
}else if(count == 7) {
p.wake();
}
if(p.status.equals("我在散步...")) {
p.Hungry += 3;
p.Health += 1;
}else if(p.status.equals("我在玩耍...")) {
p.Hungry += 3;
p.Happiness += 1;
}else if(p.status.equals("我在吃饭...")) {
p.Hungry -= 3;
}else if(p.status.equals("我在看医生...")) {
p.Health += 4;
}else if(p.status.equals("我在睡觉...")) {
p.Hungry += 1;
}else if(p.status.equals("我醒着但我很无聊...")) {
p.Hungry += 2;
p.Happiness -= 1;
}
if((!p.status.equals("我在睡觉...")) && count < 8) {
p.Happiness -= 4;
}
if(p.Happiness < 20) {
p.Health -= 1;
}
if(p.Hungry > 80 || p.Hungry < 20) {
p.Health -= 2;
}
p.Hungry = rangeData(p.Hungry);
p.Health = rangeData(p.Health);
p.Happiness = rangeData(p.Happiness);
// System.out.println(count);
count += 1;
if(judgeStop == false) {
if(count < 24) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
run();
}else {
count = 0;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
run();
}
}
}
}).start();
// 宠物的线程
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
System.out.print("你想做什么:");
Scanner s = new Scanner(System.in);
String ans = s.next();
if(ans.equals("bye")) {
try {
p.writeToPath();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
judgeStop = true;
System.out.println("记得来找我!Bye...");
break;
}else if(ans.equals("status")) {
p.curStatus();
}else {
if(count >= 8) {
p.orderWakePet(ans);
}else {
p.orderSleepPet(ans);
}
}
}
}
}).start();
}
}
是根据之前写的python版本改写的,功能都是相同的,但是感觉写起来java和python还是有蛮大区别的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)