-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
104 lines (91 loc) · 2.42 KB
/
Copy pathPlayer.java
File metadata and controls
104 lines (91 loc) · 2.42 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
/*==============================================
class Player -- superclass for the players of Battleships
==============================================*/
public abstract class Player {
//========== Instance Variables ==========//
protected String _nation;
protected Board _battlefield;
protected boolean _isTurn;
protected int _numShips;
protected int _shields;
//something
Tile s0 = new Tile ("boat # 1", 2 , "-X-");
Tile s1 = new Tile ("boat # 2", 2,"-X-");
Tile s2 = new Tile ("submarine", 3, "-X-");
Tile s3 = new Tile ("destroyer", 3, "-X-");
Tile s4 = new Tile ("battleship", 4, "-X-");
Tile s5 = new Tile ("aircraft carrier", 5, "-X-");
protected Tile[] ships = {s0, s1, s2, s3, s4, s5};
//========== Default Constructor =========//
public Player () {
_numShips = 5;
_isTurn = false;
_battlefield = new Board();
}
//========== Constructor =========//
public Player( String nation ){
this();
_nation = nation;
}
//========== METHODS ==========//
//Accessors
public Board getBattlefield() {
return _battlefield;
}
public String getNation() {
return _nation;
}
public boolean getTurn() {
return _isTurn;
}
public void flipTurn() {
_isTurn = ! _isTurn;
}
//Instantiates new board
public void setBattlefield() {
_battlefield = new Board();
}
public String toString() {
return _nation + "'s Board\n" + _battlefield;
}
public boolean hasShips() {
return _battlefield.numShips() > 0;
}
public boolean hit (Player opp, int r, int c) {
Tile t = opp._battlefield.get(r,c);
if (t.isFaceUp() ) {
if (! t.getType().equals("water") || ! t.getType().equals("axis")) {
if (opp._shields > 0) {
opp._shields--;
System.out.println("blocked\n");
flipTurn();
}
else {
opp._battlefield.flip(r,c);
System.out.println("Yay, a hit\n");
}
}
else if (t.getType().equals("bonus")) {
opp._battlefield.flip(r,c);
opp._shields++;
System.out.println("gained a bonus, +1 shield\n");
flipTurn();
}
}
else {
flipTurn();
System.out.println("no luck\n");
}
return _isTurn;
}
//abstract
public abstract void attack(Player opp);
public abstract void setBoard ();
public static void main (String[] args) {
Player p = new Human();
Tile[] a = p.ships;
for (Tile t : a) {
System.out.println(t._face);
}
}
}