[ all classes ]
[ it.polimi.ingsw.model ]
Coverage Summary for Class: Cell (it.polimi.ingsw.model)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
Cell | 100% (1/ 1) | 100% (8/ 8) | 100% (26/ 26) |
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 /**
8 * Class that is used to store a single board cell information
9 */
10 public class Cell implements Cloneable {
11 /**
12 * Blocks in this Cell
13 */
14 private ArrayList<Block> blocks;
15
16 /**
17 * Create an instance of Cell with no blocks
18 */
19 public Cell() {
20 blocks = new ArrayList<>();
21 }
22
23 /**
24 * Create an instance of Cell with specified blocks
25 *
26 * @param blocks blocks to put in this Cell
27 */
28 public Cell(List<Block> blocks) {
29 this.blocks = (ArrayList<Block>) blocks.stream().map(Block::clone).collect(Collectors.toList());
30 }
31
32 /**
33 * Add a block on top of the stack
34 *
35 * @param blockToAdd block to add
36 */
37 public void addBlock(Block blockToAdd) {
38 if (blockToAdd == null)
39 return;
40 if (getBlock().getTypeBlock().equals(TypeBlock.WORKER)) {
41 Block block = popBlock();
42 blocks.add(blockToAdd);
43 blocks.add(block);
44 } else {
45 blocks.add(blockToAdd);
46 }
47 }
48
49 /**
50 * Remove and Return the top block of the stack, null in case there is nothing
51 *
52 * @return top block on the stack
53 */
54 public Block popBlock() {
55 if (!blocks.isEmpty())
56 return blocks.remove(blocks.size() - 1);
57 return null;
58 }
59
60 /**
61 * Get the top block of the stack
62 *
63 * @return Top block of the stack
64 */
65 public Block getBlock() {
66 if (blocks.isEmpty())
67 return new Block(TypeBlock.LEVEL0);
68 return blocks.get(blocks.size() - 1).clone();
69 }
70
71 /**
72 * Get a specific block of the stack
73 *
74 * @param i position of the block on the stack
75 * @return block selected, in case i is invalid, it is returned the closest
76 * block
77 */
78 public Block getBlock(int i) {
79 if (blocks.isEmpty())
80 return new Block(TypeBlock.LEVEL0);
81 return blocks.get(Math.min(Math.max(0, i), blocks.size() - 1)).clone();
82
83 }
84
85
86
87
88 @Override
89 public Cell clone() {
90 return new Cell(blocks.stream().map(Block::clone).collect(Collectors.toList()));
91 }
92
93
94
95
96 /**
97 * Get the number of blocks in the Cell
98 *
99 * @return number of blocks in the Cell
100 */
101 public int getSize() {
102 return blocks.size();
103 }
104 }