close

最近因為天氣炎熱,房間常飆在34~36度,導致洗腦訓練英文聽力被迫打斷。

突然靈機一動,不如來做個英文單字填填樂! <<取名超爛

靈感源自個人喜愛看公視-一字X金,看到表格內填成語挺有趣的!

當然這程式也能改成國文或日文之類等等,只要改寫這段

String re4="[a-z]{"+Integer.toString(min_voca)+","+Integer.toString(max_voca)+"}";

[a-z]改成[\u4e00-\u9fa5](中文)或[\u0800-\u4e00](日文)就可以。

 

Reqular Expressions(正規表示式)參考資料:

維基百科

JWorld論壇

REs+matches運用


結果圖:

1.jpg

2.jpg

3.jpg

4.jpg

5.jpg

 

程式碼:

import java.lang.*;
import java.util.Scanner;

class TablePlay
{
   private int tableArrNo;
   private String input_table[][];
   private String one_char="";//table初始化
   private String vocabulary[][] = 
   {{"F","d","i","d"},
    {"F","a","m"},
    {"F","d","o"},
    {"F","a","r","e"},
    {"F","w","a","s"},
    {"F","m","e"},
    {"F","y","o","u"}};
   private String top,down,left,right,upLe,upRi,loLe,loRi;
   private int min_voca = 0;
   private int max_voca = 0;
   private String x_num,y_num;
   private String dir_num;
   private String in_word;
   private int voca_find;

