TicTacToe Problem bei kontrolle

Jak111

Mitglied
Hallo,

Ich habe die Methode "kontrolle" erstellt, welche kontrollieren soll ob X,O Senkrecht, Waagrecht oder Diagonal gewonnen hat. Aus irgendeinem Grund funktioniert es jedoch nicht. Was mache ich falsch?

Java:
package com.example.jonas.tictactoe;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
    int count = 0;
    Button[][] buttons = new Button[3][3];
    @Override
    protected void onNewIntent(Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("TicTacToe");
        int z = 1;
        for (int i = 0; i <= 2; i++) {
            for(int j = 0; j <=2; j++){
                String buttonID = "button" + z;
                int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                buttons[i][j] = (Button) findViewById(resID);
                buttons[i][j].setOnClickListener(listener);
                z++;

            }
    }

    }
    public boolean kontrolle (String sp){       //sp = Spieler (X,O)
        for(int i = 0; i < 3; i++) {
            //Kontrolle der Zeilen
            if (buttons[i][0].getText().equals(sp)&&buttons[i][1].getText().equals(sp)&&buttons[i][2].equals(sp)) {
                return true;
            }
        }

        //Kontrolle der Spalten
        for(int i = 0; i < 3; i++) {
            if (buttons[0][i].getText().equals(sp) && buttons[1][i].getText().equals(sp) && buttons[2][i].equals(sp)) {
                return true;
            }
        }

        //Kontrolle der Diagonalen
        if(buttons[0][0].getText().equals(sp)&&buttons[1][1].getText().equals(sp)&&buttons[2][2].equals(sp)){
            return true;
        }
        if(buttons[2][0].getText().equals(sp)&&buttons[1][1].getText().equals(sp)&&buttons[0][2].equals(sp)){
            return true;
        }

        return false;
    }
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
{
                Button pressedButton = (Button) findViewById(v.getId());
                if (pressedButton.getText().equals("")) {
                    pressedButton.setClickable(false);
                    String zeichen;
                    if (count % 2 == 0) {
                        pressedButton.setBackgroundColor(Color.RED);
                        zeichen = "X";
                    } else {
                        pressedButton.setBackgroundColor(Color.BLUE);
                        zeichen = "O";
                    }
                    pressedButton.setText(zeichen);
                    count++;

                    if (kontrolle("X")) {             // X hat gewonnen
                        count = 0;
                        System.out.println("X hat gewonnen");
                        for (int i = 0; i < 3; i++) {
                            for (int j = 0; j < 3; j++) {
                                buttons[i][j].setText("");
                            }
                        }
                    } else if (kontrolle("O")) {
                            count = 0;
                        System.out.println("O hat gewonnen");
                        for (int i = 0; i < 3; i++) {
                            for (int j = 0; j < 3; j++) {
                                buttons[i][j].setText("");
                            }
                        }
                    } else if (count == 9) {
                        count = 0;
                        System.out.println("Unentschieden!");
                        for (int i = 0; i < 3; i++) {
                            for (int j = 0; j < 3; j++) {
                                buttons[i][j].setText("");
                                buttons[i][j].setBackgroundColor(Color.WHITE);
                                buttons[i][j].setClickable(true);
                            }
                        }
                }

                }

            }
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
 

Jak111

Mitglied
Es passiert gar nichts das Spiel läuft normal weiter bis das Spielfeld gefüllt ist. Unentschieden funktioniert allerdings.
 

VfL_Freak

Top Contributor
Moin,

was genau steht denn jeweils in "button[n][m]" ??
IMHO setzt Du die Werte 'X' und 'O' zwar so: "pressedButton.setText(zeichen)", aber nie in das Array !

Wie @thecain schon schrieb: versuch' es mal mit dem Debugger :)

Gruß Klaus
 

Jardcore

Top Contributor
Grundsätzlich Falsch ist, dass du soviel in einer Methode machst :)
Versuche doch mal deine Methode in kleinere Methoden zu kapseln.

Wenn du zum Beispiel jeweils einen Methode erstellst, die prüft ob ein Spieler in einer Zeile, Spalte oder Diagonal gewonnen hat.

Dadurch könntest du auch deutlich schneller herausfinden wo der Fehler liegt.

Du hast es ja auch schon selbst gemerkt, da du Kommentare benutzt hast, dort wo die Kommentare stehen könntest du den Austausch vornehmen.
 
