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/B145_bingo.java b/rsuzuki/src/B145_bingo.java new file mode 100644 index 0000000000000000000000000000000000000000..de641e02b1e14b997cdc243675e44dd1f55b96fd --- /dev/null +++ b/rsuzuki/src/B145_bingo.java @@ -0,0 +1,212 @@ +package test; + +import java.util.Scanner; + +/** 入力されたデータを保持するクラス */ +final class BingoData { + private final int bingoSize; + private final int drawCount; + private final int[][] bingoNumbers; + private final int[] drawNumbers; + private final boolean[][] openedCells; + + /** + * BingoDataの新しいインスタンスを生成 + * + * @param bingoSize 移動回数 + * @param drawCount 抽選回数 + * @param ySeat 縦の数字 + * @param xSeat 横の数字 + * @param bingoNumber ビンゴカードの数字を格納する二次元配列 + * @param drawNumber 抽選された数字 + */ + public BingoData(int bingoSize, int drawCount, int[][] bingoNumbers, int[] drawNumbers) { + this.bingoSize = bingoSize; + this.drawCount = drawCount; + this.bingoNumbers = bingoNumbers; + this.drawNumbers = drawNumbers; + this.openedCells = new boolean[bingoSize][bingoSize]; + + // 中央のセルを最初から開いているとしてマークする + if (bingoSize % 2 != 0) { // 奇数サイズのカードのみ(ビンゴでは一般的) + openedCells[bingoSize / 2][bingoSize / 2] = true; + } + } + + public int getBingoSize() { + return bingoSize; + } + + public int getDrawCount() { + return drawCount; + } + + public int getBingoNumbers(int y, int x) { + return bingoNumbers[y][x]; + } + + public int[] getDrawNumbers() { + return drawNumbers; + } + + public boolean isCellOpened(int y, int x) { + return openedCells[y][x]; + } + + public void openCell(int y, int x) { + openedCells[y][x] = true; + } + +} + +/** ロジック */ +final class BingoLogic { + private final BingoData data; + + public BingoLogic(BingoData data) { + this.data = data; + } + + public void markOpenedCells() { + int bingoSize = data.getBingoSize(); + int[] drawNumbers = data.getDrawNumbers(); + + for (int drawNum : drawNumbers) { + for (int i = 0; i < bingoSize; i++) { + for (int j = 0; j < bingoSize; j++) { + if (data.getBingoNumbers(i, j) == drawNum) { + data.openCell(i, j); + } + } + } + } + } +} + +/** ビンゴの数を数える */ +final class BingoCount { + private final BingoData data; + + public BingoCount(BingoData data) { + this.data = data; + } + + /** + * ビンゴ(行、列、対角線)の総数を計算します。 + * + * @return ビンゴの総数 + */ + public int calculateBingos() { + int totalBingos = 0; + totalBingos += countVerticalBingos(); + totalBingos += countHorizontalBingos(); + totalBingos += countDiagonalBingos(); + return totalBingos; + } + + // --- 縦のビンゴ --- + public int countVerticalBingos() { + int verticalBingos = 0; + int bingoSize = data.getBingoSize(); + + for (int col = 0; col < bingoSize; col++) { // 各列をチェック + boolean isBingo = true; + for (int row = 0; row < bingoSize; row++) { + if (!data.isCellOpened(row, col)) { // 1つでも開いていないセルがあればビンゴではない + isBingo = false; + break; + } + } + if (isBingo) { + verticalBingos++; + } + } + return verticalBingos; + } + + // --- 横のビンゴ --- + public int countHorizontalBingos() { + int horizontalBingos = 0; + int bingoSize = data.getBingoSize(); + + for (int row = 0; row < bingoSize; row++) { // 各行をチェック + boolean isBingo = true; + for (int col = 0; col < bingoSize; col++) { + if (!data.isCellOpened(row, col)) { // 1つでも開いていないセルがあればビンゴではない + isBingo = false; + break; + } + } + if (isBingo) { + horizontalBingos++; + } + } + return horizontalBingos; + } + + // --- 斜めのビンゴ --- + public int countDiagonalBingos() { + int diagonalBingos = 0; + int bingoSize = data.getBingoSize(); + + // メイン対角線(左上から右下)をチェック + boolean isMainDiagonalBingo = true; + for (int i = 0; i < bingoSize; i++) { + if (!data.isCellOpened(i, i)) { + isMainDiagonalBingo = false; + break; + } + } + if (isMainDiagonalBingo) { + diagonalBingos++; + } + + // 反対対角線(右上から左下)をチェック + boolean isAntiDiagonalBingo = true; + for (int i = 0; i < bingoSize; i++) { + if (!data.isCellOpened(i, bingoSize - 1 - i)) { + isAntiDiagonalBingo = false; + break; + } + } + if (isAntiDiagonalBingo) { + diagonalBingos++; + } + + return diagonalBingos; + } +} + +/** メイン */ +public class B145_bingo { + public static void main(String[] args) { + final Scanner sc = new Scanner(System.in); + final int bingoSize = sc.nextInt(); + final int drawCount = sc.nextInt(); + + final int[][] bingoNumbers = new int[bingoSize][bingoSize]; + for (int i = 0; i < bingoSize; i++) { + for (int j = 0; j < bingoSize; j++) { + + bingoNumbers[i][j] = sc.nextInt(); + } + } + + final int[] drawNumbers = new int[drawCount]; + for (int i = 0; i < drawCount; i++) { + drawNumbers[i] = sc.nextInt(); + } + + sc.close(); + + BingoData bingoData = new BingoData(bingoSize, drawCount, bingoNumbers, drawNumbers); + + BingoLogic bingoLogic = new BingoLogic(bingoData); + bingoLogic.markOpenedCells(); + + BingoCount bingoCount = new BingoCount(bingoData); + int totalBingos = bingoCount.calculateBingos(); + System.out.println(totalBingos); + + } +} diff --git a/rsuzuki/src/BingoTest.java b/rsuzuki/src/BingoTest.java new file mode 100644 index 0000000000000000000000000000000000000000..74f60a2af7d8f678b60ff1694b0cc9ce25b5a0a5 --- /dev/null +++ b/rsuzuki/src/BingoTest.java @@ -0,0 +1,103 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class BingoTest { + // BingoDataクラスのテスト + public static class BingoDataTest { + private BingoData bingoData; + private int[][] bingoNumbers; + private int[] drawNumbers; + private boolean[][] openCells; + + @Test + public void Getterで入力された値を取得できるか() { + bingoNumbers = new int[][] { { 13, 3, 9 }, { 8, 0, 2 }, { 16, 17, 15 } }; + drawNumbers = new int[] { 7, 14, 9, 10, 3, 13, 16, 8 }; + openCells = new boolean[3][3]; + bingoData = new BingoData(3, 8, bingoNumbers, drawNumbers); + assertEquals(3, bingoData.getBingoSize()); + assertEquals(8, bingoData.getDrawCount()); + assertEquals(13, bingoData.getBingoNumbers(0, 0)); + assertEquals(0, bingoData.getBingoNumbers(1, 1)); + assertArrayEquals(drawNumbers, bingoData.getDrawNumbers()); + + } + + } + + // BingoLogicクラスのテスト + public static class BingoLogicTest { + private BingoData bingoData; + private BingoLogic bingoLogic; + private int[][] bingoNumbers; + private int[] drawNumbers; + private boolean[][] openCells; + + @Test + public void BingoLogicクラスにおいてdrawNumbersと一致するbingoNumbersを開けているか() { + bingoNumbers = new int[][] { { 13, 3, 9 }, { 8, 0, 2 }, { 16, 17, 15 } }; + drawNumbers = new int[] { 7, 14, 9, 10, 3, 13, 16, 8 }; + openCells = new boolean[3][3]; + bingoData = new BingoData(3, 8, bingoNumbers, drawNumbers); + bingoLogic = new BingoLogic(bingoData); + + bingoLogic.markOpenedCells(); + assertTrue(bingoData.isCellOpened(0, 0)); + assertTrue(bingoData.isCellOpened(1, 0)); + assertTrue(bingoData.isCellOpened(0, 2)); + + assertFalse(bingoData.isCellOpened(1, 2)); + assertFalse(bingoData.isCellOpened(2, 2)); + } + + } + + // BingoCountクラスのテスト + public static class BingoCountTest { + private BingoData bingoData; + private BingoLogic bingoLogic; + private BingoCount bingoCount; + private int[][] bingoNumbers; + private int[] drawNumbers; + // private boolean[][] openCells; + + @Before + public void setup() { + bingoNumbers = new int[][] { { 13, 3, 9 }, { 8, 0, 2 }, { 16, 17, 15 } }; + drawNumbers = new int[] { 7, 14, 9, 10, 3, 13, 16, 8 }; + // openCells = new boolean[3][3]; + bingoData = new BingoData(3, 8, bingoNumbers, drawNumbers); + bingoLogic = new BingoLogic(bingoData); + bingoCount = new BingoCount(bingoData); + bingoLogic.markOpenedCells(); + } + + @Test + public void calculateBingosメソッドにおいて縦横斜めのビンゴした総数を出力できること() { + assertEquals(3, bingoCount.calculateBingos()); + } + + @Test + public void countVerticalBingosメソッドにおいて縦のビンゴした数を出力できること() { + assertEquals(1, bingoCount.countVerticalBingos()); + + } + + @Test + public void countHorizontalBingosメソッドにおいて横のビンゴした数を出力できること() { + assertEquals(1, bingoCount.countHorizontalBingos()); + + } + + @Test + public void countDiagonalBingosメソッドにおいて斜めのビンゴした数を出力できること() { + assertEquals(1, bingoCount.countDiagonalBingos()); + + } + + } +}