Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- /**
- * Write a description of class Spielfeld here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Spielfeld extends JPanel
- {
- private int[][] felder;
- private static final int BREITE = 3;
- private static final int HÖHE = 3;
- private static final int FELD_BREITE = 50;
- private static final int FELD_HÖHE = 50;
- private static final int LÜCKEN_GRÖßE = 5;
- class Punkt{ public int x, y; public Punkt( int px, int py ) { x = px; y = py; } }
- final int STRICH_BREITE = 8; //Strichbreite der Kreuze
- private int spielerAktiv = 1;
- public Spielfeld( )
- {
- super();
- felder = new int[HÖHE][BREITE];
- setDoubleBuffered( true );
- final Spielfeld ref = this;
- addMouseListener(
- new MouseAdapter()
- {
- public void mouseClicked( MouseEvent e )
- {
- ref.mouseClicked( e );
- }
- }
- );
- }
- public void feldClicked( int zeile, int spalte )
- {
- int angeklickt = felder[zeile][spalte];
- if( angeklickt != 0 )
- return; //Feld gehört schon einem Spieler
- //Spieler tauschen
- felder[zeile][spalte] = spielerAktiv;
- if( spielerAktiv == 1 )
- spielerAktiv = 2;
- else
- spielerAktiv = 1;
- //Felder neu malen
- invalidate();
- repaint();
- //Gewinner überprüfen
- int gewinner = checkWin();
- if( gewinner > 0 )
- {
- JOptionPane.showMessageDialog( this, "Spieler " + gewinner + " hat gewonnen!", "Glückwunsch!", JOptionPane.INFORMATION_MESSAGE );
- spielfeldFüllen();
- }
- else if( gewinner < 0 )
- {
- JOptionPane.showMessageDialog( this, "Kein Spieler hat gewonnen", "Unentschieden", JOptionPane.INFORMATION_MESSAGE );
- spielfeldFüllen();
- }
- }
- public void spielfeldFüllen()
- {
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- for( int posW = 0; posW < BREITE; posW++ )
- {
- felder[posH][posW] = 0;
- }
- }
- invalidate();
- repaint();
- }
- public int clamp( int value, int min, int max )
- {
- if( value > max )
- value = max;
- if( value < min )
- value = min;
- return value;
- }
- private Punkt[] gewinnendePunkte;
- public int checkWin()
- {
- gewinnendePunkte = new Punkt[3];
- //ZEILEN
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- int gewonnen = felder[posH][0];
- for( int posW = 1; posW < BREITE; posW++ )
- {
- if( felder[posH][posW] != gewonnen )
- {
- gewonnen = 0;
- break;
- }
- gewinnendePunkte[posW] = new Punkt( posW, posH );
- }
- if( gewonnen > 0 )
- return gewonnen;
- }
- //SPALTEN
- for( int posW = 0; posW < BREITE; posW++ )
- {
- int gewonnen = felder[0][posW];
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- if( felder[posH][posW] != gewonnen )
- {
- gewonnen = 0;
- break;
- }
- gewinnendePunkte[posH] = new Punkt( posW, posH );
- }
- if( gewonnen > 0 )
- return gewonnen;
- }
- //Diagonal
- int diagonalGewinner = felder[0][0];
- int gegenDiagonalGewinner = felder[HÖHE - 1][0];
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- for( int posW = 0; posW < BREITE; posW++ )
- {
- if( posW == posH )
- {
- if( diagonalGewinner != felder[posH][posW] )
- {
- diagonalGewinner = 0;
- }
- if( gegenDiagonalGewinner != felder[posH][BREITE - posW - 1] )
- {
- gegenDiagonalGewinner = 0;
- }
- }
- }
- }
- if( diagonalGewinner > 0 )
- {
- gewinnendePunkte[0] = new Punkt( 0, 0 );
- gewinnendePunkte[1] = new Punkt( 1, 1 );
- gewinnendePunkte[2] = new Punkt( 2, 2 );
- return diagonalGewinner;
- }
- if( gegenDiagonalGewinner > 0 )
- {
- gewinnendePunkte[0] = new Punkt( 0, 2 );
- gewinnendePunkte[1] = new Punkt( 1, 1 );
- gewinnendePunkte[2] = new Punkt( 0, 2 );
- return gegenDiagonalGewinner;
- }
- //Diagonal(Gegenrichtung)
- //Feld voll?
- boolean feldFrei = false;
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- for( int posW = 0; posW < BREITE; posW++ )
- {
- if( felder[posH][posW] == 0 )
- feldFrei = true;
- }
- }
- if( !feldFrei )
- return -1;
- return 0;
- }
- public void mouseClicked( MouseEvent e )
- {
- int posX = e.getX();
- int posY = e.getY();
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- for( int posW = 0; posW < BREITE; posW++ )
- {
- int feldPosX = posW * FELD_BREITE + LÜCKEN_GRÖßE * ( posW + 1 );
- int feldPosY = posH * FELD_HÖHE + LÜCKEN_GRÖßE * ( posH + 1 );
- if(
- posX >= feldPosX && posY >= feldPosY
- && posX <= feldPosX + FELD_BREITE && posY <= feldPosY + FELD_HÖHE
- )
- {
- feldClicked( posH, posW );
- System.out.println( "Clicked " + posH + " " + posW );
- }
- }
- }
- }
- public void paintComponent( Graphics g )
- {
- g.setColor( Color.white );
- g.fillRect( 0, 0, FELD_BREITE * BREITE + ( BREITE + 1 ) * LÜCKEN_GRÖßE, 15 );
- g.setColor( Color.black );
- g.drawString( "Spieler " + spielerAktiv + " ist dran!", 0, 12 );
- Graphics2D graphics2D = (Graphics2D)g;
- graphics2D.setTransform( AffineTransform.getTranslateInstance( 0, 15 ) );
- g.setColor( Color.black );
- g.fillRect( 0,
- 0,
- FELD_BREITE * BREITE + ( BREITE + 1 ) * LÜCKEN_GRÖßE,
- FELD_HÖHE * HÖHE + ( HÖHE + 1 ) * LÜCKEN_GRÖßE
- );
- for( int posH = 0; posH < HÖHE; posH++ )
- {
- for( int posW = 0; posW < BREITE; posW++ )
- {
- int posX = posW * FELD_BREITE + LÜCKEN_GRÖßE * ( posW + 1 );
- int posY = posH * FELD_HÖHE + LÜCKEN_GRÖßE * ( posH + 1 );
- g.setColor( Color.gray );
- g.fillRect( posX, posY, FELD_BREITE, FELD_HÖHE );
- if( felder[posH][posW] == 1 )
- g.setColor( Color.red );
- else if( felder[posH][posW] == 2 )
- g.setColor( Color.blue );
- if( felder[posH][posW] == 1 )
- {
- g.fillOval( posX + 5, posY + 5, 40, 40 );
- g.setColor( Color.gray );
- g.fillOval( posX + 15, posY + 15, 20, 20 );
- }
- else if( felder[posH][posW] == 2 )
- {
- BufferedImage buf = new BufferedImage( FELD_BREITE, FELD_HÖHE, BufferedImage.TYPE_INT_ARGB );
- Graphics2D g2d = (Graphics2D)buf.getGraphics( );
- AffineTransform transform = AffineTransform.getTranslateInstance( 0, 0 );
- transform.rotate( Math.toRadians( 45 ), FELD_BREITE / 2, FELD_HÖHE / 2 );
- g2d.setTransform( transform );
- g2d.fillRect( 0, FELD_HÖHE / 2 - STRICH_BREITE / 2, FELD_BREITE , STRICH_BREITE );
- transform.rotate( Math.toRadians( 270 ), FELD_BREITE / 2, FELD_HÖHE / 2 );
- g2d.setTransform( transform );
- g2d.fillRect( 0, FELD_HÖHE / 2 - STRICH_BREITE / 2, FELD_BREITE, STRICH_BREITE );
- g.drawImage( buf, posX, posY, null );
- }
- }
- }
- }
- public Dimension getPreferredSize()
- {
- 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 );
- }
- }
Add Comment
Please, Sign In to add comment