package java111;

import io.zzax.jadeite.console.Console;

public class Game {

    int[][] board;
    int size = 15;

    public static void main(String[] args) {
        new Game().run();
    }

    public void run() {
        //  获取玩家名称
        String[] names = readNames();

        //  创建棋盘
        size = 15;
        board = new int[size][size];

        int turn = 0; // 0 black, 1 white
        while (true) {
            //  打印棋盘
            printBoard();

            //  叫玩家下棋
            String name = names[turn];
            Console.println("该 " + name + " 下棋了");

            //  获取坐标信息
            int[] location = readLocation();

            //  下棋
            int x = location[0];
            int y = location[1];
            board[y][x] = turn == 0 ? 1 : 2;

            //  检测输赢
            if (overFiveAt(x, y)) {
                break;
            }

            //  切换到下一个玩家
            turn += 1;
            turn %= 2;
        }

        //  打印棋盘
        printBoard();

        //  显示结果
        String name = names[turn];
        Console.println(name + " 赢了");

    }

    public String[] readNames() {
        Console.println("输入黑棋昵称");
        String nameBlack = Console.readLine();
        Console.println("输入白棋昵称");
        String nameWhite = Console.readLine();
        String[] names = {nameBlack, nameWhite};
        return names;
    }

    public void printBoard() {
        Console.print(String.format("%-3s", ""));
        for (int x = 0; x < size; x++) {
            int colNumber = x + 1;
            Console.print(String.format("%-3d", colNumber));
        }
        Console.println();

        for (int y = 0; y < size; y++) {
            char rowCode = (char)('A' + y);
            Console.print(String.format("%-3s", rowCode));

            for (int x = 0; x < size; x++) {
                int value = board[y][x];
                String[] icons = {".", "x", "o"};
                String icon = icons[value];
                Console.print(String.format("%-3s", icon));
            }

            Console.println();
        }
    }

    public int[] readLocation() {
        int y;
        int x;
        while (true) {
            String locationString = Console.readLine();

            char yCode = locationString.charAt(0);
            y = yCode - 'A';
            String xString = locationString.substring(1);
            x = Integer.parseInt(xString) - 1;

            if (x < 0 || x > size || y < 0 || y > size) {
                Console.println("坐标超出棋盘区域，请重新输入");
                continue;
            }
            if (board[y][x] != 0) {
                Console.println("该位置已有棋子，请重新输入");
                continue;
            }
            break;
        }
        int[] location = {x, y};
        return location;
    }


    boolean overFiveAt(int x, int y) {
        int[][] directions = {{0, -1}, {1, -1}, {1, 0}, {1, 1}};
        for (int i = 0; i < directions.length; i++) {
            int[] direction = directions[i];
            if (overFiveByDirection(x, y, direction[0], direction[1])) {
                return true;
            }
        }
        return false;
    }

    boolean overFiveByDirection(int x, int y, int dx, int dy) {
        int count = 0;
        count += countOnDirection(x, y, dx, dy);
        count += countOnDirection(x, y, -dx, -dy);
        count --;
        return count >= 5;
    }

    int countOnDirection(int x, int y, int dx, int dy) {
        int referenceToken = get(x, y);
        int count = 1;
        while (true) {
            x += dx;
            y += dy;
            if (!inBoard(x, y)) {
                break;
            }
            int token = get(x, y);
            if (token == referenceToken) {
                count ++;
            } else {
                break;
            }
        }
        return count;
    }

    int get(int x, int y) {
        return board[y][x];
    }

    boolean inBoard(int x, int y) {
        return x >= 0 && x < size && y >= 0 && y < size;
    }
}
