for循环嵌套循环、break、continue、二维数组综合运用之五指棋(自下版)

本文介绍了一个用Java编写的黑白棋游戏程序,包括棋盘初始化、用户输入坐标落子、判断输赢以及左右连续棋子计数的函数。
摘要由CSDN通过智能技术生成

package com.aoduoyun.day08;
import java.util.Scanner;
public class Chess {
  public static void main(String[] args){
      //期盼长度
    int len = 20;
    //棋盘容器
    String[][] chess = new String[len][len];
    
    //期盼符号
    String[] number = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
    String add = "┼";
    String black = "●";
    String white = "○";
    
    //初始化棋盘数据
    for(int i = 0;i<chess.length;i++){
        for(int j = 0; j<chess[i].length;j++){
            //每一行的最后一列
            if(j == len -1){
                chess[i][j]= number[i];
            }else if(i == len-1){
                chess[i][j] = number[j];
            }else{
                chess[i][j] = add;
            }
        }
    }
    Scanner scan = new Scanner(System.in);
    //白子和黑子
    boolean flag = true; //白子
    boolean bool = true; //判断状态
    //打印棋盘
            for(int i = 0; i<chess.length;i++){
                for(int j = 0;j<chess[i].length;j++){
                    System.out.print(chess[i][j]);
                }
                System.out.println();
            }
    //当落子时
    while(bool){
        System.out.print("请"+((flag)?"黑":"白")+"子输入坐标");
        //确定坐标点
        int x  = scan.nextInt()-1;
        int y = scan.nextInt()-1;
        //判断坐标点是否超出棋盘范围
        if(x<0|| x>len-2|| y<0||y>len-2){
            System.out.print("坐标超出棋盘范围请从新输入");
            continue;
            
        }
        
        //判断坐标上是否有棋子
        if(!chess[x][y].equals(add)){
            System.out.println("坐标上已经有棋子,请从新输入");
             continue;    
        }
        
        //落子
        String pieces = (flag)?black:white;
        chess[x][y] = pieces;
        
        
        
        
        //判断输赢
           int  count = right(chess,x,y,pieces)+left(chess,x,y,pieces+1);
           if(count>=5){
               System.out.println(((flag)?black:white)+"方赢");
               bool = false;
           }
           
         //打印棋盘
            for(int i = 0; i<chess.length;i++){
                for(int j = 0;j<chess[i].length;j++){
                    System.out.print(chess[i][j]);
                }
                System.out.println();
            }
           
           flag = !flag;
           
           
        
    }
    
       
    
    
  }

//判断落子左边连续的棋子数
    public static int left(String[][] goBang,int x,int y,String pieces){
        
        int count = 0;
        
        while(y>0){
            y--;
            if(goBang[x][y].equals(pieces)){
                count++;
            }else{
                break;
            }
            
        }
        return count;
    }
public static int right(String[][] chess,int x,int y,String pieces){
    int count =0;
    while(y<18){
        y++;
        if(chess[x][y].equals(pieces)){
            count++;
        }else{
            break;
        }
    }
    return count;
 }

}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值