import edu.neu.universe.*; /** * A simple example of a game server that accepts * an arbitrary number of players, keeps track of * the tick count, and ends the game after 10000 * ticks. * * @author Viera K. Proulx * @since 9 April 2010 */ public class MyUniverse extends Universe{ int tickcount = 0; /** * When the player disconnects, print an announcement * with the player's name. * * @param p the player that has disconnected */ public void onDisconnect(MyPlayer p){ System.out.println("Player " + p.name + " disconnected."); } /** * After the player successfully connected, send the * initial "roll" message. */ public void onConnect(MyPlayer p){ p.sendMessage("roll"); } /** * A Factory method that allows us to construct a new * Player instance for the Universe * to add to its player list. */ public MyPlayer initializePlayer(){ return new MyPlayer(); } /** * If the player roll the same die twice, send him a message. * Record the last roll and let the player roll again. * * @param message player's roll * @param p the player who just rolled */ public void onReceive(String message, MyPlayer p){ int newroll = Integer.valueOf(message); if (newroll == p.roll) p.sendMessage("deuce"); p.roll = newroll; // roll again p.sendMessage("roll"); } /** * Stop the game after 10000 ticks */ public void onTick(){ if (++this.tickcount >= 10000) while (this.getClients().size() > 0) this.getClients().get(0).disconnect(); } public static void main(String[] argv){ MyUniverse mu = new MyUniverse(); mu.start(1000, 7070); } }