java小游戏之控制台下五子棋!

import java.util.Scanner;
public class Qipan {
	static char[][] chessBoard = new char[16][16];
	//当isBlack为true时,黑方下子
	//O表示黑方,#表示白方
	static boolean isBlack =true;
    public static void main(String[] args){   
    	start();  	
    	print();
    	Scanner c = new Scanner(System.in);
    	while(true){
    		if(isBlack){
    			System.out.println("请黑方下子");
    		}else{
    			System.out.println("请白方下子");
    		}
    		//获取输入的横纵坐标
    		int i = c.nextInt();
    		int j = c.nextInt();
    		if(chessBoard[i][j] !='*'){
    			System.out.println("该位置已经有棋子,请重新输入");
    		}else{
    			if(isBlack){
    				chessBoard[i][j] = 'O';
    				//判断输赢的方法
    				if(isWin(i,j)){
    					System.out.println("黑方胜利!");
    					break;
    				}
    			}else{
    				chessBoard[i][j] = '#';
    				if(isWin(i,j)){
    					System.out.println("白方胜利!");
    					break;
    				}
    			}
    			isBlack = !isBlack;
    			print();
    		}
    	}
    }
    public static void start(){
    	for(int i=0;i<chessBoard.length;i++){
     	   for(int j=0;j<chessBoard[i].length;j++){
     		   chessBoard[i][j]='*';
     	   }
  		 
     	}    
    }
    public static void print(){
    	char[] c = {'0','1','2','3','4','5','6','7','8','9'};        	
    	System.out.print(" ");
    	for(int i=0;i<c.length;i++){
    		System.out.print(c[i]);
    	}
    		System.out.println();        	              	   
   	        for(int i =0;i<10;i++){
    		      System.out.print(c[i]);
			for(int j =0;j<10;j++){
				System.out.print(chessBoard[i][j]);
			}
			//换行
			System.out.println();
    	   }
    	
    }
    //判断输赢的总方法
    public static boolean isWin(int i,int j){
    	return isLeftAndRught(i,j) || 
    	       isUpAndDown(i,j) || 
    	       isLeftUpAndRightDown(i,j)|| 
    	       isRightUpAndLeftDown(i,j);
    }
    //判断左右方向的输赢
    public static boolean isLeftAndRught(int i ,int j){
    	int j1 = j-1;
    	while(true){
    		if(j1<0||chessBoard[i][j1] != chessBoard[i][j]){
    		break;
    	}else{
    		j1--;
    	}
    	}
    	j1++;
    	int count = 0;
    	while(true){
    		if(j1>=16||chessBoard[i][j1] != chessBoard[i][j]){
    		break;
    	}else{
    		count++;
    		j1++;
    	}
 }
    	return count >= 5; 
    	
    	
    }
    //判断上下方向
    public static boolean isUpAndDown(int i,int j){
    	int i1 = i-1;
    	while(true){
    		if(i1<0||chessBoard[i1][j] != chessBoard[i][j]){
    		break;
    	}else{
    		i1--;
    	}
    	}
    	i1++;
    	int count = 0;
    	while(true){
    		if(i1>=16||chessBoard[i1][j] != chessBoard[i][j]){
    		break;
    	}else{
    		count++;
    		i1++;
    	}
 }
    	return count >= 5; 
    	
    	
    }
    //判断左上右下
    public static boolean isLeftUpAndRightDown(int i,int j){
    	int j1 = j-1;
    	int i1 = i-1;
    	while(true){
    		if(j1<0||i1<0||chessBoard[i1][j1] != chessBoard[i][j]){
    		break;
    	}else{
    		j1--;
    		i1--;
    	}
    	}
    	j1++;
    	i1++;
    	int count = 0;
    	while(true){
    		if(j1>=16||i1>=16||chessBoard[i1][j1] != chessBoard[i][j]){
    		break;
    	}else{
    		count++;
    		j1++;
    		i1++;
    	}
 }
    	return count >= 5; 
    	
    	
    }
    //判断右上左下
    public static boolean isRightUpAndLeftDown(int i,int j){
    	int j1 = j-1;
    	int i1 = i+1;
    	while(true){
    		if(j1<0||i1>=16||chessBoard[i1][j1] != chessBoard[i][j]){
    		break;
    	}else{
    		j1--;
    		i1++;
    	}
    	}
    	j1++;
    	i1--;
    	int count = 0;
    	while(true){
    		if(j1>=16||i1<0||chessBoard[i1][j1] != chessBoard[i][j]){
    		break;
    	}else{
    		count++;
    		j1++;
    		i1--;
    	}
 }
    	return count >= 5; 
    	
    }
}

打印结果:

 0123456789
0**********
1**********
2**********
3**********
4**********
5**********
6**********
7**********
8**********
9**********
请黑方下子

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值