-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
149 lines (134 loc) · 4.04 KB
/
Game.java
File metadata and controls
149 lines (134 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.tw.trainning.fightergame;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Random;
import com.tw.trainning.fightergame.player.Player;
import com.tw.trainning.fightergame.player.Soldier;
import com.tw.trainning.fightergame.weapon.Weapon;
import com.tw.trainning.fightergame.weapon.WeaponRespository;
public class Game {
Player playerA;
Player playerB;
PrintStream out;
public Game(Player playerA, Player playerB, PrintStream print) {
this.playerA = playerA;
this.playerB = playerB;
this.out = print;
}
public void start(){
boolean isPlayerBDefeated = false;
while(playerA.isLive(out)){
playerA.attack(playerB, out);
if(!playerB.isLive(out)){
isPlayerBDefeated = true;
break;
}
playerB.attack(playerA, out);
}
Player player = isPlayerBDefeated ? playerB : playerA;
player.outputStatus(out);
}
private void start(InputStream in){
int i = 1;
boolean isPlayerBDefeated = false;
System.out.println("--------第"+i+"回合-------------");
while(playerA.isLive(out)){
playerA.attack(playerB, out);
wait123(2);
if(!playerB.isLive(out)){
isPlayerBDefeated = true;
break;
}
playerB.attack(playerA, out);
//continue123(in);
wait123(2);
i++;
System.out.println();
System.out.println("--------第"+i+"回合-------------");
}
Player player = isPlayerBDefeated ? playerB : playerA;
player.outputStatus(out);
System.out.println("输入bye结束游戏,按回车键游戏继续");
}
private void wait123(int seconds){
try {
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
}
}
public static void main(String[] args){
Random random = new Random();
WeaponRespository weaponRes = new WeaponRespository(random);
Game game;
System.out.println("按回车键游戏开始");
while(!isBye()){
game = gameGenerator(random, weaponRes);
game.start(System.in);
weaponRes = weaponRes.shuttle();
}
}
private static boolean isBye(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try{
return "bye".equalsIgnoreCase(in.readLine());
}
catch(Exception e){
return true;
}
}
private static Game gameGenerator(Random random, WeaponRespository weaponRes){
String name;
int attackValue;
String[] inputs = getInput();
name = inputs[0];
attackValue = Integer.parseInt(inputs[1]);
Player myself;
Player other;
myself = generateSelf(random, weaponRes, name, attackValue);
other = generateCompetor(random, 10, weaponRes);
return new Game(myself, other, System.out);
}
private static Player generateSelf(Random random, WeaponRespository weaponRes, String name, int attackValue){
Weapon weapon;
Player myself;
weapon = weaponRes.fetchWeapon(random.nextInt(weaponRes.size()));
System.out.println("系统为您选择的武器是:\r\n"+weapon.toString());
myself = new Soldier(name, attackValue, 100, weapon, 10);
System.out.println("根据您的输入,您的信息如下:\r\n"+myself.toString());
return myself;
}
private static String[] getInput(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name;
int attackValue;
while(true){
try {
System.out.println("请输入您的名字:");
name = in.readLine();
name = (name == null || "".equals(name) ? "王小二" : name);
System.out.println("请输入您的攻击力:");
try{
attackValue = Integer.parseInt(in.readLine());
}
catch(Exception e){
attackValue = 5;
}
break;
} catch (Exception e) {
System.err.println("输入有误,请重新输入!");
}
}
String[] strs = {name, String.valueOf(attackValue)};
return strs;
}
private static Player generateCompetor(Random random, int attackValue, WeaponRespository weaponRes) {
Weapon weapon = weaponRes.fetchWeapon(random.nextInt(weaponRes.size()));
String name = "武大郎";
int value = random.nextInt(attackValue) + 1;
Player player = new Soldier(name, value, 100, weapon, 10);
System.out.println("您的对手信息如下:\r\n"+player.toString());
return player;
}
}