paizaラ-ニング Bランクレベルアップメニュー五目並べ Java 解答

# STEP: 51 文字列の出力

## 問題

https://paiza.jp/works/mondai/prob60/tic_tac_toe_0

## 解答

```java:step51.java

import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            System.out.println(line);
        }
    }
}

```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/fca619e3-a6c4-ac1c-cf49-90320fc946a2.png)


 
# STEP: 52 五目並べ(1行)

## 問題
https://paiza.jp/works/mondai/prob60/tic_tac_toe_1

## 解答

```java:step52.java

import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] arrays = line.split("");
     
        String previous = arrays[0];

        for(int i = 1; i < arrays.length; i++ ){
            String next = arrays[i];
            String keep = previous;
 
            if(Objects.equals(next, keep) && !Objects.equals(next,".")){
                previous = next;
                if(i ==  arrays.length-  1){
                     System.out.println(previous);
                }
            }else{
               System.out.println("D");
               break;
            }
        }
        
        sc.close();
    }
}


```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/ccae0022-616c-61f3-c93e-724ee5ae4517.png)




# STEP: 53 五目並べ(横)

## 問題

https://paiza.jp/works/mondai/prob60/tic_tac_toe_2


## 解答

```java:step53.java
import java.util.*;


public class Main {
    private static final String X_LINE ="XXXXX";
    private static final String O_LINE ="OOOOO";
    private static final String X ="X";
    private static final String O ="O";
    private static final String D ="D";
     
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String judgement = D;
        
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            if(Objects.equals(line, X_LINE)){
                judgement = X;
                break;
            }else if(Objects.equals(line, O_LINE)){
                judgement = O;
                break;
            }
        }
        
        System.out.println(judgement);
        
        sc.close();
    }
}

```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/7a9ae43a-7c0e-fdc4-7e80-d8d6ac278679.png)


# STEP: 54 五目並べ（縦）

## 問題
https://paiza.jp/works/mondai/prob60/tic_tac_toe_3

## 解答

```java:step54.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[][] array = new String[5][5];

        int y = 0;
        
        // 2次元配列の作成
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] xArray = line.split("");
            array[y] = xArray;
            y++;
        }
        
        String result = "D";
        String previous = "";
        String next = "";
        for(int i = 0; i < 5; i ++){
            int count = 0;
            String keep = array[0][i];
            
            for(int j = 0; j < 5; j ++){
                // 縦から参照
                previous = keep;
                next = array[j][i];

                if(Objects.equals(previous, next) && !Objects.equals(previous, ".")){
                    if(count == 4){
                        result = next;
                        break;
                    }
                    keep = next;
                    count++;
                } else{
                    // 1回でも前と違う文字が出た場合は終了
                    break;  
                }
            }
            
            // 引き分け以外の判定だった場合は終了
            if(result!="D"){
               break;
            }
        }
        System.out.println(result);
        sc.close();
    }
}

```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/edd0661b-8dd7-314d-4fe4-523d3ae8cd58.png)


# STEP: 55 五目並べ（斜め）

## 問題
https://paiza.jp/works/mondai/prob60/tic_tac_toe_4

## 解答

```java:step55.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // 2次元配列で碁盤を作成
        String[][] array = new String[5][5];

        int y = 0;
        
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] xArray = line.split("");
            array[y] = xArray;
            y++;
        }
        
        
        String result = leftDiagonalSearch(array);
        if(result != "D"){
            System.out.println(result);
        }else{
            result = diagonalRightSearch(array);
            System.out.println(result);
        }
        
        sc.close();
    }
    
    /**
     * 左斜め探索
     * 
     * @param 碁盤
     * @result 結果
     * */
    public static String leftDiagonalSearch(String[][] array){
        String result = "D";
        String previous = "";
        String next = "";
        String keep = array[0][0];
        int count = 0;
        for(int i = 0; i < 5; i++){
            previous = keep;
            next = array[i][i];
            if(Objects.equals(previous, next) && !Objects.equals(previous, ".")){
                if(count == 4){
                    result = next;
                }
                keep = previous;
                count++;
            } else {
                break;
            }
        } 
        
        return result;
    }
    
    /**
     * 右斜め探索
     * 
     * @param 碁盤
     * @result 結果
     * */
    public static String diagonalRightSearch(String[][] array){
        String result = "D";
        String previous = "";
        String next = "";
        String keep = array[0][4];
        int count = 0;
        int y = 0;
        for(int x = 4; x >= 0; x--){
   
            previous = keep;
            next = array[x][y];
            if(Objects.equals(previous, next) && !Objects.equals(previous, ".")){
                if(count == 4){
                    result = next;
                }
                keep = previous;
                count++;
                y++;
            } else {
                break;
            }
        } 
        
        return result;
    }
    

}
```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/14db1801-8bb3-9418-e136-9d9f64a620d0.png)

# FINAL問題 五目並べ
## 問題
https://paiza.jp/works/mondai/prob60/tic_tac_toe_9

## 解答

```java:final.java
import java.util.*;

public class Main {
    
    // 碁盤のマスの数
    private static final int X_AXIS = 5;
    private static final int Y_AXIS = 5;
    
    // 駒など
    private static final String DRAW = "D";
    private static final String NO_PIECE= ".";

    
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        
        // 盤面作成
        String[][] board = new String[Y_AXIS][X_AXIS]; 
        
        int y = 0;
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] xArray = line.split("");
            board[y] = xArray;    
            y++;
        }

        String result = explore(board);
        System.out.println(result);
        // test();
     
        sc.close();
    }
    
    /**
     * 盤面を探索する関数
     * 横、縦、右斜め、左斜めを探索する
     * 
     * @param board 盤面
     * 
     * @return 結果 引き分けの場合、D。D以外の場合、勝利した駒の記号
     */
    public static String explore(String[][] board){
        
        String result = DRAW;
        
        // 横の探索
        result = searchSideways(board);

        if(Objects.equals(result,DRAW)){
            // 縦の探索
            result = verticalSearch(board);
        }
        
        if(Objects.equals(result,DRAW)){
            // 右斜めの探索をする関数
            result = searchDiagonallyRight(board);
        }
        
        if(Objects.equals(result,DRAW)){
            // 左斜めめの探索をする関数
            result = searchDiagonallyLeft(board);
        }
        
        return result;
   
    }
    
    /**
     * 横の探索をする関数
     * 
     * @param board 盤面
     * 
     * @return 結果 引き分けの場合、D。D以外の場合、勝利した駒の記号。
     * */
    public static String searchSideways(String[][] board){
        String result = DRAW;
     
        String keep = "";
        String previous = "";
        String next = "";
        for(int y = 0; y < Y_AXIS; y++){
            keep = board[y][0];
            for(int x = 0; x < X_AXIS; x++){
                previous = keep;
                next = board[y][x];
                if(Objects.equals(previous, next) && !Objects.equals(previous, NO_PIECE)) {
                
                    if(x == X_AXIS-1){
                        // 最後の列まで同じ記号だった場合、終了
                        result = next;
                        return result;
                    }
                    keep = next;
                } else {
                    // 1回でも違う駒か駒がないマスがでたら次の行を探索
                    break;
                }
            }
        }
     
        return result;
     
    }
    
        
    /**
     * 縦の探索をする関数
     * 
     * @param board 盤面
     * 
     * @return 結果 引き分けの場合、D。D以外の場合、勝利した駒の記号。
     * */
    public static String verticalSearch(String[][] board){
        String result = DRAW;
     
        String keep = "";
        String previous = "";
        String next = "";
        for(int x = 0; x < X_AXIS; x++){
            
            keep = board[0][x];
            
            for(int y = 0; y < Y_AXIS; y++){
                previous = keep;
                next = board[y][x];
                if(Objects.equals(previous, next) && !Objects.equals(previous, NO_PIECE)) {
                    if(y == Y_AXIS-1){
                        // 最後の行まで同じ記号だった場合、終了
                        result = next;
                        return result;
                    }
                    keep = next;
                } else {
                    // 1回でも違う駒か駒がないマスがでたら次の行を探索
                    break;
                }
            }
        }
        return result;   
    }

    /**
     * 右斜めの探索をする関数
     * 
     * @param board 盤面
     * 
     * @return 結果 引き分けの場合、D。D以外の場合、勝利した駒の記号。
     * */
    public static String searchDiagonallyRight(String[][] board){
        String result = DRAW;
     
        String keep = board[0][0];
        String previous = "";
        String next = "";

        for(int i = 0; i < X_AXIS; i++){
            previous = keep;
            next = board[i][i];

            if(Objects.equals(previous, next) && !Objects.equals(previous, NO_PIECE)) {
                
                if(i == X_AXIS-1){
                    // 最後の列まで同じ記号だった場合、終了
                    result = next;
                    return result;
                }
                keep = next;
            } else {
                // 1回でも違う駒か駒がないマスがでたら終了
                break;
            }
        }
     
        return result;
    }
    
    /**
     * 左斜めめの探索をする関数
     * 
     * @param board 盤面
     * 
     * @return 結果 引き分けの場合、D。D以外の場合、勝利した駒の記号。
     * */
    public static String searchDiagonallyLeft(String[][] board){
        String result = DRAW;
     
        String keep = board[0][X_AXIS-1];
        String previous = "";
        String next = "";
        int y = Y_AXIS - 1;

        for(int x = 0; x < X_AXIS; x++){
            previous = keep;
            next = board[x][y];

            if(Objects.equals(previous, next) && !Objects.equals(previous, NO_PIECE)) {
            
                if(x == X_AXIS-1){
                    // 最後の列まで同じ記号だった場合、終了
                    result = next;
                    return result;
                }
                keep = next;
                y--;
            } else {
                // 1回でも違う駒か駒がないマスがでたら終了
                break;
            }
        }
        
        return result;
    }

    
    private static String[][] test1 = {
        {"X", "X", "O", "X", "O"}, 
        {"O", "X", "O", "X", "X"}, 
        {"O", "O", "O", "O", "O"}, 
        {"O", "X", "O", "X", "."}, 
        {"X", "O", "X", "X", "O"}};

    private static String[][] test2 = {
        {"X", "X", "O", "X", "O"}, 
        {"O", "X", "O", "X", "X"}, 
        {".", "O", "X", "X", "O"}, 
        {"O", "X", "O", "O", "."}, 
        {"X", "X", "X", "X", "X"}};
        
    private static String[][] test3 = {
        {".", ".", ".", "X", "."}, 
        {".", ".", ".", "X", "."}, 
        {".", ".", ".", "X", "."}, 
        {".", ".", ".", "X", "."}, 
        {"X", "X", "X", "X", "X"}};
        
    private static String[][] test4 = {
        {"O", "O", "O", "O", "O"}, 
        {".", ".", ".", "X", "."}, 
        {".", ".", ".", "X", "."}, 
        {".", ".", ".", "X", "."}, 
        {".", ".", ".", "X", "."}};

    private static String[][] test5 = {
        {"O", ".", "O", "O", "O"}, 
        {".", "O", ".", "X", "."}, 
        {".", ".", "O", "X", "."}, 
        {".", ".", ".", "O", "."}, 
        {".", ".", ".", "X", "O"}};
        
    private static String[][] test6 = {
        {"O", "O", "O", "O", "X"}, 
        {".", "O", ".", "X", "."}, 
        {".", ".", "X", "X", "."}, 
        {".", "X", ".", "O", "."}, 
        {"X", ".", ".", "X", "O"}};

    private static void test(){
        String result = explore(test1);
        System.out.println("O:" + result);
        
        result = explore(test2);
        System.out.println("X:" + result);
        
        result = explore(test3);
        System.out.println("X:" + result);
        
        result = explore(test4);
        System.out.println("O:" + result);
        
        result = explore(test5);
        System.out.println("O:" + result);
        
        result = explore(test6);
        System.out.println("X:" + result);
    }

}

```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/a0f84d2b-c173-6194-7201-7b2c89d84856.png)