Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aufgabe2;
- import java.util.Arrays;
- public class TicTacToe {
- public static char[] SPIELER = new char[] { 'X', 'O' };
- public static char FREI = ' ';
- public static char UNENTSCHIEDEN = '?';
- private char[][] brett;
- private int istAnDerReihe;
- public TicTacToe() {
- // hier ergaenzen: Instanzvariablen initialisieren
- char[][] brett = new char[3][3];
- //stimmt noch nicht, wie könnt ich das schreiben?
- for(SPIELER['X'] = SPIELER[istAnDerReihe]) {
- return SPIELER['O'] != SPIELER[istAnDerReihe];
- return SPIELER['X'] = SPIELER[istAnDerReihe];
- }
- SPIELER['O'] = SPIELER[istAnDerReihe];
- }
- public boolean istFrei(int zeile, int spalte) {
- // hier ergaenzen, wie in der Angabe beschrieben
- /**public static String toString(int[] brett);*/
- for(int i=0; i< brett.length;i++){
- for(int j=0; j< brett.length;j++){
- int x = 0;
- while(brett = brett[x][x]){//Abfragen ob Platz im Array belegt ist oder frei
- return true;
- System.out.print(true);//Gib aus ob die Stelle des Arrays noch frei ist
- }
- }
- }
- return false;
- System.out.print(false);//Gib aus ob die Stelle des Arrays noch frei ist
- }
- public char getGewinner() {
- //wenn in einer Reihe {'X','X','X'} steht, dann
- /**if(brett[0]['X'] && brett[1]['X'] && brett[2]['X']) {
- char gewinner = SPIELER ['X'];*///Wenn eine Reihe/Zeile des Arrays mit gleichen Zeichen gefüllt ist...
- if (SPIELER = SPIELER['X']){
- }
- // hier ergaenzen, wie in der Angabe beschrieben
- }
- public boolean allesBelegt() {
- // hier ergaenzen, wie in der Angabe beschrieben
- }
- public String ziehen(char spieler, int zeile, int spalte) {
- char gewinner = getGewinner();
- if (gewinner != UNENTSCHIEDEN || allesBelegt()) {
- String meldung = "Das ist Spiel ist beendet, keine Zuege mehr moeglich. ";
- if (gewinner == UNENTSCHIEDEN)
- meldung += "Niemand hat gewonnen!";
- else
- meldung += "Spieler " + gewinner + " hat gewonnen!";
- return meldung;
- } else {
- if (SPIELER[istAnDerReihe] == spieler) {
- if (istFrei(zeile, spalte)) {
- // hier Teil der Spiellogik ergaenzen
- } else {
- return "Feld (" + zeile + "," + spalte + ") ist bereits von Spieler "
- + brett[zeile][spalte] + " belegt!";
- }
- } else {
- return "Spieler " + spieler + " ist nicht an der Reihe!";
- }
- }
- }
- @Override
- public String toString() {
- String ln = System.getProperty("line.separator");
- String result = brett[0][0] + " | " + brett[0][1] + " | " + brett[0][2] + ln +
- "-" + "---" + "-" + "---" + "-" + ln +
- brett[1][0] + " | " + brett[1][1] + " | " + brett[1][2] + ln +
- "-" + "---" + "-" + "---" + "-" + ln +
- brett[2][0] + " | " + brett[2][1] + " | " + brett[2][2];
- return result;
- }
- public static void main(String[] args) {
- // Spielverlauf 1
- System.out.println("Neues Spiel gestartet");
- TicTacToe tictactoe = new TicTacToe();
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 0, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 0, 0));
- System.out.println(tictactoe);
- // Spieler O ist hier nicht an der Reihe
- System.out.println(tictactoe.ziehen('O', 0, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 2, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 1, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 2, 2));
- System.out.println(tictactoe);
- // Spieler O versucht hier zu schummeln, denn Feld (2,2) ist bereits belegt
- System.out.println(tictactoe.ziehen('O', 2, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 1, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 2, 1));
- System.out.println(tictactoe);
- // Zu diesem Zeitpunkt hat Spieler X bereits gewonnen
- System.out.println(tictactoe.ziehen('O', 1, 0));
- System.out.println(tictactoe);
- // Spielverlauf 2
- System.out.println();
- System.out.println("Neues Spiel gestartet");
- tictactoe = new TicTacToe();
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 0, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 2, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 2, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 0, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 1, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 2, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 1, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 1, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 0, 0));
- System.out.println(tictactoe);
- // Spielverlauf 3
- System.out.println();
- System.out.println("Neues Spiel gestartet");
- tictactoe = new TicTacToe();
- System.out.println(tictactoe);
- // Spieler O ist hier nicht an der Reihe
- System.out.println(tictactoe.ziehen('O', 0, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 0, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 1, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 2, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 1, 0));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 1, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 0, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 2, 1));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('O', 2, 2));
- System.out.println(tictactoe);
- System.out.println(tictactoe.ziehen('X', 0, 2));
- System.out.println(tictactoe);
- }
- }
Add Comment
Please, Sign In to add comment