Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Random;
- public class exe{
- public int cow;
- public int bull;
- public Process proc;
- BufferedReader stdInput;
- BufferedReader stdError;
- OutputStream stdIn;
- Runtime rt ;
- int FLAG;
- public void init(){
- FLAG = 0;
- try{
- String strOut = null;
- rt=Runtime.getRuntime();
- String[] commands = {"E:\\Doc\\nmap-7.40\\ncat.exe","bullandcow-challenge.framgia.vn","2015"};
- proc = rt.exec(commands);
- stdInput = new BufferedReader(new
- InputStreamReader(proc.getInputStream()));
- stdError = new BufferedReader(new
- InputStreamReader(proc.getErrorStream()));
- stdIn = proc.getOutputStream();
- for (int i=1;i<=10;++i)
- {
- strOut = stdInput.readLine();
- System.out.println(strOut);
- }
- } catch (IOException ex){}
- }
- public void getBullCow(String input){
- //Count Bull
- String str = input;
- String findStr = "bull";
- int lastIndex = 0;
- int count = 0;
- while(lastIndex != -1){
- lastIndex = str.indexOf(findStr,lastIndex);
- if(lastIndex != -1){
- count ++;
- lastIndex += findStr.length();
- }
- }
- bull=count;
- //Count Cow
- findStr = "cow";
- lastIndex = 0;
- count = 0;
- while(lastIndex != -1){
- lastIndex = str.indexOf(findStr,lastIndex);
- if(lastIndex != -1){
- count ++;
- lastIndex += findStr.length();
- }
- }
- cow=count;
- findStr = "nothing";
- lastIndex = 0;
- count = 0;
- while(lastIndex != -1){
- lastIndex = str.indexOf(findStr,lastIndex);
- if(lastIndex != -1){
- count ++;
- lastIndex += findStr.length();
- }
- }
- String strOut=null;
- if ((cow==0 && bull==0 && count ==0) || (bull==4)){
- try{
- while ((strOut = stdInput.readLine())!=null)
- {
- System.out.println(strOut);
- FLAG=1;
- Thread.sleep(10000000);
- }
- } catch (Exception ex){}
- }
- System.out.println("Bull:"+bull + '\n'+"Cow:"+cow);
- }
- public String send(String num){
- String strOut = null,strTemp=null;
- try{
- num+='\n';
- stdIn.write(num.getBytes ("US-ASCII"));
- stdIn.flush();
- strOut = stdInput.readLine();
- System.out.println(strOut);
- strTemp = stdInput.readLine();
- //System.out.println(strOut);
- } catch( IOException ex) {}
- System.out.println(num);
- String bullCowStr=strOut.substring(36);
- getBullCow(bullCowStr);
- return strOut;
- }
- String buf;
- String list;
- static int n=4;
- public void clear(){
- stdInput=null;
- stdError=null;
- stdIn=null;
- rt =null;
- }
- public void game(){
- }
- public static void main(String[] args){
- exe CTF=new exe();
- CTF.init();
- CTF.send("1234");//return string là kết quả đồng thời tự thay đổi số cow, bull luôn
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement