-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputer.java
More file actions
94 lines (76 loc) · 2.04 KB
/
Copy pathComputer.java
File metadata and controls
94 lines (76 loc) · 2.04 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
/*==================================
class Computer -- subclass of Player
==================================*/
public class Computer extends Player {
//========== Instance Variables ==========//
private int _prevR, _prevC;
private String _difficulty;
//========== Default Constructor ==========//
public Computer() {
super();
_nation = "Computerland";
_prevR = 100;
_prevC = 100;
setBoard();
_battlefield.sprinkle();
}
public Computer (String diff) {
this();
_nation = "Computerland";
_difficulty = diff;
}
//========== METHODS ==========//
public int guessR() {
//Computer uses previous guess (if there was one) to make an educauted guess
//Will add a random number from the range [-1,1]
if (_difficulty.equals("Hard") && _prevR != 100) {
return _prevR + (int)(3 * Math.random()) - 1;
}
//Otherwise just pick a random # from 1 to 11
else
return 1 + (int) (10 * Math.random());
}
public int guessC() {
if (_difficulty.equals("Hard") && _prevC != 100) {
return _prevC + (int) (3 * Math.random()) - 1;
}
else
return 1 + (int) (10 * Math.random());
}
public void attack (Player opp) {
int r = guessR();
int c = guessC();
if (hit (opp, r, c)) {
_prevR = r;
_prevC = c;
}
}
public void setBoard() {
int r, c, i;
String dir;
String[] compass = {"north", "south", "east", "west"};
i = 0;
while (i < ships.length) {
r = (int) (10 * Math.random());
c = (int) (10 * Math.random());
dir = compass[(int)( 4 * Math.random())];
if (_battlefield.place(r,c,dir, ships[i])) {
i++;
}
}
}
public static void main (String[] args) {
Player pc = new Computer("Hard");
System.out.println(pc._battlefield.reveal());
Human h = new Human("human");
Tile t = new Tile("destroyer", 3, "-x-");
//t.flip();
pc._battlefield.set(3,1,t);
System.out.println(pc);
//System.out.println(h);
h.attack(pc);
/*
((Computer)pc).setPrevC(0);
System.out.println(((Computer)pc).guessC());*/
}
}