public class GameServer extends Object
PS4 instructions: you MUST NOT change the specs of main() or runGameServer(), or the implementation of main().
Constructor and Description |
---|
GameServer(int port)
Make a new game server that listens for connections on port.
|
Modifier and Type | Method and Description |
---|---|
static void |
main(String[] args)
Start a game server using the given arguments.
|
static void |
runGameServer(Optional<File> file,
int sizeX,
int sizeY,
int port)
Start a new GameServer running on the specified port, with either a random new board or a
board loaded from a file.
|
void |
serve()
Run the server, listening for and handling client connections.
|
public GameServer(int port) throws IOException
port
- port number, requires 0 <= port <= 65535IOException
- if an error occurs opening the server socketpublic void serve() throws IOException
IOException
- if an error occurs waiting for a connection
(IOExceptions from individual clients do *not* terminate serve())public static void main(String[] args)
minesweeper.GameServer [--port PORT] [--size SIZE_X,SIZE_Y | --file FILE]
PORT is an optional integer in the range 0 to 65535 inclusive, specifying the port the server
should be listening on for incoming connections.
E.g. "--port 1234" starts the server listening on port 1234.
SIZE_X and SIZE_Y are optional positive integer arguments, specifying that a random board of size
SIZE_X*SIZE_Y should be generated.
E.g. "--size 42,58" starts the server initialized with a random board of size 42 x 58.
FILE is an optional argument specifying a file pathname where a board has been stored. If this
argument is given, the stored board should be loaded as the starting board.
E.g. "--file boardfile.txt" starts the server initialized with the board stored in
boardfile.txt.
The board file format, for use with the "--file" option, is specified by the following grammar:
FILE ::= BOARD LINE+ BOARD ::= X SPACE Y NEWLINE LINE ::= (VALUE SPACE)* VALUE NEWLINE VALUE ::= "0" | "1" X ::= INT Y ::= INT SPACE ::= " " NEWLINE ::= "\n" | "\r" "\n"? INT ::= [0-9]+The file must contain Y LINEs where each LINE contains X VALUEs. 1 indicates a bomb, 0 indicates no bomb.
If neither --file nor --size is given, generate a random board of size 12 x 12.
Note that --file and --size may not be specified simultaneously.
args
- arguments as describedpublic static void runGameServer(Optional<File> file, int sizeX, int sizeY, int port) throws IOException
file
- if file.isPresent(), start with a board loaded from the specified file,
according to the input file format defined in the documentation for main(..)sizeX
- if (!file.isPresent()), start with a random board with width sizeX
(and require sizeX > 0)sizeY
- if (!file.isPresent()), start with a random board with height sizeY
(and require sizeY > 0)port
- the network port on which the server should listen, requires 0 <= port <= 65535IOException
- if a network error occurs