⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lianliankan.java

📁 java小游戏 基础 连连看 普通界面 初学者适用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   //use the repaint method to protract the game body again  (用repaint方法重新绘制游戏区域。)
      public static String repaint(int state,String[] resetS,String[] hintS, String[] markS,int row,int column,String[][] playArea,int[][] playAreaNumber,String[][] patten,String[] mode){
    	 int  reset=Integer.parseInt(resetS[0]);
    	 int  hint=Integer.parseInt(hintS[0]);
    	 int  mark=Integer.parseInt(markS[0]);
    	 String  output="重排:"+reset+" 次      提示:"+hint+" 次       分数: "+mark+" 分\n";
         //protract the lianliankan game area (绘制连连看游戏界面(方框))
         output+="        ";
         for (int k=1;k<=column;k++){
        	 if (k<10){
        	 output+="  ("+k+")  ";
        	 }
        	 else output+="("+k+") " ;
         }
             output+="\n       ┏";
             for (int k=1;k<column;k++)
             output+="━┳";
             output+="━┓\t\n";
         for (int i=1;i<=row;i++){
        	 if (i<10)
        	 output+="  ("+i+")"+"┃";
        	 else output+="("+i+")"+"┃";
        	 for (int j=1;j<=column;j++){
        		 output+=playArea[i-1][j-1]+"┃";
        	 }
        	 output+="\n";
        	 if (i!=row) output+="       ┣";
        	 else output+="       ┗";
        	 for (int j=1;j<column;j++)
        	 if (i!=row)output+="━╋";	 
        	 else output+="━┻";
        	 if (i!=row) output+="━┫\t\n";
        	 else output+="━┛\t\n";
         }
         String coordinateString;
         //*****************************reset automatically自动重排功能。
         if ((gameContinue(playArea,playAreaNumber,row,column,patten)==false)){
         	 if (Integer.parseInt(resetS[0])>0){
         	 JOptionPane.showMessageDialog(null,output+"\n无可消的牌.\n系统提供自动重排,并将重排数减1","连连看--自动重排",JOptionPane.INFORMATION_MESSAGE);
         	 resetS[0]=reset(resetS,row,column,playArea,playAreaNumber);
   		     reset=Integer.parseInt(resetS[0]);
   	    	 output="重排:"+reset+" 次      提示:"+hint+" 次       分数: "+mark+" 分\n";
   	         //protract the lianliankan game area (绘制连连看游戏界面(方框))
   	         output+="        ";
   	         for (int k=1;k<=column;k++){
   	        	 output+="  ("+k+")  ";
   	         }
   	             output+="\n       ┏";
   	             for (int k=1;k<column;k++)
   	             output+="━┳";
   	             output+="━┓\t\n";
   	         for (int i=1;i<=row;i++){
   	        	 if (i<10)
   	        	 output+="  ("+i+")"+"┃";
   	        	 else output+="("+i+")"+"┃";
   	        	 for (int j=1;j<=column;j++){
   	        		 output+=playArea[i-1][j-1]+"┃";
   	        	 }
   	        	 output+="\n";
   	        	 if (i!=row) output+="       ┣";
   	        	 else output+="       ┗";
   	        	 for (int j=1;j<column;j++)
   	        	 if (i!=row)output+="━╋";	 
   	        	 else output+="━┻";
   	        	 if (i!=row) output+="━┫\t\n";
   	        	 else output+="━┛\t\n";
   	         }
          } 
         	 else {
         		 JOptionPane.showMessageDialog(null,output+"无法提供重排,无牌可消,游戏结束","连连看--游戏结束",JOptionPane.INFORMATION_MESSAGE);
         		 JOptionPane.showMessageDialog(null,"游戏结束....\n您的分数为"+markS[0],"连连看--游戏结束",JOptionPane.INFORMATION_MESSAGE);
         	     System.exit(0);
         	 }
         }
         //when there are icons which can be deleted ,game continue
             output+="请输入坐标:\n [格式为x1,y1-x2,y2]";

        	 coordinateString=JOptionPane.showInputDialog(null,output,"连连看--第"+state+"关["+mode[state%5]+"]",JOptionPane.DEFAULT_OPTION);
            while(coordinateString.equals("help")){
            	 help();
            	 coordinateString=repaint(state,resetS,hintS,markS,row,column,playArea,playAreaNumber,patten,mode);
            	 break;
             }
             while (coordinateString.equals("hint")){
            	 if (hint>0) {
            		 hintS[0]=hint(hintS,state,resetS,markS,row,column,playArea,playAreaNumber,patten);
            		 coordinateString=repaint(state,resetS,hintS,markS,row,column,playArea, playAreaNumber,patten,mode);
            		 break;
            	 }
            	 else {
            		 JOptionPane.showMessageDialog(null,"提示次数=0,无法提供提示","连连看--错误信息",JOptionPane.INFORMATION_MESSAGE);
            		 coordinateString=repaint(state,resetS,hintS,markS,row,column,playArea, playAreaNumber,patten,mode);
            		 break;
            	 }
             }
             while (coordinateString.equals("reset")){
            	 if (reset>0) {
            		 resetS[0]=reset(resetS,row,column,playArea,playAreaNumber);
            		 coordinateString=repaint(state,resetS,hintS,markS,row,column,playArea, playAreaNumber,patten,mode);           	
            		 break;
            	 }
            	 else {
            		 JOptionPane.showMessageDialog(null,"重排次数=0,无法提供重排","连连看--错误信息",JOptionPane.INFORMATION_MESSAGE);
            		 coordinateString=repaint(state,resetS,hintS,markS,row,column,playArea, playAreaNumber,patten,mode);
            		 break;
            	 }
             }
            while (coordinateString.equals("exit")) {
            	JOptionPane.showMessageDialog(null,"游戏结束....\n您的分数为"+markS[0],"连连看--游戏结束",JOptionPane.INFORMATION_MESSAGE);
            	System.exit(0);
            }
         return coordinateString;
     }
      //use help method to display the help information ********用help方法显示提示信息。****
      public static void help(){
    		  String helpString="规则:\n (1)花色为\"☆★○●◇◆□■△▲\"的牌消除路线无限制\n";
    		  	helpString+=" (2)花色为\"①②③④⑤⑥⑦⑧⑨⑩\"的牌消除路线最多有一次转弯\n";
    		  	helpString+=" (3)花色为\"甲乙丙丁戊己庚辛任癸\"的牌消除路线最多有两次转弯\n\n\n";
    		     helpString+="命令:\n (1) reset: 重新排列剩余的游戏牌\n (2) hint: 显示提示信息\n (3) help: 显示帮助信息\n (4) exit: 推出游戏";
    		  JOptionPane.showMessageDialog(null,helpString,"连连看--帮助",JOptionPane.INFORMATION_MESSAGE);
      }
      //use error method to display the error information ******用error方法显示错误信息。*****
      public static void error(int error){
    	  switch(error){
    	  case 1:  JOptionPane.showMessageDialog(null,"数据输入格式不规范","连连看--错误信息",JOptionPane.INFORMATION_MESSAGE);break;
    	  case 2:  JOptionPane.showMessageDialog(null,"数据输入超出范围","连连看--错误信息",JOptionPane.INFORMATION_MESSAGE);break;
    	  case 3:  JOptionPane.showMessageDialog(null,"不同花色的牌不能消除","连连看--错误信息",JOptionPane.INFORMATION_MESSAGE);break;
    	  case 4:  JOptionPane.showMessageDialog(null,"不满足消除规则","连连看--错误信息",JOptionPane.INFORMATION_MESSAGE);break;
    	  default: break;
    	              } 	  
      }
      //use hint method to display the hint information ******用hint方法做提示******
      public static String hint(String[] hintS,int state,String[] resetS, String[] markS,int row,int column,String[][] playArea,int[][] playAreaNumber,String[][] patten){
    	  int hint=Integer.parseInt(hintS[0]);
    	  hint--;
    	  hintS[0]=""+hint;   	 
    	  boolean canHint=false;
    	  outer:
    	  for (int i=0;i<row;i++){
    		  for (int j=0;j<column;j++){
    			  if (playAreaNumber[i][j]==1){
    				  inner:
    				  for(int p=0;p<row;p++){
    					  for (int q=0;q<column;q++){
    						  if ((playArea[i][j].equals(playArea[p][q]))&&((i!=p)||(j!=q))){
    							  canHint=isContinueHintEndition((i+1),(j+1),(p+1),(q+1),row,column,playArea,playAreaNumber,patten);
    							  if (canHint==true){
    							  JOptionPane.showMessageDialog(null,"["+(i+1)+","+(j+1)+"] -- ["+(p+1)+","+(q+1)+"]","连连看--提示信息",JOptionPane.INFORMATION_MESSAGE);  
    						      break outer;
    							  }
    						  }
    					  }
    				  }
    			  }
    		  }
    	  }
    	  if(canHint==false){
    		  JOptionPane.showMessageDialog(null,"没有可消的牌","连连看--提示信息",JOptionPane.INFORMATION_MESSAGE);
    	  }
    	  return hintS[0];    	  
      }
      //use the reset method to do the recomposition ******通过reset方法实现重排功能。*****
      public static String reset(String[] resetS,int row,int column,String[][] playArea,int[][] playAreaNumber){
    	  int numbers=0;
    	  int reset=Integer.parseInt(resetS[0]);
    	  reset--;
    	  resetS[0]=""+reset;
    	  String[] storeString=new String[400];
    	  for (int i=0;i<row;i++){
    		  for (int j=0;j<column;j++){
    			  if (playAreaNumber[i][j]==1){
    				  numbers++;
    			  playAreaNumber[i][j]=0;
    			  storeString[numbers-1]=playArea[i][j];
    			  playArea[i][j]="    ";
    			  }
    		  }
    	  }  	  
    	  for (int i=0;i<numbers;i++){
    	  int randomRow=(int)(Math.random()*row);
    	  int randomColumn=(int)(Math.random()*column);
    	  while (playAreaNumber[randomRow][randomColumn]!=0){
    		  randomRow=(int)(Math.random()*row);
    		  randomColumn=(int)(Math.random()*column);
    	  }
    	  playArea[randomRow][randomColumn]=storeString[i];
    	  playAreaNumber[randomRow][randomColumn]=1;
      }
    	  return resetS[0];
      }
      //use the isContinue method to judge whether the two icons can be deleted ****判断棋子是否可以消除。****
      public static boolean isContinue(String coordinateString,int state,String[] resetS,String[] hintS, String[] markS,int row,int column,String[][] playArea,int[][] playAreaNumber,String[][] patten){
    	  boolean isDelete=true;
    	  boolean isSymbolRight=false;
    	  boolean isAllInteger=false;
    	  int countInteger=0;
    	  String[] m=new String[4];
    	  String symbol1,symbol2,symbol3;
    	  m[0]=m[1]=m[2]=m[3]="";
          StringTokenizer s=new StringTokenizer(coordinateString,",-",true);
          if (s.countTokens()==7){
          m[0]=(String)s.nextElement();
          symbol1=(String)s.nextElement();
          m[1]=(String)s.nextElement();
          symbol2=(String)s.nextElement();
          m[2]=(String)s.nextElement();
          symbol3=(String)s.nextElement();
          m[3]=(String)s.nextElement();
          if ((symbol1.equals(",")) && (symbol2.equals("-"))&& (symbol3.equals(","))){
        	  isSymbolRight=true;
          		}
          for (int i=0;i<=3;i++){
        	  for (int j=0;j<300;j++){
        		 if  (m[i].equals(""+j)) countInteger++;
        	  }
          }
          if (countInteger==4) isAllInteger=true;
          }
    	  boolean syntax=((isAllInteger==true)&& (isSymbolRight==true));
    		if (syntax==false){
    		     error(1);
    		     isDelete=false;
    	  	}
    	  if (syntax==true){
          int x1=Integer.parseInt(m[0]);
          int y1=Integer.parseInt(m[1]);
          int x2=Integer.parseInt(m[2]);
          int y2=Integer.parseInt(m[3]);
          if ((isDelete==true)&&((x1>row)||(x2>row)||(y1>column)||(y2>column))){
        	  	error(2);
        	  	isDelete=false;
          }
          if ((isDelete==true)&&(!playArea[x1-1][y1-1].equals(playArea[x2-1][y2-1]))){
        	  error(3);
        	  isDelete=false;
          }
          if (isDelete==true){
        	  //judge whether the two icons can be deleted ,if they cn ,isDelete=true;
        	  //***************判断能不能消除的过程,如果可以消除的话,isDelete=true;*****
        	 IsDelete judgement=new IsDelete(x1,y1,x2,y2,row,column,playAreaNumber);
        	 int number=0;
        	 for (int i=0;i<3;i++){
        		 for (int j=0;j<10;j++){
        			 if (playArea[x1-1][y1-1].equals(patten[i][j])){
        				number=i; 
        			 }
        		 }
        	 }
        	 switch(number){
        	 case 0: isDelete=true;break;
        	 case 1: isDelete=(judgement.isDeleteCorner0) ||(judgement.isDeleteCorner1);
        	 break;
        	 case 2: isDelete=(judgement.isDeleteCorner0) ||(judgement.isDeleteCorner1)||(judgement.isDeleteCorner2);
        	 break;
        	 }
        	 if (isDelete==false){
        		 error(4);
        	 	}      	 
          	}
    	  }
          return isDelete;
      }
      //use isContinueHintEndition method for the hint method to find the same icons 
      //******************用isContinueHintEndition方法做hint提示中用于找相同的牌********
      public static boolean isContinueHintEndition(int x1,int y1,int x2,int y2,int row,int column,String[][] playArea,int[][] playAreaNumber,String[][] patten){
    	     boolean isDelete=true;
    	   	 IsDelete judgement=new IsDelete(x1,y1,x2,y2,row,column,playAreaNumber);
        	 int number=0;
        	 for (int i=0;i<3;i++){
        		 for (int j=0;j<10;j++){
        			 if (playArea[x1-1][y1-1].equals(patten[i][j])){
        				number=i; 
        			 }
        		 }
        	 }
        	 switch(number){
        	 case 0: isDelete=true;break;
        	 case 1: isDelete=(judgement.isDeleteCorner0) ||(judgement.isDeleteCorner1);break;
        	 case 2: isDelete=(judgement.isDeleteCorner0) ||(judgement.isDeleteCorner1)||(judgement.isDeleteCorner2);
        	 break;
        	 }
       return isDelete;       	      
      }
     //use mark method to record the mark.********用mark方法,记录分数********
     public static void mark(int x1,int y1,String[] markS,String[][] playArea,String[][] patten){
    	 int number=0;
    	 int mark=Integer.parseInt(markS[0]);
    	 for (int i=0;i<3;i++){
    		 for (int j=0;j<10;j++){
    			 if (playArea[x1-1][y1-1]==patten[i][j]){
    				 number=i;
    			 }
    		 }
    	 }
    	 switch (number){
    	 case 0: mark+=3;markS[0]=""+mark;break;
    	 case 1: mark+=5;markS[0]=""+mark;break;
    	 case 2: mark+=10;markS[0]=""+mark;break;
    	 }
     }
     //use gameContinue method to judge whether there are any icons which can be deleted,and whether the game is over
     //************************用gameContinue方法,判断是否还有可消的牌,,是否游戏结束。*************8
     public static boolean gameContinue(String[][] playArea,int[][] playAreaNumber,int row,int column,String[][] patten){
    	 boolean canContinue=false;
   	  outer:
    	  for (int i=0;i<row;i++){
    		  for (int j=0;j<column;j++){
    			  if (playAreaNumber[i][j]==1){
    				  inner:
    				  for(int p=0;p<row;p++){
    					  for (int q=0;q<column;q++){
    						  if ((playArea[i][j].equals(playArea[p][q]))&&((i!=p)||(j!=q))){
    							  canContinue=isContinueHintEndition((i+1),(j+1),(p+1),(q+1),row,column,playArea,playAreaNumber,patten);
    							  if (canContinue==true){
    						      break outer;
    							  }
    						  }
    					  }
    				  }
    			  }
    		  }
    	  }
    	 return canContinue;   
     }
}  	  


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -