Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. package tictactoe;
  2.  
  3.  
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class TicTacToe {
  8. private TicTacToePlayer[][] spelBord;
  9. private String puzzelString;
  10.  
  11.  
  12. public TicTacToe() {
  13. spelBord = new TicTacToePlayer[3][3];
  14. for (int i = 0; i < 3; i++) {
  15. for (int j = 0; j < 3; j++) {
  16. this.spelBord[i][j]=null;
  17. }
  18. }
  19. }
  20.  
  21. public TicTacToePlayer getCell(int row, int column){
  22. if (row>=1&&row<=3 &&column>=1&&column<=3){
  23. return spelBord[row-1][column-1];
  24. }else{
  25. return null;
  26. }
  27. }
  28.  
  29. public TicTacToePlayer getCurrentTurn(){
  30. int aantalX = 0;
  31. int aantalO = 0;
  32.  
  33. for (int i = 0; i < 3; i++) {
  34. for (int j = 0; j < 3; j++) {
  35. if (this.spelBord[i][j]==TicTacToePlayer.X){
  36. aantalX++;
  37. } else if (this.spelBord[i][j]==TicTacToePlayer.O){
  38. aantalO++;
  39. }
  40. }
  41. }
  42. if (aantalX > aantalO) {
  43. return TicTacToePlayer.O;
  44.  
  45. }
  46. return TicTacToePlayer.X;
  47. }
  48.  
  49. public TicTacToePlayer getWinner(){
  50. int aantalX = 0;
  51. int aantalO = 0;
  52. for (int i = 0; i < 3; i++) {
  53. for (int j = 0; j < 3; j++) {
  54. if (this.spelBord[i][j]==TicTacToePlayer.X){
  55. aantalX++;
  56. if (aantalX == 3) {return TicTacToePlayer.X;}
  57. }
  58. if (this.spelBord[i][j]==TicTacToePlayer.O){
  59. aantalO++;
  60. if (aantalO == 3) {return TicTacToePlayer.O;}
  61. }
  62. }
  63. aantalX = 0;
  64. aantalO = 0;
  65. }
  66. // en herbeginnen maar voor de rijen
  67. for (int i = 0; i < 3; i++) {
  68. for (int j = 0; j < 3; j++) {
  69. if (this.spelBord[j][i]==TicTacToePlayer.X){
  70. aantalX++;
  71. if (aantalX == 3) {return TicTacToePlayer.X;}
  72. }
  73. if (this.spelBord[j][i]==TicTacToePlayer.O){
  74. aantalO++;
  75. if (aantalO == 3) {return TicTacToePlayer.O;}
  76. }
  77. }
  78. aantalX = 0;
  79. aantalO = 0;
  80. }
  81. // en de diagonaal linksboven naar rechtsonder
  82. for (int i = 0; i < 3; i++) {
  83. if (this.spelBord[i][i]==TicTacToePlayer.X){
  84. aantalX++;
  85. if (aantalX == 3) {return TicTacToePlayer.X;}
  86. }
  87. if (this.spelBord[i][i]==TicTacToePlayer.O){
  88. aantalO++;
  89. if (aantalO == 3) {return TicTacToePlayer.O;}
  90. }
  91. }
  92. aantalX = 0;
  93. aantalO = 0;
  94. // en de diagonaal van rechtsboven naar linksonder
  95. if (this.spelBord[2][0]==TicTacToePlayer.X) {aantalX++;}
  96. if (this.spelBord[2][0]==TicTacToePlayer.O) {aantalO++;}
  97. if (this.spelBord[1][1]==TicTacToePlayer.X) {aantalX++;}
  98. if (this.spelBord[1][1]==TicTacToePlayer.O) {aantalO++;}
  99. if (this.spelBord[0][2]==TicTacToePlayer.X) {aantalX++;}
  100. if (this.spelBord[0][2]==TicTacToePlayer.O) {aantalO++;}
  101. if (aantalX == 3) {return TicTacToePlayer.X;}
  102. if (aantalO == 3) {return TicTacToePlayer.O;}
  103.  
  104. return null;
  105. }
  106.  
  107. public boolean isEmpty(int row, int column){
  108. return this.spelBord[row-1][column-1] == null;
  109. }
  110.  
  111. public boolean isFinished(){
  112. // eerst kijken of er een winnaar is (dat kan zonder dat spelbord volledig is.
  113. if (this.getWinner()!=null){
  114. return true;
  115. }
  116. // indien er nog een vakje is met een null , is het spel nog niet gedaan.
  117. for (int i = 0; i < 3; i++) {
  118. for (int j = 0; j < 3; j++) {
  119. if (this.spelBord[i][j]==null){
  120. return false;
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126.  
  127. public boolean setCell(int row, int column){
  128. if (row>=1 && row<=3 && column>=1 &&column <=3) {
  129. if (this.spelBord[row-1][column-1]==null) {
  130. this.spelBord[row-1][column-1]=this.getCurrentTurn();
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136.  
  137. public String toString(){
  138. puzzelString = "╔═══╦═══╦═══╗"+ System.lineSeparator();
  139. for (int i = 0; i < 3; i++) {
  140. puzzelString = puzzelString + "║ ";
  141. for (int j = 0; j < 3; j++) {
  142. if (this.spelBord[i][j]==null) {
  143. puzzelString = puzzelString + " ";
  144. }else {
  145. puzzelString = puzzelString + this.spelBord[i][j];
  146.  
  147. }
  148. if (j<2) {
  149. puzzelString = puzzelString + " ║ ";
  150. }
  151. }
  152. puzzelString = puzzelString + " ║"+ System.lineSeparator();
  153. if (i<2){ puzzelString = puzzelString + "╠═══╬═══╬═══╣" + System.lineSeparator();}
  154. }
  155. puzzelString = puzzelString + "╚═══╩═══╩═══╝";
  156. return puzzelString;
  157. }
  158.  
  159. public static void main(String[] args) {
  160. String positie;
  161. String sRow;
  162. String sCol;
  163. int iCol;
  164. int iRow;
  165. TicTacToe newSpel = new TicTacToe();
  166. System.out.println(newSpel.toString());
  167. while (!newSpel.isFinished()){
  168. System.out.println("De beurt is aan " + newSpel.getCurrentTurn()+". Kies een vakje (rij,kolom).");
  169. Scanner keyboard = new Scanner(System.in);
  170. positie = keyboard.nextLine();
  171. String[] p = positie.split(",");
  172. // er zouden nooit meer dan 2 posities mogen zijn in deze array
  173. iRow = Integer.parseInt(p[0]);
  174. iCol = Integer.parseInt(p[1]);
  175. newSpel.setCell(iRow, iCol);
  176. System.out.println(newSpel.toString());
  177. }
  178. if (newSpel.getWinner()== null) {
  179. System.out.println("Gelijkspel!");
  180. } else {
  181. System.out.println(newSpel.getWinner() + " heeft gewonnen!");
  182. }
  183.  
  184.  
  185.  
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement