import ship.*;
import java.util.*;
import java.io.*;
import java.net.*;
class Player
{
private Socket client;
private ObjectOutputStream output;
private ObjectInputStream input;
String name;
public int counter = 0;
/** Creates a new instance of Player */
public Player(String newName)
{
this.name = newName;
} // end Player
// requirements for setting a ship are:
// all spaces have to euqal 'w'
// and can not run off the board
boolean canSetShip(int spacesNeeded, int startRow, int startCol, int direction, Board board)
{
int spacesNeededOne = 0;
try
{
// place ship horizontal
if (direction == 0)
{
if (startCol + spacesNeeded > 9)
return false;
else
{
for (int i = startCol; i < startCol + spacesNeeded; i++)
{
if (board.ar[startRow][i] != 'w')
return false;
} // end for
} // end else
} // end if
// place ship verticle
else if (direction == 1)
{
if (startRow + spacesNeeded > 9)
return false;
else
{
for (int i = startRow; i < startRow + spacesNeeded; i++)
{
if (board.ar[i][startCol] != 'w')
return false;
} // end for
} // end else
} // end else if
} // end try
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException thrown");
return false;
} // end catch
return true;
} // end canSetShip
void setSub(Board b, Sub s)
{
// find a random row
Random startRowRand = new Random();
int startRow = startRowRand.nextInt(10);
// find a random col
Random startColRand = new Random();
int startCol = startColRand.nextInt(10);
// find a random direction
Random directionRand = new Random();
int direction = directionRand.nextInt(2);
int counter = 0;
boolean isSet = false;
while (!isSet)
{
if (canSetShip(s.getSize(), startRow, startCol, direction, b))
{
// place ship horizontally
if (direction == 0)
{
for (int i = startCol; counter < s.getSize(); i++)
{
b.ar[startRow][i] = 'S';
counter++;
} // end for
} // end if
// place ship vertically
else if (direction == 1)
{
for (int i = startRow; counter < s.getSize(); i++)
{
b.ar[i][startCol] = 'S';
counter++;
} // end for
} // end else if
isSet = true;
} // end if
else
{
direction = directionRand.nextInt(2);
startRow = startRowRand.nextInt(10);
startCol = startColRand.nextInt(10);
} // end else
} // end while
} // end setSub
void setBattleShip(Board b, BattleShip bS)
{
// find a random row
Random startRowRand = new Random();
int startRow = startRowRand.nextInt(10);
// find a random col
Random startColRand = new Random();
int startCol = startColRand.nextInt(10);
// find a random direciton
Random directionRand = new Random();
int direction = directionRand.nextInt(2);
int counter = 0;
boolean isSet = false;
while (!isSet)
{
if (canSetShip(bS.getSize(), startRow, startCol, direction, b))
{
// place ship horizontally
if (direction == 0)
{
for (int i = startCol; counter < bS.getSize(); i++)
{
b.ar[startRow][i] = 'B';
counter++;
} // end for
} // end if
// place ship vertically
else if (direction == 1)
{
for (int i = startRow; counter < bS.getSize(); i++)
{
b.ar[i][startCol] = 'B';
counter++;
} // end for
} // end else if
isSet = true;
} // end if
else
{
direction = directionRand.nextInt(2);
startRow = startRowRand.nextInt(10);
startCol = startColRand.nextInt(10);
} // end else
} // end while
} // end setBattleShip
void setPatrol(Board b, Patrol p)
{
// find a random row
Random startRowRand = new Random();
int startRow = startRowRand.nextInt(10);
// find a random col
Random startColRand = new Random();
int startCol = startColRand.nextInt(10);
// find a random direction
Random directionRand = new Random();
int direction = directionRand.nextInt(2);
int counter = 0;
boolean isSet = false;
while (!isSet)
{
if (canSetShip(p.getSize(), startRow, startCol, direction, b))
{
// place ship horizontally
if (direction == 0)
{
for (int i = startCol; counter < p.getSize(); i++)
{
b.ar[startRow][i] = 'P';
counter++;
} // end for
} // end if
// place ship vertically
else if (direction == 1)
{
for (int i = startRow; counter < p.getSize(); i++)
{
b.ar[i][startCol] = 'P';
counter++;
} // end for
} // end else if
isSet = true;
} // end if
else
{
direction = directionRand.nextInt(2);
startRow = startRowRand.nextInt(10);
startCol = startColRand.nextInt(10);
} // end else
} // end while
} // end setPatrol
void setCarrier(Board b, Carrier c)
{
// find a random row
Random startRowRand = new Random();
int startRow = startRowRand.nextInt(10);
// find a random col
Random startColRand = new Random();
int startCol = startColRand.nextInt(10);
// find a random direction
Random directionRand = new Random();
int direction = directionRand.nextInt(2);
int counter = 0;
boolean isSet = false;
while (!isSet)
{
if (canSetShip(c.getSize(), startRow, startCol, direction, b))
{
// place ship horizontally
if (direction == 0)
{
for (int i = startCol; counter < c.getSize(); i++)
{
b.ar[startRow][i] = 'C';
counter++;
} // end for
} // end if
// place ship vertically
else if (direction == 1)
{
for (int i = startRow; counter < c.getSize(); i++)
{
b.ar[i][startCol] = 'C';
counter++;
} // end for
} // end else if
isSet = true;
} // end if
else
{
direction = directionRand.nextInt(2);
startRow = startRowRand.nextInt(10);
startCol = startColRand.nextInt(10);
} // end else
} // end while
} // end setCarrier
// now add in to check if it is sunk and add a counter to keep track of sunk ships when all of a players ships are sunk or the counter is 4
// then the game ends, or all of the spaces are filled with 'x' then declare a draw
void takeTurn(Board b, Sub s, BattleShip bs, Patrol p, Carrier c) throws IOException
{
//Random tryRowRand = new Random();
//int tryRow = tryRowRand.nextInt(10);
//Random tryColRand = new Random();
// int tryCol = tryColRand.nextInt(10);
System.out.println("Please enter a row 0-9");
String row = promptUser();
//sendData(row);
toServer(row);
//System.out.println(row);
//System.out.println("waiting to receive");
// String tryRowS = receive();
int tryRow = Integer.parseInt(row);
System.out.println("Please enter a column 0-9");
String col = promptUser();
//sendData(col);
toServer(col);
//String tryColS = receive();
//System.out.println(col);
int tryCol = Integer.parseInt(col);
// try to hit the sub
if (b.ar[tryRow][tryCol] == 'S')
{
b.ar[tryRow][tryCol] = 'X';
System.out.println(this.name + " hit the Sub!");
s.hits++;
System.out.println("hits equals: " + s.hits);
// see if sub is sunk
if (s.isSunk(s.hits))
{
System.out.println(this.name + " sunk the Sub!");
counter++;
} // end if
} // end if
// try to hit the BattleShip
else if (b.ar[tryRow][tryCol] == 'B')
{
b.ar[tryRow][tryCol] = 'X';
System.out.println(this.name + " hit the BattleShip!");
bs.hits++;
System.out.println("hits equals: " + bs.hits);
if (bs.isSunk(bs.hits))
{
System.out.println(this.name + " sunk the BattleShip!");
counter++;
} // end if
} // end else if
// try to hit the Patrol
else if (b.ar[tryRow][tryCol] == 'P')
{
b.ar[tryRow][tryCol] = 'X';
System.out.println(this.name + " hit the Patrol!");
p.hits++;
System.out.println("hits equals: " + p.hits);
if (p.isSunk(p.hits))
{
System.out.println(this.name + " sunk the Patrol!");
counter++;
} // end if
} // end else if
// try to hit the Carrier
else if (b.ar[tryRow][tryCol] == 'C')
{
b.ar[tryRow][tryCol] = 'X';
System.out.println(this.name + " hit the Carrier!");
c.hits++;
System.out.println("hits equals: " + c.hits);
if (c.isSunk(c.hits))
{
System.out.println(this.name + " sunk the Carrier!");
counter++;
} // end if
} // end else if
else if (b.ar[tryRow][tryCol] == 'w')
{
b.ar[tryRow][tryCol] = 'x';
System.out.println(this.name + " Missed!");
} // end else if
} // end takeTurn
// decide endGame
boolean endGame(int counter)
{
if (counter == 4)
return true;
return false;
} // end endGame
// get input from the keyboard
// get input from the user to play game or exit
public static String promptUser() throws IOException
{
BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try
{
line = keyInput.readLine();
} // end try
//catches bad user input and throws exception
catch (NumberFormatException ex)
{
System.out.print("Error bad data: ");
} // end catch
return line;
} // end promptUser
public void connect() throws IOException
{
//System.out.print("Attempting Connection\n");
client = new Socket(InetAddress.getByName("192.168.1.102"), 1234);
//System.out.print("Connected to: " + client.getInetAddress().getHostName());
} // end connect
// establish the streams
public void streams() throws IOException
{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
//System.out.print("\nStreams Estabolished\n");
} // end streams
// receive the message from Server
public String receive() throws IOException
{
String test = "-1";
try
{
test = (String)input.readObject();
System.out.println("\n" + test);
} // end try
catch(ClassNotFoundException classNotFoundException)
{
System.out.print("\nUnknown object recieved");
} // end catch
return test;
} // end processConnection
public void closeConnection()
{
try
{
output.close();
input.close();
client.close();
} // end try
catch(IOException ioException)
{
ioException.printStackTrace();
} // end catch
} //closeConnection
public void sendData(String data)
{
try
{
output.writeObject(data);
output.flush();
} // end try
catch(IOException ioException)
{
System.out.print("\nError writing object");
} // end catch
} // end sendData
// actually sends data to server
public void toServer(String line) throws IOException
{
connect();
streams();
receive();
//line = c.promptUser();
//System.out.println(line);
sendData(line);
}
} // end Player class