Skip to content

Commit

Permalink
Add a lot of command interactions and fixed the command structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowe64 committed Sep 4, 2013
1 parent 386a545 commit 0790270
Show file tree
Hide file tree
Showing 66 changed files with 597 additions and 161 deletions.
Binary file added Battleship$GameState.class
Binary file not shown.
Binary file modified Battleship.class
Binary file not shown.
28 changes: 14 additions & 14 deletions Battleship.ctxt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ comment1.text=\n\ Create\ the\ game\ and\ initialise\ the\ grids\n
comment10.params=
comment10.target=void\ createGrids()
comment10.text=\n\ Create\ grids\n
comment2.params=
comment2.target=void\ play()
comment2.text=\n\ Main\ play\ routine.\ Loops\ until\ one\ player's\ fleet\ is\ sunk.\n
comment3.params=
comment3.target=void\ printOpening()
comment3.text=\n\ Opening\ statement\n
comment2.params=serverName\ portNumber
comment2.target=void\ client(java.lang.String,\ int)
comment2.text=\n\ Start\ Client\n
comment3.params=portNumber
comment3.target=void\ host(int)
comment3.text=\n\ Start\ Server\n
comment4.params=
comment4.target=void\ printClosing()
comment4.text=\n\ Closing\ statement\n
comment5.params=serverName\ portNumber
comment5.target=void\ client(java.lang.String,\ int)
comment5.text=\n\ Start\ Client\n
comment6.params=portNumber
comment6.target=void\ host(int)
comment6.text=\n\ Start\ Server\n
comment4.target=void\ play()
comment4.text=\n\ Main\ play\ routine.\ Loops\ until\ one\ player's\ fleet\ is\ sunk.\n
comment5.params=
comment5.target=void\ printOpening()
comment5.text=\n\ Opening\ statement\n
comment6.params=
comment6.target=void\ printClosing()
comment6.text=\n\ Closing\ statement\n
comment7.params=
comment7.target=void\ setPlayer()
comment7.text=\n\ A\ new\ player\n
Expand Down
224 changes: 134 additions & 90 deletions Battleship.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
//public class Battleship extends JFrame implements MouseListener, WindowListener
public class Battleship
{

private enum GameState
{
INITIALISE, SERVERREADY, SERVERTURN
}

private GameState gameState = GameState.INITIALISE;
private Parser parser;
private Protocol protocol;
Player currentPlayer;
Expand Down Expand Up @@ -84,78 +91,20 @@ public Battleship(String gameState, String serverName, int portNumber)
case "Client":
client(serverName, portNumber);
//player sets up grids, sends <READY> message
play();
System.out.println("Client Breaks");
break;

case "Host":
host(portNumber);
//host randomly decides <FIRSTGO> and sends message to user on decision
//player sets up grids, sends <READY> message
play();
System.out.println("Host Breaks");
break;

default: System.err.println("That went wrong");
}
}

/**
* Main play routine. Loops until one player's fleet is sunk.
*/
public void play()
{
printOpening();

boolean end = false;

//Main gameplay loop
while(!end)
{
Command command = parser.getCommand();
if(command == null)
{
System.out.println("Please enter your orders again, ");
System.out.println("and remember that orders are in ALL CAPS...");
}
else
{
end = command.execute(getPlayer());
}
}

printClosing();
}

/**
* Opening statement
*/
private void printOpening()
{
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("Welcome to Battleships!");
System.out.println("Type 'help' if you need help.");
System.out.println("All commands are in ALL CAPS.");
System.out.println();
}

/**
* Closing statement
*/
private void printClosing()
{
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("Sorry to see you go soldier.");
System.out.println("Game has ended.");
System.out.println();
}

/**
* Start Client
*/
Expand All @@ -181,21 +130,17 @@ public void client(String serverName, int portNumber)
System.exit(1);
}

while(!end)
{
//responds to <SUPPORTS> message with <YES> or <NO> to each
//items set as <NO> will go unsupported on the host
end = END.execute(getPlayer());
if (end == true)
{
try {
connection.close();
} catch (IOException e) {
System.err.println("Could not find close connection.");
}
}
}
//responds to <SUPPORTS> message with <YES> or <NO> to each
//items set as <NO> will go unsupported on the host

//Play Game
play();

try {
connection.close();
} catch (IOException e) {
System.err.println("Could not find close connection.");
}
//out.close();
//in.close();

Expand All @@ -218,8 +163,6 @@ public void host(int portNumber)
Command NO = protocol.getCommand("NO");
Command END = protocol.getCommand("END");



