Guest User

Untitled

a guest
Aug 21st, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. package aufgabe2;
  2. import java.util.Arrays;
  3. public class TicTacToe {
  4.  
  5. public static char[] SPIELER = new char[] { 'X', 'O' };
  6. public static char FREI = ' ';
  7. public static char UNENTSCHIEDEN = '?';
  8. private char[][] brett;
  9. private int istAnDerReihe;
  10.  
  11. public TicTacToe() {
  12. // hier ergaenzen: Instanzvariablen initialisieren
  13. char[][] brett = new char[3][3];
  14. //stimmt noch nicht, wie könnt ich das schreiben?
  15. for(SPIELER['X'] = SPIELER[istAnDerReihe]) {
  16. return SPIELER['O'] != SPIELER[istAnDerReihe];
  17. return SPIELER['X'] = SPIELER[istAnDerReihe];
  18. }
  19. SPIELER['O'] = SPIELER[istAnDerReihe];
  20. }
  21.  
  22. public boolean istFrei(int zeile, int spalte) {
  23. // hier ergaenzen, wie in der Angabe beschrieben
  24. /**public static String toString(int[] brett);*/
  25. for(int i=0; i< brett.length;i++){
  26. for(int j=0; j< brett.length;j++){
  27. int x = 0;
  28. while(brett = brett[x][x]){//Abfragen ob Platz im Array belegt ist oder frei
  29. return true;
  30. System.out.print(true);//Gib aus ob die Stelle des Arrays noch frei ist
  31. }
  32. }
  33. }
  34. return false;
  35. System.out.print(false);//Gib aus ob die Stelle des Arrays noch frei ist
  36. }
  37.  
  38. public char getGewinner() {
  39. //wenn in einer Reihe {'X','X','X'} steht, dann
  40. /**if(brett[0]['X'] && brett[1]['X'] && brett[2]['X']) {
  41. char gewinner = SPIELER ['X'];*///Wenn eine Reihe/Zeile des Arrays mit gleichen Zeichen gefüllt ist...
  42. if (SPIELER = SPIELER['X']){
  43.  
  44. }
  45. // hier ergaenzen, wie in der Angabe beschrieben
  46. }
  47.  
  48. public boolean allesBelegt() {
  49. // hier ergaenzen, wie in der Angabe beschrieben
  50. }
  51.  
  52. public String ziehen(char spieler, int zeile, int spalte) {
  53. char gewinner = getGewinner();
  54. if (gewinner != UNENTSCHIEDEN || allesBelegt()) {
  55. String meldung = "Das ist Spiel ist beendet, keine Zuege mehr moeglich. ";
  56. if (gewinner == UNENTSCHIEDEN)
  57. meldung += "Niemand hat gewonnen!";
  58. else
  59. meldung += "Spieler " + gewinner + " hat gewonnen!";
  60. return meldung;
  61. } else {
  62. if (SPIELER[istAnDerReihe] == spieler) {
  63. if (istFrei(zeile, spalte)) {
  64.  
  65. // hier Teil der Spiellogik ergaenzen
  66.  
  67. } else {
  68. return "Feld (" + zeile + "," + spalte + ") ist bereits von Spieler "
  69. + brett[zeile][spalte] + " belegt!";
  70. }
  71. } else {
  72. return "Spieler " + spieler + " ist nicht an der Reihe!";
  73. }
  74. }
  75. }
  76.  
  77. @Override
  78. public String toString() {
  79. String ln = System.getProperty("line.separator");
  80. String result = brett[0][0] + " | " + brett[0][1] + " | " + brett[0][2] + ln +
  81. "-" + "---" + "-" + "---" + "-" + ln +
  82. brett[1][0] + " | " + brett[1][1] + " | " + brett[1][2] + ln +
  83. "-" + "---" + "-" + "---" + "-" + ln +
  84. brett[2][0] + " | " + brett[2][1] + " | " + brett[2][2];
  85. return result;
  86. }
  87.  
  88. public static void main(String[] args) {
  89. // Spielverlauf 1
  90. System.out.println("Neues Spiel gestartet");
  91. TicTacToe tictactoe = new TicTacToe();
  92. System.out.println(tictactoe);
  93. System.out.println(tictactoe.ziehen('X', 0, 2));
  94. System.out.println(tictactoe);
  95. System.out.println(tictactoe.ziehen('O', 0, 0));
  96. System.out.println(tictactoe);
  97. // Spieler O ist hier nicht an der Reihe
  98. System.out.println(tictactoe.ziehen('O', 0, 0));
  99. System.out.println(tictactoe);
  100. System.out.println(tictactoe.ziehen('X', 2, 0));
  101. System.out.println(tictactoe);
  102. System.out.println(tictactoe.ziehen('O', 1, 1));
  103. System.out.println(tictactoe);
  104. System.out.println(tictactoe.ziehen('X', 2, 2));
  105. System.out.println(tictactoe);
  106. // Spieler O versucht hier zu schummeln, denn Feld (2,2) ist bereits belegt
  107. System.out.println(tictactoe.ziehen('O', 2, 2));
  108. System.out.println(tictactoe);
  109. System.out.println(tictactoe.ziehen('O', 1, 2));
  110. System.out.println(tictactoe);
  111. System.out.println(tictactoe.ziehen('X', 2, 1));
  112. System.out.println(tictactoe);
  113. // Zu diesem Zeitpunkt hat Spieler X bereits gewonnen
  114. System.out.println(tictactoe.ziehen('O', 1, 0));
  115. System.out.println(tictactoe);
  116. // Spielverlauf 2
  117. System.out.println();
  118. System.out.println("Neues Spiel gestartet");
  119. tictactoe = new TicTacToe();
  120. System.out.println(tictactoe);
  121. System.out.println(tictactoe.ziehen('X', 0, 1));
  122. System.out.println(tictactoe);
  123. System.out.println(tictactoe.ziehen('O', 2, 0));
  124. System.out.println(tictactoe);
  125. System.out.println(tictactoe.ziehen('X', 2, 2));
  126. System.out.println(tictactoe);
  127. System.out.println(tictactoe.ziehen('O', 0, 2));
  128. System.out.println(tictactoe);
  129. System.out.println(tictactoe.ziehen('X', 1, 1));
  130. System.out.println(tictactoe);
  131. System.out.println(tictactoe.ziehen('O', 2, 1));
  132. System.out.println(tictactoe);
  133. System.out.println(tictactoe.ziehen('X', 1, 2));
  134. System.out.println(tictactoe);
  135. System.out.println(tictactoe.ziehen('O', 1, 0));
  136. System.out.println(tictactoe);
  137. System.out.println(tictactoe.ziehen('X', 0, 0));
  138. System.out.println(tictactoe);
  139. // Spielverlauf 3
  140. System.out.println();
  141. System.out.println("Neues Spiel gestartet");
  142. tictactoe = new TicTacToe();
  143. System.out.println(tictactoe);
  144. // Spieler O ist hier nicht an der Reihe
  145. System.out.println(tictactoe.ziehen('O', 0, 0));
  146. System.out.println(tictactoe);
  147. System.out.println(tictactoe.ziehen('X', 0, 0));
  148. System.out.println(tictactoe);
  149. System.out.println(tictactoe.ziehen('O', 1, 1));
  150. System.out.println(tictactoe);
  151. System.out.println(tictactoe.ziehen('X', 2, 0));
  152. System.out.println(tictactoe);
  153. System.out.println(tictactoe.ziehen('O', 1, 0));
  154. System.out.println(tictactoe);
  155. System.out.println(tictactoe.ziehen('X', 1, 2));
  156. System.out.println(tictactoe);
  157. System.out.println(tictactoe.ziehen('O', 0, 1));
  158. System.out.println(tictactoe);
  159. System.out.println(tictactoe.ziehen('X', 2, 1));
  160. System.out.println(tictactoe);
  161. System.out.println(tictactoe.ziehen('O', 2, 2));
  162. System.out.println(tictactoe);
  163. System.out.println(tictactoe.ziehen('X', 0, 2));
  164. System.out.println(tictactoe);
  165. }
  166. }
Add Comment
Please, Sign In to add comment