This repository has been archived by the owner on Jul 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square.java
112 lines (91 loc) · 1.73 KB
/
Square.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Class: Square
ROLES:
1. Hold one or more player pieces or hold a snake or ladder piece.
Objects of this class will be created when a new board is created and
deleted when the board is deleted.
*/
import java.util.*;
class Square {
private ArrayList<Piece> pieces;
private int row, col;
public Square() {
/*
Pre-conditions: None
Post-conditions: pieces will be instantiated.
*/
}
public Square(int r, int c) {
pieces = new ArrayList<Piece>();
row = r; col = c;
}
int getRow() {
return row;
}
int getCol() {
return col;
}
boolean hasPiece() {
/*
Pre-conditions: None
Post-conditions:
Will return true if there are one or more player pieces
on the square, false otherwise.
*/
return pieces.size() > 0;
}
boolean hasPiece(Piece p) {
/*
Pre-conditions: None
Post-conditions:
Will return true if the given piece p is on the square,
false otherwise.
*/
return pieces.contains(p);
}
boolean hasSnake() {
boolean result = false;
for(Piece p : pieces) {
if(p.getId() >= 0 && p.getId() < 3) {
result = true;
break;
}
}
return result;
}
boolean hasLadder() {
boolean result = false;
for(Piece p : pieces) {
if(p.getId() >= 3 && p.getId() < 6) {
result = true;
break;
}
}
return result;
}
Piece getSnake() {
Piece result = null;
for(Piece p : pieces) {
if(p.getId() >= 0 && p.getId() < 3) {
result = p;
}
}
return result;
}
Piece getLadder() {
Piece result = null;
for(Piece p : pieces) {
if(p.getId() >= 3 && p.getId() < 6) {
result = p;
}
}
return result;
}
void setPiece(Piece p) {
pieces.add(p);
}
void removePiece(Piece p) {
// What if p not present?
pieces.remove(p);
}
}