import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.applet.Applet; public class laevaclient extends Applet implements MouseListener,Runnable{ PrintWriter v2lja; BufferedReader sisse; Choice valik1=new Choice(); Choice valik2=new Choice(); Label L1=new Label("Mis suurusega laev"); Label L2=new Label(", mis pidi"); int[] mass1=new int[100]; int[] mass2=new int[100]; int laev1=0,laev2=0,laev3=0,laev4=0; boolean algus=false; boolean kord; public laevaclient(){ valik1.add("1"); valik1.add("2"); valik1.add("3"); valik1.add("4"); valik2.add("|"); valik2.add("-"); add(L1); add(valik1); add(L2); add(valik2); addMouseListener(this); try{ Socket sc=new Socket("ws15.cs1.tpu.ee", 22222); v2lja=new PrintWriter(sc.getOutputStream(), true); sisse=new BufferedReader(new InputStreamReader(sc.getInputStream())); new Thread(this).start(); }catch(Exception viga){} } public void paint(Graphics g){ for (int i=0;i<100;i++){ switch(mass1[i]){ case 0: g.setColor(Color.green); break; case 1: g.setColor(Color.yellow); break; case 2: g.setColor(Color.blue); break; case 3: g.setColor(Color.red); } g.fillRect((i%10)*20+20,(i/10)*20+50,18,18); switch(mass2[i]){ case 0: g.setColor(Color.green); break; case 1: g.setColor(Color.yellow); break; case 2: g.setColor(Color.blue); break; case 3: g.setColor(Color.red); break; } g.fillRect((i%10)*20+520,(i/10)*20+50,18,18); } } public void mousePressed(MouseEvent e){ if (!algus){ int x=e.getX(); int y=e.getY(); AsetaLaev(x,y); }else{ int x=e.getX(); int y=e.getY(); if (kord)Pommita(x,y); } } public void AsetaLaev(int x, int y){ int posx=(x-20)/20; int posy=(y-50)/20; if (posx<10 && posy<10){ switch (Integer.parseInt(valik1.getSelectedItem())){ case 1: if (laev1<4){ mass1[posx+10*posy]=2; v2lja.println(""+(posx+10*posy)); laev1++; } break; case 2: if (laev2<3){ if (valik2.getSelectedIndex()==0){ mass1[posx+posy*10]=2; v2lja.println(""+(posx+posy*10)); mass1[posx+(posy+1)*10]=2; v2lja.println(""+(posx+(posy+1)*10)); }else{ mass1[posx+posy*10]=2; v2lja.println(""+(posx+posy*10)); mass1[posx+1+posy*10]=2; v2lja.println(""+(posx+1+posy*10)); } laev2++; } break; case 3: if (laev3<2){ if (valik2.getSelectedIndex()==0){ mass1[posx+posy*10]=2; v2lja.println(""+(posx+posy*10)); mass1[posx+(posy+1)*10]=2; v2lja.println(""+(posx+(posy+1)*10)); mass1[posx+(posy+2)*10]=2; v2lja.println(""+(posx+(posy+2)*10)); }else{ mass1[posx+posy*10]=2; v2lja.println(""+(posx+posy*10)); mass1[posx+1+posy*10]=2; v2lja.println(""+(posx+1+posy*10)); mass1[posx+2+posy*10]=2; v2lja.println(""+(posx+2+posy*10)); } laev3++; } break; case 4: if (laev4<1){ if (valik2.getSelectedIndex()==0){ mass1[posx+posy*10]=2; v2lja.println(""+(posx+posy*10)); mass1[posx+(posy+1)*10]=2; v2lja.println(""+(posx+(posy+1)*10)); mass1[posx+(posy+2)*10]=2; v2lja.println(""+(posx+(posy+2)*10)); mass1[posx+(posy+3)*10]=2; v2lja.println(""+(posx+(posy+3)*10)); }else{ mass1[posx+posy*10]=2; v2lja.println(""+(posx+posy*10)); mass1[posx+1+posy*10]=2; v2lja.println(""+(posx+1+posy*10)); mass1[posx+2+posy*10]=2; v2lja.println(""+(posx+2+posy*10)); mass1[posx+3+posy*10]=2; v2lja.println(""+(posx+3+posy*10)); } laev4++; } break; } } if (laev1==4 && laev2==3 && laev3==2 && laev4==1){ algus=true; remove(valik1); remove(valik2); L1.setText("Mäng algas"); L2.setText(""); v2lja.println("valmis"); } repaint(); } public void Pommita(int x, int y){ int posx=(x-520)/20 ; int posy=(y-50)/20; int index=posx+posy*10; if (index<100 && index>0) v2lja.println(""+index); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void run (){ try{ while(true){ String rida=sisse.readLine(); System.out.println(""+rida); if (rida.equals("m")){ kord=false; } if (rida.equals("k")){ kord=true; }else if (kord){ ++mass2[Integer.parseInt(rida)]; repaint(); }else {++mass1[Integer.parseInt(rida)];repaint();} } }catch(Exception e){e.printStackTrace();} } public static void main(String[] argumendid){ Frame f=new Frame(); f.add(new laevaclient()); f.setSize(800, 400); f.setVisible(true); f.addWindowListener(new Raamikuular()); } }