五子棋练习

import java.util.Arrays;
import java.util.Scanner;
public class lianxi2 {
    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];
    static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
    static String line = "十";
    static boolean flag=true;
    static Scanner ac=new Scanner(System.in);
    public static void main(String[] a) {
       lianxi2.chushi();
       lianxi2.dayin();
       lianxi2.start();
    }
    public static void chushi(){
        for(int q=0;q< qp.length;q++){
            for (int w=0;w< qp.length;w++){
                qp[q][w]=line;
                if(w==qp.length-1){
                    qp[q][w]=num[q];
                }
                if(q== qp.length-1){
                    qp[q][w]=num[w];
                }
            }
        }
    }
    public static void dayin(){
        for(int q=0;q< qp.length;q++){
            for (int w=0;w< qp.length;w++){
                System.out.print(qp[q][w]);
            }
            System.out.println();
        }
    }
    public static void start() {
       out:
        while (true) {
            if (flag==true) {
                System.out.println("请黑方输入行与列");
                int y = ac.nextInt() - 1;
                int x = ac.nextInt() - 1;
                boolean sd = lianxi2.check(x, y);
                if (sd == true) {
                    qp[y][x] = black;
                    lianxi2.dayin();
                    boolean cs = lianxi2.iswin(y,x,black);
                   if(cs==true){
                       System.out.println("黑方胜利");
                       break out;
                   }
                   else {
                    flag = false;}
                }else {
                    System.out.println("坐标有误,请重新输入");
                }
            } else {
                     System.out.println("请白方输入行和列");
                int y = ac.nextInt() - 1;
                int x = ac.nextInt() - 1;
                boolean sd = lianxi2.check(x, y);
                if (sd==true) {
                    qp[y][x] = white;
                    lianxi2.dayin();
                    if(lianxi2.iwin(y,x,white)){
                        System.out.println("白方胜利");
                        break out;
                    }
                    else {
                        flag = true;}
                } else {
                    System.out.println("坐标有误,请重新输入");
                }
            }
        }
    }
//判断
            public static boolean check ( int x, int y){
            if (x < 0 || x > qp.length - 1 || y < 0 || y > qp.length - 1) {
                return false;
            }
            if (!qp[y][x].equals(line)) {
                return false;
            }
            return true;
        }
        public static boolean iswin(int y,int x,String black) {
            int heng = 1,xie1=1,shu=1,xie2=1;
            for (int lf = x - 1; lf >= 0; lf--) {
                if (!qp[y][lf].equals(black)) break;
                else heng++;
            }
            for (int rg = x + 1; rg =0;sh--){
                if (!qp[sh][x].equals(black)) break;
                else shu++;
            }
            for(int xi=y+1;xi=0&&xwq>=0;xiq--,xwq--){
                if (!qp[xiq][xwq].equals(black)) break;
                else xie1++;
            }
            for(int xid=y+1,xwd=x+1;xid=0;xia++,xwa--){
                if (!qp[xia][xwa].equals(black)) break;
                else xie2++;
            }
            for(int xie=y-1,xwe=x+1;xie>=0&&xwe= 5 || xie1 >= 5 || xie2 >= 5 || shu >= 5;
    }
    public static boolean iwin(int y,int x,String white) {
        int heng = 1,xie1=1,shu=1,xie2=1;
        for (int lf = x - 1; lf >= 0; lf--) {
            if (!qp[y][lf].equals(white)) break;
            else heng++;
        }
        for (int rg = x + 1; rg < qp.length-1; rg++) {
            if (!qp[y][rg].equals(white)) break;
            else heng++;
        }
        for(int sh=y-1;sh>=0;sh--){
            if (!qp[sh][x].equals(white)) break;
            else shu++;
        }
        for(int xi=y+1;xi=0&&xwq>=0;xiq--,xwq--){
            if (!qp[xiq][xwq].equals(white)) break;
            else xie1++;
        }
        for(int xid=y+1,xwd=x+1;xid=0;xia++,xwa--){
            if (!qp[xia][xwa].equals(white)) break;
            else xie2++;
        }
        for(int xie=y-1,xwe=x+1;xie>=0&&xwe=5||xie1>=5||xie2>=5||shu>=5)
            return true;
        else return false;
    }
}

你可能感兴趣的:(java)