-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumn.java
More file actions
92 lines (79 loc) · 1.97 KB
/
Column.java
File metadata and controls
92 lines (79 loc) · 1.97 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
package connect4;
/**
* Column class
*/
public class Column {
private int numRows; // number of rows in column
private Counter[] rows; // Array of counters
private int counterPos = 0; // last counter position
/**
* Constructor
*/
public Column(int numRows) {
this.numRows = numRows;
counterPos = numRows; // Initial position is the bottom of the column (top - 0)
rows = new Counter[numRows]; // Resize the array to fit correct number of counters
}
/**
* Checks if the column is full. If counter position is 0 then column is full
*
* @return true if is full, false if not
*/
public boolean isFull() {
if (counterPos == 0)
return true;
return false;
}
/**
* Adds the counter to the column
*/
public boolean add(Counter c) {
// If column is full - return false
if (isFull())
return false;
// Add the counter to the next position
rows[counterPos - 1] = c;
// Update the last counter position
counterPos--;
return true;
}
/**
* Gets counter from the given position
*
* @param pos position of the counter
* @return counter or null
*/
public Counter getCounter(int pos) {
return rows[pos];
}
/**
* Return the last counter position
*
* @return
*/
public int getLastFilledRowNum() {
return counterPos;
}
/**
* Displays the current counter symbol or space if it's empty
*
* @param num
* @return
*/
public String displayRow(int num) {
if (rows[num] == null)
return " ";
Counter row = rows[num];
return row.toString();
}
/**
* Utility method to display the whole column
*
* @return
*/
public void display() {
for (int i = 0; i < numRows; i++) {
System.out.println(displayRow(i));
}
}
}