From dd4af9170ba1b51cbf9519fa5c99c82f8419fb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Fri, 29 Jul 2022 10:20:27 +0900 Subject: [PATCH 1/3] =?UTF-8?q?B030=E6=B0=B7=E3=81=AE=E3=83=80=E3=83=B3?= =?UTF-8?q?=E3=82=B8=E3=83=A7=E3=83=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B030_IceGame.java | 126 ++++++++++++++++++++++++++++++ ykoiso/test/B030_IceGameTest.java | 42 ++++++++++ 2 files changed, 168 insertions(+) create mode 100644 ykoiso/src/B030_IceGame.java create mode 100644 ykoiso/test/B030_IceGameTest.java diff --git a/ykoiso/src/B030_IceGame.java b/ykoiso/src/B030_IceGame.java new file mode 100644 index 0000000..3ce6dc6 --- /dev/null +++ b/ykoiso/src/B030_IceGame.java @@ -0,0 +1,126 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B030_IceGame { + public enum MOVE { + // 命令のenum + UP("U"), RIGHT("R"), DOWN("D"), LEFT("L"); + + private String str; + + MOVE(String str) { + this.str = str; + } + + public String getStr() { + return str; + } + + // 逆検索 + public static MOVE getByStr(String str) { + for (MOVE move : MOVE.values()) { + if (move.getStr().equals(str)) { + return move; + } + } + // 異常はぬるぽを返す + return null; + } + } + + // 定数 + private static final String ICE = "#"; + private static final String FLOOR = "."; + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 各種読み込み + final int height = scan.nextInt(); + final int width = scan.nextInt(); + String[][] map = new String[height][width]; + for (int H = 0; H < height; H++) { + String str = scan.next(); + map[H] = str.split(""); + } + int playerX = scan.nextInt() - 1; + int playerY = scan.nextInt() - 1; + final int orderNum = scan.nextInt(); + final List orderList = new ArrayList<>(); + for (int i = 0; i < orderNum; i++) { + // enum型で保持 + orderList.add(MOVE.getByStr(scan.next())); + } + // 命令に合わせて移動 + for (int i = 0; i < orderNum; i++) { + switch (orderList.get(i)) { + case UP: + playerY = moveUp(map, height, playerX, playerY); + break; + case RIGHT: + playerX = moveRight(map, width, playerX, playerY); + break; + case DOWN: + playerY = moveDown(map, height, playerX, playerY); + break; + case LEFT: + playerX = moveLeft(map, width, playerX, playerY); + break; + default: + break; + } + } + // 最終座標の表示 + System.out.println((playerX + 1) + " " + (playerY + 1)); + scan.close(); + } + + // 各方向にどれだけ動けるか計算 + public static int moveUp(final String[][] map, final int height, final int playerX, + final int playerY) { + for (int H = playerY - 1; H >= 0; H--) { + if (map[H][playerX].equals(ICE)) { + continue; + } else { + return H; + } + } + return 0; + } + + public static int moveRight(final String[][] map, final int width, final int playerX, + final int playerY) { + for (int W = playerX + 1; W < width; W++) { + if (map[playerY][W].equals(ICE)) { + continue; + } else { + return W; + } + } + return width - 1; + } + + public static int moveDown(final String[][] map, final int height, final int playerX, + final int playerY) { + for (int H = playerY + 1; H < height; H++) { + if (map[H][playerX].equals(ICE)) { + continue; + } else { + return H; + } + } + return height - 1; + } + + public static int moveLeft(final String[][] map, final int width, final int playerX, + final int playerY) { + for (int W = playerX - 1; W >= 0; W--) { + if (map[playerY][W].equals(ICE)) { + continue; + } else { + return W; + } + } + return 0; + } +} diff --git a/ykoiso/test/B030_IceGameTest.java b/ykoiso/test/B030_IceGameTest.java new file mode 100644 index 0000000..b21b01f --- /dev/null +++ b/ykoiso/test/B030_IceGameTest.java @@ -0,0 +1,42 @@ +import static org.junit.Assert.assertThat; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; + +public class B030_IceGameTest { + String[][] map = {{".", ".", "#", ".", ".",}, {".", ".", ".", ".", ".",}, + {"#", "#", ".", ".", ".",}, {".", ".", "#", ".", ".",}, {".", ".", ".", ".", ".",}}; + final int width = 5; + final int height = 5; + final int playerX = 2; + final int playerY = 2; + + @Test + public void 上向きに移動できる() { + final int actual = B030_IceGame.moveUp(map, height, playerX, playerY); + final int expected = playerY - 1; + assertThat(actual, is(expected)); + } + + @Test + public void 右向きに移動できる() { + final int actual = B030_IceGame.moveRight(map, height, playerX, playerY); + final int expected = playerX + 1; + assertThat(actual, is(expected)); + } + + @Test + public void 下向きに移動できる() { + final int actual = B030_IceGame.moveDown(map, height, playerX, playerY); + final int expected = playerY + 2; + assertThat(actual, is(expected)); + } + + @Test + public void 左向きに移動できる() { + final int actual = B030_IceGame.moveLeft(map, height, playerX, playerY); + final int expected = playerX - 2; + assertThat(actual, is(expected)); + } + +} -- GitLab From b6bcd5eae48e28dbc95bcd55229745c8ee4ab95c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Fri, 29 Jul 2022 15:50:20 +0900 Subject: [PATCH 2/3] =?UTF-8?q?B030=E6=B0=B7=E3=81=AE=E3=83=80=E3=83=B3?= =?UTF-8?q?=E3=82=B8=E3=83=A7=E3=83=B3=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B030_IceGame.java | 136 +++++++++++++++--------------- ykoiso/test/B030_IceGameTest.java | 102 +++++++++++++++++----- 2 files changed, 148 insertions(+), 90 deletions(-) diff --git a/ykoiso/src/B030_IceGame.java b/ykoiso/src/B030_IceGame.java index 3ce6dc6..df9b352 100644 --- a/ykoiso/src/B030_IceGame.java +++ b/ykoiso/src/B030_IceGame.java @@ -3,14 +3,28 @@ import java.util.List; import java.util.Scanner; public class B030_IceGame { - public enum MOVE { + public enum DIRECTION { // 命令のenum - UP("U"), RIGHT("R"), DOWN("D"), LEFT("L"); + UP("U", 0, -1), + RIGHT("R", 1, 0), + DOWN("D", 0, 1), + LEFT("L", -1, 0); private String str; + private final int x, y; - MOVE(String str) { + DIRECTION(String str, int x, int y) { this.str = str; + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; } public String getStr() { @@ -18,10 +32,10 @@ public class B030_IceGame { } // 逆検索 - public static MOVE getByStr(String str) { - for (MOVE move : MOVE.values()) { - if (move.getStr().equals(str)) { - return move; + public static DIRECTION getByStr(final String str) { + for (final DIRECTION DIRECTION : DIRECTION.values()) { + if (DIRECTION.name().startsWith(str)) { + return DIRECTION; } } // 異常はぬるぽを返す @@ -34,93 +48,75 @@ public class B030_IceGame { private static final String FLOOR = "."; public static void main(String[] args) { - Scanner scan = new Scanner(System.in); + /* finalつけ忘れない */ + final Scanner scan = new Scanner(System.in); // 各種読み込み final int height = scan.nextInt(); final int width = scan.nextInt(); String[][] map = new String[height][width]; - for (int H = 0; H < height; H++) { + /* 混乱を招く変数名をやめようHは高さで文中に使われている */ + for (int i = 0; i < height; i++) { String str = scan.next(); - map[H] = str.split(""); + map[i] = str.split(""); } - int playerX = scan.nextInt() - 1; - int playerY = scan.nextInt() - 1; + int x = scan.nextInt() - 1; + int y = scan.nextInt() - 1; final int orderNum = scan.nextInt(); - final List orderList = new ArrayList<>(); + final List orderList = new ArrayList<>(); for (int i = 0; i < orderNum; i++) { // enum型で保持 - orderList.add(MOVE.getByStr(scan.next())); + orderList.add(DIRECTION.getByStr(scan.next())); } + B030_IceGame ice = new B030_IceGame(); + Position position = ice.new Position(x, y); // 命令に合わせて移動 for (int i = 0; i < orderNum; i++) { - switch (orderList.get(i)) { - case UP: - playerY = moveUp(map, height, playerX, playerY); - break; - case RIGHT: - playerX = moveRight(map, width, playerX, playerY); - break; - case DOWN: - playerY = moveDown(map, height, playerX, playerY); - break; - case LEFT: - playerX = moveLeft(map, width, playerX, playerY); - break; - default: - break; - } + position.move(map, width, height, orderList.get(i)); } // 最終座標の表示 - System.out.println((playerX + 1) + " " + (playerY + 1)); + position.showPosition(); scan.close(); } - // 各方向にどれだけ動けるか計算 - public static int moveUp(final String[][] map, final int height, final int playerX, - final int playerY) { - for (int H = playerY - 1; H >= 0; H--) { - if (map[H][playerX].equals(ICE)) { - continue; - } else { - return H; - } + // ポジションのクラス + class Position { + private int x, y; + + Position(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; } - return 0; - } - public static int moveRight(final String[][] map, final int width, final int playerX, - final int playerY) { - for (int W = playerX + 1; W < width; W++) { - if (map[playerY][W].equals(ICE)) { - continue; - } else { - return W; - } + public int getY() { + return y; } - return width - 1; - } - public static int moveDown(final String[][] map, final int height, final int playerX, - final int playerY) { - for (int H = playerY + 1; H < height; H++) { - if (map[H][playerX].equals(ICE)) { - continue; - } else { - return H; + // 位置を命令に従って変える + public void move(final String[][] map, final int width, final int height, + final DIRECTION direction) { + int currentX = x + direction.getX(); + int currentY = y + direction.getY(); + // 壁に隣接しているか + while (currentY < height && currentY >= 0 && currentX < width && currentX >= 0) { + x = currentX; + y = currentY; + // 床が氷ではないか + if (map[currentY][currentX].equals(FLOOR)) { + break; + } + currentX += direction.getX(); + currentY += direction.getY(); } } - return height - 1; - } - public static int moveLeft(final String[][] map, final int width, final int playerX, - final int playerY) { - for (int W = playerX - 1; W >= 0; W--) { - if (map[playerY][W].equals(ICE)) { - continue; - } else { - return W; - } + // 現在の位置を表示 + public void showPosition() { + System.out.println((x + 1) + " " + (y + 1)); } - return 0; } + } diff --git a/ykoiso/test/B030_IceGameTest.java b/ykoiso/test/B030_IceGameTest.java index b21b01f..14b730f 100644 --- a/ykoiso/test/B030_IceGameTest.java +++ b/ykoiso/test/B030_IceGameTest.java @@ -1,42 +1,104 @@ import static org.junit.Assert.assertThat; import org.junit.Test; - import static org.hamcrest.CoreMatchers.*; public class B030_IceGameTest { - String[][] map = {{".", ".", "#", ".", ".",}, {".", ".", ".", ".", ".",}, - {"#", "#", ".", ".", ".",}, {".", ".", "#", ".", ".",}, {".", ".", ".", ".", ".",}}; + String[][] map = { + {".", ".", "#", ".", ".",}, + {".", ".", ".", ".", ".",}, + {"#", "#", ".", ".", ".",}, + {".", ".", "#", ".", ".",}, + {".", ".", ".", ".", ".",} + }; final int width = 5; final int height = 5; - final int playerX = 2; - final int playerY = 2; + int x = 2; + int y = 2; + B030_IceGame ice = new B030_IceGame(); + B030_IceGame.Position position; @Test public void 上向きに移動できる() { - final int actual = B030_IceGame.moveUp(map, height, playerX, playerY); - final int expected = playerY - 1; - assertThat(actual, is(expected)); + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.UP); + final int actualX = position.getX(); + final int expectedX = x; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y - 1; + assertThat(actualY, is(expectedY)); } - @Test public void 右向きに移動できる() { - final int actual = B030_IceGame.moveRight(map, height, playerX, playerY); - final int expected = playerX + 1; - assertThat(actual, is(expected)); + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.RIGHT); + final int actualX = position.getX(); + final int expectedX = x+1; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y; + assertThat(actualY, is(expectedY)); } - @Test public void 下向きに移動できる() { - final int actual = B030_IceGame.moveDown(map, height, playerX, playerY); - final int expected = playerY + 2; - assertThat(actual, is(expected)); + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.DOWN); + final int actualX = position.getX(); + final int expectedX = x; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y + 2; + assertThat(actualY, is(expectedY)); } - @Test public void 左向きに移動できる() { - final int actual = B030_IceGame.moveLeft(map, height, playerX, playerY); - final int expected = playerX - 2; - assertThat(actual, is(expected)); + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.LEFT); + final int actualX = position.getX(); + final int expectedX = x-2; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y; + assertThat(actualY, is(expectedY)); + } + @Test + public void 移動先が氷の時2マス移動する() { + x=2; + y=4; + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.UP); + final int actualX = position.getX(); + final int expectedX = x; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y-2; + assertThat(actualY, is(expectedY)); + } + @Test + public void 移動先が床の時2マス移動する() { + x=3; + y=4; + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.UP); + final int actualX = position.getX(); + final int expectedX = x; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y-1; + assertThat(actualY, is(expectedY)); + } + @Test + public void 移動先が壁の時移動しない() { + x=3; + y=0; + position = ice.new Position(x, y); + position.move(map, width, height, B030_IceGame.DIRECTION.UP); + final int actualX = position.getX(); + final int expectedX = x; + assertThat(actualX, is(expectedX)); + final int actualY = position.getY(); + final int expectedY = y; + assertThat(actualY, is(expectedY)); } } -- GitLab From ee4e49b8762457c5dd0eee58f58a3f9fcc8b80ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Fri, 29 Jul 2022 16:01:26 +0900 Subject: [PATCH 3/3] =?UTF-8?q?B030=20=E6=B0=B7=E3=81=AE=E3=83=80=E3=83=B3?= =?UTF-8?q?=E3=82=B8=E3=83=A7=E3=83=B3=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/test/B030_IceGameTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykoiso/test/B030_IceGameTest.java b/ykoiso/test/B030_IceGameTest.java index 14b730f..907d9ab 100644 --- a/ykoiso/test/B030_IceGameTest.java +++ b/ykoiso/test/B030_IceGameTest.java @@ -75,7 +75,7 @@ public class B030_IceGameTest { assertThat(actualY, is(expectedY)); } @Test - public void 移動先が床の時2マス移動する() { + public void 移動先が床の時1マス移動する() { x=3; y=4; position = ice.new Position(x, y); -- GitLab