K

kneitzel

Gast
Wo wir bei grundlegenden Fehlern sind:
In meinen Augen ist es auch falsch, eine Benutzeroberfläche und Business-Daten zu mischen. Deine Benutzeroberfläche sollte nur irgendwas anzeigen, was in den Business-Daten steckt. Aber der Text eines Buttons ist nicht gedacht, Business-Daten zu speichern.

Daher erst einmal die reinen Business-Daten / Klassen definieren und testen (Unit Tests sind da sinnvoll). Dann hast Du ein universelles Tic-Tac-Toe Spiel und Du kannst jederzeit irgendwelche UIs dafür bauen. Sei es reine Consolen-Spiele, Swing, AWT, JavaFX, Webseiten, .... egal.

Also sauber objektorientiert könntest Du erst einmal eine Enumeration erstellen mit Feldwerten: Die sind dann x, o oder leer. Dann kannst Du hin gehen und Dein Spielfeld definieren und da hast Du dann Felder mit solchen Werten. Das Spielfeld kannst Du löschen, Felder setzen u.s.w. Und Du kannst prüfen, ob das Spiel beendet ist, wer gewonnen hat u.s.w.

Und wenn Du dann die Oberfläche baust kann es ganz einfach sein, weil Du mit etwas Glück nur für die Bindings sorgen musst. je nach Technologie geht das dann ganz trivial und schnell. Und du hast auch sofort die Separation of Concerns und nicht alles in einem File oder so.

Aber das nur ganz am Rande und als kleiner Hinweis für die Zukunft.
 

Jak111

Mitglied
Moin,

was genau steht denn jeweils in "button[n][m]" ??
IMHO setzt Du die Werte 'X' und 'O' zwar so: "pressedButton.setText(zeichen)", aber nie in das Array !

Wie @thecain schon schrieb: versuch' es mal mit dem Debugger :)

Gruß Klaus

Ich habe es jetzt mit dem Debugger versucht. Es scheint tatsächlich ein Problem mit der Zuweisung der Werte und dem Array zu liegen, weil die Methode kontrolle("X") oder kontrolle("O") nie true wird. Wie kann ich das Problem beheben ?

Danke für eure Hilfe:)
 

VfL_Freak

Top Contributor
Moin,

ist zum einen mal die Frage, was zu dem Zeitpunkt wirklich auf allen Feldern von "buttons" steht, sprich: ob eine der Bedingungen überhaupt zutrifft!

Sodann ist mir eben aufgefallen, dass Du beiden Vergleichen
Java:
if (buttons[i][0].getText().equals(sp)&&buttons[i][1].getText().equals(sp)&&buttons[i][2].equals(sp))
nur bei den ersten beiden jeweils wirklich den Text holst!!
Ein "buttons[n][m].equals(sp)" wird wohl nicht funktionieren ;)

