Package scotlandyard.server

Source Code of scotlandyard.server.GameManager

package scotlandyard.server;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import scotlandyard.server.util.Highlighter;
import scotlandyard.shared.BoardState;
import scotlandyard.shared.Player;
import scotlandyard.shared.messaging.MessageErro;
import scotlandyard.shared.messaging.MessageGiveTurn;
import scotlandyard.shared.messaging.MessageMove;

/**
* @author Felipe
*
*/
public class GameManager {
  public static final int SWAP_NUMBER = 143;
  private Random randomGenerator;
  private ArrayList<Player> players;
  // private Player[] players;
  private int currentPlayer;
  // private int playersNumber;
  GameServer game;

  public boolean stopServer = false;

  public GameManager(GameServer game) {
    randomGenerator = new Random();
    // this.players = players;
    // this.playersNumber = playersNumber;

    // currentPlayer = 0;
    this.game = game;
    players = game.getPlayers();
    // add Default Board State
    // currentBoard = new BoardState();
  }

  public void startGame() {
    generatePlayingOrder();
    setPlayersToStartPosition();
  }

  private void setPlayersToStartPosition() {
    for (Player p : players) {
      p.setCurrentPosition(90);
    }
  }

  private void generatePlayingOrder() {
    int a;
    Player tmp;
    int playersNumber = players.size();

    for (int i = 0; i < SWAP_NUMBER; i++) {
      a = randomGenerator.nextInt(playersNumber);

      tmp = players.get(a);
      players.remove(a);
      players.add(tmp);
    }
  }

  public int rowDice() {
    return (randomGenerator.nextInt(4) + 1);
  }

  public void proximoJogador() {
    currentPlayer++;
    // players.size() = playersNumber
    currentPlayer = currentPlayer % players.size();
   
    System.out.println("\tGAMEMANAGER: Proximo jogador: "+players.get(currentPlayer).getName());
    int qtdMovimentos = rowDice();
    Player p = players.get(currentPlayer);
    p.setQtdMovimentos(qtdMovimentos);
   
    game.sendPrivateMessage(
        p,
        new MessageGiveTurn(
            //sender name
            "servidor",
            //Player current position
            String.valueOf(
                p.getCurrentPosition()
                ),
            //player rolled dice
            String.valueOf(
                qtdMovimentos
                )
            ));
  }

  public int move(Player player, int destination) {
    // Checks Movement

    // Takes proper measures: sends message to player

    // Update BoardState
    int src = player.getCurrentPosition();
    MessageMove movimento = new MessageMove(
                  player.getName(),
                  String.valueOf(destination),
                  String.valueOf(src)
                  );

   
   
    if(Highlighter.isValidMove(src, destination, player.getQtdMovimentos())){
      game.sendMessage (movimento);
      player.setCurrentPosition(destination);
      proximoJogador();
    }else{
      System.out.println("GAMEMANAGER: Movimente inv�lido!");
      game.sendPrivateMessage(player,new MessageErro( movimento));
    }
    return src;
    // currentBoard.setPositionOfPlayer(playerIndex, destination);

  }

  public BoardState getCurrentBoardState() {
    int[] currentPlayerPosition = new int[players.size()];
    BoardState bs = new BoardState();

    for (int i = 0; i < players.size(); i++) {
      currentPlayerPosition[i] = players.get(i).getCurrentPosition();
    }

    bs.setPlayerPositions(currentPlayerPosition);
    bs.setMovableAreas(DefaultBoardState.getDefaultBoardStateInstance()
        .getDefaultMovableAreasArray());
    return bs;
  }

  /*
   * public void setPlayersNumber(int playersNumber) { this.playersNumber =
   * playersNumber; }
   */

}
 
TOP

Related Classes of scotlandyard.server.GameManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.