Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import basicpackage.BasicBoard;
  4. import basicpackage.Game;
  5.  
  6. public class TicTacToe extends BasicBoard implements Game {
  7.  
  8. String s = "";
  9. private String player1 = "Spieler 1 ";
  10. private String player2 = "Spieler 2 ";
  11. public boolean won = false;
  12. String id = "";
  13. private String[] line = { "A", "B", "C" };
  14. private String[] row = { "D", "E", "F" };
  15. int spalte;
  16. int zeile;
  17. int[][] content = new int[3][3];
  18. private Scanner scan = new Scanner(System.in);
  19. boolean state = false;
  20.  
  21. public void initBoard() {
  22.  
  23. this.board = new int[3][3];
  24. this.boardname = "Tic Tac Toe";
  25. setBoardname("Tic Tac Toe");
  26.  
  27. for (String x : line) {
  28. System.out.print(" " + x + " ");
  29. }
  30. System.out.println();
  31.  
  32. for (int i = 0; i < board.length; i++) {
  33. System.out.print(row[i]);
  34. for (int j = 0; j < board.length; j++) {
  35.  
  36. int cell = board[i][j];
  37. cell = 0;
  38. System.out.print(" " + cell + " ");
  39.  
  40. }
  41. System.out.println("");
  42. }
  43. System.out.println();
  44.  
  45. for (int i = 0; i < content.length; i++) {
  46. for (int j = 0; j < content.length; j++) {
  47. content[i][j] = 0;
  48. }
  49. }
  50. }
  51.  
  52. public void drawBoard(int player) {
  53.  
  54. // zeichne board mit neuen werten
  55. for (String x : line) {
  56. System.out.print(" " + x + " ");
  57. }
  58. System.out.println();
  59.  
  60. for (int i = 0; i < board.length; i++) {
  61. System.out.print(row[i]);
  62. for (int j = 0; j < board.length; j++) {
  63.  
  64. int cell = board[i][j];
  65.  
  66. // //überschreiben
  67. if (i == zeile && j == spalte) {
  68. cell = board[spalte][zeile];
  69. content[i][j] = board[spalte][zeile];
  70. } else {
  71. cell = content[i][j];
  72. }
  73. System.out.print(" " + cell + " ");
  74.  
  75. }
  76. System.out.println("");
  77. }
  78. System.out.println();
  79.  
  80. }
  81.  
  82. public void play(int player) {
  83. move(player);
  84. winGame();
  85. noWin();
  86. System.out.println(gameStatus());
  87. }
  88.  
  89. public void move(int player) {
  90.  
  91. spalte = -1;
  92. zeile = -1;
  93.  
  94. if (player == 1) {
  95. System.out.println("Spieler 1 ist dran. Bitte gib eine gültige Position ein (SpalteZeile). \n");
  96. } else {
  97. System.out.println("Spieler 2 ist dran. Bitte gib eine gültige Position ein (SpalteZeile). \n");
  98. }
  99.  
  100. s = scan.next();
  101. String rows = s.substring(0, 1);
  102. String lines = s.substring(1, 2);
  103.  
  104. // eingabe auswerten
  105. for (int i = 0; i <= line.length - 1; i++) {
  106. if (line[i].equals(rows)) {
  107. spalte = i;
  108. break;
  109. }
  110. }
  111. if (spalte == -1) {
  112. System.out.println(
  113. "Das war keine gültige Eingabe. Bitte bleib im Spielfeld und achte auf die Eingabe. \nIn Großbuchstaben erst die Spalte, dann die Zeile");
  114. move(player);
  115. }
  116.  
  117. for (int i = 0; i <= row.length - 1; i++) {
  118. if (row[i].equals(lines)) {
  119. zeile = i;
  120. break;
  121. }
  122. }
  123. if (zeile == -1) {
  124. System.out.println("Das war keine gültige Eingabe. Bitte bleib im Spielfeld und achte auf die Eingabe. \nIn Großbuchstaben erst die Spalte, dann die Zeile");
  125. move(player);
  126. }
  127.  
  128. // prüfe feld leer
  129. if (board[spalte][zeile] == 0) {
  130. board[spalte][zeile] = player;
  131. drawBoard(player);
  132. } else {
  133. System.out.println("Dieses Feld ist bereits belegt. Bitte suche dir ein neues Feld aus.");
  134. move(player);
  135. }
  136.  
  137. }
  138.  
  139. public void winGame() {
  140.  
  141. int i = 0;
  142. int j = 0;
  143.  
  144.  
  145. // columns
  146. while (j < board.length) {
  147. for (i = 0; i < board.length - 1; i++) {
  148. if (board[i][j] == board[i + 1][j] && board[i][j] != 0) {
  149. state = true;
  150. } else {
  151. break;
  152. }
  153. }
  154. if (state == false) {
  155. j++;
  156. } else {
  157. whoWon(i, j);
  158. break;
  159. }
  160. }
  161.  
  162. // rows
  163. while (i < board.length) {
  164. for (j = 0; j < board.length - 1; j++) {
  165. if (board[i][j] == board[i][j + 1] && board[i][j] != 0) {
  166. state = true;
  167. } else {
  168. break;
  169. }
  170. }
  171. if (state == false) {
  172. i++;
  173. } else {
  174. whoWon(i, j);
  175. break;
  176. }
  177.  
  178. }
  179.  
  180. // dia
  181. while (i < board.length && j < board.length) {
  182.  
  183. if (board[i][j] == board[i + 1][j + 1] && board[i][j] != 0) {
  184. state = true;
  185. } else {
  186. break;
  187. }
  188. i++;
  189. j++;
  190. break;
  191. }
  192.  
  193. // dia return
  194. int ii = board.length - 1;
  195. int jj = board.length - 1;
  196.  
  197. while (ii > 0 && jj > 0) {
  198.  
  199. if (board[ii][jj] == board[ii - 1][jj - 1] && board[ii][jj] != 0) {
  200. state = true;
  201. } else {
  202. break;
  203. }
  204. ii--;
  205. jj--;
  206. break;
  207. }
  208. }
  209.  
  210. public void noWin() {
  211.  
  212. // zähle belegte plätze
  213. int count = 0;
  214. for (int k = 0; k <= board.length - 1; k++) {
  215. for (int l = 0; l <= board.length - 1; l++) {
  216. if (board[k][l] != 0) {
  217. count++;
  218. }
  219. }
  220. }
  221.  
  222. // alle Felder belegt aber kein Gewinner
  223. if (count == 9) {
  224. System.out.println("Es steht unentschieden");
  225. won = true;
  226. }
  227.  
  228. }
  229.  
  230. private void whoWon(int i, int j) {
  231. if (board[i][j] == 1) {
  232. System.out.println(player1 + " hat gewonnen! Glückwunsch!");
  233. state = true;
  234. } else if (board[i][j] == 2) {
  235. System.out.println(player2 + " hat gewonnen! Glückwunsch!");
  236. state = true;
  237. }
  238. won = true;
  239. }
  240.  
  241. @Override
  242. public String gameStatus() {
  243.  
  244. String status = "Das Spiel läuft...";
  245. String stat = "Das Spiel ist beendet.";
  246. if (won == true) {
  247. return stat;
  248. }
  249. return status;
  250.  
  251. }
  252.  
  253. public void finish() {
  254. scan.close();
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement