import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; public class Klient extends Applet implements ActionListener, MouseListener, Runnable{ int mangulaud1[][]={ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; int mangulaud2[][]={ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; int vaenlane[][]={ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; String tahed[]={"M", "E", "R", "O", "L", "A", "H", "I", "N", "G"}; String arvud[]={"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; int i; int j; int hiirex; int hiirey; int klikkidearv=0; int laevad=20; int kaikx=0; int kaiky=0; int kaik=0; int kordinaatx; int kordinaaty; int loendur=0; int pihtasloendur=0; boolean minukaik=true; TextField tf1=new TextField(15); Button nupp1=new Button("Ühenda"); Panel p1=new Panel(new GridLayout(2, 1)); PrintWriter pw1; BufferedReader br1; public Klient(){ setLayout(new BorderLayout()); p1.add(tf1); p1.add(nupp1); add(p1, BorderLayout.NORTH); tf1.addActionListener(this); nupp1.addActionListener(this); addMouseListener(this); } public void paint(Graphics g){ for(i=0; i<10; i++){ for(j=0; j<10; j++){ if(mangulaud1[i][j]==0){ g.setColor(Color.blue); } if(mangulaud1[i][j]==3){ g.setColor(Color.red); } if(mangulaud1[i][j]==1){ g.setColor(Color.green); } if(mangulaud1[i][j]==2){ g.setColor(Color.yellow); } g.fillRect(30+i*20, 80+j*20, 20, 20); } } g.setColor(Color.black); for(i=0; i<11; i++){ g.drawLine(30+i*20, 60, 30+i*20, 280); g.drawLine(10, 80+i*20, 230, 80+i*20); } for(i=0; i<10; i++){ g.drawString(tahed[i], 35+i*20, 75); g.drawString(arvud[i], 10, 95+i*20); } for(i=0; i<10; i++){ for(j=0; j<10; j++){ if(mangulaud2[i][j]==0){ g.setColor(Color.blue); } if(mangulaud2[i][j]==3){ g.setColor(Color.red); } if(mangulaud2[i][j]==1){ g.setColor(Color.green); } if(mangulaud2[i][j]==2){ g.setColor(Color.yellow); } g.fillRect(30+i*20, 320+j*20, 20, 20); } } g.setColor(Color.black); for(i=0; i<11; i++){ g.drawLine(30+i*20, 300, 30+i*20, 520); g.drawLine(10, 320+i*20, 230, 320+i*20); } for(i=0; i<10; i++){ g.drawString(tahed[i], 35+i*20, 315); g.drawString(arvud[i], 10, 335+i*20); } } public void mousePressed(MouseEvent e){ int hiirex=e.getX(); int hiirey=e.getY(); if(minukaik==true){ tf1.setText("Vastase käik"); if(laevad>klikkidearv){ for(i=0; i<10; i++){ for(j=0; j<10; j++){ if (hiirex>30+i*20 && hiirex<50+i*20 && hiirey>80+j*20 && hiirey<100+j*20){ mangulaud1[i][j]=1; kaik=(i+1)+(j*10); pw1.println(kaik); klikkidearv++; } } } } } if(minukaik==false){ for(i=0; i<10; i++){ for(j=0; j<10; j++){ if (hiirex>30+i*20 && hiirex<50+i*20 && hiirey>320+j*20 && hiirey<340+j*20){ kaik=(i+1)+(j*10); } } } pw1.println(kaik); } repaint(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==tf1){ pw1.println(tf1.getText()); tf1.setText(""); } if(e.getSource()==nupp1){ try{ Socket sc=new Socket("ws35.hk.tpu.ee", 3001); pw1=new PrintWriter(sc.getOutputStream(), true); br1=new BufferedReader(new InputStreamReader(sc.getInputStream())); new Thread(this).start(); }catch(Exception viga){ tf1.setText(viga.getMessage()); } } } public void run(){ try{ while(true){ String tekst=br1.readLine(); try{ Integer.parseInt(tekst); loendur++; if(loendur%2==0){ minukaik=false; } else { minukaik=true; } if(tekst.equals("Sina alustad")){ } else { int arv=Integer.parseInt(tekst); for(i=0; i<10; i++){ for(j=0; j<10; j++){ int kontrollkaik=(i+1)+(j*10); if (kontrollkaik==arv){ kordinaatx=i; kordinaaty=j; } } } } }catch(Exception ex){ tf1.setText(tekst); } if(minukaik==true){ if(loendur<42){ vaenlane[kordinaatx][kordinaaty]=1; } else { if (vaenlane[kordinaatx][kordinaaty]==0){ mangulaud2[kordinaatx][kordinaaty]=2; } if (vaenlane[kordinaatx][kordinaaty]==1){ mangulaud2[kordinaatx][kordinaaty]=3; pihtasloendur++; if(pihtasloendur>=20){ pw1.println("Laevad põhjas"); } } } repaint(); } } }catch(Exception viga){ viga.printStackTrace(); tf1.setText("Oled väljas"); } } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] argumendid){ Frame f=new Frame(); f.add(new Klient()); f.setSize(255, 560); f.setVisible(true); f.addWindowListener(new Raamikuular()); } static class Raamikuular extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.out.println("Programmi ots"); System.exit(0); } } }