Skip to content

Commit

Permalink
Final commit
Browse files Browse the repository at this point in the history
Needs the commands to work
  • Loading branch information
jlowe64 committed Sep 9, 2013
1 parent 45391a5 commit f9d63ea
Show file tree
Hide file tree
Showing 31 changed files with 397 additions and 259 deletions.
Binary file modified Battleship$GameState.class
Binary file not shown.
Binary file modified Battleship.class
Binary file not shown.
87 changes: 43 additions & 44 deletions Battleship.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import java.awt.event.*; // Use some events
//import java.awt.*; // Graphics
//import javax.swing.*; // Use swing for GUI
import java.util.*; // Util
import java.io.*; //I/O
import java.net.*; //Sockets
Expand All @@ -11,8 +9,7 @@
* @author Jerrett Fowler
* @version 1.2 (August 2013)
*/

//public class Battleship extends JFrame implements MouseListener, WindowListener

public class Battleship
{

Expand All @@ -25,14 +22,17 @@ private enum GameState
private Parser parser;
private Protocol protocol;
Player currentPlayer;
Grid grids;
Fleet fleet;

/**
* Main
*/
public static void main(String[] args) throws IOException
{
int i = 0, portArg;
String arg, machine;
String arg = new String();
String machine = new String();
boolean vflag = false;
while (i < args.length && args[i].startsWith("-"))
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public Battleship(String gameState, String serverName, int portNumber)
//set up fleet
newFleet();
//set up new grids
//createGrids();
createGrids();

switch(gameState)
{
Expand Down Expand Up @@ -205,6 +205,22 @@ public void play()
{
printOpening();

System.out.println(" GAME BOARD");

if(gameState == GameState.INITIALISE) //change this when READY works
{
grids.addShipFromFleet(fleet.getShipInFleet("SUBMARINE1"));
grids.addShipFromFleet(fleet.getShipInFleet("SUBMARINE2"));
grids.addShipFromFleet(fleet.getShipInFleet("SUBMARINE3"));
grids.addShipFromFleet(fleet.getShipInFleet("BATTLESHIP1"));
grids.addShipFromFleet(fleet.getShipInFleet("DESTROYER1"));
grids.addShipFromFleet(fleet.getShipInFleet("DESTROYER2"));
grids.printGrid();
}

System.out.println();
System.out.println("Board is set up for you. Command for Placing a ship is not fully implemented.");

//Main gameplay loop
while(!getPlayer().getEnd())
{
Expand Down Expand Up @@ -242,40 +258,49 @@ else if(command.getName().equals("HELP"))
else if(command.getName().equals("TALK"))
{
command.execute(getPlayer());

//send message to other player
}

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

//send message to other player
}

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

//send message to other player
}

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

//edit Grid

//send message to other player
}

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

//remove ship from ArrayList

//send message to other player
}

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

else if(command.getName().equals("SHIP"))
{
command.execute(getPlayer());
//send message to other player
}


//need to add more stuff here

}

printClosing();
Expand Down Expand Up @@ -328,7 +353,7 @@ public Player getPlayer()
*/
public void newFleet()
{
Fleet fleet = new Fleet();
fleet = new Fleet();
}

/**
Expand All @@ -337,34 +362,8 @@ public void newFleet()
private void createGrids()
{
// Create player grid
Grid grids = new Grid();
grids = new Grid();


}

/**
* Start of program
public void init()
{
setSize(256, 256);
}*/

/**
* Mouse Event Overrides
*/
//public void mouseClicked(MouseEvent event) {}
//public void mouseEntered(MouseEvent event) {}
//public void mouseExited(MouseEvent event) {}
//public void mousePressed(MouseEvent event) {}
//public void mouseReleased(MouseEvent event) {}

/**
* GFX Event Overrides
*/
//public void windowActivated(WindowEvent event) {}
//public void windowClosed(WindowEvent event) {}
//public void windowClosing(WindowEvent event) {}
//public void windowDeactivated(WindowEvent event) {}
//public void windowDeiconified(WindowEvent event) {}
//public void windowIconified(WindowEvent event) {}
//public void windowOpened(WindowEvent event) {}
}
2 changes: 1 addition & 1 deletion Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author Jerrett Fowler
* @version 1.0 (July 2013)
*/
public abstract class Command //Why use abstract: http://stackoverflow.com/questions/7262892/java-best-practices-put-get-subclass-objects-into-hashmap-that-expects-supercla
public abstract class Command
{
public String secondWord;
public String thirdWord;
Expand Down
Binary file modified CommandWords.class
Binary file not shown.
1 change: 1 addition & 0 deletions CommandWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public CommandWords()
commands.put("STRING", new StringCommand());
commands.put("INT", new IntCommand());
commands.put("HELP", new HelpCommand());
commands.put("PLACE", new PlaceCommand());
}

