Little Alchemy 游戏 - 扒答案。。 Java

Little Alchemy 是个用四个基本元素组合生成的小游戏。。。连Excalibur都可以有

写的代码是从这个游戏的hint页面得到答案的

 

  1 import java.io.*;
  2 import java.net.URL;
  3 import java.util.*;
  4 
  5 public class Main {
  6 
  7     public static void main(String[] args) {
  8         String urlStr = "https://littlealchemy.com/cheats/?element=";
  9         String mark = "<a href=\"?element=";
 10 
 11         Map<String, Integer> map = new HashMap<String, Integer>();
 12         String[][] c = new String[600][3];
 13 
 14 
 15         try{
 16             int cnt = 0;
 17             String curr = "christmas stocking";
 18             URL url = new URL(urlStr + curr.replaceAll(" ", "%20"));
 19 
 20             Scanner in = new Scanner(new File("result.txt"));
 21             while(in.hasNext()){
 22                 String line;
 23                 line = in.nextLine();
 24                 String[] ss = line.split(" [=\\+] ");
 25                 map.put(ss[0], cnt);
 26                 c[cnt][0] = ss[0];
 27                 c[cnt][1] = ss[1];
 28                 c[cnt][2] = ss[2];
 29                 cnt++;
 30             }
 31             in.close();
 32 
 33 //            550-4
 34             int maxn = 525;//找到maxn个条目就停止
 35             int times = 0;
 36             while(map.size() < maxn) {
 37                 ++times;
 38                 if(times % 100 == 0){
 39                     System.out.println(times);
 40                 }
 41                 BufferedInputStream stream = new BufferedInputStream(url.openStream());
 42                 byte[] data = new byte[10000];
 43                 stream.read(data);
 44                 stream.close();
 45 
 46                 String str = new String(data);
 47                 int start = str.indexOf(mark) + mark.length();
 48                 int end = str.indexOf("\" class=\"randomButtonTop\">try random element</a>");
 49                 if(start > 0 && end > 0) {
 50 
 51                     int e1s = str.indexOf(mark, start);
 52                     int e1e = str.indexOf("\"><img class=\"elementImg\"", e1s>0?e1s:0);
 53                     e1s += mark.length();
 54 
 55                     int e2s = str.indexOf(mark, e1e);
 56                     int e2e = str.indexOf("\"><img class=\"elementImg\"", e2s>0?e2s:0);
 57                     e2s += mark.length();
 58 
 59 
 60 //                    if(!map.containsKey(curr)){
 61 //                        System.out.println(curr);
 62 //                    }
 63 //                    if(e1s>start && e1e>e1s && !map.containsKey(str.substring(e1s, e1e))){
 64 //                        System.out.println(str.substring(e1s, e1e));
 65 //                    }
 66 //                    if(e2s>e1e && e2e>e2s && !map.containsKey(str.substring(e2s, e2e))){
 67 //                        System.out.println(str.substring(e2s, e2e));
 68 //                    }
 69 
 70                     if(!map.containsKey(curr) && e1s>start && e1e>e1s && e2s>e1e && e2e>e2s){
 71                         map.put(curr, cnt);
 72                         c[cnt][0] = curr;
 73                         c[cnt][1] = str.substring(e1s, e1e);
 74                         c[cnt][2] = str.substring(e2s, e2e);
 75                         cnt++;
 76                         System.out.println(cnt+". "+curr+" = "+str.substring(e1s, e1e)+" + "+str.substring(e2s, e2e));
 77                     }
 78 
 79                     String next = str.substring(start, end);
 80                     url = new URL(urlStr + next.replaceAll(" ", "%20"));
 81                     curr = next;
 82                 }
 83                 else {
 84 //                    System.out.println("\tWAITING...");
 85                     Thread.sleep(2000);
 86                 }
 87             }
 88 
 89             Arrays.sort(c, 0, cnt, new Comparator<String[]>() {
 90                 @Override
 91                 public int compare(String[] o1, String[] o2) {
 92                     return o1[0].compareTo(o2[0]);
 93                 }
 94             });
 95             PrintWriter out = new PrintWriter("result.txt");
 96             for(int i = 0; i < cnt; ++i){
 97                 out.printf("%s = %s + %s\n", c[i][0], c[i][1], c[i][2]);
 98 //                System.out.printf("%d. %s = %s + %s\n", i+1, c[i][0], c[i][1], c[i][2]);
 99             }
100             out.close();
101         }
102         catch (Exception e){
103             e.printStackTrace();
104         }
105     }
106 }

 

posted @ 2016-01-08 16:05  可乐君  阅读(419)  评论(0编辑  收藏  举报