diff --git a/rsuzuki/README.md b/rsuzuki/README.md new file mode 100644 index 0000000000000000000000000000000000000000..796c95d353f961f632c60d81ef2bdf62fa8ea5ff --- /dev/null +++ b/rsuzuki/README.md @@ -0,0 +1,2 @@ +# Paiza +knoda用 diff --git a/rsuzuki/src/B136_chocolate.java b/rsuzuki/src/B136_chocolate.java new file mode 100644 index 0000000000000000000000000000000000000000..d3d3b8616c8e234e3f580d54289d10e29ff59caa --- /dev/null +++ b/rsuzuki/src/B136_chocolate.java @@ -0,0 +1,190 @@ +package paiza_c; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** データを保持するクラス */ +final class ChocoData { + /** 移動回数 */ + private final int movingTime; + /** 縦の席数 */ + private final int ySeat; + /** 横の席数 */ + private final int xSeat; + /** 自分の座席がどこか */ + private int yPlayer; + private int xPlayer; + /** 移動経路FBLR */ + private final String movingCommand; + /** 座席でもらえるチョコの数を格納する配列 */ + final int[][] chocoNumber; + + /** + * ChocoDataの新しいインスタンスを生成 + * + * @param movingTime 移動回数 + * @param ySeat 縦の席数 + * @param xSeat 横の席数 + * @param yPlayer プレイヤーの初期縦位置 + * @param xPlayer プレイヤーの初期横位置 + * @param movingCommand 移動コマンド文字列 + * @param chocoNumber 各座席のチョコの数を格納する二次元配列 + */ + public ChocoData(int movingTime, int ySeat, int xSeat, int yPlayer, int xPlayer, String movingCommand, + int[][] chocoNumber) { + this.movingTime = movingTime; + this.ySeat = ySeat; + this.xSeat = xSeat; + this.yPlayer = yPlayer; + this.xPlayer = xPlayer; + this.movingCommand = movingCommand; + this.chocoNumber = chocoNumber; + } + + public int getMovingTime() { + return movingTime; + } + + public int getYSeat() { + return ySeat; + } + + public int getXSeat() { + return xSeat; + } + + public int getYPlayer() { + return yPlayer; + } + + public int getXPlayer() { + return xPlayer; + } + + public String getMovingCommand() { + return movingCommand; + } + + public int getChocoNumber(int y, int x) { + return chocoNumber[y][x]; + } + + public void setSy(int sy) { + this.yPlayer = sy; + } + + public void setSx(int sx) { + this.xPlayer = sx; + } +} + +/** + * ロジック + */ +final class ChocoLogic { + private final ChocoData data; + /** 集計用リスト */ + private final List chocoValues; + + public ChocoLogic(ChocoData data) { + this.data = data; + this.chocoValues = new ArrayList<>(); + } + + /** + * 移動コマンドに基づいてプレイヤーを移動させ、通過した座席のチョコの数を集計 + */ + public void Commands() { + for (int i = 0; i < data.getMovingTime(); i++) { + char command = data.getMovingCommand().charAt(i); + movePlayer(command); + chocoValues.add(data.getChocoNumber(data.getYPlayer(), data.getXPlayer())); + } + } + + /** + * 指定されたコマンドに基づいてプレイヤーを移動 + */ + private void movePlayer(char command) { + int yPlayer = data.getYPlayer(); + int xPlayer = data.getXPlayer(); + int ySeat = data.getYSeat(); + int xSeat = data.getXSeat(); + + if (command == 'F') { // 前進 (上) + if (yPlayer > 0) { + data.setSy(yPlayer - 1); + } + } else if (command == 'B') { // 後退 (下) + if (yPlayer < ySeat - 1) { + data.setSy(yPlayer + 1); + } + } else if (command == 'L') { // 左 + if (xPlayer > 0) { + data.setSx(xPlayer - 1); + } + } else if (command == 'R') { // 右 + if (xPlayer < xSeat - 1) { + data.setSx(xPlayer + 1); + } + } + } + + /** + * 集計されたチョコの数のリストを取得 + * + * @return チョコの数のリスト + */ + public List getChocoValues() { + return chocoValues; + } +} + +/** 結果を出力するクラス */ +final class ChocoResult { + public void printResult(List chocoValues) { + for (int value : chocoValues) { + System.out.println(value); + } + } + +} + +public class B136_chocolate { + + public static void main(String[] args) { + final Scanner sc = new Scanner(System.in); + final int movingTime = sc.nextInt(); + final int ySeat = sc.nextInt(); + final int xSeat = sc.nextInt(); + int yPlayer = sc.nextInt(); + int xPlayer = sc.nextInt(); + + sc.nextLine();// 残った改行文字を消費 + + final String movingCommand = sc.nextLine(); + + // HxW のグリッドを宣言し、読み込む + final int[][] chocoNumber = new int[ySeat][xSeat]; + for (int i = 1; i < ySeat; i++) { + for (int j = 1; j < xSeat; j++) { + chocoNumber[i][j] = sc.nextInt(); + } + } + + // チョコデータオブジェクトを生成 + ChocoData data = new ChocoData(movingTime, ySeat, xSeat, yPlayer, xPlayer, movingCommand, chocoNumber); + + // チョコロジックオブジェクトを生成し、コマンドを実行 + ChocoLogic chocoLogic = new ChocoLogic(data); + chocoLogic.Commands(); + + // チョコ結果オブジェクトを生成し、結果を出力 + ChocoResult chocoResult = new ChocoResult(); + chocoResult.printResult(chocoLogic.getChocoValues()); + + sc.close(); + + } +} diff --git a/rsuzuki/src/ChocoTest.java b/rsuzuki/src/ChocoTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2e8091bdab4bdbdcff6649cc20efc13e4868f54f --- /dev/null +++ b/rsuzuki/src/ChocoTest.java @@ -0,0 +1,110 @@ +package paiza_c; + +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class ChocoTest { + + // ChocoData クラスのテスト + public static class ChocoDataTest { + + private ChocoData chocoData; + private int[][] chocoNumbers; + + @Before + public void setUp() { + chocoData = new ChocoData(3, 3, 3, 2, 1, "FRB", chocoNumbers); + } + + @Test + public void Getterで入力された値を取得できるか() { + chocoNumbers = new int[][] { { 3, 6, 2 }, { 0, 4, 1 }, { 5, 0, 7 } }; + assertEquals(3, chocoData.getMovingTime()); + assertEquals(3, chocoData.getYSeat()); + assertEquals(3, chocoData.getXSeat()); + assertEquals(2, chocoData.getYPlayer()); + assertEquals(1, chocoData.getXPlayer()); + assertEquals("FRB", chocoData.getMovingCommand()); + assertEquals(4, chocoData.getChocoNumber(1, 1)); + assertEquals(0, chocoData.getChocoNumber(2, 1)); + } + } + + // ChocoLogic クラスのテスト + public static class ChocoLogicTest { + + private ChocoData chocoData; + private ChocoLogic chocoLogic; + private int[][] chocoNumbers; + + @Test + public void Logicクラスにおいてコマンドによってチョコを得ることができる() { + // 初期位置: (1,0) チョコ: 0 + // コマンド: F (上) -> (0,0) チョコ: 3 + // コマンド: R (右) -> (0,1) チョコ: 6 + // コマンド: B (下) -> (1,1) チョコ: 4 + + chocoNumbers = new int[][] { { 3, 6, 2 }, { 0, 4, 1 }, { 5, 0, 7 } }; + chocoData = new ChocoData(3, 3, 3, 1, 0, "FRB", chocoNumbers); + chocoLogic = new ChocoLogic(chocoData); + + chocoLogic.Commands(); + List expectedChocoValues = Arrays.asList(3, 6, 4); + assertEquals(expectedChocoValues, chocoLogic.getChocoValues()); + assertEquals(1, chocoData.getYPlayer()); // 最終的なプレイヤーの位置のY座標 + assertEquals(1, chocoData.getXPlayer()); // 最終的なプレイヤーの位置のX座標 + } + + @Test + public void Logicクラスにおいて座席が存在しない時チョコを得ていない() { + // 初期位置: (0,0) チョコ: 0 + // 座席が存在しないので、コマンドを受け取ってもチョコを得られない + chocoNumbers = new int[][] { { 0 } }; + chocoData = new ChocoData(4, 1, 1, 0, 0, "FRBL", chocoNumbers); + chocoLogic = new ChocoLogic(chocoData); + + chocoLogic.Commands(); + List expectedChocoValues = Arrays.asList(0, 0, 0, 0); + assertEquals(expectedChocoValues, chocoLogic.getChocoValues()); + + } + + } + + // ChocoResult クラスのテスト + public static class ChocoResultTest { + + // テスト中にSystem.outの出力をキャプチャするためのByteArrayOutputStream + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + // 元のSystem.outを保持するためのPrintStream + private final PrintStream originalOut = System.out; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + } + + @Test + public void PrintResultの出力が正しいか() { + ChocoResult chocoResult = new ChocoResult(); + List testValues = Arrays.asList(100, 200, 300); + chocoResult.printResult(testValues); + + String expectedOutput = "100\n200\n300\n"; + assertEquals(expectedOutput, outContent.toString()); + } + + // 各テストの後に元のSystem.outを復元 + @org.junit.After + public void restoreStreams() { + System.setOut(originalOut); + } + } +} \ No newline at end of file