Java/TicTacToe

Aus ZUM-Unterrichten

Tictactoe.jpg

Variante mit Konsolenein- und ausgabe

Eine einfache TicTacToe-Variante für BlueJ. Ein und Ausgabe in der Konsole.

import java.io.*; // wird von der Eingabe benötigt

public class Spielfeld {
    //Eigenschaften
    private Kaestchen[][] feld;
    private static BufferedReader in; // Eingabe
    
    //Konstruktor
    public Spielfeld() {
        feld = new Kaestchen[3][3];
        for (int i=0;i<3;i++){// senkrecht
            for (int j=0;j<3;j++){// waagerecht
                feld[j][i]=new Kaestchen();
            }
        }
        
        // Vorbereitung für die Eingabe
        in = new BufferedReader(new InputStreamReader(System.in));
                              
    }
    
    //Methoden
    public void setzeSpielstein(int x, int y, String stein){
        feld[x][y].setzeStein(stein);
    }
    
    public void gibSpielAus(){
        System.out.println("\n  012");
        for (int i=0;i<3;i++){
            System.out.print(i+" ");
            for (int j=0;j<3;j++){
                System.out.print(feld[j][i].getZeichen());
            }
            System.out.println(); // Zeilenumbruch nach 3 Kaestchen
        }
        
    }
    
    public void spieleEineRunde(){
        // unguenstig: Starre Anzahl von Zügen, immer 10 Züge
        int x,y;
        for (int i=1;i<5;i++){
            gibSpielAus();
            System.out.print("Spieler X bitte die x-Koordinate (0-2)");
            try {
            x=liesInt();
            }
             catch(IOException e) {
            x=-1;
            }
            System.out.print("Spieler X bitte die y-Koordinate (0-2)");
            try {
            y=liesInt();
            }
            catch(IOException e) {
            y=-1;
            }
            setzeSpielstein(x, y, "X");
            gibSpielAus();
            System.out.print("Spieler O bitte die x-Koordinate (0-2)");
            try {
            x=liesInt();
            }
             catch(IOException e) {
            x=-1;
            }
            System.out.print("Spieler O bitte die y-Koordinate (0-2)");
            try {
            y=liesInt();
            }
            catch(IOException e) {
            y=-1;
            }            
            setzeSpielstein(x, y, "O");
        }
    }
    
    // Methode für die Eingabe
    // Mehr hier: http://www.wifo.uni-mannheim.de/Java/oop/OOPinJava/kapitel11/StandardEingabe_java.html
    public int liesInt() throws IOException {
        try {
        return Integer.parseInt(in.readLine());
        }
        catch(IOException e) {
          return (-1);
        }
    }
}
public class Kaestchen {
    String zeichen;
    
    public Kaestchen() {
        zeichen=" "; // Am Anfang leer
    }
    
    public void setzeStein(String pZeichen) {
        if (!zeichen.equals(" ")) {System.out.println("Besetzt!");} 
        else {
            if (pZeichen.equals("X") || pZeichen.equals("O")) {zeichen=pZeichen;} 
            else System.out.println("Ungueltige Eingabe, bitte X oder O waehlen!");
        }
    }
    
    public String getZeichen(){
        return zeichen;
    }
}

Variante mit GUI

Die folgende Variante wurde mit Netbeans-BlueJ erstellt. Sie soll ein Beispiel für den Umgang mit GUIs sein.

/*
 * TicTacGUI.java
 *
 * Created on 12. März 2007, 19:15
 */

/**
 *
 * @author  ugh
 */
public class TicTacGUI extends javax.swing.JFrame {
    private int amZug;    
    /** Creates new form TicTacGUI */
    public TicTacGUI() {
        initComponents();
        amZug=0;
        textField2.setText("0 startet das Spiel.");
        label1.setText("Tic-Tac-Toe");
        button1.setLabel("_");
        button2.setLabel("_");
        button3.setLabel("_");
        button4.setLabel("_");
        button5.setLabel("_");
        button6.setLabel("_");
        button7.setLabel("_");
        button8.setLabel("_");
        button9.setLabel("_");        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();
        button3 = new java.awt.Button();
        button4 = new java.awt.Button();
        button5 = new java.awt.Button();
        button6 = new java.awt.Button();
        button7 = new java.awt.Button();
        button8 = new java.awt.Button();
        button9 = new java.awt.Button();
        textField1 = new java.awt.TextField();
        label1 = new java.awt.Label();
        textField2 = new java.awt.TextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        button1.setLabel("button1");
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button1MouseClicked(evt);
            }
        });

        button2.setLabel("button2");
        button2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button2MouseClicked(evt);
            }
        });

        button3.setLabel("button3");
        button3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button3MouseClicked(evt);
            }
        });

        button4.setLabel("button4");
        button4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button4MouseClicked(evt);
            }
        });

        button5.setLabel("button5");
        button5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button5MouseClicked(evt);
            }
        });

        button6.setLabel("button6");
        button6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button6MouseClicked(evt);
            }
        });

        button7.setLabel("button7");
        button7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button7MouseClicked(evt);
            }
        });

        button8.setLabel("button8");
        button8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button8MouseClicked(evt);
            }
        });

        button9.setLabel("button9");
        button9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button9MouseClicked(evt);
            }
        });

        textField1.setText("textField1");

        label1.setText("label1");

        textField2.setText("textField2");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(25, 25, 25)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(textField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(button7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                                        .add(button8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                            .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(button4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                        .add(20, 20, 20)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                            .add(button5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(button2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                                .add(29, 29, 29)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(button6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(button9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(button3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(55, 55, 55))
                            .add(textField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE))
                        .add(75, 75, 75))))
        );

        layout.linkSize(new java.awt.Component[] {button1, button2, button3, button4, button5, button6, button7, button8, button9}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(textField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(23, 23, 23)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(button2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(button3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(10, 10, 10)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(button6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(0, 0, 0))
                    .add(button5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, button4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(button7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(button9, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(button8, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(textField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {button1, button2, button3, button4, button5, button6, button7, button8, button9}, org.jdesktop.layout.GroupLayout.VERTICAL);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button9MouseClicked
        if (button9.getLabel()=="_") {
            if (amZug==0) button9.setLabel("0");
            else button9.setLabel("X");
            werteAus(2,2);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button9MouseClicked

    private void button8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button8MouseClicked
        if (button8.getLabel()=="_") {
            if (amZug==0) button8.setLabel("0");
            else button8.setLabel("X");
            werteAus(1,2);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button8MouseClicked

    private void button7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button7MouseClicked
        if (button7.getLabel()=="_") {
            if (amZug==0) button7.setLabel("0");
            else button7.setLabel("X");
            werteAus(0,2);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button7MouseClicked

    private void button6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button6MouseClicked
        if (button6.getLabel()=="_") {
            if (amZug==0) button6.setLabel("0");
            else button6.setLabel("X");
            werteAus(2,1);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button6MouseClicked

    private void button5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button5MouseClicked
        if (button5.getLabel()=="_") {
            if (amZug==0) button5.setLabel("0");
            else button5.setLabel("X");
            werteAus(1,1);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button5MouseClicked

    private void button4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button4MouseClicked
        if (button4.getLabel()=="_") {
            if (amZug==0) button4.setLabel("0");
            else button4.setLabel("X");
            werteAus(0,1);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button4MouseClicked

    private void button3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button3MouseClicked
        if (button3.getLabel()=="_") {
            if (amZug==0) button3.setLabel("0");
            else button3.setLabel("X");
            werteAus(2,0);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button3MouseClicked

    private void button2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button2MouseClicked
        if (button2.getLabel()=="_") {
            if (amZug==0) button2.setLabel("0");
            else button2.setLabel("X");
            werteAus(1,0);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button2MouseClicked

    private void button1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button1MouseClicked
        if (button1.getLabel()=="_") {
            if (amZug==0) button1.setLabel("0");
            else button1.setLabel("X");
            werteAus(0,0);
        }
        else {
            textField1.setText("Hier ist schon ein Stein!");
        }
    }//GEN-LAST:event_button1MouseClicked


    
    private void werteAus(int pX, int pY){
        if (amZug==0) {
            amZug=1;
            textField2.setText("X setzt nun.");
        }
        else {
            amZug=0;
            textField2.setText("O ist dran.");   
        }
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TicTacGUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private java.awt.Button button2;
    private java.awt.Button button3;
    private java.awt.Button button4;
    private java.awt.Button button5;
    private java.awt.Button button6;
    private java.awt.Button button7;
    private java.awt.Button button8;
    private java.awt.Button button9;
    private java.awt.Label label1;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    // End of variables declaration//GEN-END:variables
    
}