-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
181 lines (160 loc) · 5.33 KB
/
Board.java
File metadata and controls
181 lines (160 loc) · 5.33 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.Scanner;
public abstract class Board
{
//Variables
private Piece[][] board;
private boolean player1Turn;
private boolean withComputer;
private boolean gameOver;
//Constructors
public Board (int size, boolean computer) {
this.board = new Piece[size][size];
resetBoard();
this.player1Turn = true;
this.withComputer = computer;
}
public Board (int row, int col, boolean computer) {
this.board = new Piece[row][col];
resetBoard();
this.player1Turn = true;
this.withComputer = computer;
this.gameOver = false;
}
//Normal Methods
public boolean takeTurn() { //Takes One Player Turn
printBoard();
if (player1Turn) {
System.out.println("Player 1 " + getPlayer1Char());
} else {
System.out.println("Player 2 " + getPlayer2Char());
}
Scanner input = new Scanner(System.in);
int row;
int col;
do {
System.out.print("row: ");
row = input.nextInt() - 1;
System.out.print("col: ");
col = input.nextInt() - 1;
if(!canPlayHere(row, col)) {
System.out.println("Out of bounds");
}
if(row == 98 || col == 98) {
gameOver = true;
}
} while(!canPlayHere(row, col));
if(this.canPlayHere(row, col)) {
board[row][col] = makePiece();
player1Turn ^= true;
return true;
} else {
return false;
}
}
public boolean compTakeTurn() { //Takes One Computer Turn
printBoard();
System.out.println("Comp " + getPlayer2Char());
int row;
int col;
do {
row = (int) (Math.random()*board.length);
col = (int) (Math.random()*board[0].length);
} while(!canPlayHere(row, col));
System.out.println("row: " + row);
System.out.println("col: " + col);
if(this.canPlayHere(row, col)) {
board[row][col] = makePiece();
player1Turn ^= true;
return true;
} else {
return false;
}
}
public boolean tied () { //Tie Conditions
boolean done = true;
for (int r = 0; r < getBoard().length; r++) {
for (int c = 0; c < getBoard()[0].length; c++) {
if (getPiece(r, c) == '*') {
done = false;
}
}
}
return (done && !gameOver());
}
public void printBoard () { //Prints out current Board
if( makePiece() instanceof Connect4Piece || makePiece() instanceof OthelloPiece) { //row labels
if(makePiece() instanceof OthelloPiece) { //spacing for col labels
System.out.print(" ");
}
for (int c = 0; c < board[0].length; c++) {
System.out.print((c + 1) + " ");
}
System.out.println();
}
for (int r = 0; r < board.length; r++) {
if(makePiece() instanceof OthelloPiece) { //col labels
System.out.print(r + 1 + " ");
}
for (int c = 0; c < board[0].length; c++) {
board[r][c].printPiece();
System.out.print(" ");
}
System.out.println();
}
}
public void resetBoard () { //Creates Default Board
for (int r = 0; r < board.length; r++) {
for (int c = 0; c <board[0].length; c++) {
board[r][c] = makeBlankPiece();
}
}
gameOver = false;
}
//Board Getters
public boolean getWithComputer () {
return this.withComputer;
}
public boolean getPlayer1Turn () {
return this.player1Turn;
}
public char getPiece (int row, int col) {
return board[row][col].getPiece();
}
public Piece[][] getBoard() {
return board;
}
public boolean getPlayer1Turn(int row, int col) { //returns whose piece it is
return board[row][col].isPlayer1Piece();
}
public boolean getGameOver () {
return gameOver;
}
public char getPlayer1Char () {
return makePiece().getPlayer1Char();
}
public char getPlayer2Char () {
return makePiece().getPlayer2Char();
}
public boolean getEmpty(int row, int col) {
return board[row][col].getEmpty();
}
//Board Setters
public void setPlayer1Turn () {
player1Turn ^= true;
}
public void setPiece (int row, int col) {
board[row][col] = makePiece();
}
public void switchVal (int r, int c) {
board[r][c].switchVal();
}
public void setGameOver (boolean b) {
gameOver = b;
}
//Abstract Methods
public abstract boolean play(); //Loops until GameOver
public abstract boolean canPlayHere (int row, int col); //Move conditions
public abstract boolean gameOver(); //Win conditions
public abstract Piece makePiece(); //With Specific Board Piece (For specific Game)
public abstract Piece makeBlankPiece(); //Used in resetBoard
}