Forum
Tipps
News
Menu-Icon

Memory

Ich muss in der Schule gerade ein Memory mit Kommunikation übern ein Socket programmieren. Allerdings habe ich Probleme wenn man abwechselnd spielt, da es dann nicht mehr einwandfrei zurückgedreht wird. Vielleicht kann mir jemand helfen.

package memory;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.*;

public class MyJFrame extends JFrame implements ActionListener,Runnable{

   MyJPanel myp= new MyJPanel();
   JPanel versuch= new JPanel();
   ServerSocket sev;
   Socket s;
   BufferedReader br;
   OutputStream os;
   PrintWriter pw;
   InputStream is;
   JButton end;
   ImageIcon[] icons=new ImageIcon[16];
   ImageIcon hinten= new ImageIcon("o.PNG");
   JButton buttons[]= new JButton[16];
   int j=115;
   int a=115;
   int fertig=0;
   
   boolean gedrueckt=false;
   int erstes,zweites;
   ImageIcon aufgedeckt=null;
   ImageIcon vorherg=null;
   
   // JLabel versuche= new JLabel ("Versuche: 0");
   
   
   
   
   MyJFrame(){
      
      this.setTitle("Spieler1");
      this.setSize(800,800);
      this.getContentPane().add(myp);
      
      
      Memory();
      
      try {
         sev=new ServerSocket(1000);
         s=sev.accept();
         is=s.getInputStream();
         br = new BufferedReader(new InputStreamReader(is));
         os=s.getOutputStream();
         pw=new PrintWriter(os,true);
         Thread th=new Thread(this);
         th.start();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
      this.getContentPane().add(myp);
      this.setVisible(true);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      
      
   }
   
   
class MyJPanel extends JPanel{
      
      MyJPanel()
      {
         this.setLayout(new GridLayout(4,3));
         this.setSize(800,800);
         
         
      }
   
}
   
   public static void main(String[] args){
      // TODO Auto-generated method stub
      
      MyJFrame f= new MyJFrame();
      
   }
   
   public void Memory(){
      
      int i;
      
      for(i=0; i< icons.length/2;i++)
      {
         icons=new ImageIcon(i+".PNG");
      }
      
      int j;
      
      for(j=0;j<icons.length/2;j++){
         icons[i+j]=new ImageIcon(j+".PNG");
      }
      
      for( i=0; i<buttons.length;i++){
         buttons=new JButton(hinten);
         
      }
      
      
      for(i=0;i< buttons.length;i++)
      {
         myp.add(buttons);
         buttons.setSize(200,200);
         buttons.addActionListener(this);
      }
      
      
      
      
      
   }
   
   
   
   @Override
   public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      
      
      
      for(int i=0;i<buttons.length;i++)
      {
         if(arg0.getSource()==buttons)
         {
            
            if(gedrueckt==true)
            {
               zweites=i;
               vorherg=aufgedeckt;
               aufgedeckt=icons;
               buttons.setIcon(aufgedeckt);
               fertig++;
               if(fertig==16){
                  System.out.println("sieg");
                  JOptionPane.showMessageDialog(myp, "Herzlichen Glückwunsch. Du hast gewonnen.");
                  pw.println(200);
                  
               }
               
               
               pw.println(i);
               gedrueckt=false;
            }
            else{
            
               if((aufgedeckt!=null) && (vorherg != null))
               {
                  
                  if(aufgedeckt.getImage()==vorherg.getImage())
                  {
                     aufgedeckt=null;
                     vorherg=null;
                     
                     
                     
                     
                  }
                  else
                  {
                     buttons[erstes].setIcon(hinten);
                     buttons[zweites].setIcon(hinten);
                     fertig=fertig-2;
                     pw.println(100);
                     
                  }
                  
               
               }
            
            
               
               
            
            erstes=i;
            aufgedeckt=icons;
            buttons[erstes].setIcon(aufgedeckt);
            pw.println(i);
            gedrueckt=true;
            fertig++;   
         }
         }
         else if(arg0.getSource()==end)
         {
            System.out.println("Ihre Streams und Reader/Writer werden jetzt geschlossen.");
            try {
            is.close();
            br.close();
            os.close();
            pw.close();
            s.close();
            } catch (IOException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
         }
         
         
         
      }
      
      
      
      
   }

   @Override
   public void run() {
      // TODO Auto-generated method stub
      
      while(true)
      {

         
         
      String str;
      try {
         
         str  = br.readLine();
         int i=Integer.parseInt(str);
         System.out.println(i);
         
         
         if(i==200){
            JOptionPane.showMessageDialog(myp, "Ohjee du hast wohl verloren. Vielleicht klappts beim nächsten Mal");
            
            
         }
         
         
         
         
         if(i==100)
         {
            System.out.println("hallo");
            buttons[j].setIcon(hinten);
            buttons[a].setIcon(hinten);
            a=115;
            j=115;
         }
         
         
         else {
            buttons.setIcon(icons);
            
            if((j!=115)&&(a!=115)){
               
               
               
               
               j=115;
               a=115;
            }
            
            if(j!=115)
            {
               a=i;
            }
            
            if(j==115){
               j=i;
               System.out.println("ha");
            }
            
            
            
            
         }
         
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }}
         
      
      
   
   
      
   
}}

_______________________________

package memory;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

public class MyFrame2 extends JFrame implements ActionListener,Runnable{

   MyJPanel myp= new MyJPanel();
   
   int fertig=0;
   int treffer=0;
   int j=115;
   int a=115;
   
