华为
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.net.ConnectException; import java.net.InetAddress; import java.net.Socket; public class game { public static void main(String args[]) { try { InetAddress localIP = InetAddress.getByName(args[2]); int serverPort = Integer.parseInt(args[1]); int localPort = Integer.parseInt(args[3]); Socket s = new Socket(args[0], serverPort, localIP, localPort); InputStream mmInStream = s.getInputStream(); OutputStream mmOutStream = s.getOutputStream(); PrintStream ps = new PrintStream(mmOutStream); ps.println("reg: "+args[4]+" aaa need_notify \\n"); StringBuffer resultbuffer = new StringBuffer(); String sAck; int bytes; File file =new File("/home/game/game/mylog.txt"); file.setWritable(true, false); if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsolutePath(),true); BufferedWriter bw = new BufferedWriter(fw); while(true) { byte[] buffer = new byte[10240]; try { if ((bytes = mmInStream.read(buffer)) > 0) { resultbuffer.append(new String(buffer, 0, bytes)); } } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } sAck = resultbuffer.toString(); bw.write(sAck); System.out.println(sAck); if(sAck.contains("game-over")) { bw.close(); break; } buffer = null; bytes = 0; resultbuffer.delete(0, resultbuffer.length()-1); } s.close(); } catch (ConnectException e) { System.out.println(e); } catch (IOException ee) { System.out.println(ee); } catch (Exception eee) { System.out.println(eee); } } }
华为
all_in
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class game { private static final String seat_Head = "seat/"; private static final String seat_Tail = "/seat"; private static final String blind_Head = "blind/"; private static final String blind_Tail = "/blind"; private static final String hold_Head = "hold/"; private static final String hold_Tail = "/hold"; private static final String inquire_Head = "inquire/"; private static final String inquire_Tail = "/inquire"; private static final String notify_Head = "notify/"; private static final String notify_Tail = "/notify"; private static final String flop_Head = "flop/"; private static final String flop_Tail = "/flop"; private static final String turn_Head = "turn/"; private static final String turn_Tail = "/turn"; private static final String river_Head = "river/"; private static final String river_Tail = "/river"; private static final String showdown_Head = "showdown/"; private static final String showdown_Tail = "/showdown"; private static final String common_Head = "common/"; private static final String common_Tail = "/common"; private static final String potwin_Head = "pot-win/"; private static final String potwin_Tail = "/pot-win"; private static InetAddress localIP; private static String serverIP; private static int serverPort; private static int localPort; private static String playerID; private InputStream mmInStream; private OutputStream mmOutStream; private Socket s; private BufferedWriter bw; private PrintStream ps; private boolean loopflag; private String personal_poker1; private String personal_poker2; private String commom_poker1; private String commom_poker2; private String commom_poker3; private String commom_poker4; private String commom_poker5; public static void main(String args[]) { serverIP = args[0]; serverPort = Integer.parseInt(args[1]); try { localIP = InetAddress.getByName(args[2]); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } localPort = Integer.parseInt(args[3]); playerID = args[4]; game myGame = new game(); myGame.register(); myGame.message_handler(); } public void register() { try { s = new Socket(serverIP, serverPort, localIP, localPort); mmInStream = s.getInputStream(); mmOutStream = s.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ps = new PrintStream(mmOutStream); ps.println("reg: "+ playerID+" aaa need_notify"); } public void message_handler(){ File file =new File("/home/game/game/mylog.txt"); file.setWritable(true, false); try { if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsolutePath(),true); bw = new BufferedWriter(fw); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuffer resultbuffer = new StringBuffer(); String sAck; int bytes; while(true){ sAck = ""; byte[] buffer = new byte[10240]; try { if ((bytes = mmInStream.read(buffer)) > 0) { resultbuffer.append(new String(buffer, 0, bytes)); } } catch (IOException e) { e.printStackTrace(); } sAck = resultbuffer.toString(); try { bw.write(sAck); } catch (IOException e) { e.printStackTrace(); } int start,end; if( (start = sAck.indexOf(hold_Head)) != -1) { end = sAck.indexOf(hold_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); for (int i = 0; i < ss.length; i++) { System.out.println(ss[i]); } } if( (start = sAck.indexOf(inquire_Head)) != -1) { end = sAck.indexOf(inquire_Tail); ps.println("all_in"); } if(sAck.contains("game-over")) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } buffer = null; bytes = 0; start = 0; end = 0; resultbuffer.delete(0, resultbuffer.length()-1); } } }
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class game { private static final String seat_Head = "seat/"; private static final String seat_Tail = "/seat"; private static final String blind_Head = "blind/"; private static final String blind_Tail = "/blind"; private static final String hold_Head = "hold/"; private static final String hold_Tail = "/hold"; private static final String inquire_Head = "inquire/"; private static final String inquire_Tail = "/inquire"; private static final String notify_Head = "notify/"; private static final String notify_Tail = "/notify"; private static final String flop_Head = "flop/"; private static final String flop_Tail = "/flop"; private static final String turn_Head = "turn/"; private static final String turn_Tail = "/turn"; private static final String river_Head = "river/"; private static final String river_Tail = "/river"; private static final String showdown_Head = "showdown/"; private static final String showdown_Tail = "/showdown"; private static final String common_Head = "common/"; private static final String common_Tail = "/common"; private static final String potwin_Head = "pot-win/"; private static final String potwin_Tail = "/pot-win"; private static InetAddress localIP; private static String serverIP; private static int serverPort; private static int localPort; private static String playerID; private InputStream mmInStream; private OutputStream mmOutStream; private Socket s; private BufferedWriter bw; private PrintStream ps; private boolean loopflag; private String personal_poker1; private String personal_poker2; private String commom_poker1; private String commom_poker2; private String commom_poker3; private String commom_poker4; private String commom_poker5; public static void main(String args[]) { serverIP = args[0]; serverPort = Integer.parseInt(args[1]); try { localIP = InetAddress.getByName(args[2]); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } localPort = Integer.parseInt(args[3]); playerID = args[4]; game myGame = new game(); myGame.register(); myGame.message_handler(); } public void register() { try { s = new Socket(serverIP, serverPort, localIP, localPort); mmInStream = s.getInputStream(); mmOutStream = s.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ps = new PrintStream(mmOutStream); ps.println("reg: "+ playerID+" aaa need_notify"); } public void message_handler(){ File file =new File("/home/game/game/mylog.txt"); file.setWritable(true, false); try { if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsolutePath(),true); bw = new BufferedWriter(fw); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuffer resultbuffer = new StringBuffer(); String sAck; int bytes; while(true){ sAck = ""; byte[] buffer = new byte[1024]; try { if ((bytes = mmInStream.read(buffer)) > 0) { resultbuffer.append(new String(buffer, 0, bytes)); } } catch (IOException e) { e.printStackTrace(); } sAck = resultbuffer.toString(); try { bw.write(sAck); } catch (IOException e) { e.printStackTrace(); } int start,end; if( (start = sAck.indexOf(hold_Head)) != -1) { end = sAck.indexOf(hold_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); for (int i = 0; i < ss.length; i++) { System.out.println(ss[i]); } } if( (start = sAck.indexOf(inquire_Head)) != -1) { end = sAck.indexOf(inquire_Tail); ps.println("all_in"); } if(sAck.contains("game-over")) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } buffer = null; bytes = 0; start = 0; end = 0; resultbuffer.delete(0, resultbuffer.length()-1); } } }
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class game { // private static final String[] spokers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; private static final String spokers = "2345678910JQKA"; private static final String seat_Head = "seat/"; private static final String seat_Tail = "/seat"; private static final String blind_Head = "blind/"; private static final String blind_Tail = "/blind"; private static final String hold_Head = "hold/"; private static final String hold_Tail = "/hold"; private static final String inquire_Head = "inquire/"; private static final String inquire_Tail = "/inquire"; private static final String notify_Head = "notify/"; private static final String notify_Tail = "/notify"; private static final String flop_Head = "flop/"; private static final String flop_Tail = "/flop"; private static final String turn_Head = "turn/"; private static final String turn_Tail = "/turn"; private static final String river_Head = "river/"; private static final String river_Tail = "/river"; private static final String showdown_Head = "showdown/"; private static final String showdown_Tail = "/showdown"; private static final String common_Head = "common/"; private static final String common_Tail = "/common"; private static final String potwin_Head = "pot-win/"; private static final String potwin_Tail = "/pot-win"; private static InetAddress localIP; private static String serverIP; private static int serverPort; private static int localPort; private static String playerID; private InputStream mmInStream; private OutputStream mmOutStream; private Socket s; private BufferedWriter bw; private PrintStream ps; private boolean loopflag; private String personal_poker1; private String personal_poker2; private String commom_poker1; private String commom_poker2; private String commom_poker3; private String commom_poker4; private String commom_poker5; private final int CHECK = -1; private final int CALL = -2; private final int RAISENUM = -3; private final int ALL_IN = -4; private final int FOLD = -5; private int state = CALL; private double PotOdds; private double Odds; private int bet; private int big_blind; private int small_blind; private int total_pot; private int hand_level; private int betRound; private boolean pre_flopAll_in; public static void main(String args[]) { serverIP = args[0]; serverPort = Integer.parseInt(args[1]); try { localIP = InetAddress.getByName(args[2]); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } localPort = Integer.parseInt(args[3]); playerID = args[4]; game myGame = new game(); myGame.register(); myGame.message_handler(); } public void register() { try { s = new Socket(serverIP, serverPort, localIP, localPort); mmInStream = s.getInputStream(); mmOutStream = s.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ps = new PrintStream(mmOutStream); ps.println("reg: "+ playerID+" aaa need_notify"); } public void message_handler(){ File file =new File("/home/game/game/mylog.txt"); file.setWritable(true, false); try { if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsolutePath(),true); bw = new BufferedWriter(fw); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuffer resultbuffer = new StringBuffer(); String sAck; int bytes; while(true){ sAck = ""; byte[] buffer = new byte[1024]; try { if ((bytes = mmInStream.read(buffer)) > 0) { resultbuffer.append(new String(buffer, 0, bytes)); } } catch (IOException e) { e.printStackTrace(); } sAck = resultbuffer.toString(); try { bw.write(sAck); } catch (IOException e) { e.printStackTrace(); } int start,end; if( (start = sAck.indexOf(blind_Head)) != -1) { end = sAck.indexOf(blind_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); String[] ss1 = ss[ss.length-1].split(" "); String[] ss2 = ss[ss.length-2].split(" "); int i =Integer.parseInt(ss1[1]); int j =Integer.parseInt(ss2[1]); if(i>j) { big_blind = i; small_blind = j; }else{ big_blind = j; small_blind = i; } System.out.println("big_blind="); System.out.println(big_blind); /* System.out.println(color1); System.out.println(value1); System.out.println(personal_poker1); System.out.println(color2); System.out.println(value2); System.out.println(personal_poker2); */ } else if( (start = sAck.indexOf(hold_Head)) != -1) { end = sAck.indexOf(hold_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); personal_poker1 = ss[1]; personal_poker2 = ss[2]; hand_level = hand_determine(personal_poker1, personal_poker2); betRound = 1; /* String[] poker1 = personal_poker1.split(" "); String[] poker2 = personal_poker2.split(" "); String color1 = poker1[0]; String value1 = poker1[1]; String color2 = poker2[0]; String value2 = poker2[1]; System.out.println(color1); System.out.println(value1); System.out.println(personal_poker1); System.out.println(color2); System.out.println(value2); System.out.println(personal_poker2); */ } else if( (start = sAck.indexOf(flop_Head)) != -1) { end = sAck.indexOf(flop_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); commom_poker1 = ss[1]; commom_poker2 = ss[2]; commom_poker3 = ss[3]; String[] poker1 = commom_poker1.split(" "); String[] poker2 = commom_poker1.split(" "); String[] poker3 = commom_poker1.split(" "); String color1 = poker1[0]; String value1 = poker1[1]; String color2 = poker2[0]; String value2 = poker2[1]; String color3 = poker3[0]; String value3 = poker3[1]; betRound = 2; /* System.out.println(color1); System.out.println(value1); System.out.println(personal_poker1); System.out.println(color2); System.out.println(value2); System.out.println(personal_poker2); */ } else if( (start = sAck.indexOf(turn_Head)) != -1) { end = sAck.indexOf(turn_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); commom_poker4 = ss[1]; String[] poker4 = commom_poker4.split(" "); String color4 = poker4[0]; String value4 = poker4[1]; betRound = 3; /* System.out.println(color1); System.out.println(value1); System.out.println(personal_poker1); System.out.println(color2); System.out.println(value2); System.out.println(personal_poker2); */ } else if( (start = sAck.indexOf(river_Head)) != -1) { end = sAck.indexOf(river_Tail); String str = sAck.substring(start, end); String[] ss = str.split("\\n"); commom_poker5 = ss[1]; String[] poker5 = commom_poker5.split(" "); String color5 = poker5[0]; String value5 = poker5[1]; betRound = 4; System.out.println(commom_poker5); } else if( (start = sAck.indexOf(inquire_Head)) != -1) { end = sAck.indexOf(inquire_Tail); String inquire = sAck.substring(start, end); String[] ss1 = inquire.split("\\n"); String[] ss2 = ss1[ss1.length-1].split(" "); String stotal_pot = ss2[ss2.length-1]; total_pot = Integer.parseInt(stotal_pot); System.out.print("total_pot="); System.out.println(total_pot); Pattern p = Pattern.compile("\\d+\\s[a-z_]+"); Matcher m = p.matcher(inquire); StringBuffer sb = new StringBuffer(); while(m.find()) { sb.append(m.group()); } String[] ss3 = sb.toString().split("\\D+"); String[] ss4 = sb.toString().split("\\d+\\s"); int max = 0; for(int i=0; i<ss3.length; i++) { if(Integer.parseInt(ss3[i]) > max) { max = Integer.parseInt(ss3[i]); } } bet = max; for(int i=1; i<ss4.length; i++) { if(ss4[i].equalsIgnoreCase("all_in")) { pre_flopAll_in = true; } } if(betRound==1) { if(pre_flopAll_in){ if(hand_level==10 || hand_level==9 ||hand_level==3){ ps.println("call"); }else{ ps.println("fold"); } }else{ if(hand_level==10 || hand_level==9){ ps.println("all_in"); }else{ ps.println("call"); } } }else if(betRound==2){ ps.println("call"); }else{ ps.println("call"); } } else if(sAck.contains("game-over")) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } buffer = null; bytes = 0; start = 0; end = 0; resultbuffer.delete(0, resultbuffer.length()-1); } } private int hand_determine(String personal_poker1,String personal_poker2){ String[] poker1 = personal_poker1.split(" "); String[] poker2 = personal_poker2.split(" "); String s1 = poker1[0]; String s2 = poker1[1]; String s3 = poker2[0]; String s4 = poker2[1]; if(s1.equals(s3) && s2.equals(s4)) { return 10; }else if(s2.equals(s4)){ return 9; }else if(s1.equals(s3) && Math.abs(spokers.indexOf(s2)-spokers.indexOf(s4))==1){ return 8; }else if(s1.equals(s3) && Math.abs(spokers.indexOf(s2)-spokers.indexOf(s4))==2){ return 7; }else if(s1.equals(s3) && Math.abs(spokers.indexOf(s2)-spokers.indexOf(s4))>2){ return 6; }else{ if(spokers.indexOf(s2)>=12 || spokers.indexOf(s4)>=12){ return 3; }else if(Math.abs(spokers.indexOf(s2)-spokers.indexOf(s4)) > 2){ return 1; }else { return 2; } } } private void setState(int mstate){ state = mstate; } }
\n 和 \\n的选择:
static String sAck = "inquire/"+"\n"+
"4444 2000 8000 0 fold"+ "\n"+
"1111 0 8000 2000 all_in"+ "\n"+
"5555 1800 8000 200 raise"+ "\n"+
"3333 1900 8000 100 blind"+ "\n"+
"2222 1950 8000 50 blind"+ "\n"+
"total pot: 2350"+ "\n"+
"/inquire";
System.out.print(sAck);
控制台中的结果是:
inquire/ 4444 2000 8000 0 fold 1111 0 8000 2000 all_in 5555 1800 8000 200 raise 3333 1900 8000 100 blind 2222 1950 8000 50 blind total pot: 2350 /inquire
如果换成"\\n"
static String sAck = "inquire/"+"\n"+
"4444 2000 8000 0 fold"+ "\n"+
"1111 0 8000 2000 all_in"+ "\n"+
"5555 1800 8000 200 raise"+ "\n"+
"3333 1900 8000 100 blind"+ "\n"+
"2222 1950 8000 50 blind"+ "\n"+
"total pot: 2350"+ "\\n"+
"/inquire";
System.out.print(sAck);
控制台中的结果是:
inquire/ 4444 2000 8000 0 fold 1111 0 8000 2000 all_in 5555 1800 8000 200 raise 3333 1900 8000 100 blind 2222 1950 8000 50 blind total pot: 2350\n/inquire
切割字符串的问题:
String stemp = "i love you"; String[] ss4 = stemp.split("i"); String[] ss5 = stemp.split("u"); System.out.println(ss4.length); System.out.println(ss5.length); for(int i=0; i<ss4.length; i++) { System.out.print(ss4[i]); }
结果:
2
1
love you
以首字母为分界进行切割时,仍然会产生两部分,一部分为空,一部分为分界符后面的字符,第一部分不会打印出任何东西
System.out.print(ss4[0]);相当于
System.out.print("");
以尾字母切割时,只会产生一部分