-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.java
90 lines (79 loc) · 2.03 KB
/
Utils.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
package Battleship1;
import java.awt.Point;
public class Utils {
private Utils() {
}
// CHECK DISTANCE BETWEEN TWO POINTS
public static double distanceBetweenPoints(Point from, Point to) {
double x1 = from.getX();
double y1 = from.getY();
double x2 = to.getX();
double y2 = to.getY();
if (x2 - x1 == 0) {
if (y2 == 0 || y1 == 0)
return Point.distance(x1, y1, x2, y2) + 1;
}
if (y2 - y1 == 0) {
if (x2 == 0 || x1 == 0)
return Point.distance(x1, y1, x2, y2) + 1;
}
return Point.distance(x1, y1, x2, y2);
}
// CHECK IS POINT IS EQUAL TO POSITION
public static boolean isPointEqual(Point point, Position position) {
Point from = position.getFrom();
Point to = position.getTo();
if (point.equals(from) || point.equals(to))
return true;
return false;
}
// CHECK IF BOINT IS BETWEEN POINTS IN POSITION
public static boolean isPointBetween(Point point, Position position) {
Point from = position.getFrom();
Point to = position.getTo();
return from.getY() <= point.getY() && to.getY() >= point.getY() && from.getX() <= point.getX()
&& to.getX() >= point.getX();
}
// GET RANDOM POINT FOR BOT
public static Point getRandomPoint() {
int xmin = 0;
int xmax = 9;
int x = (int) ((Math.random() * ((xmax - xmin) + 1)) + xmin);
int y = (int) ((Math.random() * ((xmax - xmin) + 1)) + xmin);
Point point = new Point(x, y);
return point;
}
// PRINT EMPTY
public static void printEmpty() {
for (int i = 0; i < Constants.emptySpace; i++) {
System.out.println("\n");
}
}
// SHIP NUMBERS
public static String shipNbr(int i) {
switch (i) {
case 0:
String first = "first";
return first;
case 1:
String second = "second";
return second;
case 2:
String third = "third";
return third;
case 3:
String fourth = "fourth";
return fourth;
}
String fifth = "fifth";
return fifth;
}
// PRESS ANY KEY..
public static void pressAnyKeyToContinue() {
System.out.println("Press Enter key to continue...");
try {
System.in.read();
} catch (Exception e) {
}
}
}