   Socket s;
   JButton end;
   ImageIcon[] icons=new ImageIcon[16];
   ImageIcon hinten= new ImageIcon("o.PNG");
   JButton buttons[]= new JButton[16];
   
   boolean gedrueckt=false;
   int erstes,zweites;
   ImageIcon aufgedeckt=null;
   ImageIcon vorherg=null;
   
   // JLabel versuche= new JLabel ("Versuche: 0");
   
   BufferedReader br;
   OutputStream os;
   PrintWriter pw;
   InputStream is;
   
   
   
   MyFrame2(){
      
      
      this.setSize(800,800);
      
      Memory();
      try {
         s=new Socket("localhost",1000);
         is=s.getInputStream();
         br = new BufferedReader(new InputStreamReader(is));
         os=s.getOutputStream();
         pw=new PrintWriter(os,true);
         Thread th=new Thread(this);
         th.start();
         
      } catch (UnknownHostException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      this.getContentPane().add(myp);
      this.setVisible(true);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      
   }
   
   
class MyJPanel extends JPanel{
      
      MyJPanel()
      {
         this.setLayout(new GridLayout(4,3));
         this.setSize(800,800);
         
         
      }
   
}
   
   public static void main(String[] args){
      // TODO Auto-generated method stub
      
      MyFrame2 f= new MyFrame2();
      
   }
   
   
   public void hinten(){
      
      buttons[erstes].setIcon(hinten);
      buttons[zweites].setIcon(hinten);
      
   }
   
   public void Memory(){
      
      int i;
      
      for(i=0; i< icons.length/2;i++)
      {
         icons=new ImageIcon(i+".PNG");
      }
      
      int j;
      
      for(j=0;j<icons.length/2;j++){
         icons[i+j]=new ImageIcon(j+".PNG");
      }
      
      for( i=0; i<buttons.length;i++){
         buttons=new JButton(hinten);
         
      }
      
      
      for(i=0;i< buttons.length;i++)
      {
         myp.add(buttons);
         buttons.setSize(200,200);
         buttons.addActionListener(this);
      }
      
      
      
      
      
   }
   
   
   
   @Override
   public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      
      
      
      for(int i=0;i<buttons.length;i++)
      {
         if(arg0.getSource()==buttons)
         {
            
            if(gedrueckt==true)
            {
               zweites=i;
               vorherg=aufgedeckt;
               aufgedeckt=icons;
               buttons.setIcon(aufgedeckt);
               fertig++;
               if(fertig==16){
                  System.out.println("sieg");
                  JOptionPane.showMessageDialog(myp, "Herzlichen Glückwunsch. Du hast gewonnen.");
                  pw.println(200);
               }
               
               pw.println(i);
               
               gedrueckt=false;
            }
            
            else{
               if((aufgedeckt!=null) && (vorherg != null))
               {
                  
                  if(aufgedeckt.getImage()==vorherg.getImage())
                  {
                     aufgedeckt=null;
                     vorherg=null;
                     
                     
                     
                  }
                  else
                  {
                     hinten();
                     fertig=fertig-2;
                     pw.println(100);
                     
                  }
                  
               
               }
            
            
            
            erstes=i;
            aufgedeckt=icons;
            buttons[erstes].setIcon(aufgedeckt);
            
            pw.println(i);
            gedrueckt=true;
            fertig++;
            
            }
         }
         
         else if(arg0.getSource()==end)
         {
            System.out.println("Ihre Streams und Reader/Writer werden jetzt geschlossen.");
            try {
            is.close();
            br.close();
            os.close();
            pw.close();
            s.close();
            } catch (IOException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
         
         
      }
      
      }
      
      
   }

   @Override
   public void run() {
      // TODO Auto-generated method stub
      
      while(true)
      {

         
         
         
      String str;
      try {
         System.out.println("binda");
         str  = br.readLine();
         int i=Integer.parseInt(str);
         System.out.println(i);
         
         
         if(i==200){
            JOptionPane.showMessageDialog(myp, "Ohjee du hast wohl verloren. Vielleicht klappts beim nächsten Mal");
            
            
         }
         
         
         
         if(i==100)
         {
            buttons[j].setIcon(hinten);
            buttons[a].setIcon(hinten);
            a=115;
            j=115;
         }
         
         
         else {
            buttons.setIcon(icons);
            
            if((j!=115)&&(a!=115)){
               
               
               
               
               
               
               j=115;
               a=115;
            }
            
            if(j!=115)
            {
               a=i;
            }
            
            
            if(j==115){
               j=i;
            }
            
            
            
            
         }
         
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }}
         
      
      
   
   
      
   
}
   
      
      
      
      
   }
   
   
   


« Hilfe bei Netlogo Agent Based ModellingWindows Batch »
 

Schnelle Hilfe: Hier nach ähnlichen Fragen und passenden Tipps suchen!

Fremdwörter? Erklärungen im Lexikon!
Grundstrich
Der Begriff des Grundstrichs im Bereich der Typografie, bezeichnet den senkrechten Strich der Buchstaben. Bei Schriftarten mit variabler Strichstärke, wie zum Beispi...

Haarstrich
Der Begriff Haarstrich stammt aus dem Bereich der Typographie. Bei Schriften, wie zum Beispiel der Antiquaschrift mit unterschiedlichen Strichstärken, wird zwischen ...

Memory-Stick
  Der Memory Stick ist ein digitales Speichermedium, das 1998 von Sony eingeführt wurdep - parallel zu Konkurrenzprodukten wie CompactFlash-Karten (CF) und S...