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();
 }//加入Java开发交流君样:756584822一起吹水聊天

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

保证输入的坐标是(x,y);
下标越界处理;
判断此坐标有无棋子;
确保坐标输入为数字。

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]="○ ";
             }//加入Java开发交流君样:756584822一起吹水聊天
             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条线

上方&下方
左方&右方
左斜上&右斜下
右斜上&左斜下

//判断是否五子连珠:
          int count=1;
          String currentZiQi=qipan[x][y];//储存当前下的棋子;
         //判断上方://加入Java开发交流君样:756584822一起吹水聊天
          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;
         }
         }//加入Java开发交流君样:756584822一起吹水聊天
        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;
        }
     }
      
      
  } 
 }
 }

[image
最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:756584822

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值