-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
139 lines (120 loc) · 3.37 KB
/
Board.java
File metadata and controls
139 lines (120 loc) · 3.37 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
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
131
132
133
134
135
136
137
138
139
// Abstract Class for any 2-Dimensional game board.
import java.util.ArrayList;
// It is entirely up to you to decide how you want to store the game
// state, what variables you want you use, what helper methods you want to
// write, etc.
// Your code will be graded primarily on correctness. It will also be graded
// on good style, efficiency, organization, and proper encapsulation
public abstract class Board {
//We don't really use there anymore
private char vertLine = '|';
private char horzLine = '-';
private char crossLine = '+';
//Added for lab 9
private ArrayList<Integer> history = new ArrayList<Integer>();
/*
* Return the total number of rows of the Board
*/
abstract int getRows();
/*
* Return the total number of Columns of the Board
*/
abstract int getCols();
/*
* Return the piece at row r, column c on the board.
* Row 0 means the bottom row
* Column 0 means the left most column
*/
abstract int getBoardPiece(int r, int c);
/*
* Make a move for the current player
* Return true if the move was successfully made
* Return false otherwise
*
* Edited for lab 9
* Your subclasses should call super.makeMove as part of their own makeMove
* methods.
*/
public boolean makeMove(int move){
if(!isValidMove(move)) {
return false;
}
history.add(new Integer(move));
return true;
}
/*
* Remove the top piece in the given column
* Return true if the removal was successfully done
* Return false otherwise
*/
abstract boolean undoMove(int move);
/*
* Return 0 if no player has won this board.
* Return 1 is Player 1 has won this board.
* Return 2 if Player 2 has won this board.
*/
abstract int isWinning();
abstract boolean isValidMove(int move);
abstract int minMove();
abstract int maxMove();
public ArrayList<Integer> validMoves() {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = minMove(); i <= maxMove(); i++) {
if (isValidMove(i)) {
list.add(new Integer(i));
}
}
return list;
}
public int numTotalMoves(){
return history.size();
}
public int nextTurn(){
return numTotalMoves()%2;
}
public void revert(int moveNum) {
if (moveNum < 0) {
moveNum = 0;
}
int i = history.size();
while (i > moveNum) {
undoMove(history.get(i-1));
history.remove(i-1);
i--;
}
}
/*
* Return a String representation of the current board state.
* It should be understandable enough for two players to play the game.
* We don't really use this anymore
*/
public String toString() {
//A StringBuilder is an advanced way to avoid creating lots of string
//objects as you do your addition.
int rows = getRows();
int cols = getCols();
StringBuilder s = new StringBuilder(2*(2*rows+1)*(cols+1));
s.append(crossLine);
for(int c = 0; c < cols; c++) {
s.append(horzLine);
s.append(crossLine);
}
s.append('\n');
for(int r = rows-1; r >= 0; r--) {
for(int c = 0; c < cols; c++) {
s.append(vertLine);
s.append(getBoardPiece(r,c));
}
s.append(vertLine);
s.append('\n');
//A horizontal Line
s.append(crossLine);
for(int c = 0; c < cols; c++) {
s.append(horzLine);
s.append(crossLine);
}
s.append('\n');
}
return s.toString();
}
}