Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. /*
  2. * Created on 2007 feb 8
  3. */
  4. package lab4.data;
  5.  
  6. import java.util.Observable;
  7. import java.util.Observer;
  8.  
  9. import lab4.client.GomokuClient;
  10.  
  11. /**
  12. * Represents the state of a game
  13. */
  14.  
  15. public class GomokuGameState extends Observable implements Observer{
  16.  
  17. // Game variables
  18. private final int DEFAULT_SIZE = 15;
  19. private GameGrid gameGrid;
  20.  
  21. //Possible game states
  22. private final int NOT_STARTED = 0,MY_TURN = 1,OTHER_TURN = 2,FINISHED = 3;
  23. private int currentState;
  24.  
  25. private GomokuClient client;
  26.  
  27. private String message;
  28.  
  29. /**
  30. * The constructor
  31. *
  32. * @param gc The client used to communicate with the other player
  33. */
  34. public GomokuGameState(GomokuClient gc){
  35. client = gc;
  36. client.addObserver(this);
  37. gc.setGameState(this);
  38. currentState = NOT_STARTED;
  39. gameGrid = new GameGrid(DEFAULT_SIZE);
  40. }
  41.  
  42.  
  43. /**
  44. * Returns the message string
  45. *
  46. * @return the message string
  47. */
  48. public String getMessageString(){
  49. return message;
  50. }
  51.  
  52. /**
  53. * Returns the game grid
  54. *
  55. * @return the game grid
  56. */
  57. public GameGrid getGameGrid(){
  58. return gameGrid;
  59. }
  60.  
  61. /**
  62. * This player makes a move at a specified location
  63. *
  64. * @param x the x coordinate
  65. * @param y the y coordinate
  66. */
  67. public void move(int x, int y){
  68. if (currentState == MY_TURN) {
  69. if (gameGrid.move(x,y,MY_TURN)) {
  70. message = "moved the boi";
  71. client.sendMoveMessage(x,y);
  72. if (gameGrid.isWinner(MY_TURN)) {
  73. currentState = FINISHED;
  74. }
  75. } else {
  76. message = "error";
  77. }
  78. setChanged();
  79. notifyObservers();
  80. }
  81. }
  82.  
  83. /**
  84. * Starts a new game with the current client
  85. */
  86. public void newGame(){
  87. gameGrid.clearGrid();
  88. currentState = OTHER_TURN;
  89. message = "NEW GAME STARTED BY YOU";
  90. setChanged();
  91. notifyObservers();
  92. }
  93.  
  94. /**
  95. * Other player has requested a new game, so the
  96. * game state is changed accordingly
  97. */
  98. public void receivedNewGame(){
  99. gameGrid.clearGrid();
  100. currentState = MY_TURN;
  101. message = "NEW GAME STARTED BY OTHER";
  102. client.sendNewGameMessage();
  103. setChanged();
  104. notifyObservers();
  105. }
  106.  
  107. /**
  108. * The connection to the other player is lost,
  109. * so the game is interrupted
  110. */
  111. public void otherGuyLeft(){
  112. gameGrid.clearGrid();
  113. currentState = FINISHED;
  114. message = "other guy disconnected";
  115. setChanged();
  116. notifyObservers();
  117. }
  118.  
  119. /**
  120. * The player disconnects from the client
  121. */
  122. public void disconnect(){
  123. gameGrid.clearGrid();
  124. currentState = FINISHED;
  125. message = "disconnected";
  126. client.disconnect();
  127. setChanged();
  128. notifyObservers();
  129. }
  130.  
  131. /**
  132. * The player receives a move from the other player
  133. *
  134. * @param x The x coordinate of the move
  135. * @param y The y coordinate of the move
  136. */
  137. public void receivedMove(int x, int y){
  138. message = "opponent moved the boi";
  139. client.sendMoveMessage(x,y);
  140. if (gameGrid.isWinner(OTHER_TURN)) {
  141. currentState = FINISHED;
  142. }
  143. setChanged();
  144. notifyObservers();
  145. }
  146.  
  147. public void update(Observable o, Object arg) {
  148.  
  149. switch(client.getConnectionStatus()){
  150. case GomokuClient.CLIENT:
  151. message = "Game started, it is your turn!";
  152. currentState = MY_TURN;
  153. break;
  154. case GomokuClient.SERVER:
  155. message = "Game started, waiting for other player...";
  156. currentState = OTHER_TURN;
  157. break;
  158. }
  159. setChanged();
  160. notifyObservers();
  161.  
  162.  
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement