Browse Source

Added getSidebarFigures method for displaying ChessFigures as a List with test

Nick Stolbov 3 years ago
parent
commit
787955505a
  1. 27
      src/main/java/Game/Chess.java
  2. 36
      src/test/java/Game/ChessTest.java

27
src/main/java/Game/Chess.java

@ -1,6 +1,9 @@
package Game; package Game;
import Game.ChessObj.ChessBoard; import Game.ChessObj.ChessBoard;
import Game.ChessObj.ChessFigure;
import java.util.ArrayList;
public class Chess extends Game { public class Chess extends Game {
@ -50,4 +53,28 @@ public class Chess extends Game {
else else
return temp; return temp;
} }
public ArrayList<String> getSidebarFigures(ArrayList<ChessFigure> chessFigureArrayList, int maxPerLine) {
ArrayList<String> result = new ArrayList<>();
String line = "";
int counter = 0;
for(int i = 0; i < chessFigureArrayList.size(); i++){
if(i == chessFigureArrayList.size() - 1) {
line += chessFigureArrayList.get(i).getSymbol() + "";
result.add(line);
return result;
}
line += chessFigureArrayList.get(i).getSymbol() + ",";
counter++;
if(counter >= maxPerLine) {
result.add(line);
line = "";
counter = 0;
}
}
return result;
}
} }

36
src/test/java/Game/ChessTest.java

@ -1,11 +1,13 @@
package Game; package Game;
import Game.ChessObj.ChessFigure;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
class ChessTest { class ChessTest {
@ -40,4 +42,34 @@ class ChessTest {
assertArrayEquals((chess.convertInput("3G")), test3); assertArrayEquals((chess.convertInput("3G")), test3);
assertArrayEquals((chess.convertInput("G3")), test3); assertArrayEquals((chess.convertInput("G3")), test3);
} }
@Test
void getSidebarFigures() {
ArrayList<ChessFigure> array1 = new ArrayList<>();
ArrayList<ChessFigure> array2 = new ArrayList<>();
ArrayList<ChessFigure> array3 = new ArrayList<>();
ArrayList<String> expectedArray2 = new ArrayList<>();
ArrayList<String> expectedArray3 = new ArrayList<>();
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE));
array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE));
expectedArray2.add(" o , I , o , o , T ,");
expectedArray2.add(" o ");
array3.add(new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK));
array3.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
array3.add(new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK));
array3.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK));
expectedArray3.add("|Q|,|o|,|K|,|o|");
assertEquals(0, chess.getSidebarFigures(array1, 5).size());
assertEquals(expectedArray2, chess.getSidebarFigures(array2, 5));
assertEquals(expectedArray3, chess.getSidebarFigures(array3, 5));
}
} }
Loading…
Cancel
Save