From 91eaa7fff57f25c9dded51318a738c6d404b534e 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: Tue, 5 Jul 2022 10:26:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?B034=20=E3=83=AD=E3=83=9C=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=81=AE=E6=AD=A9=E8=A1=8C=E5=AE=9F=E9=A8=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B034_Robot.java | 190 ++++++++++++++++++++++++++++ ykoiso/test/B034_RobotTest.java | 211 ++++++++++++++++++++++++++++++++ 2 files changed, 401 insertions(+) create mode 100644 ykoiso/src/B034_Robot.java create mode 100644 ykoiso/test/B034_RobotTest.java diff --git a/ykoiso/src/B034_Robot.java b/ykoiso/src/B034_Robot.java new file mode 100644 index 0000000..1aef6d7 --- /dev/null +++ b/ykoiso/src/B034_Robot.java @@ -0,0 +1,190 @@ +import java.util.Scanner; + +public class B034_Robot { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 各定数読み込み + // 初期座標 + final int X = scan.nextInt(); + final int Y = scan.nextInt(); + // 移動量 + final int W = scan.nextInt(); + final int D = scan.nextInt(); + final int S = scan.nextInt(); + final int A = scan.nextInt(); + // 命令数 + final int N = scan.nextInt(); + // ロボットの生成 + Robot robot = new Robot(X, Y, W, A, S, D); + String order, direction; + // 命令の実行 + for (int i = 0; i < N; i++) { + order = scan.next(); + direction = scan.next(); + robot.executeOrder(order, direction); + } + // 現在の座標の出力 + robot.showCoodinate(); + + scan.close(); + } + + static class Robot { + private int x, y, W, A, S, D; + // ロボットの向いている方向 + // 角度で表すdeg表記 + private int deg = 0; + + // コンストラクタ + Robot(int x, int y, int W, int A, int S, int D) { + // 座標 + this.x = x; + this.y = y; + // 移動量 + this.W = W; + this.A = A; + this.S = S; + this.D = D; + } + + // 方向取得 + // テスト用 + public int getDeg() { + return deg; + } + + // 座標取得 + // テスト用 + public int getX() { + return x; + } + + public int getY() { + return y; + } + + // 座標表示 + public void showCoodinate() { + System.out.println(x + " " + y); + } + + // 命令実行関数 + public void executeOrder(String order, String direction) { + if (order.equals("m")) { + // 移動命令判定 + // 移動関数は絶対座標系 + // ここで相対から絶対に変更 + switch (deg) { + case 0: + moveFront(direction); + break; + case 90: + moveRight(direction); + break; + case 180: + moveBack(direction); + break; + case 270: + moveLeft(direction); + break; + } + } else { + changeDirection(direction); + } + } + + // 方向変更関数 + public void changeDirection(String direction) { + switch (direction) { + case "R": + deg += 90; + break; + case "B": + deg += 180; + break; + case "L": + deg += 270; + break; + } + if (deg >= 360) { + deg -= 360; + } + } + + // 前方移動関数 + // 絶対座標系 + public void moveFront(String direction) { + switch (direction) { + case "F": + y += W; + break; + case "R": + x += D; + break; + case "B": + y -= S; + break; + case "L": + x -= A; + break; + } + } + + // 右方向移動関数 + // 絶対座標系 + public void moveRight(String direction) { + switch (direction) { + case "F": + x += W; + break; + case "R": + y -= D; + break; + case "B": + x -= S; + break; + case "L": + y += A; + break; + } + } + + // 後方移動関数 + // 絶対座標系 + public void moveBack(String direction) { + switch (direction) { + case "F": + y -= W; + break; + case "R": + x -= D; + break; + case "B": + y += S; + break; + case "L": + x += A; + break; + } + } + + // 左方向移動関数 + // 絶対座標系 + public void moveLeft(String direction) { + switch (direction) { + case "F": + x -= W; + break; + case "R": + y += D; + break; + case "B": + x += S; + break; + case "L": + y -= A; + break; + } + } + } +} diff --git a/ykoiso/test/B034_RobotTest.java b/ykoiso/test/B034_RobotTest.java new file mode 100644 index 0000000..e33fe0a --- /dev/null +++ b/ykoiso/test/B034_RobotTest.java @@ -0,0 +1,211 @@ +import static org.junit.Assert.assertThat; +import org.hamcrest.core.Is; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import static org.hamcrest.CoreMatchers.*; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class B034_RobotTest { + public static class 方向転換テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 1, 1, 1); + } + + @Test + public void 右に方向転換できる() { + sut.executeOrder("t", "R"); + int actual = sut.getDeg(); + assertThat(actual, is(90)); + } + + @Test + public void 後ろに方向転換できる() { + sut.executeOrder("t", "B"); + int actual = sut.getDeg(); + assertThat(actual, is(180)); + } + + @Test + public void 左に方向転換できる() { + sut.executeOrder("t", "L"); + int actual = sut.getDeg(); + assertThat(actual, is(270)); + } + + } + public static class 前向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 1}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {2, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -3}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-4, 0}; + assertThat(actual, is(expected)); + } + + } + public static class 右向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + sut.changeDirection("R"); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {1, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -2}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-3, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 4}; + assertThat(actual, is(expected)); + } + + } + + + + public static class 後ろ向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + sut.executeOrder("t", "B"); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -1}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-2, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 3}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {4, 0}; + assertThat(actual, is(expected)); + } + + } + public static class 左向き移動テスト { + B034_Robot.Robot sut; + + @Before + public void setUp() { + sut = new B034_Robot.Robot(0, 0, 1, 4, 3, 2); + sut.changeDirection("L"); + } + + @Test + public void 前に1マス移動できる() { + sut.executeOrder("m", "F"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {-1, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 右に2マス移動できる() { + sut.executeOrder("m", "R"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, 2}; + assertThat(actual, is(expected)); + } + + @Test + public void 後ろに3マス移動できる() { + sut.executeOrder("m", "B"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {3, 0}; + assertThat(actual, is(expected)); + } + + @Test + public void 左に4マス移動できる() { + sut.executeOrder("m", "L"); + int[] actual = {sut.getX(), sut.getY()}; + int[] expected = {0, -4}; + assertThat(actual, is(expected)); + } + + } +} -- GitLab From 5b075a1d6dc120e2a299821b7dbe1287f5e69dfe 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: Tue, 5 Jul 2022 14:15:53 +0900 Subject: [PATCH 2/3] =?UTF-8?q?B034=20=E3=83=AD=E3=83=9C=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=81=AE=E6=AD=A9=E8=A1=8C=E5=AE=9F=E9=A8=93=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B034_Robot.java | 57 ++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/ykoiso/src/B034_Robot.java b/ykoiso/src/B034_Robot.java index 1aef6d7..ed5b9bd 100644 --- a/ykoiso/src/B034_Robot.java +++ b/ykoiso/src/B034_Robot.java @@ -33,7 +33,23 @@ public class B034_Robot { private int x, y, W, A, S, D; // ロボットの向いている方向 // 角度で表すdeg表記 - private int deg = 0; + private Direction direction = Direction.F; + + public enum Direction { + F(0), R(90), B(180), L(270); + + private int deg; + + Direction(int deg) { + this.deg = deg; + } + + public int getDeg() { + return deg; + } + } + + // コンストラクタ Robot(int x, int y, int W, int A, int S, int D) { @@ -50,7 +66,7 @@ public class B034_Robot { // 方向取得 // テスト用 public int getDeg() { - return deg; + return direction.getDeg(); } // 座標取得 @@ -69,22 +85,22 @@ public class B034_Robot { } // 命令実行関数 - public void executeOrder(String order, String direction) { + public void executeOrder(final String order, final String direction) { if (order.equals("m")) { // 移動命令判定 // 移動関数は絶対座標系 // ここで相対から絶対に変更 - switch (deg) { - case 0: + switch (this.direction) { + case F: moveFront(direction); break; - case 90: + case R: moveRight(direction); break; - case 180: + case B: moveBack(direction); break; - case 270: + case L: moveLeft(direction); break; } @@ -94,26 +110,13 @@ public class B034_Robot { } // 方向変更関数 - public void changeDirection(String direction) { - switch (direction) { - case "R": - deg += 90; - break; - case "B": - deg += 180; - break; - case "L": - deg += 270; - break; - } - if (deg >= 360) { - deg -= 360; - } + public void changeDirection(final String direction) { + this.direction = Direction.valueOf(direction); } // 前方移動関数 // 絶対座標系 - public void moveFront(String direction) { + public void moveFront(final String direction) { switch (direction) { case "F": y += W; @@ -132,7 +135,7 @@ public class B034_Robot { // 右方向移動関数 // 絶対座標系 - public void moveRight(String direction) { + public void moveRight(final String direction) { switch (direction) { case "F": x += W; @@ -151,7 +154,7 @@ public class B034_Robot { // 後方移動関数 // 絶対座標系 - public void moveBack(String direction) { + public void moveBack(final String direction) { switch (direction) { case "F": y -= W; @@ -170,7 +173,7 @@ public class B034_Robot { // 左方向移動関数 // 絶対座標系 - public void moveLeft(String direction) { + public void moveLeft(final String direction) { switch (direction) { case "F": x -= W; -- GitLab From 73c3da696c5da45bd4c63e449377c657c5ddaed7 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: Wed, 6 Jul 2022 09:40:42 +0900 Subject: [PATCH 3/3] =?UTF-8?q?B034=20=E3=83=AD=E3=83=9C=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=81=AE=E6=AD=A9=E8=A1=8C=E5=AE=9F=E9=A8=93=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A32=E5=9B=9E=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B034_Robot.java | 23 ++++++++++++++++++----- ykoiso/test/B034_RobotTest.java | 6 +++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ykoiso/src/B034_Robot.java b/ykoiso/src/B034_Robot.java index ed5b9bd..b48623c 100644 --- a/ykoiso/src/B034_Robot.java +++ b/ykoiso/src/B034_Robot.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.Map; import java.util.Scanner; public class B034_Robot { @@ -36,17 +38,27 @@ public class B034_Robot { private Direction direction = Direction.F; public enum Direction { - F(0), R(90), B(180), L(270); + F(0), R(1), B(2), L(3); - private int deg; + private Integer deg; + private static Map map = new HashMap<>(); + static { + for (Direction d : Direction.values()) { + map.put(d.getDeg(), d); + } + } - Direction(int deg) { + Direction(final Integer deg) { this.deg = deg; } - public int getDeg() { + public Integer getDeg() { return deg; } + + public Direction turn(final Direction direction) { + return map.get((this.getDeg() + direction.getDeg()) % Direction.values().length); + } } @@ -106,12 +118,13 @@ public class B034_Robot { } } else { changeDirection(direction); + System.out.println(this.direction); } } // 方向変更関数 public void changeDirection(final String direction) { - this.direction = Direction.valueOf(direction); + this.direction = this.direction.turn(Direction.valueOf(direction)); } // 前方移動関数 diff --git a/ykoiso/test/B034_RobotTest.java b/ykoiso/test/B034_RobotTest.java index e33fe0a..6b7cb11 100644 --- a/ykoiso/test/B034_RobotTest.java +++ b/ykoiso/test/B034_RobotTest.java @@ -20,21 +20,21 @@ public class B034_RobotTest { public void 右に方向転換できる() { sut.executeOrder("t", "R"); int actual = sut.getDeg(); - assertThat(actual, is(90)); + assertThat(actual, is(1)); } @Test public void 後ろに方向転換できる() { sut.executeOrder("t", "B"); int actual = sut.getDeg(); - assertThat(actual, is(180)); + assertThat(actual, is(2)); } @Test public void 左に方向転換できる() { sut.executeOrder("t", "L"); int actual = sut.getDeg(); - assertThat(actual, is(270)); + assertThat(actual, is(3)); } } -- GitLab