Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calculates and makes a move on the board.
- *
- * @param row is the Yth position of the board.
- * @param col is the Xth position of the board.
- * @param team is either the human or the computer. It determines which tile
- * will be set on the board.
- * @return The manipulated board is returned if a move has been made or null
- * if not.
- * @see Direction
- */
- public GameBoard moveHelp(int row, int col, Player team) {
- GameBoard changedBoard = this.clone();
- Player ownTileState;
- Player enemyTileState;
- // determine which player is moving
- if (team == Player.HUMAN) {
- enemyTileState = Player.COM;
- ownTileState = Player.HUMAN;
- } else {
- enemyTileState = Player.HUMAN;
- ownTileState = Player.COM;
- }
- if (board[row][col] != Player.EMPTY)
- return null;
- // perform the move
- for (Direction dir : Direction.values()) {
- int i = 1;
- int rowDir = row + i * dir.getRow();
- int colDir = col + i * dir.getCol();
- while (isWithinBoard(rowDir, colDir)) {
- Player boardState = board[rowDir][colDir];
- Player previousBoardState = board[rowDir - dir.getRow()][colDir
- - dir.getCol()];
- if ((boardState == Player.EMPTY)) {
- break;
- } else if ((boardState == enemyTileState)
- && (previousBoardState == ownTileState)) {
- break;
- } else if ((boardState == ownTileState)
- && (previousBoardState == enemyTileState)) {
- while (i >= 0) {
- rowDir = row + i * dir.getRow();
- colDir = col + i * dir.getCol();
- changedBoard.board[rowDir][colDir] = ownTileState;
- i--;
- }
- break;
- }
- i++;
- rowDir = row + i * dir.getRow();
- colDir = col + i * dir.getCol();
- }
- }
- // Return null if no move operation has been made.
- if (this.equals(changedBoard)) {
- return null;
- }
- return changedBoard;
- }
Advertisement
Add Comment
Please, Sign In to add comment