Java:数组实现五子棋

一、五子棋

import java.util.Scanner;

public class GoBang {
	
	public static void main(String[] args) {
		//棋盘长度
		int length = 20;
		
		//棋盘容器
		String[][] gobang = new String[length][length];
		
		//棋盘坐标
		String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
		
		//棋盘符号
		String add = "┼";
		String black = "●";
		String white = "○";
		
		//初始化棋盘
		for(int i = 0;i<gobang.length;i++){
			for(int j = 0;j<gobang[i].length;j++){
				if(j == length-1){//每行的最后一列,设置行数
					gobang[i][j] = nums[i];
				}else if(i == length-1){//最后一行,设置列数
					gobang[i][j] = nums[j];
				}else{
					gobang[i][j] = add;
				}
			}
		}
		
		//打印棋盘
		for(String[] strings : gobang){
			for(String str : strings){
				System.out.print(str);
			}
			System.out.println();
		}
		
		Scanner scan = new Scanner(System.in);
		boolean flag = true;//黑子-true 白字 - false
		
		while(true){
			//输入坐标
			System.out.println("请" + ((flag)?"黑":"白") + "子输入坐标:");
			int x = scan.nextInt() - 1;//-1是因为用户看到界面是坐标(从1开始),数组是下标(从0开始)
			int y = scan.nextInt() - 1;//-1是因为用户看到界面是坐标(从1开始),数组是下标(从0开始)
			
			if(x<0 || x>length-2 || y<0 || y>length-2){
				System.out.println("坐标错误,坐标超出棋盘范围,请重新输入");
				continue;
			}
			
			if(!gobang[x][y].equals(add)){
				System.out.println("坐标错误,坐标上已有棋子,请重新输入");
				continue;
			}
			
			//落子
			String piece  = (flag)?black:white;
			gobang[x][y] = piece;
			
			//打印棋盘
			for(String[] strings : gobang){
				for(String str : strings){
					System.out.print(str);
				}
				System.out.println();
			}
			
			//判断输赢
			boolean leftAndRight = leftAndRight(x, y, piece, gobang, length);//左右
			boolean topAndDown = topAndDown(x, y, piece, gobang, length);//上下
			boolean leftTopAndRightDown = leftTopAndRightDown(x, y, piece, gobang, length);//左上和左下
			boolean leftDownAndRightTop = leftDownAndRightTop(x, y, piece, gobang, length);//左下和右上
			
			if(leftAndRight || topAndDown || leftTopAndRightDown || leftDownAndRightTop){
				System.out.println(((flag)?"黑":"白") + "子赢");
				break;
			}
			
			
			
			//置返
			flag = !flag;
		}
		
	}
	public static boolean leftAndRight(int x,int y,String piece,String[][] gobang,int length){
		
		int count = 1;//落入的1子算1子
		
		//计算左边连续棋子
		int index = y;
		while(index>0){
			index--;
			if(gobang[x][index].equals(piece)){
				count ++;
			}else{
				break;
			}
		}
		
		//就算右边连续棋子
		index = y;
		while(index<length-2){
			index++;
			if(gobang[x][index].equals(piece)){
				count ++;
			}else{
				break;
			}
		}
	
		if(count>=5){
			return true;
		}
		return false;
	
	}
	
	public static boolean topAndDown(int x,int y,String piece,String[][] gobang,int length){
		int count = 1;
		
		
		//计算上方的连续棋子
		int index = x;
		while(index>0){
			index--;
			if(gobang[index][y].equals(piece)){
				count ++;
			}else{
				break;
			}
		}
		
		index = x;
		while(index<length-2){
			index++;
			if(gobang[index][y].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		if(count>=5){
			return true;
		}
		return false;
		
	}
	
	public static boolean leftTopAndRightDown(int x,int y,String piece,String[][] gobang,int length){
		int count = 1;
		
		
		//计算上方的连续棋子
		int index1 = x;
		int index2 = y;
		while(index1>0 && index2>0){
			index1--;
			index2--;
			if(gobang[index1][index2].equals(piece)){
				count ++;
			}else{
				break;
			}
		}
		
		index1 = x;
		index2 = y;
		while(index1<length-2 && index2<length-2){
			index1++;
			index2++;
			if(gobang[index1][index2].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		if(count>=5){
			return true;
		}
		return false;
		
	}
	
	public static boolean leftDownAndRightTop(int x,int y,String piece,String[][] gobang,int length){
		int count = 1;
		
		
		//计算上方的连续棋子
		int index1 = x;
		int index2 = y;
		while(index1<length-2 && index2>0){
			index1++;
			index2--;
			if(gobang[index1][index2].equals(piece)){
				count ++;
			}else{
				break;
			}
		}
		
		index1 = x;
		index2 = y;
		while(index1>0 && index2<length-2){
			index1--;
			index2++;
			if(gobang[index1][index2].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		if(count>=5){
			return true;
		}
		return false;
		
	}
	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值