[ all classes ]
[ it.polimi.ingsw.model ]
Coverage Summary for Class: Swap (it.polimi.ingsw.model)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
Swap | 100% (1/ 1) | 100% (10/ 10) | 100% (55/ 55) |
1 package it.polimi.ingsw.model;
2
3 class Swap implements Action {
4 /**
5 * Initial position 1
6 */
7 private int[] x1 = new int[2];
8
9 /**
10 * Finale position 1
11 */
12 private int[] x2 = new int[2];
13
14 /**
15 * Initial position 2
16 */
17 private int[] y1 = new int[2];
18
19 /**
20 * Final position 2
21 */
22 private int[] y2 = new int[2];
23
24 /**
25 * swap's status (if it can be executed)
26 */
27 private boolean status;
28
29 /**
30 * if any god prohibits the swap
31 */
32 private boolean blocked;
33
34 /**
35 * type of the action
36 */
37 private final String typeAction;
38
39 /**
40 * last god that changed its status
41 */
42 private God god;
43
44 /**
45 * Swap
46 */
47 public Swap() {
48 this.typeAction = "Swap";
49 }
50
51 /**
52 * Get type of the action
53 *
54 * @return type of the action
55 */
56 @Override
57 public String getTypeAction() {
58 return typeAction;
59 }
60
61
62 public void set(int[] x1, int[] x2, int[] y1, int[] y2, boolean status) {
63 for (int i = 0; i < 2; i++) {
64 this.x1[i] = x1[i];
65 this.x2[i] = x2[i];
66 this.y1[i] = y1[i];
67 this.y2[i] = y2[i];
68 }
69 if (!blocked) {
70 set(status);
71 }
72 }
73
74 /**
75 * Set action status
76 *
77 * @param status status to set
78 */
79 @Override
80 public void set(boolean status) {
81 if (!blocked) {
82 this.status = status;
83 }
84 }
85
86 /**
87 * Get action status
88 *
89 * @return action status
90 */
91 @Override
92 public boolean getStatus() {
93 return status && (!blocked);
94 }
95
96
97 /**
98 * Execute the action on the given game board
99 *
100 * @param map where to apply the action effects
101 * @return result event of the current action
102 */
103 @Override
104 public Event[] execute(Cell[][] map) {
105 Event[] events = new Event[3];
106 events[0] = Event.FOUR;
107 if (getStatus()) {
108 events[0] = Event.MOVE;
109 if (y2[0] == y1[0] && y2[1] == y1[1]) {
110 switch (map[x1[0]][x1[1]].getSize() - map[x2[0]][x2[1]].getSize()) {
111 case 1:
112 events[1] = Event.ZERO;
113 break;
114 case 0:
115 events[1] = Event.UP;
116 break;
117 case 2:
118 events[1] = Event.DOWN;
119 events[2] = Event.ONE;
120 break;
121 case 3:
122 events[1] = Event.DOWN;
123 events[2] = Event.TWO;
124 break;
125 case 4:
126 events[1] = Event.DOWN;
127 events[2] = Event.THREE;
128 break;
129 }
130 } else {
131 if (map[x2[0]][x2[1]].getSize() - map[x1[0]][x1[1]].getSize() >= 1)
132 events[1] = Event.UP;
133 else
134 events[1] = Event.ZERO;
135 }
136 Block block1 = map[x1[0]][x1[1]].popBlock();
137 Block block2 = map[y1[0]][y1[1]].popBlock();
138 map[y2[0]][y2[1]].addBlock(block2);
139 map[x2[0]][x2[1]].addBlock(block1);
140 }
141 return events;
142 }
143
144 /**
145 * Set the last god that changed this action
146 *
147 * @param god god to set as last god that changed this action
148 */
149 @Override
150 public void setGod(God god) {
151 this.god = god;
152 }
153
154
155 /**
156 * Get the last god that changed this action
157 *
158 * @return last god that changed this action
159 */
160 @Override
161 public God getGod() {
162 return god;
163 }
164
165
166 /**
167 * Disable any further changes on this action
168 *
169 * @param blocked disable this action from further changes
170 */
171 @Override
172 public void setBlocked(boolean blocked) {
173 this.blocked = blocked;
174 }
175
176 /**
177 * Clone
178 *
179 * @return clone of the action
180 */
181 @Override
182 public Action clone() {
183 Swap swapCopy = new Swap();
184 swapCopy.set(x1, x2, y1, y2, this.getStatus());
185 return swapCopy;
186
187 }
188
189 }