From dafff9e6e6f9c56537ab403cdb18256b06f2f691 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Fri, 4 Jul 2025 16:44:21 +0900 Subject: [PATCH 1/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB138?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 190 +++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 kkanazawa/src/SpiralDecoder.java diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java new file mode 100644 index 0000000..8ecb5e8 --- /dev/null +++ b/kkanazawa/src/SpiralDecoder.java @@ -0,0 +1,190 @@ +package paiza.src; + +import java.lang.reflect.Array; +import java.util.Scanner; + +/** + * 受け取った二次元の文字リストをうずまき型で解読する + * + * @author 金澤継心 + */ +public class SpiralDecoder { + + public static void main(final String[] args) { + Scanner scanner = new Scanner(System.in); + final int lineAmount = scanner.nextInt(); // 入力値の行の数 + final int columnAmount = scanner.nextInt(); // 入力値の列の数; + final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス + final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + + // もととなる二次元の文字リストのを入力から受け取る + final String[][] cipher = inputCipher(scanner, lineAmount, columnAmount); + scanner.close(); + + // 入力値の二次元の文字リストの周りをNULLで埋める + final String[][] paddedCipher = nullPaddingCipher(cipher); + // 復号処理 + final String selectedDecode = decodingCipher(lineAmount, columnAmount, startIndex, endIndex, + paddedCipher); + System.out.println(selectedDecode); + } + + /** + * もととなる文字列を入力から受け取る + * + * @param scanner 入力を受け取るスキャナー + * @param lineAmount 文字リストの行数 + * @param columnAmount 文字リストの列数 + */ + public static String[][] inputCipher(final Scanner scanner, final int lineAmount, final int columnAmount) { + String[][] cipher = new String[lineAmount][columnAmount]; + for (int i = 0; i < lineAmount; i++) { + String inputLine = scanner.next(); + cipher[i] = inputLine.split(""); + } + return cipher; + } + + /** + * 文字リストの周りをNULLで埋める + * + * @param originCipher もととなる文字リスト + */ + public static String[][] nullPaddingCipher(final String[][] originCipher) { + final int lineAmount = Array.getLength(originCipher); + final int columnAmount = Array.getLength(originCipher[0]); + String[][] paddedCipher = new String[lineAmount + 2][columnAmount + 2]; + + // 元の文字リストをNULLしかない文字リストの中央にコピーする + for (int i = 0; i < lineAmount; i++) { + System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, columnAmount); + } + return paddedCipher; + } + + /** + * 周りをNULLで埋めた文字リストの解読 NULLにぶつかったら方向転換するイメージでうずまき状に要素を移動する + * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える + * + * @param lineAmount 文字列リストの行数 + * @param columnAmount 文字列リストの列数 + * @param startIndex 解読後読み始める文字の順番 + * @param endIndex 解読後読み終える文字の順番 + */ + public static String decodingCipher(final int lineAmount, final int columnAmount, final int startIndex, + final int endIndex, final String[][] cipher) { + // 現在いる要素を示すカーソルとして使うクラス + DecodeCursor cursor = new DecodeCursor(); + // NULLでかこっているため、始まりのインデックスは行、列ともに1から + cursor.setLineIndex(1); + cursor.setColumnIndex(1); + // 移動する方向を表すインスタンス変数 + cursor.setDirection(0); + // 現在移し終えた文字数のカウンタ + int charCounter = 0; + // この配列に文字移して最終的には復号した順番に文字が並ぶ + String[] decordResult = new String[lineAmount * columnAmount]; + + while (charCounter < lineAmount * columnAmount) { + // NULLの要素に移動した際、カーソルの位置を戻す処理 + cursor.modifying(charCounter); + // NULLのぶつかるまで一方向に要素を読み取り続ける + while (cipher[cursor.getLineIndex()][cursor.getColumnIndex()] != null) { + decordResult[charCounter] = cipher[cursor.getLineIndex()][cursor.getColumnIndex()]; + cipher[cursor.getLineIndex()][cursor.getColumnIndex()] = null; + charCounter++; + // カーソルを現在進むべき方向に移動させる + cursor.shifting(); + } + // 方向転換の処理 + cursor.changeDirection(); + } + return String.join("", decordResult).substring(startIndex - 1, endIndex); + } + +} + + +/** + * 現在読み取っている要素を示すカーソルとして使うクラス + * + */ +class DecodeCursor { + private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している + private int lineIndex;// 現在の行の位置 + private int columnIndex;// 現在の列の位置 + + public int getDirection() { + return direction; + } + + public void setDirection(int direction) { + this.direction = direction; + } + + public void changeDirection() { + this.direction++; + } + + public int getLineIndex() { + return lineIndex; + } + + public void setLineIndex(int lineIndex) { + this.lineIndex = lineIndex; + } + + public int getColumnIndex() { + return columnIndex; + } + + public void setColumnIndex(int columnIndex) { + this.columnIndex = columnIndex; + } + + // NULLの要素に移動した際、カーソルの位置を戻す処理 + // 4つの方向はdirectionを4で割った剰余として区別している + // 0:右 1:下 2:左 3:上 + public void modifying(final int charCounter) { + switch (direction % 4) { + case 0: + if (charCounter != 0) { + this.columnIndex++; + this.lineIndex++; + } + break; + case 1: + this.columnIndex--; + this.lineIndex++; + break; + case 2: + this.columnIndex--; + this.lineIndex--; + break; + case 3: + this.columnIndex++; + this.lineIndex--; + break; + } + } + + // カーソルを現在進むべき方向に移動させる + // 4つの方向はdirectionを4で割った剰余として区別している + // 0:右 1:下 2:左 3:上 + public void shifting() { + switch (this.direction % 4) { + case 0: + this.columnIndex++; + break; + case 1: + this.lineIndex++; + break; + case 2: + this.columnIndex--; + break; + case 3: + this.lineIndex--; + break; + } + } +} -- GitLab From a9235b5fba8dc52b4e5898f3b0a88d08a4ef01d8 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 7 Jul 2025 16:04:29 +0900 Subject: [PATCH 2/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB156?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 181 ++++++++++++++++++------------- 1 file changed, 105 insertions(+), 76 deletions(-) diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java index 8ecb5e8..be0dc1b 100644 --- a/kkanazawa/src/SpiralDecoder.java +++ b/kkanazawa/src/SpiralDecoder.java @@ -1,6 +1,7 @@ package paiza.src; import java.lang.reflect.Array; +import java.util.Arrays; import java.util.Scanner; /** @@ -12,86 +13,104 @@ public class SpiralDecoder { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); - final int lineAmount = scanner.nextInt(); // 入力値の行の数 - final int columnAmount = scanner.nextInt(); // 入力値の列の数; - final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス - final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + final int ROW_COUNT = scanner.nextInt(); // 入力値の行の数 + final int COLUMN_COUNT = scanner.nextInt(); // 入力値の列の数; + final int START_INDEX = scanner.nextInt(); // 読み始まる文字のインデックス + final int END_INDEX = scanner.nextInt(); // 読み終える文字のインデックス + final String BARRIR_CHAR = "@"; // もととなる二次元の文字リストのを入力から受け取る - final String[][] cipher = inputCipher(scanner, lineAmount, columnAmount); + final String[][] CIPHER = inputCipher(scanner, ROW_COUNT, COLUMN_COUNT); scanner.close(); // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] paddedCipher = nullPaddingCipher(cipher); - // 復号処理 - final String selectedDecode = decodingCipher(lineAmount, columnAmount, startIndex, endIndex, - paddedCipher); + final String[][] PADDED_CIPHER = paddingCipher(CIPHER, BARRIR_CHAR); + + // 解読処理 + final String selectedDecode = decodingCipher(ROW_COUNT, COLUMN_COUNT, START_INDEX, END_INDEX, + PADDED_CIPHER, BARRIR_CHAR); System.out.println(selectedDecode); } /** * もととなる文字列を入力から受け取る * - * @param scanner 入力を受け取るスキャナー - * @param lineAmount 文字リストの行数 - * @param columnAmount 文字リストの列数 + * @param SCANNER 入力を受け取るスキャナー + * @param ROW_COUNT 文字リストの行数 + * @param COLUMN_COUNT 文字リストの列数 */ - public static String[][] inputCipher(final Scanner scanner, final int lineAmount, final int columnAmount) { - String[][] cipher = new String[lineAmount][columnAmount]; - for (int i = 0; i < lineAmount; i++) { - String inputLine = scanner.next(); - cipher[i] = inputLine.split(""); + public static String[][] inputCipher(final Scanner SCANNER, final int ROW_COUNT, + final int COLUMN_COUNT) { + String[][] cipher = new String[ROW_COUNT][COLUMN_COUNT]; + for (int i = 0; i < ROW_COUNT; i++) { + String inputRow = SCANNER.next(); + cipher[i] = inputRow.split(""); } return cipher; } /** - * 文字リストの周りをNULLで埋める + * 文字リストの周りを@で埋める * - * @param originCipher もととなる文字リスト + * @param ORIGIN_CIPHER もととなる文字リスト */ - public static String[][] nullPaddingCipher(final String[][] originCipher) { - final int lineAmount = Array.getLength(originCipher); - final int columnAmount = Array.getLength(originCipher[0]); - String[][] paddedCipher = new String[lineAmount + 2][columnAmount + 2]; - - // 元の文字リストをNULLしかない文字リストの中央にコピーする - for (int i = 0; i < lineAmount; i++) { - System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, columnAmount); + public static String[][] paddingCipher(final String[][] ORIGIN_CIPHER, final String BARRIR_CHAR) { + final int ROW_COUNT = Array.getLength(ORIGIN_CIPHER); + final int COLUMN_COUNT = Array.getLength(ORIGIN_CIPHER[0]); + String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; + + for (String[] o : paddedCipher) { + Arrays.fill(o, BARRIR_CHAR); + } + + // 元の文字リストを"@"しかない文字リストの中央にコピーする + for (int i = 0; i < ROW_COUNT; i++) { + System.arraycopy(ORIGIN_CIPHER[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); } return paddedCipher; } /** - * 周りをNULLで埋めた文字リストの解読 NULLにぶつかったら方向転換するイメージでうずまき状に要素を移動する + * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える * - * @param lineAmount 文字列リストの行数 - * @param columnAmount 文字列リストの列数 - * @param startIndex 解読後読み始める文字の順番 - * @param endIndex 解読後読み終える文字の順番 + * @param ROW_COUNT 文字列リストの行数 + * @param COLUMN_COUNT 文字列リストの列数 + * @param START_INDEX 解読後読み始める文字の順番 + * @param END_INDEX 解読後読み終える文字の順番 */ - public static String decodingCipher(final int lineAmount, final int columnAmount, final int startIndex, - final int endIndex, final String[][] cipher) { + public static String decodingCipher(final int ROW_COUNT, final int COLUMN_COUNT, + final int START_INDEX, + final int END_INDEX, final String[][] CIPHER, final String BARRIR_CHAR) { // 現在いる要素を示すカーソルとして使うクラス DecodeCursor cursor = new DecodeCursor(); // NULLでかこっているため、始まりのインデックスは行、列ともに1から - cursor.setLineIndex(1); + cursor.setRowIndex(1); cursor.setColumnIndex(1); // 移動する方向を表すインスタンス変数 cursor.setDirection(0); // 現在移し終えた文字数のカウンタ int charCounter = 0; // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decordResult = new String[lineAmount * columnAmount]; - - while (charCounter < lineAmount * columnAmount) { - // NULLの要素に移動した際、カーソルの位置を戻す処理 - cursor.modifying(charCounter); - // NULLのぶつかるまで一方向に要素を読み取り続ける - while (cipher[cursor.getLineIndex()][cursor.getColumnIndex()] != null) { - decordResult[charCounter] = cipher[cursor.getLineIndex()][cursor.getColumnIndex()]; - cipher[cursor.getLineIndex()][cursor.getColumnIndex()] = null; + String[] decodedResult = new String[ROW_COUNT * COLUMN_COUNT]; + + // 初期位置から初めて@にぶつかるまでの解読処理 + while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { + decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; + CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + charCounter++; + cursor.shifting(); + } + cursor.changeDirection(); + + // 初めて@にぶつかってから解読し終わるまでの処理 + while (charCounter < ROW_COUNT * COLUMN_COUNT) { + // @の要素に移動した際、カーソルの位置を戻す処理 + cursor.modifying(); + // @のぶつかるまで一方向に要素を読み取り続ける + while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { + decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; + CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; charCounter++; // カーソルを現在進むべき方向に移動させる cursor.shifting(); @@ -99,7 +118,7 @@ public class SpiralDecoder { // 方向転換の処理 cursor.changeDirection(); } - return String.join("", decordResult).substring(startIndex - 1, endIndex); + return String.join("", decodedResult).substring(START_INDEX - 1, END_INDEX); } } @@ -111,59 +130,63 @@ public class SpiralDecoder { */ class DecodeCursor { private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している - private int lineIndex;// 現在の行の位置 + private int rowIndex;// 現在の行の位置 private int columnIndex;// 現在の列の位置 public int getDirection() { return direction; } - public void setDirection(int direction) { - this.direction = direction; + public void setDirection(final int DIRECTION) { + this.direction = DIRECTION; } public void changeDirection() { this.direction++; } - public int getLineIndex() { - return lineIndex; + public int getRowIndex() { + return rowIndex; } - public void setLineIndex(int lineIndex) { - this.lineIndex = lineIndex; + public void setRowIndex(final int ROW_INDEX) { + this.rowIndex = ROW_INDEX; } public int getColumnIndex() { return columnIndex; } - public void setColumnIndex(int columnIndex) { - this.columnIndex = columnIndex; + public void setColumnIndex(final int COLUMN_INDEX) { + this.columnIndex = COLUMN_INDEX; } - // NULLの要素に移動した際、カーソルの位置を戻す処理 - // 4つの方向はdirectionを4で割った剰余として区別している + // NULL(要検討)の要素に移動した際、カーソルの位置を戻す処理 + // 4つの方向はdirectionを4で割った剰余として区別する // 0:右 1:下 2:左 3:上 - public void modifying(final int charCounter) { - switch (direction % 4) { - case 0: - if (charCounter != 0) { - this.columnIndex++; - this.lineIndex++; - } + public void modifying() { + final int DIRECTION_NUM = this.direction % 4; + final int RIGHT_NUM = 0; + final int DOWN_NUM = 1; + final int LEFT_NUM = 2; + final int UP_NUM = 3; + + switch (DIRECTION_NUM) { + case RIGHT_NUM: + this.columnIndex++; + this.rowIndex++; break; - case 1: + case DOWN_NUM: this.columnIndex--; - this.lineIndex++; + this.rowIndex++; break; - case 2: + case LEFT_NUM: this.columnIndex--; - this.lineIndex--; + this.rowIndex--; break; - case 3: + case UP_NUM: this.columnIndex++; - this.lineIndex--; + this.rowIndex--; break; } } @@ -172,18 +195,24 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別している // 0:右 1:下 2:左 3:上 public void shifting() { - switch (this.direction % 4) { - case 0: + final int DIRECTION_NUM = this.direction % 4; + final int RIGHT_NUM = 0; + final int DOWN_NUM = 1; + final int LEFT_NUM = 2; + final int UP_NUM = 3; + + switch (DIRECTION_NUM) { + case RIGHT_NUM: this.columnIndex++; break; - case 1: - this.lineIndex++; + case DOWN_NUM: + this.rowIndex++; break; - case 2: + case LEFT_NUM: this.columnIndex--; break; - case 3: - this.lineIndex--; + case UP_NUM: + this.rowIndex--; break; } } -- GitLab From cad12ab177baec88577281987f99500f6fe0c6b4 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 7 Jul 2025 17:44:16 +0900 Subject: [PATCH 3/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB156?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 119 +++++++++++++++---------------- 1 file changed, 58 insertions(+), 61 deletions(-) diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java index be0dc1b..45c2046 100644 --- a/kkanazawa/src/SpiralDecoder.java +++ b/kkanazawa/src/SpiralDecoder.java @@ -13,37 +13,37 @@ public class SpiralDecoder { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); - final int ROW_COUNT = scanner.nextInt(); // 入力値の行の数 - final int COLUMN_COUNT = scanner.nextInt(); // 入力値の列の数; - final int START_INDEX = scanner.nextInt(); // 読み始まる文字のインデックス - final int END_INDEX = scanner.nextInt(); // 読み終える文字のインデックス - final String BARRIR_CHAR = "@"; + final int rowCount = scanner.nextInt(); // 入力値の行の数 + final int columnCount = scanner.nextInt(); // 入力値の列の数; + final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス + final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + final String barrirchar = "@"; // もととなる二次元の文字リストのを入力から受け取る - final String[][] CIPHER = inputCipher(scanner, ROW_COUNT, COLUMN_COUNT); + final String[][] cipher = inputCipher(scanner, rowCount, columnCount); scanner.close(); // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] PADDED_CIPHER = paddingCipher(CIPHER, BARRIR_CHAR); + final String[][] paddedCipher = paddingCipher(cipher, barrirchar); // 解読処理 - final String selectedDecode = decodingCipher(ROW_COUNT, COLUMN_COUNT, START_INDEX, END_INDEX, - PADDED_CIPHER, BARRIR_CHAR); + final String selectedDecode = decodingCipher(rowCount, columnCount, startIndex, endIndex, + paddedCipher, barrirchar); System.out.println(selectedDecode); } /** * もととなる文字列を入力から受け取る * - * @param SCANNER 入力を受け取るスキャナー - * @param ROW_COUNT 文字リストの行数 - * @param COLUMN_COUNT 文字リストの列数 + * @param scanner 入力を受け取るスキャナー + * @param rowCount 文字リストの行数 + * @param columnCount 文字リストの列数 */ - public static String[][] inputCipher(final Scanner SCANNER, final int ROW_COUNT, - final int COLUMN_COUNT) { - String[][] cipher = new String[ROW_COUNT][COLUMN_COUNT]; - for (int i = 0; i < ROW_COUNT; i++) { - String inputRow = SCANNER.next(); + public static String[][] inputCipher(final Scanner scanner, final int rowCount, + final int columnCount) { + String[][] cipher = new String[rowCount][columnCount]; + for (int i = 0; i < rowCount; i++) { + String inputRow = scanner.next(); cipher[i] = inputRow.split(""); } return cipher; @@ -52,20 +52,20 @@ public class SpiralDecoder { /** * 文字リストの周りを@で埋める * - * @param ORIGIN_CIPHER もととなる文字リスト + * @param originCipher もととなる文字リスト */ - public static String[][] paddingCipher(final String[][] ORIGIN_CIPHER, final String BARRIR_CHAR) { - final int ROW_COUNT = Array.getLength(ORIGIN_CIPHER); - final int COLUMN_COUNT = Array.getLength(ORIGIN_CIPHER[0]); + public static String[][] paddingCipher(final String[][] originCipher, final String barrirchar) { + final int ROW_COUNT = Array.getLength(originCipher); + final int COLUMN_COUNT = Array.getLength(originCipher[0]); String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; for (String[] o : paddedCipher) { - Arrays.fill(o, BARRIR_CHAR); + Arrays.fill(o, barrirchar); } // 元の文字リストを"@"しかない文字リストの中央にコピーする for (int i = 0; i < ROW_COUNT; i++) { - System.arraycopy(ORIGIN_CIPHER[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); + System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); } return paddedCipher; } @@ -74,14 +74,14 @@ public class SpiralDecoder { * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える * - * @param ROW_COUNT 文字列リストの行数 - * @param COLUMN_COUNT 文字列リストの列数 - * @param START_INDEX 解読後読み始める文字の順番 - * @param END_INDEX 解読後読み終える文字の順番 + * @param rowCount 文字列リストの行数 + * @param columnCount 文字列リストの列数 + * @param startIndex 解読後読み始める文字の順番 + * @param endIndex 解読後読み終える文字の順番 */ - public static String decodingCipher(final int ROW_COUNT, final int COLUMN_COUNT, - final int START_INDEX, - final int END_INDEX, final String[][] CIPHER, final String BARRIR_CHAR) { + public static String decodingCipher(final int rowCount, final int columnCount, + final int startIndex, + final int endIndex, final String[][] cipher, final String barrirchar) { // 現在いる要素を示すカーソルとして使うクラス DecodeCursor cursor = new DecodeCursor(); // NULLでかこっているため、始まりのインデックスは行、列ともに1から @@ -92,25 +92,25 @@ public class SpiralDecoder { // 現在移し終えた文字数のカウンタ int charCounter = 0; // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decodedResult = new String[ROW_COUNT * COLUMN_COUNT]; + String[] decodedResult = new String[rowCount * columnCount]; // 初期位置から初めて@にぶつかるまでの解読処理 - while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { - decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; - CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { + decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; + cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; charCounter++; cursor.shifting(); } cursor.changeDirection(); // 初めて@にぶつかってから解読し終わるまでの処理 - while (charCounter < ROW_COUNT * COLUMN_COUNT) { + while (charCounter < rowCount * columnCount) { // @の要素に移動した際、カーソルの位置を戻す処理 cursor.modifying(); // @のぶつかるまで一方向に要素を読み取り続ける - while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { - decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; - CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { + decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; + cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; charCounter++; // カーソルを現在進むべき方向に移動させる cursor.shifting(); @@ -118,7 +118,7 @@ public class SpiralDecoder { // 方向転換の処理 cursor.changeDirection(); } - return String.join("", decodedResult).substring(START_INDEX - 1, END_INDEX); + return String.join("", decodedResult).substring(startIndex - 1, endIndex); } } @@ -132,6 +132,10 @@ class DecodeCursor { private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している private int rowIndex;// 現在の行の位置 private int columnIndex;// 現在の列の位置 + final static int RIGHT = 0; + final static int DOWN = 1; + final static int LEFT = 2; + final static int UP = 3; public int getDirection() { return direction; @@ -165,26 +169,22 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別する // 0:右 1:下 2:左 3:上 public void modifying() { - final int DIRECTION_NUM = this.direction % 4; - final int RIGHT_NUM = 0; - final int DOWN_NUM = 1; - final int LEFT_NUM = 2; - final int UP_NUM = 3; - - switch (DIRECTION_NUM) { - case RIGHT_NUM: + final int directionNum = this.direction % 4; + + switch (directionNum) { + case RIGHT: this.columnIndex++; this.rowIndex++; break; - case DOWN_NUM: + case DOWN: this.columnIndex--; this.rowIndex++; break; - case LEFT_NUM: + case LEFT: this.columnIndex--; this.rowIndex--; break; - case UP_NUM: + case UP: this.columnIndex++; this.rowIndex--; break; @@ -195,23 +195,20 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別している // 0:右 1:下 2:左 3:上 public void shifting() { - final int DIRECTION_NUM = this.direction % 4; - final int RIGHT_NUM = 0; - final int DOWN_NUM = 1; - final int LEFT_NUM = 2; - final int UP_NUM = 3; - - switch (DIRECTION_NUM) { - case RIGHT_NUM: + final int directionNum= this.direction % 4; + + + switch (directionNum) { + case RIGHT: this.columnIndex++; break; - case DOWN_NUM: + case DOWN: this.rowIndex++; break; - case LEFT_NUM: + case LEFT: this.columnIndex--; break; - case UP_NUM: + case UP: this.rowIndex--; break; } -- GitLab