Java五子棋

import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		
		//棋盘长度
		int length = 20;
		//棋盘容器
		String[][] goBang = new String[length][length];
		//棋盘符号
		String add = "┼";
		String white = "○";
		String black = "●";
		String[] indexs = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
		
		
		//初始化棋盘
		for (int i = 0; i < goBang.length; i++) {
			for (int j = 0; j < goBang[i].length; j++) {
				
				if(i == length-1){//最后一行
					goBang[i][j] = indexs[j];
					
				}else if(j == length-1){//每行的最后一列
					goBang[i][j] =indexs[i];
					
				}else{
					goBang[i][j] = add;
				}
			}
		}
		
		//打印棋盘
		for (String[] ss : goBang) {
			for (String element : ss) {
				System.out.print(element);
			}
			System.out.println();
		}
		
		
		Scanner scan = new Scanner(System.in);
		//棋子
		boolean bool = true;//true-黑子 false-白子
		
		boolean flag = true;//赢后可以设置为false
		while(flag){
			//输入坐标
			System.out.println("请" + ((bool)?"黑":"白") + "子输入坐标:");
			int x = scan.nextInt()-1;
			int y = scan.nextInt()-1;
			
			//判断下标是否越界
			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 = (bool)?black:white;
			goBang[x][y] = piece;
			
			//打印棋盘
			for (String[] ss : goBang) {
				for (String element : ss) {
					System.out.print(element);
				}
				System.out.println();
			}
			
			//判断输赢
			if(method01(x, y, piece, length, goBang) + method02(x, y,piece, length, goBang) + 1 >= 5){
				System.out.println(((bool)?"黑":"白") + "赢得比赛");
				flag = false;
			}
            if(method03(x, y, piece, length, goBang) + method04(x, y,piece, length, goBang) + 1 >= 5){
				System.out.println(((bool)?"黑":"白") + "赢得比赛");
				flag = false;
			}
			//判断输赢
			if(method05(x, y, piece, length, goBang) + method06(x, y,piece, length, goBang) + 1 >= 5){
				System.out.println(((bool)?"黑":"白") + "赢得比赛");
				flag = false;
			}
            if(method07(x, y, piece, length, goBang) + method08(x, y,piece, length, goBang) + 1 >= 5){
				System.out.println(((bool)?"黑":"白") + "赢得比赛");
				flag = false;
			}
			//置反
			bool = !bool;
		}
		
		scan.close();		
	}
	
	//→ (12,13)
	public static int method01(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		
		while(y < length-1){
			y++;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
		return count;
	}
	
	//← (12,13)  (12,12)
	public static int method02(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		
		while(y > 0){
			y--;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
		return count;
	}
	//↓
    public static int method03(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		
		while(x < length-1){
			x++;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
        return count;
    }
    //↑
    public static int method04(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		while(x > 0){
			x--;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
        return count;  
    }
    //↖
    public static int method05(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		while(x > 0&&y > 0){
			x--;
            y--;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
        return count;  
    }
    //↘
    public static int method06(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		while(x < length-1 && y < length-1){
			x++;
            y++;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
        return count;  
    }

    //↙
    public static int method07(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		while(x > 0 && y < length-1){
			x--;
            y++;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
        return count;  
    }
 
    //↗
    public static int method08(int x,int y,String piece,int length,String[][] goBang){
		int count = 0;
		while(x < length-1 && y >0 ){
			x++;
            y--;
			if(goBang[x][y].equals(piece)){
				count++;
			}else{
				return count;
			}
		}
        return count;  
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值