Java控制台版五子棋

Java控制台版五子棋

设计一个10*10的棋盘:
行号、列号单独输出

package yu;

import java.util.Scanner;

public class WuZiQi {
    
    
	/*●  棋子1
    ○  棋子2
	 * 
	 */
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
  String [] [] qipan=new String [10] [10];
   //初始化棋盘:
   for(int k=0;k<qipan.length;k++){
    
    
  	 for(int q=0;q<qipan[k].length;q++){
    
    
  		 qipan[k][q]="+ ";
  		 }
   }
   //输出棋盘:
   System.out.print("  ");
   for(int i=0;i<10;i++){
    
    
  	 System.out.print(i+" ");
   }
   System.out.println();
   for(int k=0;k<qipan.length;k++){
    
    
  	 System.out.print(k+" ");
  	 for(int q=0;q<qipan[k].length;q++){
    
    
  		  System.out.print(qipan[k][q]);
  		 }
  	 System.out.println();
   }

输入坐标下棋(x,y),并作容错处理:

  1. 保证输入的坐标是(x,y);
  2. 下标越界处理;
  3. 判断此坐标有无棋子;
  4. 确保坐标输入为数字。
int x,y;//储存下棋坐标:
   Scanner sc=new Scanner(System.in);
   boolean flag=true;//区分黑白棋;
   while(true){
    
    
   System.out.println("请输入坐标下棋,坐标格式(x,y)");
   String str=sc.nextLine();
   String [] str1=str.split(",");
     //容错处理1
     if(str1.length!=2){
    
    
  	   System.out.println("坐标输入错误,请重新输入!!");
  	  
     }else{
    
    
     //容错处理3
  	   try{
    
    
  		   x=Integer.parseInt(str1[0]);
             y=Integer.parseInt(str1[1]);
  	   }catch(Exception e){
    
    
  		   System.out.println("坐标输入错误,请重新输入!!");
  		   continue;
  	   }
  	   //容错处理2--下标越界
  	   if(x>=10||y>=10){
    
    
  		   System.out.println("坐标输入错误,请重新输入!!");
  	   }else{
    
    
  		  //容错处理--判断当前位置是否有棋子:
  		    //黑白棋:
  		   if(qipan[x][y].equals("+ ")){
    
    
  			   if(flag){
    
    
  				   qipan[x][y]="● ";
  			   }else{
    
    
  				   qipan[x][y]="○ ";
  			   }
  			   flag=!flag;
  		   }else{
    
    
  			   System.out.println("当前位置已有棋子,请重新输入坐标!!");
  			   continue;
  		   }
  		   
  		 //输出棋盘:
  		     System.out.print("  ");
  		     for(int i=0;i<10;i++){
    
    
  		    	 System.out.print(i+" ");
  		     }
  		     System.out.println();
  		     for(int k=0;k<qipan.length;k++){
    
    
  		    	 System.out.print(k+" ");
  		    	 for(int q=0;q<qipan[k].length;q++){
    
    
  		    		  System.out.print(qipan[k][q]);
  		    		 }
  		    	 System.out.println();
  		     
  		     }

判断是否五子连珠:
8个方向,4条线

  1. 上方&下方
  2. 左方&右方
  3. 左斜上&右斜下
  4. 右斜上&左斜下
//判断是否五子连珠:
  		     int count=1;
  		     String currentZiQi=qipan[x][y];//储存当前下的棋子;
  		   //判断上方:
  		     for(int k=x-1;k>=0;k--){
    
    
  		    	 if(qipan[k][y].equals(currentZiQi)){
    
    
  		    		 count++;
  		    	 }else{
    
    
  		    		 break;
  		    	 }
  		    	  }
  		     if(count>=5){
    
    
  		    	System.out.println(currentZiQi+"获胜!!!");
  		    	break;
  		     }
  		   //判断下方:
  		    for(int k=x+1;k<10;k++){
    
    
      		    if(qipan[k][y].equals(currentZiQi)){
    
    
      		       count++;
      		    }else{
    
    
      		       break;
      		    }
      		  }
      		if(count>=5){
    
    
      		   System.out.println(currentZiQi+"获胜!!!");
      		   break;
  		           }
      		count=1;//重置count;
      	  //判断左边:
     		for(int k=y-1;k>=0;k--){
    
    
      		    if(qipan[x][k].equals(currentZiQi)){
    
    
      		       count++;
      		    }else{
    
    
      		       break;
      		    }
      		  }
      		if(count>=5){
    
    
      		   System.out.println(currentZiQi+"获胜!!!");
      		   break;
  		           }
      	   //判断右边:
      		for(int k=y+1;k<10;k++){
    
    
      		    if(qipan[x][k].equals(currentZiQi)){
    
    
      		       count++;
      		    }else{
    
    
      		       break;
      		    }
      		  }
      		if(count>=5){
    
    
      		   System.out.println(currentZiQi+"获胜!!!");
      		   break;
  		           }
      		count=1;      	
      		//判断左上斜边:
      		for(int k=x-1,j=y-1;k>=0&&j>=0;k--,j--){
    
    
      			if(qipan[k][j].equals(currentZiQi)){
    
    
       		       count++;
       		    }else{
    
    
       		       break;
       		    }
                 		  }
       		if(count>=5){
    
    
       		   System.out.println(currentZiQi+"获胜!!!");
       		   break;
   		           }
       		//右下斜方:
       		for(int k=x+1,j=y+1;k<10&&j<10;k++,j++){
    
    
      			if(qipan[k][j].equals(currentZiQi)){
    
    
       		       count++;
       		    }else{
    
    
       		       break;
       		    }
       		  }
       		if(count>=5){
    
    
       		   System.out.println(currentZiQi+"获胜!!!");
       		   break;
   		           }
       		count=1;
       		//左下斜方:
       		for(int k=x-1,j=y+1;k>=0&&j<10;k--,j++){
    
    
      			if(qipan[k][j].equals(currentZiQi)){
    
    
       		       count++;
       		    }else{
    
    
       		       break;
       		    }
       		  }
       		if(count>=5){
    
    
       		   System.out.println(currentZiQi+"获胜!!!");
       		   break;
   		           }       		
       		//右上斜方:
       		for(int k=x+1,j=y-1;k<10&&j>=0;k++,j--){
    
    
      			if(qipan[k][j].equals(currentZiQi)){
    
    
       		       count++;
       		    }else{
    
    
       		       break;
       		    }
       		  }
      		if(count>=5){
    
    
       		   System.out.println(currentZiQi+"获胜!!!");
      		   break;
   		           }
       		count=1;
      		}
  	   }
  	  
  	   
     }  
   }
   }

猜你喜欢

转载自blog.csdn.net/m0_53246877/article/details/113333770