Coverage Summary for Class: IslandBoard (it.polimi.ingsw.model)

Class Class, % Method, % Line, %
IslandBoard 100% (1/ 1) 100% (10/ 10) 100% (106/ 106)


1 package it.polimi.ingsw.model; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.stream.Collectors; 6  7 class IslandBoard { 8  9  /** 10  * List of gods 11  */ 12  private List<GodInterface> god = new ArrayList<>(); 13  14  /** 15  * Game board 16  */ 17  private Cell[][] board = new Cell[5][5]; 18  19  /** 20  * Array of actions'positions 21  */ 22  private Action[][][] actions = new Action[5][5][3]; 23  24  /** 25  * Island board's initialization 26  */ 27  public IslandBoard() { 28  int i; 29  int j; 30  for (i = 0; i < 5; i++) { 31  for (j = 0; j < 5; j++) { 32  board[i][j] = new Cell(); 33  actions[i][j][0] = new Swap(); 34  actions[i][j][1] = new Build(); 35  actions[i][j][2] = new Build(); 36  actions[i][j][0].setGod(God.STANDARD); 37  actions[i][j][1].setGod(God.STANDARD); 38  actions[i][j][2].setGod(God.STANDARD); 39  } 40  } 41  god.add(new GodStandard(new GodPower(God.STANDARD, null))); 42  god.get(0).addInfo(new CurrentPlayer()); 43  44  } 45  46  /** 47  * Get a copy of the board 48  * 49  * @return Board's copy 50  */ 51  public Cell[][] getBoard() { 52  Cell[][] boardCopy = new Cell[5][5]; 53  for (int i = 0; i < 5; i++) { 54  for (int j = 0; j < 5; j++) { 55  boardCopy[i][j] = board[i][j].clone(); 56  } 57  } 58  return boardCopy; 59  } 60  61  /** 62  * Gives a copy of available actions 63  * 64  * @return copy of available actions 65  */ 66  public Action[][][] getActions() { 67  Action[][][] actionsCopy = new Action[5][5][3]; 68  for (int i = 0; i < 5; i++) { 69  for (int j = 0; j < 5; j++) { 70  for (int k = 0; k < 3; k++) { 71  actionsCopy[i][j][k] = actions[i][j][k].clone(); 72  } 73  } 74  } 75  return actionsCopy; 76  } 77  78  /** 79  * Reset available actions with priority (MUST, MAY ...) 80  * 81  * @param priority action's priority 82  */ 83  private void resetAction(boolean priority) { 84  for (int i = 0; i < 5; i++) { 85  for (int j = 0; j < 5; j++) { 86  if (priority) { 87  actions[i][j][0].setBlocked(false); 88  actions[i][j][1].setBlocked(false); 89  actions[i][j][2].setBlocked(false); 90  } 91  actions[i][j][0].set(false); 92  actions[i][j][1].set(false); 93  actions[i][j][2].set(false); 94  actions[i][j][0].setGod(God.STANDARD); 95  actions[i][j][1].setGod(God.STANDARD); 96  actions[i][j][2].setGod(God.STANDARD); 97  } 98  } 99  } 100  101  /** 102  * Adds chosen gods 103  * 104  * @param name Owner name 105  * @param god God 106  * @return true if god is added 107  */ 108  public boolean addGod(String name, God god) { 109  try { 110  this.god.add((GodInterface) Class 111  .forName("it.polimi.ingsw.model.God" + god.toString().charAt(0) 112  + god.toString().toLowerCase().substring(1)) 113  .getConstructor(GodInterface.class).newInstance(new GodPower(god, name))); 114  return true; 115  } catch (Exception e) { 116  return false; 117  } 118  } 119  120  /** 121  * Choose a worker 122  * 123  * @param name current player's username 124  * @param position worker's position that the player wanto to choose 125  */ 126  public void chooseWorker(String name, int[] position) { 127  resetAction(true); 128  129  if (board[position[0]][position[1]].getBlock().getTypeBlock().equals(TypeBlock.WORKER) 130  && board[position[0]][position[1]].getBlock().getOwner().equals(name)) { 131  CurrentPlayer currentPlayer = new CurrentPlayer(position, name, StatusPlayer.GAMING, God.STANDARD); 132  for (GodInterface godInterface : god) { 133  godInterface.addInfo(currentPlayer); 134  } 135  Event[] event = new Event[1]; 136  event[0] = Event.ZERO; 137  setActions(event); 138  } 139  } 140  141  /** 142  * Add worker to players 143  * 144  * @param playerId Player's username 145  * @param color Player's color 146  * @param position Worker's position to place 147  */ 148  public void addWorker(String playerId, Color color, int[] position) { 149  board[position[0]][position[1]].addBlock(new Block(TypeBlock.WORKER, playerId, color));/* two addWorker */ 150  } 151  152  /** 153  * Set actions 154  */ 155  private void setActions(Event[] events) { 156  for (GodInterface godInterface : god) { 157  godInterface.getEvent(events, board, actions); 158  } 159  } 160  161  /** 162  * Check if the player can end his turn 163  * 164  * @return if the player can end his turn or not 165  */ 166  public boolean canEndTurn() { 167  Event[] events = new Event[1]; 168  events[0] = Event.THREE; 169  god.get(0).getEvent(events, board, actions); 170  boolean state = god.get(0).getPlayerStatus().equals(StatusPlayer.IDLE); 171  god.get(0).setStatusPlayer(StatusPlayer.GAMING); 172  return state; 173  } 174  175  /** 176  * Execute a chosen action 177  * 178  * @param player Player's username 179  * @param positionAction Action's position 180  * @return action's consequence 181  */ 182  public ReportAction executeAction(String player, int[] positionAction) { 183  Event[] event = new Event[3]; 184  if (positionAction != null) { 185  event = actions[positionAction[0]][positionAction[1]][positionAction[2]].execute(board); 186  resetAction(true); 187  if (event[0] == Event.MOVE) { 188  god.get(0).setWorker(positionAction); 189  } 190  setActions(event); 191  event[0] = Event.TWO; // End turn automatic 192  } else { 193  event[0] = Event.ONE; 194  if (god.get(0).getCurrentPlayer() == null || !god.get(0).getCurrentPlayer().equals(player)) { 195  int count = 0; 196  for (int i = 0; i < 25; i++) { 197  if (board[i / 5][i % 5].getBlock().getTypeBlock().equals(TypeBlock.WORKER) 198  && board[i / 5][i % 5].getBlock().getOwner().equals(player)) { 199  chooseWorker(board[i / 5][i % 5].getBlock().getOwner(), new int[] { i / 5, i % 5 }); 200  god.get(0).getEvent(event, board, actions); 201  if (god.get(0).getPlayerStatus() == StatusPlayer.GAMING) { 202  count++; 203  break; 204  } 205  } 206  } 207  ReportAction reportAction; 208  if (count == 0) { 209  reportAction = new ReportAction(StatusPlayer.LOSE, God.STANDARD); 210  } else { 211  reportAction = new ReportAction(StatusPlayer.GAMING, God.STANDARD); 212  } 213  resetAction(true); 214  return reportAction; 215  } 216  } 217  god.get(0).getEvent(event, board, actions); 218  if (god.get(0).getPlayerStatus().equals(StatusPlayer.IDLE)) { 219  resetAction(true); 220  } 221  if (god.get(0).getPlayerStatus().equals(StatusPlayer.LOSE)) { 222  god = god.stream().filter(e -> e.equals(god.get(0)) || !e.getName().equals(player)) 223  .collect(Collectors.toList()); 224  for (int i = 0; i < 5; i++) { 225  for (int j = 0; j < 5; j++) { 226  if (board[i][j].getBlock().getOwner() != null 227  && board[i][j].getBlock().getOwner().equals(god.get(0).getCurrentPlayer())) { 228  board[i][j].popBlock(); 229  } 230  } 231  } 232  } 233  return new ReportAction(god.get(0).getPlayerStatus(), god.get(0).getLastGod()); 234  } 235 }