-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameHelper.java
More file actions
108 lines (87 loc) · 3.29 KB
/
Copy pathGameHelper.java
File metadata and controls
108 lines (87 loc) · 3.29 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
import java.util.*;
public class GameHelper {
private static final String ALPHABET = "abcdefg"; // constants
private static final int GRID_LENGTH = 7;
private static final int GRID_SIZE = 49;
private static final int MAX_ATTEMPTS = 200;
static final int HORIZONTAL_INCREMENT = 1;
static final int VERTICAL_INCREMENT = GRID_LENGTH;
private final int[] grid = new int[GRID_SIZE];
private final Random random = new Random();
private int shipCount = 0;
public String getUserInput(String text){
System.out.print(text + ": ");
Scanner sc = new Scanner(System.in);
return sc.nextLine().toLowerCase();
}
public ArrayList<String> placeShip(int shipsSize) {
//holds index to grid (0 - 48)
int[] shipCoordinates = new int[shipsSize]; //current candidate coords
int attempts = 0;
boolean success = false;
shipCount++;
int increment = getIncrement(); //alternate vertical&horizontal align
while(!success & attempts++ < MAX_ATTEMPTS) { // MAIN SEARCH LOOP
int location = random.nextInt(GRID_SIZE); //get random start point
for(int i = 0; i < shipCoordinates.length; i++) { //create arr of proposed coords
shipCoordinates[i] = location; //put current loc in array
location += increment; //calc next location
}
System.out.println("Trying: " + Arrays.toString(shipCoordinates));
if(shipFits(shipCoordinates, increment)) { //if ship fits on the grid
success = coordsAvailable(shipCoordinates);
}
}
savePositionToGrid(shipCoordinates); // coordinates passed checks, save
ArrayList<String> alphaCells = convertCoordsToAlphaFormat(shipCoordinates);
System.out.println("Placed at: " + alphaCells);
return alphaCells;
}
private int getIncrement() {
if(shipCount % 2 == 0) { // if an even ship
return HORIZONTAL_INCREMENT; //place horizontally
} else { //else odd ship
return VERTICAL_INCREMENT; //place vertically
}
}
private boolean shipFits(int[] shipCoords, int increment) {
int finalLocation = shipCoords[shipCoords.length - 1];
if(increment == HORIZONTAL_INCREMENT) {
//check end is on same row as start
return calcRowFromIndex(shipCoords[0]) == calcRowFromIndex(finalLocation);
} else {
return finalLocation < GRID_SIZE; //check end isn't off the bottom
}
}
private boolean coordsAvailable(int[] shipCoords) {
for(int coord : shipCoords){ // check all potential positions
if(grid[coord] != 0){ //this position already taken
System.out.println("position: " + coord + " already taken.");
return false;
}
}
return true; // no clashes
}
private void savePositionToGrid(int[] shipCoords) {
for(int index : shipCoords) {
grid[index] = 1; //mark grid position as used''
}
}
private ArrayList<String> convertCoordsToAlphaFormat(int[] shipCoords) {
ArrayList<String> alphaCells = new ArrayList<String>();
for(int index : shipCoords) { // for each grid coordinate
String alphaCoords = getAlphaFromIndex(index); //turn it in a 'a0' style
alphaCells.add(alphaCoords); //add to a list
}
return alphaCells;
}
private String getAlphaFromIndex(int index){
int row = calcRowFromIndex(index); //get row val
int column = index % GRID_LENGTH; //get numeric comlumn value
String letter = ALPHABET.substring(column, column + 1); //convert to letter
return letter + row;
}
private int calcRowFromIndex(int index){
return index / GRID_LENGTH;
}
}//end class