Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package edu.udel.jatlas.tictactoe5x5;
  2.  
  3. import edu.udel.jatlas.gameframework.ConsoleListener;
  4. import edu.udel.jatlas.gameframework.Game;
  5. import edu.udel.jatlas.gameframework.JavaTicker;
  6.  
  7. /**
  8. * A basic TicTacToe game model that has been modified to allow
  9. * play on a 5x5 board where the winner must get 4 in a row.
  10. * In addition, pieces may be specified to be empty, x, o, or
  11. * blocked (neither player can place a piece there).
  12. *
  13. * @author jatlas
  14. */
  15. public class TicTacToe5x5Game extends Game {
  16. private Piece[][] board;
  17. private char turn;
  18. private char notTurn;
  19. private int whoseturn;
  20.  
  21.  
  22. public TicTacToe5x5Game(Piece[][] board, char turn, char notTurn) {
  23. this.board = board;
  24. this.turn = turn;
  25. this.notTurn = notTurn;
  26. }
  27.  
  28. public Piece[][] getBoard() {
  29. return board;
  30. }
  31.  
  32. public char getTurn() {
  33. return turn;
  34. }
  35.  
  36. public char getNotTurn() {
  37. return notTurn;
  38. }
  39.  
  40. /**
  41. * Swaps the current turn with the notTurn
  42. */
  43. public void changeTurn() {
  44.  
  45. }
  46.  
  47. /**
  48. * A simple setter for board at a row, column
  49. */
  50. public void setPiece(int row, int column, Piece piece) {
  51. board[row][column] = piece;
  52. }
  53.  
  54. public String getStatus() {
  55. if (isEnd()) {
  56. if (isWinner(getTurn())) {
  57. return "Player " + getTurn() + " wins!";
  58. }
  59. else if (isWinner(getNotTurn())) {
  60. return "Player " + getNotTurn() + " wins!";
  61. }
  62. else {
  63. return "It is a draw.";
  64. }
  65. }
  66. else {
  67. return "Player " + turn + "'s turn";
  68. }
  69. }
  70.  
  71. /**
  72. * Returns a "visual" representation of a TicTacToe5x5 board
  73. */
  74. public String toString() {
  75. StringBuilder buffer = new StringBuilder();
  76. buffer.append(getStatus()+"\n");
  77. for (int i = 0; i < board.length; i++) {
  78. for (int j = 0; j < board.length; j++) {
  79. buffer.append(board[i][j]);
  80. }
  81. buffer.append("\n");
  82. }
  83. return buffer.toString();
  84. }
  85.  
  86. /**
  87. * Checkes to see if the row and column are within the bounds of
  88. * the board.
  89. *
  90. * @param row
  91. * @param column
  92. * @return
  93. */
  94. public boolean isWithinBounds(int row, int column) {
  95. return row >= 0 && column >= 0 &&
  96. row < getBoard().length &&
  97. column < getBoard()[row].length;
  98. }
  99.  
  100. /**
  101. * Gets the maximum sequential times that the given symbol appears
  102. * on a Piece in the board. Starts at row, column and iterates by
  103. * dr, dc each loop iteration as long as row, column are within
  104. * bounds.
  105. */
  106. public int getMaxSequence(int row, int column, int dr, int dc, char symbol) {
  107. int maxSequence = 0;
  108. // you need to complete this method
  109.  
  110. return maxSequence;
  111. }
  112.  
  113. /**
  114. * Gets the score for the given symbol in the board. The score is
  115. * the max value from all results returned from calling getMaxSequence
  116. * for each unique left-to-right, top-to-bottom, down-right-diagonal,
  117. * and down-left-diagonal path.
  118. */
  119. public int getScore(char symbol) {
  120. // you need to complete this method
  121.  
  122. int maxScore = 0;
  123. return maxScore;
  124. }
  125.  
  126. /**
  127. * Checks to see if the player represented by the given symbol is
  128. * a winner.
  129. *
  130. * @param symbol
  131. * @return
  132. */
  133. public boolean isWinner(char symbol) {
  134. return getScore(symbol) >= 4;
  135. }
  136.  
  137. /**
  138. * Returns true if there is any empty space in the board.
  139. * Uses the isEmpty() method on Piece.
  140. *
  141. * @return
  142. */
  143. public boolean hasEmptySpace() {
  144. // you need to complete this method
  145. return false;
  146. }
  147.  
  148. /**
  149. * Determines if the game is at an end state (i.e. no further moves can be
  150. * made). For TicTacToe this method returns true if there are no empty
  151. * spaces on the board or if either player has won.
  152. *
  153. * @return
  154. */
  155. public boolean isEnd() {
  156. return !hasEmptySpace() || isWinner(turn) || isWinner(notTurn);
  157. }
  158.  
  159. public static TicTacToe5x5Game makeStartGame(char turn, char notTurn) {
  160. // creates a starting board
  161. Piece E = new EmptyPiece();
  162. Piece B = new BlockedPiece();
  163.  
  164. Piece[][] board = new Piece[][]
  165. {{E, E, E, E, E},
  166. {E, E, E, E, E},
  167. {E, E, B, E, E},
  168. {E, E, E, E, E},
  169. {E, E, E, E, E}};
  170. return new TicTacToe5x5Game(board, turn, notTurn);
  171. }
  172.  
  173. public static void main(String[] args) {
  174. TicTacToe5x5Game game = makeStartGame('x', 'o');
  175. game.addGameListener(new ConsoleListener());
  176. game.addGameListener(new TicTacToe5x5AI("x"));
  177. game.addGameListener(new TicTacToe5x5AI("o"));
  178. // uncomment the below line to see the game run "instantly"
  179. // game.setRealTimeTickLength(0);
  180. game.start(new JavaTicker());
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement