JAVA p258 T7.20

import java.util.Scanner;
public class HelloWorld {
	public static int[][] Board=new int[7][7]; 		//建立游戏场地
	
	public static int player1=1;	//player1为red
	public static int player2=2;	//player2为yellow
	public static int player=player1;
	public static int count=0;		//计数器,记录场地上的棋子数
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		 while(true) {   							//建立游戏循环
	            while(true) {   					//建立输入循环
	                System.out.print("Drop a ");
	                if(player == player1)
	    	             System.out.print("red at column(0 - 6):"); 
	                else
   	                	 System.out.print("yellow at column(0 - 6):"); 
	                int column = input.nextInt();   		//输入列column上的位置
	                if (Judgingrange(column, player) == true) {  //判断输入是否在场地内且输入列上棋子未满,
	                    break; 								//如果符合则跳出输入循环
	                }  
	            } 
	            ShowCheckerboard(); //显示当前局势
	            if (JudgingWin(player) == true) { 			//判断是否有人赢 
	            	if (player==player1) 
	            		System.out.println("The red player win");
	            	else
	            		System.out.println("The yellow player win");
	                System.out.print("Would you like to continue? (Y or N):");
	                String choose = input.next();
	                if (choose.equals("Y"))
	                {
	                	for (int i=0; i<7 ; i++)
	                		for (int j=0; j<7 ; j++)
	                			Board[i][j]=0;
	                	count=0;
	                	player=player1;
	                	continue;
	                }//重新开始游戏
	                else if (choose.equals("N"))
	                {
		               	break;
		            }//跳出游戏循环							
	            }
	            if (count == 49) {  							//如果九宫格每个格上都有棋子
	            	System.out.println("red and yellow draw"); 	//则是平局  
	            	System.out.print("Would you like to continue? (Y or N):");
		            String choose = input.next();
		            if (choose.equals("Y"))
	                {
	                	for (int i=0; i<7 ; i++)
	                		for (int j=0; j<7 ; j++)
	                			Board[i][j]=0;
	                	count=0;
	                	player=player1;
	                	continue;
	                }//重新开始游戏
	                else if (choose.equals("N"))
	                {
		               	break;
		            }//跳出游戏循环							
	            }
	            if (player==player1)
	            {
	            	player=player2;
	            }
	            else
	            {
	            	player=player1;
	            }
	        }  
	}
	public static boolean Judgingrange(int x, int num)
	{
		if (x < 0 || x > 6) {           //检查是否越界
            System.out.println("Input out, please input again.");  
            return false;  
        }
		int i = 6;
		while (i>=0)
		{
			if (Board[i][x] == 0) {                            //如果当前位置没有棋子
        		Board[i][x] = num;
              count++;
              return true;  
			}
			else
			{
				i--;
			}
		}
		System.out.print("This column is full, please input again.");
		return false;
	}
	public static boolean JudgingWin(int num)    
	{
		for (int i = 0; i < 7; i++) {   	
			for (int j=0 ; j < 4 ; j++){
			if (Board[i][j] == Board[i][j+1] && Board[i][j+1] == Board[i][j+2]  
					&& Board[i][j+2] == Board[i][j+3] && Board[i][j+3] == num) //横向检查
			{  
                return true;  
            }
            else if (Board[j][i] == Board[j+1][i] && Board[j+1][i] == Board[j+2][i] 
            		&& Board[j+2][i] == Board[j+3][i] && Board[j+3][i] == num) //竖线检查
            {  
                return true;  
            }
            
		}
	}
		//检查主对角线和副对角线上是否有四个一样的棋子
		for (int i = 0; i < 4; i++) {   	
			for (int j = 0 ; j < 4 ; j++){
				if (Board[i][j] == Board[i+1][j+1] && Board[i+1][j+1] == Board[i+2][j+2]
	            		&& Board[i+2][j+2] == Board[i+3][j+3]&&Board[i+3][j+3]==num)
	            {
	            	return true;
	            }
				if (Board[i][6-j] == Board[i+1][5-j] && Board[i+1][5-j] == Board[i+2][4-j]
	            		&& Board[i+2][4-j] == Board[i+3][3-j]&&Board[i+3][3-j]==num)
	            {
	            	return true;
	            }
			}
		}
		return false;
	}
	
	public static void ShowCheckerboard()	//输出九宫格局势
	{
	
        for (int i = 0; i < 7; ++i) {  
        	System.out.print("|");
        	for (int j = 0; j < 7; ++j) {  
                if (Board[i][j] == 1 )
                	System.out.print("R|");
                else if(Board[i][j] == 2)
                	System.out.print("Y|");
                else
                	System.out.print(" |");
                }  
            System.out.println();
         } 
      }  
}


运行结果:

Drop a red at column(0 - 6):0
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | | | | |
Drop a yellow at column(0 - 6):3
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | |Y| | | |
Drop a red at column(0 - 6):4
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | |Y|R| | |
Drop a yellow at column(0 - 6):1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R|Y| |Y|R| | |
Drop a red at column(0 - 6):2
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R|Y|R|Y|R| | |
Drop a yellow at column(0 - 6):3
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |Y| | | |
|R|Y|R|Y|R| | |
Drop a red at column(0 - 6):2
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |R|Y| | | |
|R|Y|R|Y|R| | |
Drop a yellow at column(0 - 6):3
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |Y| | | |
| | |R|Y| | | |
|R|Y|R|Y|R| | |
Drop a red at column(0 - 6):3
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |Y| | | |
| | |R|Y| | | |
|R|Y|R|Y|R| | |
Drop a yellow at column(0 - 6):4
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |Y| | | |
| | |R|Y|Y| | |
|R|Y|R|Y|R| | |
Drop a red at column(0 - 6):5
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |Y| | | |
| | |R|Y|Y| | |
|R|Y|R|Y|R|R| |
Drop a yellow at column(0 - 6):5
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |Y| | | |
| | |R|Y|Y|Y| |
|R|Y|R|Y|R|R| |
Drop a red at column(0 - 6):6
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |Y| | | |
| | |R|Y|Y|Y| |
|R|Y|R|Y|R|R|R|
Drop a yellow at column(0 - 6):6
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |Y| | | |
| | |R|Y|Y|Y|Y|
|R|Y|R|Y|R|R|R|
The yellow player win
Would you like to continue? (Y or N):N


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值