Advertisement
finalshare

123321

Apr 15th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Random;
  3. public class exe{
  4.     public int cow;
  5.     public int bull;
  6.     public Process proc;
  7.     BufferedReader stdInput;
  8.     BufferedReader stdError;
  9.     OutputStream stdIn;
  10.     Runtime rt ;
  11.     int FLAG;
  12.     public void init(){
  13.         FLAG = 0;
  14.         try{
  15.             String strOut = null;
  16.             rt=Runtime.getRuntime();
  17.             String[] commands = {"E:\\Doc\\nmap-7.40\\ncat.exe","bullandcow-challenge.framgia.vn","2015"};
  18.             proc = rt.exec(commands);
  19.  
  20.             stdInput = new BufferedReader(new
  21.                 InputStreamReader(proc.getInputStream()));
  22.             stdError = new BufferedReader(new
  23.                 InputStreamReader(proc.getErrorStream()));
  24.            
  25.             stdIn = proc.getOutputStream();
  26.             for (int i=1;i<=10;++i)
  27.             {
  28.                 strOut = stdInput.readLine();
  29.                 System.out.println(strOut);
  30.             }
  31.         } catch (IOException ex){}
  32.     }
  33.     public void getBullCow(String input){
  34.         //Count Bull
  35.        
  36.         String str = input;
  37.         String findStr = "bull";
  38.         int lastIndex = 0;
  39.         int count = 0;
  40.  
  41.         while(lastIndex != -1){
  42.  
  43.             lastIndex = str.indexOf(findStr,lastIndex);
  44.  
  45.             if(lastIndex != -1){
  46.                 count ++;
  47.                 lastIndex += findStr.length();
  48.             }
  49.         }
  50.         bull=count;
  51.  
  52.         //Count Cow
  53.  
  54.         findStr = "cow";
  55.         lastIndex = 0;
  56.         count = 0;
  57.  
  58.         while(lastIndex != -1){
  59.  
  60.             lastIndex = str.indexOf(findStr,lastIndex);
  61.  
  62.             if(lastIndex != -1){
  63.                 count ++;
  64.                 lastIndex += findStr.length();
  65.             }
  66.         }
  67.         cow=count;
  68.        
  69.  
  70.         findStr = "nothing";
  71.         lastIndex = 0;
  72.         count = 0;
  73.  
  74.         while(lastIndex != -1){
  75.  
  76.             lastIndex = str.indexOf(findStr,lastIndex);
  77.  
  78.             if(lastIndex != -1){
  79.                 count ++;
  80.                 lastIndex += findStr.length();
  81.             }
  82.         }
  83.  
  84.         String strOut=null;
  85.         if ((cow==0 && bull==0 && count ==0) || (bull==4)){
  86.             try{
  87.             while ((strOut = stdInput.readLine())!=null)
  88.             {
  89.                
  90.                 System.out.println(strOut);
  91.                 FLAG=1;
  92.                 Thread.sleep(10000000);
  93.                
  94.             }
  95.             } catch (Exception ex){}
  96.         }
  97.  
  98.         System.out.println("Bull:"+bull + '\n'+"Cow:"+cow);
  99.  
  100.  
  101.     }
  102.     public String send(String num){
  103.         String strOut = null,strTemp=null;
  104.         try{
  105.             num+='\n';
  106.             stdIn.write(num.getBytes ("US-ASCII"));
  107.             stdIn.flush();
  108.             strOut = stdInput.readLine();
  109.             System.out.println(strOut);
  110.             strTemp = stdInput.readLine();
  111.             //System.out.println(strOut);
  112.         } catch( IOException ex) {}
  113.         System.out.println(num);
  114.         String bullCowStr=strOut.substring(36);
  115.         getBullCow(bullCowStr);
  116.  
  117.         return strOut;
  118.  
  119.     }
  120.  
  121.     String buf;
  122.     String list;
  123.     static int n=4;
  124.  
  125.     public void clear(){
  126.         stdInput=null;
  127.         stdError=null;
  128.         stdIn=null;
  129.         rt =null;
  130.     }
  131.  
  132.     public void game(){
  133.        
  134.  
  135.     }
  136.  
  137.    
  138.     public static void main(String[] args){
  139.        
  140.         exe CTF=new exe();
  141.         CTF.init();
  142.         CTF.send("1234");//return string là kết quả đồng thời tự thay đổi số cow, bull luôn
  143.  
  144.        
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement