Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 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.  
  10. import java.util.Random;
  11.  
  12. import lab4.client.GomokuClient;
  13.  
  14. /**
  15. * Represents the state of a game
  16. */
  17.  
  18. public class GomokuGameState extends Observable implements Observer{
  19.  
  20. // Game variables
  21. private final int DEFAULT_SIZE = 15;
  22. private GameGrid gameGrid;
  23.  
  24. //Possible game states
  25. private final int NOT_STARTED = 0;
  26. private final int MY_TURN = 1, OTHERS_TURN = 2, FINISHED = 3;
  27. private int currentState;
  28.  
  29. private GomokuClient client;
  30.  
  31. private String message;
  32.  
  33. /**
  34. * The constructor
  35. *
  36. * @param gc The client used to communicate with the other player
  37. */
  38. public GomokuGameState(GomokuClient gc){
  39. client = gc;
  40. client.addObserver(this);
  41. gc.setGameState(this);
  42. currentState = NOT_STARTED;
  43. gameGrid = new GameGrid(DEFAULT_SIZE);
  44. }
  45.  
  46.  
  47. /**
  48. * Returns the message string
  49. *
  50. * @return the message string
  51. */
  52. public String getMessageString(){
  53. return message;
  54. }
  55.  
  56. /**
  57. * Returns the game grid
  58. *
  59. * @return the game grid
  60. */
  61. public GameGrid getGameGrid(){
  62. return gameGrid;
  63. }
  64.  
  65. /**
  66. * This player makes a move at a specified location
  67. *
  68. * @param x the x coordinate
  69. * @param y the y coordinate
  70. */
  71. public void move(int x, int y){
  72. if(currentState != MY_TURN){
  73. message = "fak u men))";
  74. }
  75. else if (!gameGrid.move(x, y, MY_TURN)){
  76. message = "fakink idiots team rash b";
  77. }
  78. else if (currentState == FINISHED)
  79. message = "no";
  80.  
  81. client.notifyObservers(message);
  82. client.sendMoveMessage(x, y);
  83. }
  84.  
  85. /**
  86. * Starts a new game with the current client
  87. */
  88. public void newGame(){
  89. currentState = OTHERS_TURN;
  90. }
  91.  
  92. /**
  93. * Other player has requested a new game, so the
  94. * game state is changed accordingly
  95. */
  96. public void receivedNewGame(){
  97. currentState = MY_TURN;
  98. }
  99.  
  100. /**
  101. * The connection to the other player is lost,
  102. * so the game is interrupted
  103. */
  104. public void otherGuyLeft(){
  105. currentState = FINISHED;
  106. message = "rage quit";
  107. client.notifyObservers(message);
  108. }
  109.  
  110. /**
  111. * The player disconnects from the client
  112. */
  113. public void disconnect(){
  114. currentState = FINISHED;
  115. message = "rage quit";
  116. client.notifyObservers(message);
  117. }
  118.  
  119. /**
  120. * The player receives a move from the other player
  121. *
  122. * @param x The x coordinate of the move
  123. * @param y The y coordinate of the move
  124. */
  125. public void receivedMove(int x, int y){}
  126.  
  127. public void update(Observable o, Object arg) {
  128.  
  129. switch(client.getConnectionStatus()){
  130. case GomokuClient.CLIENT:
  131. message = "Game started, it is your turn!";
  132. currentState = MY_TURN;
  133. break;
  134. case GomokuClient.SERVER:
  135. message = "Game started, waiting for other player...";
  136. currentState = OTHERS_TURN;
  137. break;
  138. }
  139. setChanged();
  140. notifyObservers();
  141.  
  142.  
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement