java版-五子棋-个人-源码参考

package wuziqi;

importjava.io.IOException;

importjava.util.*; 

public class Wuziqi{

   public static void main(String args[]) {

          //五子棋的声明

          int[][] wuziqi=new int[16][16];

          //打印布局,并检查是否有误

          xunhun(true,0,0,0,wuziqi);

          Scanner in=new Scanner(System.in); //使用Scanner类定义对象

          System.out.println("\t\t\t欢迎来到下棋中心(人机对战马上开始)");

          System.out.println();

          //选择下棋对象

          while (true) {

              System.out.println("请选择下棋对象作为起始者:1:人类,2:电脑 ");

              String str = null;

              str = in.nextLine();

              try {

                 char a =(char)(Integer.parseInt(str)+'0');

                 System.out.println(a);     

                

                 if(a == '1' || a == '2'){

                    //下棋开始

                    start(a,wuziqi);

                 }

              } catch (NumberFormatException e){

                 System.out.println("输入格式有误,请输入 1:人类,2:电脑 ");

              }

          }

       }

   //下棋开始

   public static void start(char ab,intwuziqi[][]){

          if (ab == '1'){

              while(true){

                 //人类下棋

              people(wuziqi);

              //电脑下棋

              computer(wuziqi);

              }

             

          }

          if (ab == '2'){

              while(true){

                 //电脑下棋

              computer(wuziqi);

              //人类下棋

              people(wuziqi);

              }

          }

       }

   //电脑下棋

   public static void computer(int wuziqi[][]){

          int x=1+(int)(Math.random()*15);

          int y=1+(int)(Math.random()*15);

          System.out.println("电脑下棋坐标为:("+ x + "," + y + ")");

          //打印布局,并检查是否有误

          xunhun(false, 2, x, y, wuziqi);

          //检查是否获胜

          checkWin(wuziqi, 66);

       }

   // 人类下棋

       public static void people(int wuziqi[][]){

          int[] cun = new int[2];

          Scanner in = new Scanner(System.in);

          while (true) {

              System.out.println("人类下棋:输入横坐标,按回车键");

              String str = null;

              str = in.nextLine();

              try {

                 int x = Integer.parseInt(str);

                 System.out.println(x);     

                 if (x >0 && x <16) {

                     cun[0] = x;

                     break;

                 }

              } catch (NumberFormatException e){

                 System.out.println("输入格式有误,:输入横坐标,按回车键");

              }

          }

          while (true) {

              System.out.println("请输入纵坐标:");

              String str = null;

              str = in.nextLine();

              try {

                 int y = Integer.parseInt(str);

                 System.out.println(y);     

                

                 if (y >0 && y <16) {

                     cun[1] = y;

                     break;

                 }

              } catch (NumberFormatException e){

                 System.out.println("输入格式有误,请输入纵坐标:");

              }

          }

          // 打印布局,并检查是否有误

          xunhun(false, 1, cun[0], cun[1], wuziqi);

          // 检查是否获胜

          checkWin(wuziqi, 65);

       }

   //检查是否胜利

   public static void checkWin(int a[][],int ab){

       int i,j,tatol,k,g;

          for (i = 0; i < 16; i++) {

              for (j = 0; j < 16; j++) {

                 if (a[i][j] == ab){

                     tatol = 0;

                     //这是从第一个开始横向检查是否满足五步

                     for (k = j; k < 16; k++){

                        if (a[i][k] == ab) {

                            tatol++;

                            if (tatol == 5) {

                               System.out.println("Congratulation!You Winning!!!");

                               System.exit(0);

                            }

                        } else {

                            break;

                        }

                     }

                     tatol = 0;

                     //这是从第一个开始纵向检查是否满足五步

                     for (k = i; k < 16; k++){

                        if (a[k][j] == ab) {

                            tatol++;

                            if (tatol == 5) {

                               System.out.println("Congratulation!You Winning!!!");

                               System.exit(0);

                            }

                        } else {

                            break;

                        }

                     }

                     tatol = 0;

                     //这是从第一个开始右向下检查是否满足五步

                     for (g = i; g < 16; g++){

                        if (a[g][j] == ab) {

                            tatol++;

                            if (tatol == 5) {

                               System.out.println("Congratulation!You Winning!!!");

                               System.exit(0);

                            }

                            j++;

                            if (j > 15){

                               break;

                            }

                        } else {

                            break;

                        }

                     }

                     tatol = 0;

                     //这是从第一个开始左向下检查是否满足五步

                     for (g = j; g > 0; g--){

                        if (a[j-g+1][g] == ab) {

                            tatol++;

                            if(tatol == 5){

                               System.out.println("Congratulation!You Winning!!!");

                               System.exit(0);

                            }

                        } else {

                            break;

                        }

                     }

                 }

              }

          }

       }

   //打印布局,并检查是否有误

   public static void xunhun(boolean b,int a,intz,int h,int[][] wuziqi){

          int i, j;

          while(b){

              //第一次赋值

              for (i = 0; i < 16; i++) {

                 for( j = 0; j < 16; j++ ){

                     wuziqi[i][j] = 43;

                 }

              }

              break;

          }

          //判断是否位置重复

          if (wuziqi[z][h]==65 || wuziqi[z][h]==66){

              System.out.println("输入位置重复,程序已终止!!!");

              System.exit(0);

          }

          if (a == 1) {

              //赋值

              wuziqi[z][h] = 65;

          }

          if(a == 2 ){

              //赋值

              wuziqi[z][h] = 66;

          }

          //第一次打印布局

          for (i = 0; i < 16; i++ ){

              for (j = 0; j < 16; j++) {

                 if (i == 0 ){

                     System.out.print(j +"   ");

                 } else {

                     if (j >= 10 ){

                        System.out.printf("%c    ",wuziqi[i][j]);

                     } else {

                        if (j == 0) {

                            if (i >= 10 ){

                               System.out.printf("%d  ",i);

                            } else {

                               System.out.printf("%d   ",i);

                            }

                        } else {

                            System.out.printf("%c   ",wuziqi[i][j]);

                        }

                     }

                 }

              }

              System.out.println();

          }

          System.out.println();

       }

}  

猜你喜欢

转载自blog.csdn.net/zhichunqi/article/details/79940131