Sea battle. I am writing a method for turning the ship.
tt is the current position of the ship:
0 - vertically (by default)
1 - horizontally (0 + 90 °)
2 - vertically (0 upside down)
3 - horizontally (0 - 90 °)
ckw is the turn flag:
true- clockwise
false- counterclockwise
Rotation method code:
public void turn (boolean ckw) { if ((tt == 0 && !ckw) || (tt == 1 && ckw)) { //b -> c } else if ((tt == 1 && !ckw) || (tt == 2 && ckw)) { //c -> d } else if ((tt == 2 && !ckw) || (tt == 3 && ckw)) { //d -> a } else if ((tt == 3 && !ckw) || (tt == 0 && ckw)) { //a -> b } if (ckw) tt++; else tt--; } The comments indicate 4 for-each with a reversal logic. I’d like to cut this ladder of ifs. Is it possible
Rotation logic
The ship is an ArrayList positions in the grid. For example, 4-deck, located in the middle of the field: 35, 45, 55, 65 . To turn it clockwise, you must add to each position its index multiplied by 11 .
b -> c (90 ° -> 180 °) or (0 ° -> 270 °)
for (int i = 0; i < col.size(); i++) { int num = col.get(i); int new_num = num + 11 * i; col.set(i, new_num); } Etc.
b -> c,c -> dand other animals? and what is theвертикально (0 вверх ногами)? What are the legs of the ships? - Alexey Shimanskya -> band others) can be combined into a single code and there will be no need for anif-else(orswitch-case) at all. - Regenttt == 0 && !ckwandtt == 1 && ckwsame turn code, if in the first case it is a turn from 0 to 270 degrees, and in the second case - from 90 to 180. - RegentВполне возможно, что эти четыре поворота (a -> b и другие) можно объединить в единый код- yes, thank you for expressing it for me)) And I wanted to cut the clues in the comments in the comment ...) - Alexey Shimansky