Advertisement
Advertisement
| 03.11.2008 at 08:01PM PDT, ID: 23233942 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: |
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import javax.swing.text.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
class LODServer {
ServerBase<ClientHandler> server = null;
/*
* * Once the given number of players has connected, * the instance of
* ServerBase<ClientHandler> returns * an array of them.
*/
ArrayList<ClientHandler> clients = null;
/*
* * This is the data that encodes the state of the game. * The labyrinth is
* stored in a 2D array with * numbers corresponding to the contents of each
* location.
*/
static final int TREASURE = 1;
/* Larger positive numbers are several lots of treasure */
static final int EMPTY = 0;
static final int HEALTH = -1;
static final int LANTERN = -2;
static final int SWORD = -3;
static final int ARMOUR = -4;
static final int EXIT = -5;
static final int WALL = -6;
int mapWidth = 5;
int mapHeight = 5;
int map[/* mapHeight */][/* mapWidth */] = {
{ WALL, WALL, ARMOUR, WALL, WALL },
{ WALL, EXIT, EMPTY, TREASURE, TREASURE },
{ LANTERN, TREASURE, EMPTY, HEALTH, LANTERN },
{ WALL, SWORD, EMPTY, ARMOUR, TREASURE },
{ WALL, WALL, SWORD, WALL, WALL } };
boolean gameStarted = false;
boolean gameFinished = false;
int goal = 2; // The amount of treasure to collect
/*
* * The server has to be able to wait until the end of the player's turn. *
* It uses a semaphore to do this. * The player acquires the semaphore at
* the start of the turn * and releases it at the end of the turn. * This
* allows the server to wait until the end of the player's turn by * simply
* waiting for the semaphore to be released.
*/
Semaphore playerTurn = new Semaphore(1);
public void setup(int port, int players, String map) throws java.io.IOException {
// Limit to one player
if (players<1) {
System.err
.println("The current implementation is limited to one player.");
players = 1;
}
// Create the object to handle the incoming connections
// ( you don't have to understand how this works )
// If the last arguement is true then all incoming and outgoing messages
// to the instances of ClientHandler will be logged.
// Set to false if you don't want this.
server = new ServerBase<ClientHandler>(ClientHandler.class, players,
port, true);
xmlParser(map);
} // void setup (int port, int players, String map)
// Any set up for the game that can be done
// before the clients connect goes here...
Document document = null;
public void readMap() {
org.w3c.dom.Element element = document.getDocumentElement();
NodeList nl = element.getElementsByTagName("map");
NamedNodeMap mapAttributes = element.getAttributes();
String title = element.getAttribute("name");
if (!title.equals("")) {
System.out.println("got title");
}
String mGoal = element.getAttribute("goal");
if (!mGoal.equals("")) {
System.out.println("got map Goal");
}
String mWidth = element.getAttribute("width"); // width of map
String mHeight = element.getAttribute("height"); // height of map
if ((!mWidth.equals("")) && (!mHeight.equals(""))) {
System.out.println("got map dimensions");
} else {
System.out
.println("Map dimensions were not provied, loading will not "
+ "continue, please restart game and provide quality map");
System.exit(1);
}
if (nl != null && nl.getLength() > 0) {
System.out.println("got access to decedent elements");
for (int i = 0; i < nl.getLength(); i++) {
Element tiles = (Element) nl.item(i);
// check what element were looking at i.e tile, renderhint, item
// etc
// then add it to the corrsponding flied in the class
if (tiles.getName().equals("tile")) {
NamedNodeMap tileAttributes = element.getAttributes();
String tileColumn = element.getAttribute("x");
String tileRow = element.getAttribute("y");
if ((!tileColumn.equals("")) && (!tileRow.equals(""))) {
System.out.println("got some tile x y values");
// any tile outside of the dimensions of the map le
// is ignored
int mapTileRow = Integer.parseInt(tileRow);
int mapTileColumn = Integer.parseInt(tileColumn);
if ((mapTileColumn > Integer.parseInt(mWidth) || mapTileColumn < 0)
|| (mapTileRow > Integer.parseInt(mHeight) || mapTileRow < 0)) {
break;
}
String tileType = element.getAttribute("type");
if (!tileType.equals("")) {
System.out.println("got a type of tile");
}
// get item if item on tile
String itemOnTile = element.getAttribute("item");
if (!itemOnTile.equals("")) {
System.out.println("got an item on a tile");
}
// start location of player
String startLoc = element.getAttribute("startlocation");
if (!startLoc.equals("")) {
System.out.println("in got a starting location");
}
// get render-hint item on tile
if (tiles.getName().equals("renderhint")) {
Text textNode = (Text) ((Text) tiles).getFirstChild();
String text = textNode.getData().trim();
System.out.println(text);
}
} else {
System.out
.println("Tile provided is not valid, game is ending, "
+ "please re think your mapping");
System.exit(1);
}
}
}
}
}
public void xmlParser(String xmlMap) throws IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder parser = null;
try {
parser = factory.newDocumentBuilder();
} catch (javax.xml.parsers.ParserConfigurationException d) {
System.out.println("error with new document builder");
}
try {
document = parser.parse(new File(xmlMap));
} catch (org.xml.sax.SAXException e) {
System.out
.println("Error with parser file, xml file does not conform to "
+ "map DTD");
System.out.println(e);
System.out.println("Exiting game");
System.exit(1);
}
// read the xml file and configure maps in 2D arrays in correspondance
// to it
readMap();
}
public void play() throws InterruptedException {
// Wait for all of the clients to connect
clients = server.getConnections();
System.out.println(" done.");
// Any set up for the game that is specific
// to the clients goes here.
// Give each ClientHandler a reference to this object
// and thus access to the game data
for (ClientHandler c : clients) {
c.server = this;
}
// Fix the player's starting location
clients.get(0).x = 2;
clients.get(0).y = 2;
// Let each client know the amount of treasure it needs
for (ClientHandler c : clients) {
c.serverGoal(goal);
}
// Play the game!
System.out.println("Starting the game.");
gameStarted = true;
while (gameFinished == false) {
// Check that there is at least one player connected
if (clients.size() <= 0) {
System.out.println("Game abandoned");
break;
}
// Each player takes a turn
// for (ClientHandler c : clients) {
ClientHandler c = clients.get(0);
// Start turn
System.out
.println("Starting the turn of player \"" + c.name + "\"");
c.startTurn(); // During this, c will acquire() the playerTurn
// semaphore
// Wait for the player to finish their turn
// (they will release() the semaphore at the end of their turn)
try {
playerTurn.acquire();
} catch (InterruptedException e) {
// This shouldn't happen in normal operation
// If it happens, it is because this code
// has been used in a larger bit of software
// thus the wrapper can deal with this condition
throw (e);
}
System.out.println("End of turn");
// Check to see if the current player has won
// (gameFinished is set to true in ClientHandler.clientEndTurn())
if (gameFinished == true) {
System.out.println("Game over, \"" + c.name
+ "\" is the winner");
// Tell the player they have won
c.serverMessage("Congratulations " + c.name + " you won!");
c.serverWin();
// Tell everyone else that they haven't
for (ClientHandler d : clients) {
if (d != c) {
d.serverMessage("Sorry " + d.name + " it looks like "
+ c.name + " beat you.");
d.serverLose();
}
}
}
// Release the semaphore so that the next player can start their
// turn
playerTurn.release();
// }
}
return;
}
// A simple helper function
public static void printUsageAndDie(String error) {
System.err.println(error);
System.err.println("Usage:");
System.err.println("\tjava LODServer <port> <players> <map>");
System.exit(1);
}
// The main method just handles the command line arguements
// creates one instance of LODServer and starts it running
public static void main(String args[]) throws java.io.IOException {
// Check the command line arguements
if (args.length != 3) {
printUsageAndDie("Incorrect number of command line arguements");
} else {
// Parse the command line arguements
int port = Integer.parseInt(args[0]);
// Only ports 1025 to 65535 are suitable
if ((port <= 1024) || (port >= 65536)) {
printUsageAndDie("Invalid port number");
}
int players = Integer.parseInt(args[1]);
if (players < 0) {
printUsageAndDie("Invalid number of users");
}
// Create a LODServer object to run the game
LODServer s = new LODServer();
// Set it up
s.setup(port, players, args[2]);
// Go!
try {
s.play();
} catch (InterruptedException e) {
// Something has gone wrong - abort
System.err.println("Caught InterruptedException(?)");
System.exit(1);
}
}
}
}
|