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