-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomAI.java
38 lines (29 loc) · 895 Bytes
/
RandomAI.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
import java.io.File;
import java.io.FileNotFoundException;
public class RandomAI extends CommonAI{
public RandomAI(File boardFile) throws FileNotFoundException {
super(boardFile);
lastRowFired = (int)(Math.random()*10);
lastColFired = (int)(Math.random()*10);
}
public RandomAI(int cols, int rows) throws BattleshipException {
super(cols, rows);
lastRowFired = (int)(Math.random()*10);
lastColFired = (int)(Math.random()*10);
}
public boolean takeTurn(BattleshipBoard playerBoard)
{
if(sinkingShip)
sinkShip(playerBoard);
if(!sinkingShip)
while(spacesHit[lastColFired][lastRowFired])
{
lastRowFired = (int)(Math.random()*10);
lastColFired = (int)(Math.random()*10);
}
spacesHit[lastColFired][lastRowFired] = true;
checkSinkShip(playerBoard);
this.setTurn(false);
return playerBoard.fireShot(lastColFired, lastRowFired);
}
}