Gruß Klaus
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
F Java ME TicTacToe Android & Cross-Platform Mobile Apps 4
W Prüfen, ob App auf Gerät installiert ist Problem S10 Android & Cross-Platform Mobile Apps 11
W In App Purchase Problem? Android & Cross-Platform Mobile Apps 36
W Problem mit Android Studio Android & Cross-Platform Mobile Apps 0
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
K Android to Pi | Websocket Problem Android & Cross-Platform Mobile Apps 3
N Intent und finish() Problem Android & Cross-Platform Mobile Apps 5
B Android App Programmierung Einsteiger Problem Android & Cross-Platform Mobile Apps 4
emeraldo Android Problem mit Bottomnavmenu Android & Cross-Platform Mobile Apps 10
I Das Problem mit der Tastatur... android:windowSoftInputMode="adjustPan" Android & Cross-Platform Mobile Apps 1
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
A Android Android Studio Emulator Problem Android & Cross-Platform Mobile Apps 1
S Android Studio Bluetooth App Problem Android & Cross-Platform Mobile Apps 6
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
M Problem bei Werteübergabe, MSQL verbindung Android & Cross-Platform Mobile Apps 3
S Android Problem mit Android Virtual Device erstellung. Android & Cross-Platform Mobile Apps 2
Anfänger2011 Text to Speech Problem Android & Cross-Platform Mobile Apps 1
S Android Android java onclick listener Problem Android & Cross-Platform Mobile Apps 9
A Android Problem mit ListView und OnItemClickListener.. Android & Cross-Platform Mobile Apps 10
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
R W-Lan Problem über Sockets Android & Cross-Platform Mobile Apps 1
P ViewPager Problem Android & Cross-Platform Mobile Apps 1
A Android Problem mit Video von Youtube abspielen Android & Cross-Platform Mobile Apps 4
A Android Problem mit Zurücktaste und ausgabe der Aktuellen Seite Android & Cross-Platform Mobile Apps 6
B Android Problem mit Soundwiedergabe Android & Cross-Platform Mobile Apps 2
T Android Android Sensor: Java Problem Android & Cross-Platform Mobile Apps 1
G Problem beim Rendern von 3D-Objekt Android & Cross-Platform Mobile Apps 0
L Android Gyroscope Sensor Problem Android & Cross-Platform Mobile Apps 2
S Android GPS Problem Android & Cross-Platform Mobile Apps 24
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 1
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 0
B Android Problem mit Rückgabewert Android & Cross-Platform Mobile Apps 13
L Android komisches Bitmap-Größe-Problem Android & Cross-Platform Mobile Apps 8
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Eclipse + AndroidSDK - Problem mit Referenzen Android & Cross-Platform Mobile Apps 6
M Problem mit setOnClickListener Android & Cross-Platform Mobile Apps 4
DaniSahne96 Problem beim Appdebuggen auf Smartphone Android & Cross-Platform Mobile Apps 3
P Android Problem beim Widget - Denkfehler ? Android & Cross-Platform Mobile Apps 2
M GCM IntentService Problem Android & Cross-Platform Mobile Apps 3
D Android Gallery Problem Android & Cross-Platform Mobile Apps 5
P Problem mit Cell id Android & Cross-Platform Mobile Apps 6
L Android Problem mit "spinner" Android & Cross-Platform Mobile Apps 10
D Android problem mit geschwindigkeitsberechnung app Android & Cross-Platform Mobile Apps 2
E Android Problem mit Contact Provider Android & Cross-Platform Mobile Apps 1
H Android Problem mit ListActivity Android & Cross-Platform Mobile Apps 3
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
F Android ExpandableList, SimpleCursorTreeAdapter, Cursor Problem Android & Cross-Platform Mobile Apps 2
A Android Problem mit Long.getLong() bzw. Integer.getInteger() Android & Cross-Platform Mobile Apps 2
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
V [Java] und [JavaME] ClientServer StreamConnection . Problem beim lesen / schreiben Android & Cross-Platform Mobile Apps 2
F Eclipse JAD File erzeugen -- Problem Android & Cross-Platform Mobile Apps 10
R Ein Problem beim ausführen von folgendem Quelltext Android & Cross-Platform Mobile Apps 11
M Problem mit dem Auslesen von System Properties Android & Cross-Platform Mobile Apps 7
P wtk problem Android & Cross-Platform Mobile Apps 3
G Math exp() Problem Android & Cross-Platform Mobile Apps 4
G S40 Problem Android & Cross-Platform Mobile Apps 8
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
C Problem Device/Emulator wird nicht erkannt Android & Cross-Platform Mobile Apps 3
S Image Problem Android & Cross-Platform Mobile Apps 11
M Problem mit den Softkeys Android & Cross-Platform Mobile Apps 4
G J2ME jar-problem Android & Cross-Platform Mobile Apps 10
S Komisches Problem Android & Cross-Platform Mobile Apps 3
F Problem beim Erstellen der Jar File Android & Cross-Platform Mobile Apps 4
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
S Problem mit Einbindung einer externer Bibliothek Android & Cross-Platform Mobile Apps 2
G Random - Problem Android & Cross-Platform Mobile Apps 5
E problem mit den resourcen Android & Cross-Platform Mobile Apps 2
O Problem mit Datagramconnection Android & Cross-Platform Mobile Apps 2
P Problem mit der Uhrzeit Android & Cross-Platform Mobile Apps 2
S Problem auf dem Handy Android & Cross-Platform Mobile Apps 3
B Android Kontrolle ob in Reichweite durch zeitlich versetzte If Abfrage oder Delay? Android & Cross-Platform Mobile Apps 1

Ähnliche Java Themen

Neue Themen


Oben