   TablePlay(String input)
   {
     //表格內容
     tableArrNo=3;//3X3遊戲表格
     input_table = new String[tableArrNo][tableArrNo];
     one_char=input;
     for(int x=0;x<input_table.length;x++)
     {
        for(int y=0;y<input_table[x].length;y++){input_table[x][y]=one_char;}
     }
     top_table();
     show_table();
     //單字長度    
     top_voca();
     voca_len();
     voca_find = vocabulary.length;
     //設定方向編號
     top="8";
     down="2"; 
     left="4";
     right="6"; 
     upLe="7"; 
     upRi="9"; 
     loLe="1";
     loRi="3";
   }
   //輸入單字
   private boolean sca_input()
   {
    String re1="[0-"+Integer.toString(tableArrNo-1)+"]";
    String re2="[0-"+Integer.toString(tableArrNo-1)+"]";
    String re3="[1-4]|[6-9]";//跟TablePlay()設定方向編號有關
    String re4="[a-z]{"+Integer.toString(min_voca)+","+Integer.toString(max_voca)+"}";
    String voCatch="";
    Scanner sc = new Scanner(System.in);
    x_num=sc.next();
    y_num=sc.next();
    dir_num=sc.next();
    in_word=sc.next();
    System.out.println("X驗證:"+re1);
    System.out.println("Y驗證:"+re2);
    System.out.println("方向驗證:"+re3);
    System.out.println("單字驗證:"+re4);    
    if(x_num.matches(re1))
    {
       if(y_num.matches(re2))
       {
          if(dir_num.matches(re3))
          {
              if(in_word.matches(re4))
            {
                 //判斷單字沒有重複輸入或不存在
                for(int c=0;c<vocabulary.length;c++)
               {
                   for(int d=1;d<vocabulary[c].length;d++)voCatch+=vocabulary[c][d];
                   if(voCatch.equals(in_word))
                  {
                      if(vocabulary[c][0].equals("F")){voca_find=c;break;}//如果成功輸入單字,要把單字素材F改T
                      else {System.out.println("此單字輸入過了!");return false;}
                  }
                  else if(c==vocabulary.length-1){System.out.println("此單字不存在!");return false;}
                  else voCatch="";
                }
                //座標跟方向沒有超出範圍
               if(outTabLen(Integer.parseInt(x_num),Integer.parseInt(y_num),dir_num)){System.out.println("方向已經超出遊戲表格範圍!");return false;}
               System.out.print("輸入座標:"+x_num);
               System.out.print(",");  
               System.out.println(y_num);           
               System.out.println("輸入方向:"+dirStr(dir_num));
               System.out.println("輸入單字:"+in_word);
               return true;

            }else System.out.println("輸入單字錯誤");
          }else System.out.println("輸入方向編號錯誤");
       }else System.out.println("輸入Y座標錯誤");
    }else System.out.println("輸入X座標錯誤");
    return false;
   }
   public void play_game()
   {
     while(empVoca(max_voca)!=0 && empTable()==false)//1.單字素材沒寫完 2.遊戲表格沒寫滿
     {    
          if(sca_input()){vocaFind();top_voca();}
     }
     System.out.print("遊戲結束!");
   }
   //輸入單字是否能存入表格
   private boolean vocaFind()
   {
      int tx=0,ty=0;
      int oriX=Integer.parseInt(x_num);
      int oriY=Integer.parseInt(y_num);
      int wordLen=vocabulary[voca_find].length-1;
      int for_num=0;//迴圈次數
      String upArr[][]=new String[wordLen][3];//更新暫存
      int u=0,upAx,upAy;
      boolean upF=false;     
      //根據方向編號設定方法
      if(dir_num.equals(top)){tx=-1;}
      if(dir_num.equals(down)){tx=1;}
      if(dir_num.equals(left)){ty=-1;}
      if(dir_num.equals(right)){ty=1;}
      if(dir_num.equals(upLe)){tx=-1;ty=-1;}    
      if(dir_num.equals(upRi)){tx=1;ty=-1;}
      if(dir_num.equals(loLe)){tx=-1;ty=1;}
      if(dir_num.equals(loRi)){tx=1;ty=1;}
      if(tx==0)
      {    
           for(int y=1;y<=wordLen;y++)
          {
               for_num++;
               if(input_table[oriX][oriY].equals(one_char))
              {
                   System.out.print("X:"+Integer.toString(oriX));
                   System.out.println("Y:"+Integer.toString(oriY));
                   upArr[u][0]=Integer.toString(oriX);
                   upArr[u][1]=Integer.toString(oriY);
                   upArr[u][2]=vocabulary[voca_find][for_num];
                   u++;
                   oriY+=ty;
                   upF=true;
               }
               else if(input_table[oriX][oriY].equals(vocabulary[voca_find][for_num])){oriY+=ty;upF=true;}    
               else {System.out.println("輸入位置已填過!");return false;}
           }
      }
      else if(ty==0)
      {
           for(int x=1;x<=wordLen;x++)
          {
               for_num++;
               if(input_table[oriX][oriY].equals(one_char))
              {
                  System.out.print("X:"+Integer.toString(oriX));
                  System.out.println("Y:"+Integer.toString(oriY));
                  upArr[u][0]=Integer.toString(oriX);
                  upArr[u][1]=Integer.toString(oriY);
                  upArr[u][2]=vocabulary[voca_find][for_num];
                  u++;
                  oriX+=tx;
                  upF=true;
               }
              else if(input_table[oriX][oriY].equals(vocabulary[voca_find][for_num])){oriX+=tx;upF=true;}    
              else {System.out.println("輸入位置已填過!");return false;}
          }
      }
      else
      {
          for(int b=1;b<=wordLen;b++)
          {
                for_num++;
                if(input_table[oriX][oriY].equals(one_char))
               {
                    System.out.print("X:"+Integer.toString(oriX));
                    System.out.println("Y:"+Integer.toString(oriY));
                    upArr[u][0]=Integer.toString(oriX);
                    upArr[u][1]=Integer.toString(oriY);
                    upArr[u][2]=vocabulary[voca_find][for_num];
                    u++;
                    oriX+=tx;
                    oriY+=ty;
                    upF=true;
                }
               else if(input_table[oriX][oriY].equals(vocabulary[voca_find][for_num])){oriX+=tx;oriY+=ty;upF=true;}    
               else {System.out.println("輸入位置已填過!");return false;}      
          }
      }
      if(upF && u!=0)
      {
          for(int up=0;up<u;up++)
          {
               upAx=Integer.parseInt(upArr[up][0]);
               upAy=Integer.parseInt(upArr[up][1]);
               input_table[upAx][upAy]=upArr[up][2];       
          }
          show_table();
          vocabulary[voca_find][0]="T";         
      }      
      return true;
   }   
   //單字素材寫完
   private int empVoca(int minIn)
   {
     int reMin=minIn+1;
     for(int u=0;u<vocabulary.length;u++)
     {    
          if(vocabulary[u][0].equals("F")){if(vocabulary[u].length-1<reMin)reMin=vocabulary[u].length-1;}
     }
     if(reMin==minIn+1){System.out.print("單字素材都寫過了!");return 0;}
     else return reMin;
   }
   //遊戲表格寫滿
   private boolean empTable()
   {
     for(int x=0;x<input_table.length;x++)
     {
        for(int y=0;y<input_table[x].length;y++)
        {            
            if(input_table[x][y].equals(one_char))return false;
        }
     }
     return true;
   }
   //座標超出範圍
   private boolean outTabLen(int caX,int caY,String caMe)
   {
     int min=0;
     int i=0,j=0;
     //判斷單字素材是否寫完
     min=empVoca(max_voca);
     if(min==0) return true;
     //根據方向編號設定方法
     if(caMe.equals(top)){i=-1;}
     if(caMe.equals(down)){i=1;}
     if(caMe.equals(left)){j=-1;}
     if(caMe.equals(right)){j=1;}
     if(caMe.equals(upLe)){i=-1;j=-1;}    
     if(caMe.equals(upRi)){i=1;j=-1;}
     if(caMe.equals(loLe)){i=-1;j=1;}
     if(caMe.equals(loRi)){i=1;j=1;} 
     if(i==0)
     {
       for(int y=1;y<min;y++){if((caY+j)<0 || (caY+j)==input_table.length)return true;}
     }
     else if(j==0)
     {
       for(int x=1;x<min;x++){if((caX+i)<0 || (caX+i)==input_table.length)return true;}
     }
     else
     {
       for(int a=1;a<min;a++)
       {
           for(int b=1;b<min;b++)
           {
                if((caX+i)<0 || (caY+j)<0 || (caY+j)==input_table.length || (caX+i)==input_table.length)return true;
           }
       }
     }
     return false; 
   }
   //方向編號對應
   private String dirStr(String dirIn)
   {
     String reStr="";
     if(dirIn.equals(top))reStr="上";    
     if(dirIn.equals(down))reStr="下";
     if(dirIn.equals(left))reStr="左";
     if(dirIn.equals(right))reStr="右";
     if(dirIn.equals(upLe))reStr="左上";    
     if(dirIn.equals(upRi))reStr="右上";
     if(dirIn.equals(loLe))reStr="左下";
     if(dirIn.equals(loRi))reStr="右下";
     return reStr;
   }   
   //顯示遊戲狀況
   private void show_table() 
   {
        for(int x=0;x<input_table.length;x++)
        {
          for(int y=0;y<input_table[x].length;y++)
          {            
               System.out.print(input_table[x][y]);
          }
          System.out.println();
        }
   }
   //說明單字素材
   private void top_voca()
   {
    boolean init=false;
    System.out.print("單字素材有:");
    for(int a=0;a<vocabulary.length;a++)
    {      
      for(int b=1;b<vocabulary[a].length;b++)
      {
          if(vocabulary[a][0].equals("F"))
          {
              System.out.print(vocabulary[a][b]);
              if(init==false){min_voca=vocabulary[a].length-1;max_voca=vocabulary[a].length-1;init=true;}
              if(min_voca>(vocabulary[a].length-1))min_voca = vocabulary[a].length-1;
              if(max_voca<(vocabulary[a].length-1))max_voca = vocabulary[a].length-1;
           }
       }
       if(vocabulary[a][0].equals("F"))System.out.print(" ");
     }
     System.out.println();
   }
   //說明遊戲表格
   private void top_table(){System.out.println("遊戲表格為"+input_table.length+"X"+input_table.length);}
   //單字長度範圍
   private void voca_len(){System.out.println("單字長度範圍為"+min_voca+"到"+max_voca);}
}
public class remember
{
   public static void main(String args[])
   {
        TablePlay tp = new TablePlay("E");
        tp.play_game();        
   }
}

arrow
arrow
    全站熱搜

    o迷苓o 發表在 痞客邦 留言(0) 人氣()