import java.awt.*; import java.io.*; import java.net.*; public class TTT extends Frame { public TTT(String[] args) { init(args); setTitle("TicTacToe: "+user); Panel p = new Panel(); p.setLayout(new GridLayout(3,3)); for (int i = 0; i < 9; i++) { p.add(But[i]= new Button()); } add("Center", p); cleanup(); } public void insensitive() { for (int i=0; i<9; i++) { But[i].disable(); } } public void sensitive() { for (int i=0; i<9; i++) { if(owner[i].equals("NONE")) { But[i].enable(); } } } public String other(String x) { if (x.equals("USER1")) return("USER2"); else return("USER1"); } public void cleanup() { for (int i=0; i<9;i++) { But[i].setLabel(" "); owner[i]="NONE"; } numselected=0; if (turn.equals(user)) sensitive(); else insensitive(); turn=other(turn); mon = new Monitor(this); mon.start(); } public void callback(int i) { if (user.equals("USER2")) { But[i].setLabel(user1sym); owner[i] = "USER1"; } else { But[i].setLabel(user2sym); owner[i] = "USER2"; } numselected++; checkstate(true); } public void gameover(String win) { String str = ""; if(win.equals("NONE")) str="No winner, It is a tie!"; else if(win.equals(user)) str="You are the winner!"; else str="You are the loser!"; OverDialog d = new OverDialog(this,this,str); d.show(); } public void checkstate(boolean makesense) { String winner ="NONE"; int TOP_LT = 0; int TOP_MD = 1; int TOP_RT = 2; int MID_LT = 3; int MID_MD = 4; int MID_RT = 5; int BUT_LT = 6; int BUT_MD = 7; int BUT_RT = 8; if((owner[TOP_LT]==owner[TOP_MD])&&(owner[TOP_MD]==owner[TOP_RT])|| (owner[TOP_LT]==owner[MID_LT])&&(owner[MID_LT]==owner[BUT_LT])) if (!owner[TOP_LT].equals("NONE")) winner=owner[TOP_LT]; if((owner[BUT_RT]==owner[BUT_MD])&&(owner[BUT_MD]==owner[BUT_LT])|| (owner[BUT_RT]==owner[MID_RT])&&(owner[MID_RT]==owner[TOP_RT])) if(!owner[BUT_RT].equals("NONE")) winner=owner[BUT_RT]; if((owner[MID_MD]==owner[TOP_LT])&&(owner[TOP_LT]==owner[BUT_RT])|| (owner[MID_MD]==owner[TOP_RT])&&(owner[TOP_RT]==owner[BUT_LT])|| (owner[MID_MD]==owner[TOP_MD])&&(owner[TOP_MD]==owner[BUT_MD])|| (owner[MID_MD]==owner[MID_LT])&&(owner[MID_LT]==owner[MID_RT])) if(!owner[MID_MD].equals("NONE")) winner=owner[MID_MD]; if (!winner.equals("NONE")) gameover(winner); else if (numselected==9) gameover("NONE"); else if (makesense==true) { sensitive(); mon.stop(); } else { insensitive(); mon = new Monitor(this); mon.start(); } } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { for (int i = 0; i<9; i++) { if (evt.target==(But[i])) { if (user.equals("USER1")) { But[i].setLabel(user1sym); owner[i] = "USER1"; } else { But[i].setLabel(user2sym); owner[i] = "USER2"; } insensitive(); numselected++; out.println(" "+i+" \r"); checkstate(false); } } return true; } private void init_server() { user = "USER1"; try { ServerSocket s = new ServerSocket(0); String hostname =s.getInetAddress().getLocalHost().toString(); hostname= hostname.substring(0,hostname.indexOf('/')); System.out.println("The other player should type: java TTT "+hostname+" "+s.getLocalPort()); Socket incoming = s.accept(); in = new DataInputStream(incoming.getInputStream()); out = new PrintStream(incoming.getOutputStream()); } catch (Exception e) { System.out.println(e); } } private void init_client(String host, String strport) { int port = Integer.parseInt(strport.trim()); user="USER2"; try { incoming = new Socket(host,port); in = new DataInputStream(incoming.getInputStream()); out = new PrintStream(incoming.getOutputStream()); } catch (Exception e) { System.out.println(e); } } public void init(String[] args) { if (args.length==0) init_server(); else if (args.length==2) init_client(args[0],args[1]); else { System.out.println("TicTacToe for Java v.1.0\nError:Wrong Number of Arguments\nTTT [hostname portnumber]"); System.exit(0); } turn = "USER2"; user1sym = "X X\n X X \n X \n X X \nX X"; user2sym = " OOO \n O O \n O O \n O O \n OOO "; } public static void main(String[] args) { Frame f = new TTT(args); f.resize(300, 300); f.show(); } public Button[] But = new Button[9]; public String user; public String[] owner = new String[9]; public Socket incoming; public ServerSocket s; public int port; public DataInputStream in; public PrintStream out; public Monitor mon; public String user1sym; public String user2sym; public int numselected=0; public String turn; } class Monitor extends Thread { public Monitor(TTT t) { ttt = t; } public void run() { try { String str = ttt.in.readLine(); int i =Integer.parseInt(str.trim()); ttt.callback(i); } catch(Exception e) { System.out.println(e); } } public TTT ttt; } class OverDialog extends Dialog { public OverDialog(TTT t, Frame f, String strlabel) { super(f,"Game Over!", true); Panel p1 = new Panel(); p1.add("North",new Label(strlabel)); p1.add("South",new Label("Would you like to play another game?")); Panel p2 = new Panel(); p2.add(new Button("YES")); p2.add(new Button("NO")); add("Center",p1); add("South",p2); resize(300,120); ttt=t; } public void checkagain(int respond) { int i=0; try { String str = ttt.in.readLine(); i =Integer.parseInt(str.trim()); if ((respond==1)&&(i==1)) { ttt.cleanup(); } else { System.exit(0); } } catch(Exception e) { System.out.println(e); } } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) dispose(); else return super.handleEvent(evt); return true; } public boolean action(Event evt, Object arg) { if (arg.equals("YES")) { ttt.out.println(" 1 \r"); dispose(); checkagain(1); } else if(arg.equals("NO")) { ttt.out.println(" 0 \r"); dispose(); checkagain(0); } else return false; return true; } private TTT ttt; }