try {
server = new ServerSocket(portNumber);
System.out.println("Host set up on port: " + portNumber);
Expand All @@ -231,35 +174,136 @@ public void host(int portNumber)
//listens for connection request
Socket client = null;

while(!end)
{
try {
try {
System.out.println("Waiting on connection...");
client = server.accept();
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
} catch (IOException e) {
} catch (IOException e) {
System.err.println("Cannot accept connection. Failed.");
System.exit(1);
}

START.execute(getPlayer());
//sends <START> message to client
//sends a <SUPPORTS> message to client for each feature used
//when host cycles through all <SUPPORTS>, it sends an <END>
}
//START.execute(getPlayer());
//sends <START> message to client
//sends a <SUPPORTS> message to client for each feature used
//when host cycles through all <SUPPORTS>, it sends an <END>

//Play Game
play();

try {
server.close();
} catch (IOException e) {
//
}
}

/**
* Main play routine. Loops until one player's fleet is sunk.
*/
public void play()
{
printOpening();

//Main gameplay loop
while(!getPlayer().getEnd())
{

end = END.execute(getPlayer());
Command command = parser.getCommand();

if(command == null)
{
System.out.println("Please enter your orders again, ");
System.out.println("and remember that orders are in ALL CAPS...");
}

if (end == true)
else if(command.getName().equals("READY"))
{
try {
server.close();
} catch (IOException e) {
//
if(gameState == GameState.INITIALISE)
{
command.execute(getPlayer());
}
else
{
System.out.println("Already ready...?");
}
}

else if(command.getName().equals("END"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("HELP"))
{
command.execute(parser.getCommandWords());
}

else if(command.getName().equals("TALK"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("FIRE"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("MISS"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("HIT"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("SUNK"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("ILOSE"))
{
command.execute(getPlayer());
}

else if(command.getName().equals("SHIP"))
{
command.execute(getPlayer());
}


//need to add more stuff here
}

printClosing();
}

/**
* Opening statement
*/
private void printOpening()
{
System.out.println();
System.out.println();
System.out.println("Welcome to Battleships!");
System.out.println("Type 'HELP' if you need help.");
System.out.println("All commands are in ALL CAPS.");
System.out.println();
}

/**
* Closing statement
*/
private void printClosing()
{
System.out.println();
System.out.println();
System.out.println("Sorry to see you go soldier.");
System.out.println("Game has ended.");
System.out.println();
}

/**
Expand Down
Binary file modified Command.class
Binary file not shown.
7 changes: 5 additions & 2 deletions Command.ctxt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ comment6.target=boolean\ hasThirdWord()
comment6.text=\n\ Return\ true\ if\ the\ command\ has\ a\ third\ word\n
comment7.params=o
comment7.target=boolean\ execute(java.lang.Object)
comment7.text=\n\ Return\ true\ if\ there\ is\ an\ exit\n
numComments=8
comment7.text=\n\ Return\ true\ if\ conditions\ are\ met\n
comment8.params=
comment8.target=java.lang.String\ getName()
comment8.text=\n\ Return\ String\ of\ name\ of\ command\n
numComments=9
8 changes: 6 additions & 2 deletions Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ public boolean hasThirdWord()
return (thirdWord != null);
}

//This needs help
/**
* Return true if there is an exit
* Return true if conditions are met
*/
public abstract boolean execute(Object o);

/**
* Return String of name of command
*/
public abstract String getName();
}
Binary file modified CommandWords.class
Binary file not shown.
5 changes: 3 additions & 2 deletions CommandWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public CommandWords()
commands.put("SHIP", new ShipCommand());
commands.put("STRING", new StringCommand());
commands.put("INT", new IntCommand());
commands.put("HELP", new HelpCommand());
}

// Method to check if command is valid
Expand All @@ -53,11 +54,11 @@ public Command getCommand(String s)
*/
public void printCommands()
{
Iterator iterator = commands.entrySet().iterator();
Iterator<Map.Entry<String, Command>> iterator = commands.entrySet().iterator();

while(iterator.hasNext())
{
Map.Entry pairs = (Map.Entry)iterator.next();
Map.Entry<String, Command> pairs = iterator.next();
System.out.println(pairs.getKey());
}
}
Expand Down
Binary file modified EndCommand.class
Binary file not shown.
5 changes: 4 additions & 1 deletion EndCommand.ctxt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ comment0.text=\n\ Constructor\ for\ objects\ of\ class\ EndCommand\n
comment1.params=o
comment1.target=boolean\ execute(java.lang.Object)
comment1.text=\n\ ''END''\ \\n\n\ \n\ @param\ \ y\ \ \ a\ sample\ parameter\ for\ a\ method\n\ @return\ \ \ \ \ the\ sum\ of\ x\ and\ y\ \n
numComments=2
comment2.params=
comment2.target=java.lang.String\ getName()
comment2.text=\n\ Return\ String\ of\ name\ of\ command\n
numComments=3
Loading

0 comments on commit 0790270

Please sign in to comment.