Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created on 2007 feb 8
- */
- package lab4.data;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.Random;
- import lab4.client.GomokuClient;
- /**
- * Represents the state of a game
- */
- public class GomokuGameState extends Observable implements Observer{
- // Game variables
- private final int DEFAULT_SIZE = 15;
- private GameGrid gameGrid;
- //Possible game states
- private final int NOT_STARTED = 0;
- private final int MY_TURN = 1, OTHERS_TURN = 2, FINISHED = 3;
- private int currentState;
- private GomokuClient client;
- private String message;
- /**
- * The constructor
- *
- * @param gc The client used to communicate with the other player
- */
- public GomokuGameState(GomokuClient gc){
- client = gc;
- client.addObserver(this);
- gc.setGameState(this);
- currentState = NOT_STARTED;
- gameGrid = new GameGrid(DEFAULT_SIZE);
- }
- /**
- * Returns the message string
- *
- * @return the message string
- */
- public String getMessageString(){
- return message;
- }
- /**
- * Returns the game grid
- *
- * @return the game grid
- */
- public GameGrid getGameGrid(){
- return gameGrid;
- }
- /**
- * This player makes a move at a specified location
- *
- * @param x the x coordinate
- * @param y the y coordinate
- */
- public void move(int x, int y){
- if(currentState != MY_TURN){
- message = "fak u men))";
- }
- else if (!gameGrid.move(x, y, MY_TURN)){
- message = "fakink idiots team rash b";
- }
- else if (currentState == FINISHED)
- message = "no";
- client.notifyObservers(message);
- client.sendMoveMessage(x, y);
- }
- /**
- * Starts a new game with the current client
- */
- public void newGame(){
- currentState = OTHERS_TURN;
- }
- /**
- * Other player has requested a new game, so the
- * game state is changed accordingly
- */
- public void receivedNewGame(){
- currentState = MY_TURN;
- }
- /**
- * The connection to the other player is lost,
- * so the game is interrupted
- */
- public void otherGuyLeft(){
- currentState = FINISHED;
- message = "rage quit";
- client.notifyObservers(message);
- }
- /**
- * The player disconnects from the client
- */
- public void disconnect(){
- currentState = FINISHED;
- message = "rage quit";
- client.notifyObservers(message);
- }
- /**
- * The player receives a move from the other player
- *
- * @param x The x coordinate of the move
- * @param y The y coordinate of the move
- */
- public void receivedMove(int x, int y){}
- public void update(Observable o, Object arg) {
- switch(client.getConnectionStatus()){
- case GomokuClient.CLIENT:
- message = "Game started, it is your turn!";
- currentState = MY_TURN;
- break;
- case GomokuClient.SERVER:
- message = "Game started, waiting for other player...";
- currentState = OTHERS_TURN;
- break;
- }
- setChanged();
- notifyObservers();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement