0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

いびつなリバーシ対戦 Javaで

Posted at

ぬるりと解けてしまった。
斜めでドはまりしたが、一度お手本を理解してしまえば、新しく要求される機能を実装していくだけで特に苦労なし。
メソッドを分けると「あ、なんかそれっぽいことやってる」気分になる……かもしれない。

import java.util.Scanner;


public class Main {
    static int h, w, n, t;
    static String[][] map;

    public static void cul(int y, int x, String AB){
        //_____縦_____
        for(int i=-1; i<=1; i+=2){
            for(int j=1; ; j++){

                if(y+(i*j) == -1 || y+(i*j) == h || map[y+(i*j)][x].equals("#")){
                    break;
                }

                if(map[y+(i*j)][x].equals(AB)){
                    for(int k = Math.min(y+(i*j), y); k <= Math.max(y+(i*j), y); k++){
                        map[k][x] = AB;
                    }
                    break;
                }
            }
        }

        //_____横_____
        for(int i=-1; i <= 1; i+=2){
            for(int j=1; ; j++){

                if(x+(i*j) == -1 || x+(i*j) == w || map[y][x+i*j].equals("#")){
                    break;
                }

                if(map[y][x+(i*j)].equals(AB)){
                    for(int k = Math.min(x+(i*j), x); k <= Math.max(x+(i*j), x); k++){
                        map[y][k] = AB;
                    }
                    break;
                }
            }
        }

        //_____斜め_____
        for(int i=-1; i <= 1; i+=2){
            for(int j=1; ; j++){

                if(x+(i*j) == -1 || x+(i*j) == w || y+(i*j) == -1 || y+(i*j) == h || map[y+(i*j)][x+(i*j)].equals("#")){
                    break;
                }

                if(map[y+(i*j)][x+(i*j)].equals(AB)){
                    for(int k = Math.min(y+(i*j), y); k <= Math.max(y+(i*j), y); k++){
                        map[k][k-y+x] = AB;
                    }
                    break;
                }
            }
        }

        for(int i=-1; i <= 1; i+=2){
            for(int j=1; ; j++){

                if(x+(i*j) == -1 || x+(i*j) == w || y-(i*j) == -1 || y-(i*j) == h || map[y-(i*j)][x+(i*j)].equals("#")){
                    break;
                }

                if(map[y-(i*j)][x+(i*j)].equals(AB)){
                    for(int k = Math.min(x+(i*j), x); k <= Math.max(x+(i*j), x); k++){
                        map[y+x-k][k] = AB;
                    }
                    break;
                }
            }
        }
        map[y][x] = AB;
    }


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

        h = sc.nextInt();
        w = sc.nextInt();
        n = sc.nextInt();   //人数
        t = sc.nextInt();   //合計ターン数

        map = new String[h][w];

        for(int i=0; i<h; i++){
            String temp = sc.next();
            for(int j=0; j<w; j++){
                map[i][j] = String.valueOf(temp.charAt(j));
            }
        }
        //_____ここまで初期化______

        for(int i=0; i<t; i++){
            String p = sc.next();
            int b = sc.nextInt();
            int a = sc.nextInt();

            cul(b, a, p);
        }

        // _____ここから表示_______
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                System.out.print(map[i][j]);
            }
            System.out.println("");
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?