Create a project of your own choosing. I want your project to use MVC and gameNe
ID: 3857540 • Letter: C
Question
Create a project of your own choosing. I want your project to use MVC and gameNet to create a networked program(game?).
It will be scored with the following weighting:
This assignment will be graded more like a Class Presentation.
It is very important to turn this project in by the due date. Your JING links will be distributed to the rest of the class and this will effectively be your presentation to the rest of the class. I will solicit class inputs on all projects to come up with a grade for this assignment. So being on time with your work and participating in the evaluations is important for this to work.
Your program must be networked and use "gameNet". I would like to see you use concepts taught in the course wherever possible.
Your program can be a game of your own choosing. I would recommend not taking on more than you can handle. Sample games to consider ( not all equally difficult to implement)
In this project we are trying to simulate a task you might have in industry. Your boss gives you a specification like MVC and some software(like gameNet). Your job is to create some software which works with these specifications and the defined interface.
I would preferably like a Tic Tac Toe game
Explanation / Answer
package netgame.tictactoe;
import java.io.IOException;
import netgame.common.Hub;
/**
* A "Hub" for the network TicTacToe game. There is only one Hub
* for a game, and both network players connect to the same Hub.
* Official information about the state of the game is maintained
* on the Hub. When the state changes, the Hub sends the new
* state to both players, ensuring that both players see the
* same state.
*/
public class TicTacToeGameHub extends Hub {
private TicTacToeGameState state; // Records the state of the game.
/**
* Create a hub, listening on the specified port. Note that this
* method calls setAutoreset(true), which will cause the output stream
* to each client to be reset before sending each message. This is
* essential since the same state object will be transmitted over and
* over, with changes between each transmission.
* @param port the port number on which the hub will listen.
* @throws IOException if a listener cannot be opened on the specified port.
*/
public TicTacToeGameHub(int port) throws IOException {
super(port);
state = new TicTacToeGameState();
setAutoreset(true);
}
/**
* Responds when a message is received from a client. In this case,
* the message is applied to the game state, by calling state.applyMessage().
* Then the possibly changed state is transmitted to all connected players.
*/
protected void messageReceived(int playerID, Object message) {
state.applyMessage(playerID, message);
sendToAll(state);
}
/**
* This method is called when a player connects. If that player
* is the second player, then the server's listening socket is
* shut down (because only two players are allowed), the
* first game is started, and the new state -- with the game
* now in progress -- is transmitted to both players.
*/
protected void playerConnected(int playerID) {
if (getPlayerList().length == 2) {
shutdownServerSocket();
state.startFirstGame();
sendToAll(state);
}
}
/**
* This method is called when a player disconnects. This will
* end the game and cause the other player to shut down as
* well. This is accomplished by setting state.playerDisconnected
* to true and sending the new state to the remaining player, if
* there is one, to notify that player that the game is over.
*/
protected void playerDisconnected(int playerID) {
state.playerDisconnected = true;
sendToAll(state);
}
}
protected void messageReceived(final Object message) {
if (message instanceof TicTacToeGameState) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
// The newState() method updates the GUI for the new state.
newState( (TicTacToeGameState)message );
}
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.