五子棋完整代码

五子棋完整代码

public static void main(String[] args){
	//遍历棋盘
	int[][] game = new int[19][19];
	for(int i = 0;i<game.length;i++){
		for(int j = 0;j<game[i].length;j++){
			System.out.print(game[i][j]+" ");
		}
		System.out.println();
	}
	//输入黑子为奇数  白子为偶数;
	for(int k=0;k>=0;k++){
		if(k%2==0){    //判断为白子
			System.out.println("白棋落子:");
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入行");
			int row = scanner.nextInt();
			System.out.println("请输入列");
			int col = scanner.nextInt();
	        //重新落子
            while(game[row][col]!=0){
            System.out.println("此地已经落子,请重新落子:");
            int row1=scanner.nextInt();
            int col1=scanner.nextInt();
            row=row1;
            col=col1;
        }
			if(row>=0&row<game.length&col>=0&col<game[row].length){
				game[row][col]=2;
				for(int i = 0;i<game.length;i++){
					for(int j = 0;j<game[i].length;j++){
						System.out.print(game[i][j]+" ");
					}
				System.out.println();
				}
			}
	    }
		
		if(k%2!=0){   //判断为黑子
			System.out.println("黑棋落子:");
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入行");
			int row = scanner.nextInt();
			System.out.println("请输入列");
			int col = scanner.nextInt();
			//重新落子
            while(game[row][col]!=0){
            System.out.println("此地已经落子,请重新落子:");
            int row1=scanner.nextInt();
            int col1=scanner.nextInt();
            row=row1;
            col=col1;
        }
			if(row>=0&row<game.length&col>=0&col<game[row].length){
				game[row][col]=1;
				for(int i = 0;i<game.length;i++){
					for(int j = 0;j<game[i].length;j++){
						System.out.print(game[i][j]+" ");
					}
				System.out.println();
				}
			}
	    }
	    //判断输赢
		  for (int i = 4; i <15; i++) {
            for (int j = 0; j <game[i].length-4; j++) {
                if(game[i][j]==2&&game[i][j]==game[i+1][j]&&game[i][j]==game[i+2][j]&&game[i][j]==game[i+3][j]&&game[i][j]==game[i+4][j]
                ||game[i][j]==2&&game[i][j]==game[i+1][j+1]&&game[i][j]==game[i+2][j+2]&&game[i][j]==game[i+3][j+3]&&game[i][j]==game[i+4][j+4]
                ||game[i][j]==2&&game[i][j]==game[i][j+1]&&game[i][j]==game[i][j+2]&&game[i][j]==game[i][j+3]&&game[i][j]==game[i][j+4]
				||game[i][j]==2&&game[i][j]==game[i-1][j+1]&&game[i][j]==game[i-2][j+2]&&game[i][j]==game[i-3][j+3]&&game[i][j]==game[i-4][j+4] )
                {
                    System.out.println("白棋胜!");
                    return;
                }
                if(game[i][j]==1&&game[i][j]==game[i+1][j]&&game[i][j]==game[i+2][j]&&game[i][j]==game[i+3][j]&&game[i][j]==game[i+4][j]
                ||game[i][j]==1&&game[i][j]==game[i+1][j+1]&&game[i][j]==game[i+2][j+2]&&game[i][j]==game[i+3][j+3]&&game[i][j]==game[i+4][j+4]
                ||game[i][j]==1&&game[i][j]==game[i][j+1]&&game[i][j]==game[i][j+2]&&game[i][j]==game[i][j+3]&&game[i][j]==game[i][j+4]
				||game[i][j]==1&&game[i][j]==game[i-1][j+1]&&game[i][j]==game[i-2][j+2]&&game[i][j]==game[i-3][j+3]&&game[i][j]==game[i-4][j+4] )
                {
                    System.out.println("黑棋胜!");
                    return;
                }
            }
	    }
	}	
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值