-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmarine.java
More file actions
199 lines (162 loc) · 4.13 KB
/
Submarine.java
File metadata and controls
199 lines (162 loc) · 4.13 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
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
196
197
198
199
/**
* Submarine.java 5/17/2017
*
* Rahul Kumar
* AP Computer Science
* Mrs. Hitch 3rd Period
*
*
*/
//Each method does what the "Ship" interface, but goes to 3 positions for the "X"
public class Submarine implements Ship
{
private int numofhits;
public Submarine()
{
numofhits = 0;
}
public int[][] init(int[][] board)
{
boolean condition = false;
while(!condition) //repeats till Submarine fits somewhere
{
int randfordir = (int)(Math.random() * 3); //chooses if board should be nited up, down, left, right
System.out.println(randfordir);
int randomcol = (int)(Math.random() * board[0].length); //gets a random col pos
int randomrow = (int)(Math.random() * board.length); //gets a random row pos
if((randfordir == 0) && (goRightfromPos(randomrow, randomcol, board) != null))
{
board = goRightfromPos(randomrow, randomcol, board); //inits Sub to the right
condition = true;
}
else if(randfordir == 1 && (goLeftfromPos(randomrow, randomcol , board) != null))
{
board = goLeftfromPos(randomrow, randomcol, board); //inits Sub to the left
System.out.println("Left");
condition = true;
}
else if(randfordir == 2 && (goUpfromPos(randomrow, randomcol, board) != null))
{
board = goUpfromPos(randomrow, randomcol, board); //inits Sub Up
condition = true;
}
else if(randfordir == 3 && (goDownfromPos(randomrow, randomcol, board) != null))
{
board = goDownfromPos(randomrow, randomcol, board); //inits Sub Down
condition = true;
}
}
return board;
}
public int[][] goRightfromPos(int row, int col, int[][] paraboard)
{
int amountRight = paraboard[0].length - col; //finds how many spots are to the right of the pos
if(amountRight >= 3) //if its enough for a Sub
{
int count = 0;
for(int i = col; i < col + 3; i++) //if there is free space
{
if(paraboard[row][i] == -1)
count++;
}
if(count == 3)
{
for(int i = col; i < col + 3; i++)
{
paraboard[row][i] = 6; //setting the positions "6" for Sub
}
return paraboard; //returns new board
}
return null; //returns null if there isn't enough free space
}
else
{
return null; //not enough pos to the right
}
}
public int[][] goLeftfromPos(int row, int col, int[][] paraboard)
{
if(col >= 2) //if there is enough space to the left of the pos
{
int count = 0;
for(int i = col; i > col - 3; i--) //if there is free space
{
if(paraboard[row][i] == -1)
count++;
}
if(count == 3)
{
for(int i = col; i > col - 3; i--)
{
paraboard[row][i] = 6; //setting the positions "6" for Sub
}
return paraboard; //returns new board
}
return null; //returns null if there isn't enough free space
}
else
return null; //not enough pos to the left
}
public int[][] goUpfromPos(int row, int col, int[][] paraboard)
{
if(row >= 2) //if there is enough space up from the pos
{
int count = 0;
for(int i = row; i > row - 3; i--) //if there is free space
{
if(paraboard[i][col] == -1)
count++;
}
if(count == 3)
{
for(int i = row; i > row - 3; i--)
{
paraboard[i][col] = 6; //setting the positions "6" for Sub
}
return paraboard; //returns new board
}
return null; //returns null if there isn't enough free space
}
else
return null; //not enough pos Up
}
public int[][] goDownfromPos(int row, int col, int[][] paraboard)
{
int amountDown = paraboard.length - row; //finds how many spots are down from the pos
if(amountDown >= 3)
{
int count = 0;
for(int i = row; i < row + 3; i++) //if there is free space
{
if(paraboard[i][col] == -1)
count++;
}
if(count == 3)
{
for(int i = row; i < row + 3; i++)
{
paraboard[i][col] = 6; //setting the positions "6" for Sub
}
return paraboard; //returns new board
}
return null; //returns null if there isn't enough free space
}
else
return null; //not enough pos Down
}
public void incrementHits()
{
numofhits++;
}
public boolean hasSank()
{
if(numofhits == 3)
return true;
else
return false;
}
public void resetShip()
{
numofhits = 0;
}
}