-
Notifications
You must be signed in to change notification settings - Fork 0
/
Peg.java
130 lines (117 loc) · 2.92 KB
/
Peg.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.ArrayList;
public class Peg
{
private final int MAX_BEADS = 4;
private int xCoord = 0;
private int yCoord = 0;
private int numBeads = 0;
private Beadlike[] beads = new Beadlike[4];
/**
* The constructor is created by the Grid and creates a peg at he given x
* and y coordinate. There is no need for a programmer to create a peg
* manually as it is already done by the Grid.
*/
public Peg(int x, int y)
{
Debug.log("Creating Peg at: (x: " + x + ", y: " + y +")");
xCoord = x;
yCoord = y;
for (int z = 0; z < MAX_BEADS; z++)
{
beads[z] = new EmptyBead(x, y, z);
}
}
/**
* Call this to add a Bead to this individual Peg. Although for actual
* gameplay is is perhaps better to call PLayer.placeBead(...) instead.
* @param colour The colour of the bead you wish to create. The colour can
* be either PlayerColour.WHITE or PlayerColour.BLACK.
*/
public boolean addBead(PlayerColour colour)
{
boolean successful = false;
if (numBeads < 4)
{
beads[numBeads] = new Bead(colour, xCoord, yCoord, numBeads);
numBeads++;
successful = true;
}
else
{
successful = false;
}
return successful;
}
/**
* Removes a bead from this individual Peg.
* @return
*/
public boolean removeBead()
{
Debug.log("Attempting to remove bead.");
boolean successful = false;
if (numBeads > 0) {
beads[numBeads - 1] = new EmptyBead(xCoord, yCoord, numBeads);
numBeads--;
successful = true;
Debug.log("Successfully removed bead.");
}
else
{
successful = false;
}
return successful;
}
/**
* Gets the nth Bead from this Peg.
* @param n The nth Bead you want from the Peg, from 0-3.
* @return The Bead you wish to get.
*/
public Beadlike getBead(int n)
{
Beadlike bead = beads[n];
int x = bead.getXCoord();
int y = bead.getYCoord();
int z = bead.getZCoord();
Debug.log("Getting bead at: (x: " + x + ", " + y + ")");
return bead;
}
/**
* Gets all Beads from this Peg and returns a Beadlike array.
* @return
*/
public Beadlike[] getAllBeads()
{
Debug.log("Getting all beads from peg: (x: " + xCoord + ", y: " + yCoord + ")");
return beads;
}
/**
* Gets the x-coordinate (row) of the Peg.
* @return Integer representing the x-coordinate.
*/
public int getXCoord() {
return xCoord;
}
/**
* Gets the y-coordinate (column) of the Peg.
* @return Integer representing the x-coordinate.
*/
public int getYCoord() {
return yCoord;
}
/**
* Gets the number of Beads on the Peg.
* @return An integer representing the number of beads on the peg.
*/
public int getNumBeads() {
return numBeads;
}
/**
* Empties the beads on the Peg.
*/
public void emptyBeads(){
for (int i = 0; i < beads.length; i++)
beads[i] = null;
numBeads = 0;
}
}