diff --git a/classes/test/coding/111111111.txt b/classes/test/coding/111111111.txt new file mode 100644 index 0000000..a394366 --- /dev/null +++ b/classes/test/coding/111111111.txt @@ -0,0 +1,3 @@ + + | | | | | | | | | + | | | | | | | | | \ No newline at end of file diff --git a/classes/test/coding/123456789.txt b/classes/test/coding/123456789.txt new file mode 100644 index 0000000..9d5760e --- /dev/null +++ b/classes/test/coding/123456789.txt @@ -0,0 +1,3 @@ + _ _ _ _ _ _ _ + | _| _||_||_ |_ ||_||_| + ||_ _| | _||_| ||_| _| \ No newline at end of file diff --git a/classes/test/coding/222222222.txt b/classes/test/coding/222222222.txt new file mode 100644 index 0000000..41d43fa --- /dev/null +++ b/classes/test/coding/222222222.txt @@ -0,0 +1,3 @@ + _ _ _ _ _ _ _ _ _ + _| _| _| _| _| _| _| _| _| +|_ |_ |_ |_ |_ |_ |_ |_ |_ \ No newline at end of file diff --git a/classes/test/coding/caso1.txt b/classes/test/coding/caso1.txt new file mode 100644 index 0000000..f336768 --- /dev/null +++ b/classes/test/coding/caso1.txt @@ -0,0 +1,3 @@ + _ _ _ _ _ _ _ _ +|_||_ ||_ | ||_|| || || | + | _| | _||_||_||_||_||_| \ No newline at end of file diff --git a/classes/test/coding/caso2.txt b/classes/test/coding/caso2.txt new file mode 100644 index 0000000..be091ae --- /dev/null +++ b/classes/test/coding/caso2.txt @@ -0,0 +1,3 @@ + _ _ _ _ _ _ _ _ +|_||_ | _ | ||_|| || || | + | _| ||_||_||_| _||_||_| \ No newline at end of file diff --git a/classes/test/coding/caso3.txt b/classes/test/coding/caso3.txt new file mode 100644 index 0000000..379c532 --- /dev/null +++ b/classes/test/coding/caso3.txt @@ -0,0 +1,3 @@ + _ _ _ _ _ _ +|_ |_ |_| _| | ||_||_||_ +|_||_| | _| | | | _| _| \ No newline at end of file diff --git a/classes/test/coding/prueba.txt b/classes/test/coding/prueba.txt new file mode 100644 index 0000000..6a537b5 --- /dev/null +++ b/classes/test/coding/prueba.txt @@ -0,0 +1 @@ +1234567890 \ No newline at end of file diff --git a/src/main/java/org/fundacionjala/coding/abel/StreetFighter.java b/src/main/java/org/fundacionjala/coding/abel/StreetFighter.java new file mode 100644 index 0000000..597cc44 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/StreetFighter.java @@ -0,0 +1,74 @@ +package org.fundacionjala.coding.abel; + +import java.util.ArrayList; +import java.util.List; + +/** + * You'll have to simulate the video game's character selection screen behaviour, more specifically the selection grid. + * Such screen looks like this: + * Selection Grid Layout in text: + * | Ryu | E.Honda | Blanka | Guile | Balrog | Vega | + * | Ken | Chun Li | Zangief | Dhalsim | Sagat | M.Bison | + * Input + * the list of game characters in a 2x6 grid; + * the initial position of the selection cursor (top-left is (0,0)); + * a list of moves of the selection cursor (which are up, down, left, right); + * Output + * the list of characters who have been hovered by the selection cursor after all the moves + * (ordered and with repetition, all the ones after a move, wether successful or not, see tests); + */ +public final class StreetFighter { + + /** + * Private Constructor. + */ + private StreetFighter() { + + } + + /** + * Returns the Fighter after pushing buttons. + * + * @param fighters String[][]. + * @param position int[]. + * @param moves String[]. + * @return String[]. + */ + public static String[] streetFighterSelection(String[][] fighters, int[] position, String[] moves) { + String[] solution = new String[]{}; + List listSolution = new ArrayList(); + int[] currentPosition = position; + for (String move : moves) { + if (move.equals("up")) { + if (position[0] != 0) { + currentPosition[0] = position[0] - 1; + } + listSolution.add(fighters[currentPosition[0]][currentPosition[1]]); + } + if (move.equals("down")) { + if (position[0] != 1) { + currentPosition[0] = position[0] + 1; + } + listSolution.add(fighters[currentPosition[0]][currentPosition[1]]); + } + if (move.equals("left")) { + if (position[1] == 0) { + currentPosition[1] = fighters[0].length - 1; + } else { + currentPosition[1] = position[1] - 1; + } + listSolution.add(fighters[currentPosition[0]][currentPosition[1]]); + } + if (move.equals("right")) { + if (position[1] == fighters[0].length - 1) { + currentPosition[1] = 0; + } else { + currentPosition[1] = position[1] + 1; + } + listSolution.add(fighters[currentPosition[0]][currentPosition[1]]); + } + position = currentPosition; + } + return listSolution.toArray(solution); + } +} diff --git a/src/test/java/org/fundacionjala/coding/abel/StreetFighterTest.java b/src/test/java/org/fundacionjala/coding/abel/StreetFighterTest.java new file mode 100644 index 0000000..d3d3b7e --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/StreetFighterTest.java @@ -0,0 +1,108 @@ +package org.fundacionjala.coding.abel; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +/** + * Class to run Test for Street Fighter. + */ +public class StreetFighterTest { + + private static final String[][] FIGHTERS = new String[][]{ + new String[]{"Ryu", "E.Honda", "Blanka", "Guile", "Balrog", "Vega"}, + new String[]{"Ken", "Chun Li", "Zangief", "Dhalsim", "Sagat", "M.Bison"}, + }; + + /** + * Test for Private Constructors. + * + * @throws NoSuchMethodException Thrown when a particular method cannot be found. + * @throws IllegalAccessException Thrown when an application tries to reflectively create an instance. + * @throws InvocationTargetException thrown by an invoked method or constructor. + * @throws InstantiationException Thrown when an application tries to create an instance of a class + * using the {@code newInstance} method in class. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = StreetFighter.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * First Test. + */ + @Test + public void shouldWorkWithNoMoves() { + String[] solution = new String[]{}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, new String[]{})); + } + + /** + * First Test. + */ + @Test + public void shouldWorkWithFewMoves() { + String[] moves = new String[]{"up", "left", "right", "left", "left"}; + String[] solution = new String[]{"Ryu", "Vega", "Ryu", "Vega", "Balrog"}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, moves)); + } + + /** + * First Test. + */ + @Test + public void shouldWorkWhenAlwaysMovingLeft() { + String[] moves = new String[]{"left", "left", "left", "left", "left", "left", "left", "left"}; + String[] solution = new String[]{"Vega", "Balrog", "Guile", "Blanka", "E.Honda", "Ryu", "Vega", "Balrog"}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, moves)); + } + + /** + * First Test. + */ + @Test + public void shouldWorkWhenAlwaysMovingRight() { + String[] moves = new String[]{"right", "right", "right", "right", "right", "right", "right", "right"}; + String[] solution = new String[]{"E.Honda", "Blanka", "Guile", "Balrog", "Vega", "Ryu", "E.Honda", "Blanka"}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, moves)); + } + + /** + * First Test. + */ + @Test + public void shouldUseAll4DirectionsClockwiseTwice() { + String[] moves = new String[]{"up", "left", "down", "right", "up", "left", "down", "right"}; + String[] solution = new String[]{"Ryu", "Vega", "M.Bison", "Ken", "Ryu", "Vega", "M.Bison", "Ken"}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, moves)); + } + + /** + * First Test. + */ + @Test + public void shouldWorkWhenAlwaysMovingDown() { + String[] moves = new String[]{"down", "down", "down", "down"}; + String[] solution = new String[]{"Ken", "Ken", "Ken", "Ken"}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, moves)); + } + + /** + * First Test. + */ + @Test + public void shouldWorkWhenAlwaysMovingUp() { + String[] moves = new String[]{"up", "up", "up", "up"}; + String[] solution = new String[]{"Ryu", "Ryu", "Ryu", "Ryu"}; + assertArrayEquals(solution, StreetFighter.streetFighterSelection(FIGHTERS, new int[]{0, 0}, moves)); + } +}