Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package battleshipgame.userInterface;
- import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
- import core.Battleship;
- import core.Carrier;
- import core.Constants;
- import core.Destroyer;
- import core.PatrolBoat;
- import core.Ship;
- import core.Submarine;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class Player
- {
- // Class members
- private boolean isFirst;
- private Color shipColor = Color.RED;
- private int currentShipLength;
- private int currentShip;
- private int currentDirection;
- private String userName;
- private JButton[][] buttonBoard;
- private final static int rows = 10;
- private final static int cols = 10;
- private static PatrolBoat patrolBoat;
- private static Submarine submarine;
- private static Carrier carrier;
- private static Battleship battleship;
- private static Destroyer destroyer;
- private static BoardListener boardListener;
- public Player(String name)
- {
- userName = name;
- initObjects();
- initComponents();
- }
- private void initObjects()
- {
- battleship = new Battleship();
- carrier = new Carrier();
- destroyer = new Destroyer();
- patrolBoat = new PatrolBoat();
- submarine = new Submarine();
- boardListener = new BoardListener();
- }
- private void initComponents()
- {
- buttonBoard = new JButton [getRows()][getCols()];
- for (int row = 0; row < 10; row++)
- {
- for (int col = 0; col < 10; col++)
- {
- buttonBoard[row][col] = new JButton();
- buttonBoard[row][col].putClientProperty("row", row);
- buttonBoard[row][col].putClientProperty("col", col);
- buttonBoard[row][col].addActionListener(boardListener);
- }
- }
- }
- public String getUserName()
- {
- return userName;
- }
- public JButton[][] getBoard()
- {
- return buttonBoard;
- }
- /**
- * @return the rows
- */
- public static int getRows()
- {
- return rows;
- }
- /**
- * @return the cols
- */
- public static int getCols()
- {
- return cols;
- }
- public int getCurrentShipLength()
- {
- return currentShipLength;
- }
- public void setCurrentShip(int currentShip)
- {
- this.currentShip = currentShip;
- if(currentShip == Constants.BATTLESHIP)
- currentShipLength = Constants.BATTLESHIP_LENGTH;
- else if(currentShip == Constants.CARRIER)
- currentShipLength = Constants.CARRIER_LENGTH;
- else if(currentShip == Constants.DESTROYER)
- currentShipLength = Constants.DESTROYER_LENGTH;
- else if(currentShip == Constants.SUBMARINE)
- currentShipLength = Constants.SUBMARINE_LENGTH;
- else if(currentShip == Constants.PATROL)
- currentShipLength = Constants.PATROL_BOAT_LENGTH;
- }
- public int getCurrentDirection()
- {
- return currentDirection;
- }
- public void setCurrentDirection(int currentDirection)
- {
- this.currentDirection = currentDirection;
- }
- public boolean isIsFirst()
- {
- return isFirst;
- }
- public void setIsfirst(boolean isFirst)
- {
- this.isFirst = isFirst;
- }
- public Color getShipColor()
- {
- return shipColor;
- }
- public void setShipColor(String shipColor)
- {
- //this.shipcolor = shipColor;
- }
- public static Battleship getBattleship()
- {
- return battleship;
- }
- public static Carrier getCarrier()
- {
- return carrier;
- }
- public static Destroyer getDestroyer()
- {
- return destroyer;
- }
- public static PatrolBoat getPatrolBoat()
- {
- return patrolBoat;
- }
- public static Submarine getSubmarine()
- {
- return submarine;
- }
- private void placeShip(int rowClick, int colClick)
- {
- switch( getCurrentDirection())
- {
- case Constants.VERTICAL:
- {
- for(int row = rowClick; row < (rowClick + getCurrentShipLength());row++)
- {
- buttonBoard[row][colClick].setBackground(getShipColor());
- if(currentShip == Constants.BATTLESHIP)
- {
- if(getBattleship().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getBattleship().setShipDirection(Constants.VERTICAL);
- getBattleship().setShipLocation(row, colClick);
- }
- else if(currentShip ==Constants.CARRIER)
- {
- if(getCarrier().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getCarrier().setShipDirection(Constants.VERTICAL);
- getCarrier().setShipLocation(row, colClick);
- }
- else if(currentShip ==Constants.DESTROYER)
- {
- if(getDestroyer().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getDestroyer().setShipDirection(Constants.VERTICAL);
- getDestroyer().setShipLocation(row, colClick);
- }
- else if(currentShip ==Constants.SUBMARINE)
- {
- if(getSubmarine().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getSubmarine().setShipDirection(Constants.VERTICAL);
- getSubmarine().setShipLocation(row, colClick);
- }
- else if(currentShip ==Constants.PATROL)
- {
- if(getPatrolBoat().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getPatrolBoat().setShipDirection(Constants.VERTICAL);
- getPatrolBoat().setShipLocation(row, colClick);
- }
- }
- break;
- case Constants.HORIZONTAL:
- {
- for(int col = colClick; col < (colClick + getCurrentShipLength());col++)
- {
- buttonBoard[rowClick][col].setBackground(getShipColor());
- if(currentShip == Constants.BATTLESHIP)
- {
- if(getBattleship().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getBattleship().setShipDirection(Constants.HORIZONTAL);
- getBattleship().setShipLocation(rowClick, col);
- }
- else if(currentShip ==Constants.CARRIER)
- {
- if(getCarrier().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getCarrier().setShipDirection(Constants.HORIZONTAL);
- getCarrier().setShipLocation(rowClick, col);
- }
- else if(currentShip ==Constants.DESTROYER)
- {
- if(getDestroyer().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getDestroyer().setShipDirection(Constants.HORIZONTAL);
- getDestroyer().setShipLocation(rowClick, col);
- }
- else if(currentShip ==Constants.SUBMARINE)
- {
- if(getSubmarine().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getSubmarine().setShipDirection(Constants.HORIZONTAL);
- getSubmarine().setShipLocation(rowClick, col);
- }
- else if(currentShip ==Constants.PATROL)
- {
- if(getPatrolBoat().isShipPlaced())
- {
- //remove exisisting battleship to move it
- }
- getPatrolBoat().setShipDirection(Constants.HORIZONTAL);
- getPatrolBoat().setShipLocation(rowClick, col);
- }
- }
- break;
- }
- }
- }}
- private boolean isValid(int rowClick, int colClick)
- {
- switch( getCurrentDirection())
- {
- case Constants.VERTICAL:
- {
- if( (rowClick + getCurrentShipLength()) > getRows())
- return false;
- break;
- }
- case Constants.HORIZONTAL:
- {
- if((colClick + getCurrentShipLength())>getCols())
- return false;
- break;
- }
- }
- return true;
- }
- private void removeShip(Ship inShip)
- {
- //remove ship from location
- }
- void setCurrentDirection() {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public class BoardListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()instanceof JButton)
- {
- JButton button = (JButton)e.getSource();
- int rowClick = (int)button.getClientProperty("row");
- int colClick = (int)button.getClientProperty("col");
- if (isValid(rowClick, colClick))
- {
- placeShip(rowClick, colClick);
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Ship will not fit in selected location, select a different location",
- "Try Again", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement