import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class klient{ public static void main(String argumendid[])throws IOException{ Frame f=new Frame("Rändurid"); Panel p=new Panel(); p.setLayout(new BorderLayout()); Label SK=new Label("Sinu kord"); SK.setVisible(false); Panel nupud=new Panel(); nupud.setLayout(new BorderLayout()); Button up=new Button("Üles"); up.setEnabled(false); Button down=new Button("Alla"); down.setEnabled(false); Button right=new Button("Paremale"); right.setEnabled(false); Button left=new Button("Vasakule"); left.setEnabled(false); nupud.add("North",up); nupud.add("South",down); nupud.add("West",left); nupud.add("East",right); Panel tabel=new Panel(); tabel.setLayout(new GridLayout(10,10)); Label l[][]=new Label[10][10]; Button alg=new Button("Alusta!"); for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ l[i][j]=new Label("|__|"); l[i][j].setBackground(Color.white); tabel.add(l[i][j]); } } l[0][5].setBackground(Color.black); l[1][8].setBackground(Color.black); l[2][1].setBackground(Color.black); l[3][3].setBackground(Color.black); l[3][6].setBackground(Color.black); l[4][5].setBackground(Color.black); l[5][4].setBackground(Color.black); l[6][3].setBackground(Color.black); l[6][7].setBackground(Color.black); l[7][1].setBackground(Color.black); l[8][8].setBackground(Color.black); l[9][5].setBackground(Color.black); p.add("Center",tabel); Panel pa=new Panel(); pa.setLayout(new BorderLayout()); pa.add("East",nupud); Panel lb=new Panel(); lb.setLayout(new GridLayout(4,1)); lb.add(alg); lb.add(SK); Label v=new Label("Võitsid!"); lb.add(v); v.setVisible(false); Label k=new Label("Kaotasid."); lb.add(k); k.setVisible(false); pa.add("Center",lb); alg.addActionListener(new Mang(up,down,left,right,alg,l,v,k,SK)); p.add("South",pa); f.add(p); f.setVisible(true); f.pack(); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } } class Up implements ActionListener{ PrintWriter valja; Socket sc; public Up(PrintWriter valj, Socket s){ valja=valj; sc=s; } public void actionPerformed(ActionEvent e){ try{ valja.println("8"); }catch(Exception el){} } } class Down implements ActionListener{ PrintWriter valja; Socket sc; public Down(PrintWriter valj, Socket s){ valja=valj; sc=s; } public void actionPerformed(ActionEvent e){ try{ valja.println("2"); }catch(Exception ex){} } } class Left implements ActionListener{ PrintWriter valja; Socket sc; public Left(PrintWriter valj, Socket s){ valja=valj; sc=s; } public void actionPerformed(ActionEvent e){ try{ valja.println("4"); }catch(Exception exc){} } } class Right implements ActionListener{ PrintWriter valja; Socket sc; public Right(PrintWriter valj, Socket s){ valja=valj; sc=s; } public void actionPerformed(ActionEvent e){ try{ valja.println("6"); }catch(Exception exce){} } } class Mang implements ActionListener, Runnable{ Button up; Button down; Button left; Button right; Button alg; Label[][] l; Label v; Label k; Label SK; public Mang(Button up1,Button down1,Button left1,Button right1,Button alg1, Label[][] l1, Label v1, Label k1, Label SK1){ up=up1; down=down1; left=left1; right=right1; alg=alg1; l=l1; v=v1; k=k1; SK=SK1; } public void actionPerformed(ActionEvent e){ new Thread(this).start(); } public void run(){ try{ Socket sc=new Socket("ws8.cs2.tpu.ee", 3001); alg.setEnabled(false); BufferedReader sisse=new BufferedReader(new InputStreamReader(sc.getInputStream())); PrintWriter valja=new PrintWriter(sc.getOutputStream(), true); right.addActionListener(new Right(valja,sc)); down.addActionListener(new Down(valja,sc)); left.addActionListener(new Left(valja,sc)); up.addActionListener(new Up(valja,sc)); int x=9,y=0,tx=9,ty=0; boolean voit=false; while(!voit){ String rida=sisse.readLine(); System.out.println(rida); if(rida.equals("SK")|rida.equals("v")){ SK.setVisible(true); up.setEnabled(true); down.setEnabled(true); left.setEnabled(true); right.setEnabled(true); } if(rida.startsWith("s")){ up.setEnabled(false); down.setEnabled(false); left.setEnabled(false); right.setEnabled(false); SK.setVisible(false); String t=rida.substring(1,rida.length()); StringTokenizer str=new StringTokenizer(t, "|", false); l[x][y].setBackground(Color.white); x=Integer.valueOf(str.nextToken()).intValue(); y=Integer.valueOf(str.nextToken()).intValue(); for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ if(l[i][j].getBackground()==Color.blue){ l[x][y].setBackground(Color.gray); } } } l[x][y].setBackground(Color.blue); } if(rida.startsWith("t")){ String t=rida.substring(1,rida.length()); StringTokenizer str=new StringTokenizer(t, "|", false); l[tx][ty].setBackground(Color.white); tx=Integer.valueOf(str.nextToken()).intValue(); ty=Integer.valueOf(str.nextToken()).intValue(); l[tx][ty].setBackground(Color.red); } if(rida.startsWith("!")){ voit=true; v.setVisible(true); } if(rida.startsWith("(")){ voit=true; k.setVisible(true); } } }catch(Exception eg){System.out.println("Viga: "+eg);} } }