// Method to check if command is valid
Expand Down
Binary file modified FireCommand.class
Binary file not shown.
19 changes: 17 additions & 2 deletions FireCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ public boolean execute(Object o)
}
else
{
//Check for the secondword and thirdword being ints and convert to ints appropriately
System.out.println("Fire");
Player p = null;
p = (Player)o;

String[] strArray = getSecondWord().split(" ");
int[] intArray = new int[strArray.length];

for(int i = 1; i < strArray.length; i++)
{
if(strArray[i] != null)
{
intArray[i] = Integer.parseInt(strArray[i]);
}

}

System.out.println("Fired on coordinates: " + intArray[1] + " x " + intArray[2]);

return true;
}
}
Expand Down
Binary file modified Fleet.class
Binary file not shown.
9 changes: 6 additions & 3 deletions Fleet.ctxt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ comment0.target=Fleet()
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Fleet\n
comment1.params=
comment1.target=int\ size()
comment1.text=\n\ An\ example\ of\ a\ method\ -\ replace\ this\ comment\ with\ your\ own\n\ \n\ @param\ \ y\ \ \ a\ sample\ parameter\ for\ a\ method\n\ @return\ \ \ \ \ the\ sum\ of\ x\ and\ y\ \n
comment1.text=\n\ Size\ of\ fleet\n\ \n\ @param\ \ y\ \ \ a\ sample\ parameter\ for\ a\ method\n\ @return\ \ \ \ \ the\ sum\ of\ x\ and\ y\ \n
comment2.params=s
comment2.target=void\ sink(Ship)
comment2.text=\n\ Remove\ ships\ from\ fleet\n\ \n\ @param\ \ s\ \ \ A\ ship\n\ @return\ \ \ \ \ void\n
comment3.params=shipID
comment3.target=Ship\ getShipInFleet(java.lang.String)
comment3.text=\n\ Return\ an\ instance\ of\ a\ specific\ ship\n\ \n\ @param\ \ s\ \ \ A\ ship\n\ @return\ \ \ \ \ ship\n
numComments=4
comment3.text=\n\ Return\ an\ instance\ of\ a\ specific\ ship\n\ \n\ @param\ \ shipID\ \ \ A\ ship\n\ @return\ \ \ \ \ ship\n
comment4.params=
comment4.target=void\ getFleet()
comment4.text=\n\ Lists\ all\ ships\n
numComments=5
46 changes: 24 additions & 22 deletions Fleet.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Iterator;

/**
* Write a description of class Fleet here.
Expand All @@ -23,29 +24,30 @@ public class Fleet
public Fleet()
{
//3 Subs
sub1 = new Ship("s1", 2, 0, 0, 0);
sub2 = new Ship("s2", 2, 0, 0, 0);
sub3 = new Ship("s3", 2, 0, 0, 0);
sub1 = new Ship("SUBMARINE1", 2, 1, 0, 0);
sub2 = new Ship("SUBMARINE2", 2, 0, 2, 2);
sub3 = new Ship("SUBMARINE3", 2, 1, 1, 6);

//1 Battleship
battleship1 = new Ship("b1", 4, 0, 0, 0);
battleship1 = new Ship("BATTLESHIP1", 4, 0, 5, 2);

//2 Destroyers
destroyer1 = new Ship("d1", 3, 0, 0, 0);
destroyer2 = new Ship("d2", 3, 0, 0, 0);
destroyer1 = new Ship("DESTROYER1", 3, 1, 7, 1);
destroyer2 = new Ship("DESTROYER2", 3, 1, 6,7);

fleet = new ArrayList();
fleet = new ArrayList<Ship>();

fleet.add(sub1);
fleet.add(sub2);
fleet.add(sub3);
fleet.add(battleship1);
fleet.add(destroyer1);
fleet.add(destroyer2);

}

/**
* An example of a method - replace this comment with your own
* Size of fleet
*
* @param y a sample parameter for a method
* @return the sum of x and y
Expand All @@ -69,35 +71,35 @@ public void sink(Ship s)
}
}


/**
* Return an instance of a specific ship
*
* @param s A ship
* @param shipID A ship
* @return ship
*/
public Ship getShipInFleet(String shipID)
{
Ship ship = new Ship("id", 0, 0, 0, 0);
Ship temp = null;
for(Ship s : fleet)
{
if(s.getShipID().equals(shipID))
{
ship = s;
temp = s;
}
}
return ship;
return temp;
}

/**
* Return all ships locations/Prints
*
* @param position where is the ship?
* @return Return ship
public Ship location(int position)
* Lists all ships
*/
public void getFleet()
{
//
return ;
}*/
Iterator<Ship> itr = fleet.iterator();

while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Binary file modified Grid.class
Binary file not shown.
11 changes: 4 additions & 7 deletions Grid.ctxt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Grid\n
comment1.params=s
comment1.target=void\ addShipFromFleet(Ship)
comment1.text=\n\ Add\ Ship\ from\ fleet\n\ \n\ @param\ \ y\ \ \ a\ sample\ parameter\ for\ a\ method\n\ @return\ \ \ \ \ the\ sum\ of\ x\ and\ y\ \n
comment2.params=a
comment2.target=void\ fillArray(int[][])
comment2.text=\n\ \n
comment3.params=a
comment3.target=void\ printGrid(int[][])
comment3.text=\n\ Print\ grid\n\ \n\ @param\ \ y\ \ \ a\ sample\ parameter\ for\ a\ method\n\ @return\ \ \ \ \ the\ sum\ of\ x\ and\ y\ \n
numComments=4
comment2.params=
comment2.target=void\ printGrid()
comment2.text=\n\ Print\ grid\n\ \n\ @param\ \ y\ \ \ a\ sample\ parameter\ for\ a\ method\n\ @return\ \ \ \ \ the\ sum\ of\ x\ and\ y\ \n
numComments=3
Loading

0 comments on commit f9d63ea

Please sign in to comment.