2D-Grafik Tic Tac Toe nach Gewinn rot

The Pi

Mitglied
Hallo Leute,
Ich habe ein TicTacToe Programm mit der GUI erstellt, wo ich gerne hätte, dass die Felder nach dem Gewinn rot angezeigt werden sollen.
Das geht aber nicht. (Ich habe die Funktion "rot"):

Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoegui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
*
* @author Info_2B
*/
public class Frame extends JFrame implements ActionListener {

    JPanel p = new JPanel();

    JButton b1 = new JButton();
    JButton b2 = new JButton();
    JButton b3 = new JButton();
    JButton b4 = new JButton();
    JButton b5 = new JButton();
    JButton b6 = new JButton();
    JButton b7 = new JButton();
    JButton b8 = new JButton();
    JButton b9 = new JButton();
    JButton button = new JButton();

    JLabel l = new JLabel("Spieler 1 ist dran");

    int status = 1;
    int gewinnt = 0;

    public Frame() {
        add(p);
        setSize(306, 360);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        p.setLayout(null);

        p.add(b1);
        b1.setBounds(0, 0, 100, 100);
        b1.addActionListener(this);

        p.add(b2);
        b2.setBounds(100, 0, 100, 100);
        b2.addActionListener(this);

        p.add(b3);
        b3.setBounds(200, 0, 100, 100);
        b3.addActionListener(this);

        p.add(b4);
        b4.setBounds(0, 100, 100, 100);
        b4.addActionListener(this);

        p.add(b5);
        b5.setBounds(100, 100, 100, 100);
        b5.addActionListener(this);

        p.add(b6);
        b6.setBounds(200, 100, 100, 100);
        b6.addActionListener(this);

        p.add(b7);
        b7.setBounds(0, 200, 100, 100);
        b7.addActionListener(this);

        p.add(b8);
        b8.setBounds(100, 200, 100, 100);
        b8.addActionListener(this);

        p.add(b9);
        b9.setBounds(200, 200, 100, 100);
        b9.addActionListener(this);

        p.add(button);

        p.add(l);
        l.setBounds(0, 300, 306, 30);
        l.setHorizontalAlignment(JLabel.CENTER);

        Reset();
    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        JButton button = (JButton) ae.getSource();

        button.setEnabled(false);

        if (gewinnt == 1) {
            status = 1;
            gewinnt = 0;
        } else if (gewinnt == 2) {
            status = 2;
            gewinnt = 0;
        }

        if (status == 1) {
            button.setBackground(Color.black);
            try {
                Sieg(status);
            } catch (InterruptedException ex) {
                Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
            }
            status = 2;
        } else {
            button.setBackground(Color.yellow);
            try {
                Sieg(status);
            } catch (InterruptedException ex) {
                Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
            }
            status = 1;
        }
    }

    public void Sieg(int status) throws InterruptedException {
        if (b1.getBackground() == Color.yellow && b2.getBackground() == Color.yellow && b3.getBackground() == Color.yellow
                || b4.getBackground() == Color.yellow && b5.getBackground() == Color.yellow && b6.getBackground() == Color.yellow
                || b7.getBackground() == Color.yellow && b8.getBackground() == Color.yellow && b9.getBackground() == Color.yellow
                || b1.getBackground() == Color.yellow && b4.getBackground() == Color.yellow && b7.getBackground() == Color.yellow
                || b2.getBackground() == Color.yellow && b5.getBackground() == Color.yellow && b8.getBackground() == Color.yellow
                || b3.getBackground() == Color.yellow && b6.getBackground() == Color.yellow && b9.getBackground() == Color.yellow
                || b1.getBackground() == Color.yellow && b5.getBackground() == Color.yellow && b9.getBackground() == Color.yellow
                || b3.getBackground() == Color.yellow && b5.getBackground() == Color.yellow && b7.getBackground() == Color.yellow) {
            l.setText("Spieler 2 gewinnt!");
            gewinnt = 2;
            rot();
            Thread.sleep(1600);
            Reset();
        } else if (b1.getBackground() == Color.black && b2.getBackground() == Color.black && b3.getBackground() == Color.black
                || b4.getBackground() == Color.black && b5.getBackground() == Color.black && b6.getBackground() == Color.black
                || b7.getBackground() == Color.black && b8.getBackground() == Color.black && b9.getBackground() == Color.black
                || b1.getBackground() == Color.black && b4.getBackground() == Color.black && b7.getBackground() == Color.black
                || b2.getBackground() == Color.black && b5.getBackground() == Color.black && b8.getBackground() == Color.black
                || b3.getBackground() == Color.black && b6.getBackground() == Color.black && b9.getBackground() == Color.black
                || b1.getBackground() == Color.black && b5.getBackground() == Color.black && b9.getBackground() == Color.black
                || b3.getBackground() == Color.black && b5.getBackground() == Color.black && b7.getBackground() == Color.black) {
            l.setText("Spieler 1 gewinnt");
            gewinnt = 1;
            rot();
            Thread.sleep(1600);
            Reset();           
        } else if (b1.getBackground() != button.getBackground()
                && b2.getBackground() != button.getBackground()
                && b3.getBackground() != button.getBackground()
                && b4.getBackground() != button.getBackground()
                && b5.getBackground() != button.getBackground()
                && b6.getBackground() != button.getBackground()
                && b7.getBackground() != button.getBackground()
                && b8.getBackground() != button.getBackground()
                && b9.getBackground() != button.getBackground()) {
            l.setText("Keiner gewinnt");
            Thread.sleep(1600);
            Reset();
        } else if (status == 1) {
            l.setText("Spieler 2 ist dran");
        } else {
            l.setText("Spieler 1 ist dran");
        }
    }

