Guest User

Untitled

a guest
Apr 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.81 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.geom.AffineTransform;
  6. import java.awt.image.BufferedImage;
  7.  
  8. /**
  9.  * Write a description of class Spielfeld here.
  10.  *
  11.  * @author Vale
  12.  * @version 12/2011
  13.  */
  14. public class Spielfeld extends JPanel
  15. {
  16.     private int[][] felder;
  17.     private static final int BREITE = 3;
  18.     private static final int HÖHE = 3;
  19.     private static final int FELD_BREITE = 50;
  20.     private static final int FELD_HÖHE = 50;
  21.     private static final int LÜCKEN_GRÖßE = 5;
  22.  
  23.     class Punkt{ public int x, y; public Punkt( int px, int py ) { x = px; y = py; } }
  24.  
  25.     final int STRICH_BREITE = 8; //Strichbreite der Kreuze
  26.  
  27.     private int spielerAktiv = 1;
  28.  
  29.     public Spielfeld( )
  30.     {
  31.         super();
  32.         felder = new int[HÖHE][BREITE];
  33.         setDoubleBuffered( true );
  34.         final Spielfeld ref = this;
  35.         addMouseListener(
  36.                 new MouseAdapter()
  37.                 {
  38.                     public void mouseClicked( MouseEvent e )
  39.                     {
  40.                         ref.mouseClicked( e );
  41.                     }
  42.                 }
  43.         );
  44.     }
  45.  
  46.     public void feldClicked( int zeile, int spalte )
  47.     {
  48.         int angeklickt = felder[zeile][spalte];
  49.         if( angeklickt != 0 )
  50.             return; //Feld gehört schon einem Spieler
  51.  
  52.         //Spieler tauschen
  53.         felder[zeile][spalte] = spielerAktiv;
  54.         if( spielerAktiv == 1 )
  55.             spielerAktiv = 2;
  56.         else
  57.             spielerAktiv = 1;
  58.  
  59.         //Felder neu malen
  60.         invalidate();
  61.         repaint();
  62.  
  63.         //Gewinner überprüfen
  64.         int gewinner = checkWin();
  65.         if( gewinner > 0 )
  66.         {
  67.             JOptionPane.showMessageDialog( this, "Spieler " + gewinner + " hat gewonnen!", "Glückwunsch!", JOptionPane.INFORMATION_MESSAGE );
  68.             spielfeldFüllen();
  69.         }
  70.         else if( gewinner < 0 )
  71.         {
  72.             JOptionPane.showMessageDialog( this, "Kein Spieler hat gewonnen", "Unentschieden", JOptionPane.INFORMATION_MESSAGE );
  73.             spielfeldFüllen();
  74.         }
  75.     }
  76.  
  77.     public void spielfeldFüllen()
  78.     {
  79.         for( int posH = 0; posH < HÖHE; posH++ )
  80.         {
  81.             for( int posW = 0; posW < BREITE; posW++ )
  82.             {
  83.                 felder[posH][posW] = 0;
  84.             }
  85.         }
  86.         invalidate();
  87.         repaint();
  88.     }
  89.  
  90.     public int clamp( int value, int min, int max )
  91.     {
  92.         if( value > max )
  93.             value = max;
  94.         if( value < min )
  95.             value = min;
  96.         return value;
  97.     }
  98.  
  99.     private Punkt[] gewinnendePunkte;
  100.     public int checkWin()
  101.     {
  102.         gewinnendePunkte = new Punkt[3];
  103.         //ZEILEN
  104.         for( int posH = 0; posH < HÖHE; posH++ )
  105.         {
  106.             int gewonnen = felder[posH][0];
  107.             for( int posW = 1; posW < BREITE; posW++ )
  108.             {
  109.                 if( felder[posH][posW] != gewonnen )
  110.                 {
  111.                     gewonnen = 0;
  112.                     break;
  113.                 }
  114.                 gewinnendePunkte[posW] = new Punkt( posW, posH );
  115.             }
  116.             if( gewonnen > 0 )
  117.                 return gewonnen;
  118.  
  119.         }
  120.  
  121.         //SPALTEN
  122.         for( int posW = 0; posW < BREITE; posW++ )
  123.         {
  124.             int gewonnen = felder[0][posW];
  125.             for( int posH = 0; posH < HÖHE; posH++ )
  126.             {
  127.                 if( felder[posH][posW] != gewonnen )
  128.                 {
  129.                     gewonnen = 0;
  130.                     break;
  131.                 }
  132.                 gewinnendePunkte[posH] = new Punkt( posW, posH );
  133.             }
  134.             if( gewonnen > 0 )
  135.                 return gewonnen;
  136.         }
  137.  
  138.         //Diagonal
  139.         int diagonalGewinner = felder[0][0];
  140.         int gegenDiagonalGewinner = felder[HÖHE - 1][0];
  141.         for( int posH = 0; posH < HÖHE; posH++ )
  142.         {
  143.             for( int posW = 0; posW < BREITE; posW++ )
  144.             {
  145.                 if( posW == posH )
  146.                 {
  147.                     if( diagonalGewinner != felder[posH][posW] )
  148.                     {
  149.                         diagonalGewinner = 0;
  150.                     }
  151.                     if( gegenDiagonalGewinner != felder[posH][BREITE - posW - 1] )
  152.                     {
  153.                         gegenDiagonalGewinner = 0;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.         if( diagonalGewinner > 0 )
  159.         {
  160.             gewinnendePunkte[0] = new Punkt( 0, 0 );
  161.             gewinnendePunkte[1] = new Punkt( 1, 1 );
  162.             gewinnendePunkte[2] = new Punkt( 2, 2 );
  163.             return diagonalGewinner;
  164.         }
  165.         if( gegenDiagonalGewinner > 0 )
  166.         {
  167.             gewinnendePunkte[0] = new Punkt( 0, 2 );
  168.             gewinnendePunkte[1] = new Punkt( 1, 1 );
  169.             gewinnendePunkte[2] = new Punkt( 0, 2 );
  170.             return gegenDiagonalGewinner;
  171.         }
  172.  
  173.  
  174.         //Diagonal(Gegenrichtung)
  175.  
  176.         //Feld voll?
  177.         boolean feldFrei = false;
  178.         for( int posH = 0; posH < HÖHE; posH++ )
  179.         {
  180.             for( int posW = 0; posW < BREITE; posW++ )
  181.             {
  182.                 if( felder[posH][posW] == 0 )
  183.                     feldFrei = true;
  184.             }
  185.         }
  186.         if( !feldFrei )
  187.             return -1;
  188.  
  189.         return 0;
  190.     }
  191.  
  192.     public void mouseClicked( MouseEvent e )
  193.     {
  194.         int posX = e.getX();
  195.         int posY = e.getY();
  196.  
  197.         for( int posH = 0; posH < HÖHE; posH++ )
  198.         {
  199.             for( int posW = 0; posW < BREITE; posW++ )
  200.             {
  201.                 int feldPosX = posW * FELD_BREITE + LÜCKEN_GRÖßE * ( posW + 1 );
  202.                 int feldPosY = posH * FELD_HÖHE + LÜCKEN_GRÖßE * ( posH + 1 );
  203.                 if(
  204.                         posX >= feldPosX && posY >= feldPosY
  205.                                 && posX <= feldPosX + FELD_BREITE && posY <= feldPosY + FELD_HÖHE
  206.                         )
  207.                 {
  208.                     feldClicked( posH, posW );
  209.                     System.out.println( "Clicked " + posH + " " + posW );
  210.                 }
  211.             }
  212.         }
  213.     }
  214.  
  215.  
  216.     public void paintComponent( Graphics g )
  217.     {
  218.         g.setColor( Color.white );
  219.         g.fillRect( 0, 0, FELD_BREITE * BREITE + ( BREITE + 1 ) * LÜCKEN_GRÖßE, 15 );
  220.         g.setColor( Color.black );
  221.         g.drawString( "Spieler " + spielerAktiv + " ist dran!", 0, 12 );
  222.         Graphics2D graphics2D = (Graphics2D)g;
  223.         graphics2D.setTransform( AffineTransform.getTranslateInstance( 0, 15 ) );
  224.         g.setColor( Color.black );
  225.         g.fillRect( 0,
  226.                 0,
  227.                 FELD_BREITE * BREITE + ( BREITE + 1 ) * LÜCKEN_GRÖßE,
  228.                 FELD_HÖHE * HÖHE + ( HÖHE + 1 ) * LÜCKEN_GRÖßE
  229.         );
  230.  
  231.         for( int posH = 0; posH < HÖHE; posH++ )
  232.         {
  233.             for( int posW = 0; posW < BREITE; posW++ )
  234.             {
  235.                 int posX = posW * FELD_BREITE + LÜCKEN_GRÖßE * ( posW + 1 );
  236.                 int posY = posH * FELD_HÖHE + LÜCKEN_GRÖßE * ( posH + 1 );
  237.                 g.setColor( Color.gray );
  238.                 g.fillRect( posX, posY, FELD_BREITE, FELD_HÖHE );
  239.                 if( felder[posH][posW] == 1 )
  240.                     g.setColor( Color.red );
  241.                 else if( felder[posH][posW] == 2 )
  242.                     g.setColor( Color.blue );
  243.  
  244.                 if( felder[posH][posW] == 1 )
  245.                 {
  246.                     g.fillOval( posX + 5, posY + 5, 40, 40 );
  247.                     g.setColor( Color.gray );
  248.                     g.fillOval( posX + 15, posY + 15, 20, 20 );
  249.                 }
  250.                 else if( felder[posH][posW] == 2 )
  251.                 {
  252.                     BufferedImage buf = new BufferedImage( FELD_BREITE, FELD_HÖHE, BufferedImage.TYPE_INT_ARGB );
  253.  
  254.                     Graphics2D g2d = (Graphics2D)buf.getGraphics( );
  255.                     AffineTransform transform = AffineTransform.getTranslateInstance( 0, 0 );
  256.  
  257.                     transform.rotate( Math.toRadians( 45 ), FELD_BREITE / 2, FELD_HÖHE / 2 );
  258.                     g2d.setTransform( transform );
  259.                     g2d.fillRect( 0, FELD_HÖHE / 2 - STRICH_BREITE / 2, FELD_BREITE , STRICH_BREITE );
  260.  
  261.                     transform.rotate( Math.toRadians( 270 ), FELD_BREITE / 2, FELD_HÖHE / 2 );
  262.                     g2d.setTransform( transform );
  263.                     g2d.fillRect( 0, FELD_HÖHE / 2 - STRICH_BREITE / 2, FELD_BREITE, STRICH_BREITE );
  264.  
  265.                     g.drawImage( buf, posX, posY, null );
  266.                 }
  267.             }
  268.         }
  269.     }
  270.  
  271.     public Dimension getPreferredSize()
  272.     {
  273.         return new Dimension( FELD_BREITE * BREITE + ( BREITE + 1 ) * LÜCKEN_GRÖßE, FELD_HÖHE * HÖHE + ( HÖHE + 1 ) * LÜCKEN_GRÖßE + 15 );
  274.     }
  275. }
Add Comment
Please, Sign In to add comment