-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonAI.java
94 lines (77 loc) · 2.17 KB
/
CommonAI.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
91
import java.io.File;
import java.io.FileNotFoundException;
public abstract class CommonAI extends Player{
protected int lastRowFired, lastColFired, hitCheckRow, hitCheckCol;
protected boolean directionsChecked[], sinkingShip, hitAMiss;
public CommonAI(File boardFile) throws FileNotFoundException {
super(boardFile);
directionsChecked = new boolean[4];
for(int c = 0; c < 4; c++)
directionsChecked[c] = true;
sinkingShip = false;
hitAMiss = false;
}
public CommonAI(int cols, int rows) throws BattleshipException {
super(cols, rows);
directionsChecked = new boolean[4];
for(int c = 0; c < 4; c++)
directionsChecked[c] = true;
sinkingShip = false;
hitAMiss = false;
}
public abstract boolean takeTurn(BattleshipBoard playerBoard);
public void sinkShip(BattleshipBoard playerBoard){
boolean keepChecking = true;
if(hitAMiss){
lastRowFired = hitCheckRow;
lastColFired = hitCheckCol;
}
hitAMiss = false;
for(int c = 0; c < 4; c++){
if(!directionsChecked[c] && keepChecking){
keepChecking = false;
if(c == 0 && lastColFired > 0)
lastColFired -= 1;
if(c == 1 && lastColFired < 9)
lastColFired += 1;
if(c == 2 && lastRowFired > 0)
lastRowFired -= 1;
if(c == 3 && lastRowFired < 9)
lastRowFired += 1;
if(spacesHit[lastColFired][lastRowFired]){
directionsChecked[c] = true;
lastRowFired = hitCheckRow;
lastColFired = hitCheckCol;
keepChecking = true;
}
if(!playerBoard.isOccupied(lastColFired, lastRowFired) && !keepChecking){
directionsChecked[c] = true;
hitAMiss = true;
}
}
}
if(keepChecking){
System.out.println("notsinkinganymore");
sinkingShip = false;
lastRowFired = hitCheckRow;
lastColFired = hitCheckCol;
}
}
public void checkSinkShip(BattleshipBoard playerBoard){
if(playerBoard.isOccupied(lastColFired, lastRowFired) && sinkingShip == false)
{
sinkingShip = true;
hitCheckRow = lastRowFired;
hitCheckCol = lastColFired;
hitAMiss = false;
for(int c = 0; c < 4; c++)
directionsChecked[c] = false;
}
}
public int getLastRow(){
return lastRowFired;
}
public int getLastCol(){
return lastColFired;
}
}