-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
173 lines (155 loc) · 5.94 KB
/
Game.java
File metadata and controls
173 lines (155 loc) · 5.94 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
import java.util.ArrayList;
import java.util.Collections;
/**
* @author John Henry Cooper
* @version 15.0.2
* Includes all methods and properties to construct a Game object.
* Includes 4 property fields, an overloaded constructor,
* a method to initialize the GameBoard property, accessor and mutator methods,
* methods to change the turn, check opposing and current team,and an overridden toString method
*/
public abstract class Game {
protected GameBoard board;
protected Team team1;
protected Team team2;
protected String turn;
/**
* Creates a GameBoard object, assigns each team's pieces to a random empty
* space on the GameBoard, and then sets the board property to this GameBoard
* @param numRows int the number of rows
* @param numColumns int the number of columns
*/
private void initializeGameBoard(int numRows, int numColumns){
this.board = new GameBoard(numRows, numColumns);
for (Piece piece : team1.getTeamPieces()){
board.findRandomEmptyBlueSpace().setPiece(piece);// New Rule Modification
}
for (Piece piece : team2.getTeamPieces()){
board.findRandomEmptyRedSpace().setPiece(piece);// New Rule Modification
}
}
/**
* Four parameter constructor for a Game object that makes a call to
* initializeGameBoard
* @param numRows int the number of rows
* @param numColumns int the number of columns
* @param team1 Team the first team
* @param team2 Team the second team
*/
public Game(int numRows, int numColumns, Team team1, Team team2){
this.team1 = team1;
this.team2 = team2;
this.turn = team1.getTeamColor();
initializeGameBoard(numRows,numColumns);
}
public GameBoard getGameBoard(){
return this.board;
}
/**
* Returns the team whose turn it is
* @return Team
*/
public Team getCurrentTeam(){
if (this.turn.equals(team1.getTeamColor()) ){
return team1;
}
return team2;
}
/**
* Returns the team whose turn it isn´t
* @return Team
*/
public Team getOpponentTeam(){
if (this.turn.equals(team1.getTeamColor()) ){
return team2;
}
return team1;
}
/**
* Checks if the given team´s turn is right now
* @param team Team the team to be checked
* @return boolean
*/
public boolean isTurn(Team team){
return this.getCurrentTeam() == team;
}
/**
* Returns the 2D array of BoardSquares
* @return BoardSquare[][]
*/
public BoardSquare[][] getBoardSquares(){
return this.getGameBoard().getSquares();
}
/**
* Changes which team´s turn it is
*/
public void changeTurn(){
if (this.turn.equals(team1.getTeamColor()) ){
this.turn = this.team2.getTeamColor();
}
else{
this.turn = this.team1.getTeamColor();
}
}
/**
* returns the corner board square closest to the fromrow fromcolumn board square
* @param fromRow
* @param fromColumn
* @return BoardSquare
*/
//Added for PieceMinion Spawning
public BoardSquare NearestEmptyCorner(int fromRow, int fromColumn){
int rowRange = board.getNumRows() - 1;
int columnRange = board.getNumColumns() - 1;
int topLeftDist = Math.abs(fromRow + fromColumn);
int topRightDist = Math.abs(fromRow + (columnRange - fromColumn));
int bottomLeftDist = Math.abs((rowRange - fromRow) + fromColumn);
int bottomRightDist = Math.abs((rowRange - fromRow) + (columnRange-fromColumn));
BoardSquare square;
int minDist = Math.min(Math.min(topLeftDist,topRightDist),Math.min(bottomRightDist,bottomLeftDist));
if(minDist == topLeftDist){
if(board.getSquares()[0][0].isEmpty()){
return board.getSquares()[0][0];
}
minDist = Math.min(Math.min(bottomRightDist,bottomLeftDist),topRightDist);
}
else if(minDist == topRightDist){
if(board.getSquares()[0][columnRange].isEmpty()){
return board.getSquares()[0][columnRange];
}
minDist = (Math.min(bottomRightDist,bottomLeftDist));
}
else if(minDist == bottomLeftDist){
if(board.getSquares()[rowRange][0].isEmpty()){
return board.getSquares()[rowRange][0];
}
}
else if (board.getSquares()[rowRange][columnRange].isEmpty()){
return board.getSquares()[rowRange][columnRange];
}
return null;
}
/**
* Overridden toString method that returns the Game
* @return String message
*/
@Override
public String toString(){
StringBuilder retString = new StringBuilder();
retString.append("Game Board:\n")
.append(String.join("", Collections.nCopies(10 + board.getNumColumns()*8, "*")))
.append("\n" + getGameBoard().toString())
.append(String.join("", Collections.nCopies(10 + board.getNumColumns()*8, "*")))
.append("\n" + getCurrentTeam().toString() + "\n")
.append(String.join("", Collections.nCopies(10 + board.getNumColumns()*8, "*")))
.append("\n" + getOpponentTeam().toString() + "\n")
.append(String.join("", Collections.nCopies(10 + board.getNumColumns()*8, "*")))
.append("\nIt is Team " + getCurrentTeam().getTeamColor() + "'s turn\n");
return retString.toString();
}
// Board Square Modification
public abstract void checkTeleporter(BoardSquare teleporter);// Board Square Modification
public abstract boolean isAWinner();
public abstract Team getWinner();
public abstract boolean isGameEnded();
}