16×16大小棋盤的五子棋小程式 Java實現

helloworld370629發表於2020-12-08

16×16大小棋盤的五子棋小程式 Java實現

記錄一下每天的學習。

import java.util.Random;
import java.util.Scanner;

/**
 * @author hxf
 * * @date 2020/12/7
 */
public class Test {
    public static void main(String[] args) {
        Chessboard chessboard = new Chessboard();
        chessboard.offensive();
        while (true){
            chessboard.chess();
            chessboard.show();
            if (chessboard.check()){
                System.out.println(chessboard.player+"號選手獲勝");
                break;
            }
            chessboard.exchange();
        }
    }
}

class Chessboard{

    int size = 16;
    int[][] chessBoard = new int[size+1][size+1];
    int player1 = 1;
    int player2 = 2;
    int player;
    int row;
    int column;
    int number = 5;
    int count = 0;

    Scanner in = new Scanner(System.in);

    Chessboard(){
        for (int i = 0; i < chessBoard.length; i++){
            chessBoard[i][0] = i;
            chessBoard[0][i] = i;
        }
    }

    void show(){
        for (int[] row: chessBoard) {
            for (int column: row) {
                System.out.print(column+"\t");
            }
            System.out.println();
        }
    }

    void offensive(){
        Random random = new Random();
        if (random.nextBoolean()){
            player = player1;
        }else {
            player = player2;
        }
        System.out.println(player+"號先行");
    }

    void chess(){
        System.out.println(player+"號請落子:(先輸入行,在輸入列,空格隔開,數值為1-16)");
        row = in.nextInt();
        column = in.nextInt();
        chessBoard[row][column] = player;
    }

    void exchange(){
        if (player == player2){
            player = player1;
        }else{
            player = player2;
        }
    }

    boolean checkRow(){
        count = 0;
        for (int i = Math.max(1, column-number+1); i <= Math.min(size, column+number-1); i++){
            if (chessBoard[row][i] == player){
                count++;
                if (count == number){
                    return true;
                }
            }else{
                count = 0;
            }
        }
        return false;
    }

    boolean checkColumn(){
        count = 0;
        for (int i = Math.max(1, row-number+1); i <= Math.min(size, row+number-1); i++){
            if (chessBoard[i][column] == player){
                count++;
                if (count == number){
                    return true;
                }
            }else{
                count = 0;
            }
        }
        return false;
    }

    boolean checkLeftward(){
        count = 0;
        int temp;
        for (int i = Math.max(1, row-number+1); i <= Math.min(size, row+number-1); i++){
            temp = column - row + i;
            if (temp <1 || temp > 16){
                continue;
            }
            if (chessBoard[i][temp] == player){
                count++;
                if (count == number){
                    return true;
                }
            }else{
                count = 0;
            }
        }
        return false;
    }

    boolean checkRightward(){
        count = 0;
        int temp;
        for (int i = Math.max(1, row-number+1); i <= Math.min(size, row+number-1); i++){
            temp = column + row - i;
            if (temp <1 || temp > 16){
                continue;
            }
            if (chessBoard[i][temp] == player){
                count++;
                if (count == number){
                    return true;
                }
            }else{
                count = 0;
            }
        }
        return false;
    }

    boolean check(){
        return checkRow() || checkColumn() || checkLeftward() || checkRightward();
    }

}

相關文章