Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. package objectstructures;
  2.  
  3. public class TicTacToe {
  4.  
  5. char[][] board = new char [3][3];
  6. private char player;
  7.  
  8. public TicTacToe() {
  9. player = 'x';
  10. for(int i=0; i<3; i++) {
  11. for(int k=0; k<3; k++) {
  12. board[i][k] = ' ';
  13. }
  14. }
  15. }
  16.  
  17. public char getCell(int x, int y) {
  18. return board[x][y];
  19. }
  20.  
  21. public boolean isOccupied(int x, int y) {
  22. char chr = getCell(x, y);
  23. if (chr != ' ') {
  24. return true;
  25. }
  26. return false;
  27. }
  28.  
  29. public boolean setCell(char c, int x, int y) {
  30. if (isOccupied(x, y)) {
  31. return false;
  32. }
  33. board[x][y] = c;
  34. return true;
  35.  
  36. }
  37.  
  38. public char getCurrentPlayer() {
  39. return player;
  40. }
  41.  
  42. public String toString() {
  43. String s = "";
  44. s += "\n " + getCell(0,0) + " | " + getCell(0,1) + " | " + getCell(0,2);
  45. s += "\n-----------";
  46. s += "\n " + getCell(1,0) + " | " + getCell(1,1) + " | " + getCell(1,2);
  47. s += "\n-----------";
  48. s += "\n " + getCell(2,0) + " | " + getCell(2,1) + " | " + getCell(2,2);
  49. s += "\n";
  50.  
  51. if(isWinner('x')) {
  52. return s + "\n" + "x won!";
  53. }else if(isWinner('o')){
  54. return s + "\n"+ "o won!";
  55. }else if(isFinished()){
  56. return s + "\n" + "It's a tie";
  57. }else{
  58. return s + "\n" + "Spiller =" + player;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. }
  65.  
  66. //public static void main(String[] args) {
  67. //TicTacToe tic = new TicTacToe();
  68. //System.out.println(tic);
  69. //}
  70.  
  71. public void play(int x, int y) {
  72. if(setCell(player, x, y)){
  73. if(player == 'x'){
  74. player = 'o';
  75. }else{
  76. player = 'x';
  77. }
  78. }
  79. }
  80.  
  81. public boolean isWinner(char c) {
  82. if (getCell(0, 0) == c && getCell(0, 1) == c && getCell(0, 2) == c) {
  83. return true;
  84. }
  85. if (getCell(1, 0) == c && getCell(1, 1) == c && getCell(1, 2) == c) {
  86. return true;
  87. }
  88. if (getCell(2, 0) == c && getCell(2, 1) == c && getCell(2, 2) == c) {
  89. return true;
  90. }
  91. // Sjekker om spiller c har vunnet loddrett:
  92. if (getCell(0, 0) == c && getCell(1, 0) == c && getCell(2, 0) == c) {
  93. return true;
  94. }
  95. if (getCell(0, 1) == c && getCell(1, 1) == c && getCell(2, 1) == c) {
  96. return true;
  97. }
  98. if (getCell(0, 2) == c && getCell(1, 2) == c && getCell(2, 2) == c) {
  99. return true;
  100. }
  101. // Sjekker om spiller c har vunnet diagonalt:
  102. if (getCell(0, 0) == c && getCell(1, 1) == c && getCell(2, 2) == c) {
  103. return true;
  104. }
  105. if (getCell(0, 2) == c && getCell(1, 1) == c && getCell(2, 0) == c) {
  106. return true;
  107. }
  108. return false;
  109. }
  110.  
  111. public boolean hasWinner() {
  112. if(isWinner('x') || isWinner('o')) {
  113. return true;
  114. }
  115. return false;
  116. }
  117.  
  118. public boolean isFinished() {
  119. if(hasWinner()) {
  120. return true;
  121. }else{
  122. for(int i=0; i<3; i++) {
  123. for(int k=0; k<3; k++) {
  124. if(board[i][k] == ' ') {
  125. return false;
  126. }
  127. return true;
  128. }
  129. } return false;
  130. }
  131. }
  132.  
  133. public void getInput(String in) {
  134. int x = Integer.valueOf(in.substring(0,1));
  135. int y = Integer.valueOf(in.substring(1));
  136.  
  137. if(!isFinished()) {
  138. play(x, y);
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement