-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
executable file
·195 lines (171 loc) · 4.49 KB
/
Player.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package battleships;
import java.awt.Point;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;
public class Player {
protected String name;
protected float hitrate;
protected float damagerate;
protected float healthpercentage;
protected float opponenthealth;
protected long playertime;
protected float hit = 0;
protected float shots = 0;
protected int id;
protected int health;
protected int startHealth;
public Scanner scan = new Scanner(System.in);
private Map<Point, Boolean> targetHistory = new HashMap<>();
private Grid grid = new Grid();
public Player(String name, int health, int id, int hitrate, int damagerate) {
super();
this.setName(name);
this.setHealth(health);
this.setID(id);
this.setHitrate(hitrate);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public int getShots() {
return (int) shots;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public void setShots(float shots) {
this.shots = shots;
}
public float getHitrate() {
return hitrate;
}
public void setHitrate(float hitrate) {
this.hitrate = hitrate;
}
public float getDamagerate() {
return damagerate;
}
public void setDamagerate(float damagerate) {
this.damagerate = damagerate;
}
public void setPlayerTime(long t) {
playertime = playertime + t;
}
public long getPlayerTime() {
return playertime / 1000;
}
public void makeGrid(int c) {
for (int i = 0; i < Constants.pshipAmount; i++) {
if (c == 1)
grid.placeShips(i);
if (c == 2)
grid.placeComputerShips(i);
}
setHealth(grid.countX());
}
public void printPlayerGrid() {
grid.printPlayerGrid();
}
public void turnToPlay(Player opponent) {
long time = System.currentTimeMillis();
boolean inputCorrect = true;
boolean action = true;
if (opponent.getHealth() == 0 || getHealth() == 0)
return;
opponenthealth = opponent.grid.countX();
System.out.println(getName() + " your turn to attack.");
String f = "Your previous attacks...";
while (inputCorrect) {
try {
while (action) {
if (opponent.getHealth() == 0 || getHealth() == 0)
break;
System.out.println(f);
for (int i = 0; i < f.length(); i++) {
String n = ("=");
System.out.print(n);
}
opponent.grid.printAttackGrid();
System.out.println();
if (shots != 0) {
System.out.println("Your health left: " + getHealth() + "/" + grid.countX() + ".");
System.out.println("Your hitrate is: " + getHitrate() + "%.");
System.out.println("Your damagerate is: " + getDamagerate() + "%.");
}
System.out.println(getName() + " choose the coordinates you want to attack (x y): ");
Point point = new Point(scan.nextInt(), scan.nextInt());
while (targetHistory.get(point) != null) {
System.out.println("This position has already been tried, try again (x y); ");
point = new Point(scan.nextInt(), scan.nextInt());
}
action = attack(point, opponent);
}
setPlayerTime(System.currentTimeMillis() - time);
inputCorrect = false;
} catch (InputMismatchException e) {
System.out.println("Incorrect input. Only use numbers. Try again.");
System.out.println();
scan.nextLine();
}
}
}
public void ComputerTurnToPlay(Player opponent) {
boolean action = true;
if (opponent.getHealth() == 0 || getHealth() == 0)
return;
opponenthealth = opponent.grid.countX();
while (action) {
if (opponent.getHealth() == 0 || getHealth() == 0)
break;
Point point = new Point(Utils.getRandomPoint());
while (targetHistory.get(point) != null) {
point = Utils.getRandomPoint();
}
action = attack(point, opponent);
}
}
public boolean attack(Point point, Player opponent) {
shots++;
boolean action = false;
String name = getName();
boolean isShipHit = opponent.grid.targetShip(point, name) != null;
if (isShipHit) {
hit++;
opponent.health--;
action = true;
}
hitPercentage();
damagePercentage();
targetHistory.put(point, isShipHit);
return action;
}
public float hitPercentage() {
if (hit != 0) {
hitrate = 100 * (hit / shots);
setHitrate(hitrate);
return hitrate;
}
return hitrate;
}
public float damagePercentage() {
if (hit != 0) {
damagerate = 100 * (hit / opponenthealth);
setDamagerate(damagerate);
}
return damagerate;
}
}