    public void Reset() {
        b1.setEnabled(true);
        b1.setBackground(button.getBackground());
        b2.setEnabled(true);
        b2.setBackground(button.getBackground());
        b3.setEnabled(true);
        b3.setBackground(button.getBackground());
        b4.setEnabled(true);
        b4.setBackground(button.getBackground());
        b5.setEnabled(true);
        b5.setBackground(button.getBackground());
        b6.setEnabled(true);
        b6.setBackground(button.getBackground());
        b7.setEnabled(true);
        b7.setBackground(button.getBackground());
        b8.setEnabled(true);
        b8.setBackground(button.getBackground());
        b9.setEnabled(true);
        b9.setBackground(button.getBackground());
    }
   
    public void rot(){
        if(b1.getBackground() == Color.black || b1.getBackground() == Color.yellow && b2.getBackground() == Color.black || b2.getBackground() == Color.yellow && b3.getBackground() == Color.black || b3.getBackground() == Color.yellow){
            b1.setBackground(Color.red);
            b2.setBackground(Color.red);
            b3.setBackground(Color.red);
        } else if(b4.getBackground() == Color.black || b4.getBackground() == Color.yellow && b5.getBackground() == Color.black || b5.getBackground() == Color.yellow && b6.getBackground() == Color.black || b6.getBackground() == Color.yellow){
            b4.setBackground(Color.red);
            b5.setBackground(Color.red);
            b6.setBackground(Color.red);
        } else if(b7.getBackground() == Color.black || b7.getBackground() == Color.yellow && b8.getBackground() == Color.black || b8.getBackground() == Color.yellow && b9.getBackground() == Color.black || b9.getBackground() == Color.yellow){
            b7.setBackground(Color.red);
            b8.setBackground(Color.red);
            b9.setBackground(Color.red);
        } else if(b1.getBackground() == Color.black || b1.getBackground() == Color.yellow && b4.getBackground() == Color.black || b4.getBackground() == Color.yellow && b7.getBackground() == Color.black || b7.getBackground() == Color.yellow){
            b1.setBackground(Color.red);
            b4.setBackground(Color.red);
            b7.setBackground(Color.red);
        } else if(b2.getBackground() == Color.black || b2.getBackground() == Color.yellow && b5.getBackground() == Color.black || b5.getBackground() == Color.yellow && b8.getBackground() == Color.black || b8.getBackground() == Color.yellow){
            b2.setBackground(Color.red);
            b5.setBackground(Color.red);
            b8.setBackground(Color.red);
        } else if(b3.getBackground() == Color.black || b3.getBackground() == Color.yellow && b6.getBackground() == Color.black || b6.getBackground() == Color.yellow && b9.getBackground() == Color.black || b9.getBackground() == Color.yellow){
            b3.setBackground(Color.red);
            b6.setBackground(Color.red);
            b9.setBackground(Color.red);
        } else if(b1.getBackground() == Color.black || b1.getBackground() == Color.yellow && b5.getBackground() == Color.black || b5.getBackground() == Color.yellow && b9.getBackground() == Color.black || b9.getBackground() == Color.yellow){
            b1.setBackground(Color.red);
            b5.setBackground(Color.red);
            b9.setBackground(Color.red);
        } else if(b3.getBackground() == Color.black || b3.getBackground() == Color.yellow && b5.getBackground() == Color.black || b5.getBackground() == Color.yellow && b7.getBackground() == Color.black || b7.getBackground() == Color.yellow){
            b3.setBackground(Color.red);
            b5.setBackground(Color.red);
            b7.setBackground(Color.red);
        }
    }
}

Hat jemand eine Idee, was fehlt?
 
Es fehlt vor Allem die Einhaltung des DRY Prinzips. Don't Repeat Yourself.
Du Schreibst z.B x-Mal:
Java:
if(b3.getBackground() == Color.black || b3.getBackground() == Color.yellow &&
b5.getBackground() == Color.black || b5.getBackground() == Color.yellow &&
b7.getBackground() == Color.black || b7.getBackground() == Color.yellow){}

Du kannst das aber auch in mehreren Methode auslagern, dann musst du dich nicht immer wiederholen.
Java:
private boolean isBlack(Button b) {
    return b.getBackground() == Color.BLACK;
}
usw.

Ich glaube auch, das du diese ganzen hässlichen Abfragen auch alle gut hinter Methoden und Klasse verstecken kannst, bzw. vielleicht sogar gar nicht brauchst.

Wenn dein Code gut strukturiert ist, findest du auch leichter deine Fehler.

Hier vielleicht eine kleine Anregung, wie du erstmal komplett ohne GUI das Spiel entwickelst. Und später dann abhängig vom Status des Spiels die GUI einfach nur updatest.

Java:
public class VierGewinnt {

    private static final int PLAYER_1 = 1;
    private static final int PLAYER_2 = 2;
    private static final int EMPTY = 0;
  
    private static final int MAX_ROWS = 9;
    private static final int MAX_COLUMNS = 9;
  
    private int[][] field = new int[MAX_ROWS][MAX_COLUMNS];

    public void setField(int player, int row) {
        for(int column = 0; column < MAX_COLUMNS; column++) {
            if(field[row][column] == EMPTY) {
                field[row][column] = player;
                break;
            }
        }
    }
  
    public boolean winCondition() {
        for(int row = 0; row < MAX_ROWS; row++) {
            for(int column = 0; column < MAX_COLUMNS; column++) {
                if(horizonalVictory(row, column) ||
                verticalVictory(row, column) ||
                diagonalVictory(row, column)) {
                    return true;
                }
            }
        }
      
        return false;
    }
}
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben