Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. package battleshipgame.userInterface;
  2.  
  3. import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
  4. import core.Battleship;
  5. import core.Carrier;
  6. import core.Constants;
  7. import core.Destroyer;
  8. import core.PatrolBoat;
  9. import core.Ship;
  10. import core.Submarine;
  11. import java.awt.*;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import javax.swing.*;
  15.  
  16. public class Player
  17. {
  18. // Class members
  19. private boolean isFirst;
  20. private Color shipColor = Color.RED;
  21. private int currentShipLength;
  22. private int currentShip;
  23. private int currentDirection;
  24. private String userName;
  25.  
  26.  
  27. private JButton[][] buttonBoard;
  28.  
  29. private final static int rows = 10;
  30. private final static int cols = 10;
  31.  
  32. private static PatrolBoat patrolBoat;
  33. private static Submarine submarine;
  34. private static Carrier carrier;
  35. private static Battleship battleship;
  36. private static Destroyer destroyer;
  37.  
  38. private static BoardListener boardListener;
  39.  
  40. public Player(String name)
  41. {
  42. userName = name;
  43. initObjects();
  44. initComponents();
  45. }
  46. private void initObjects()
  47. {
  48. battleship = new Battleship();
  49. carrier = new Carrier();
  50. destroyer = new Destroyer();
  51. patrolBoat = new PatrolBoat();
  52. submarine = new Submarine();
  53.  
  54.  
  55. boardListener = new BoardListener();
  56. }
  57. private void initComponents()
  58. {
  59. buttonBoard = new JButton [getRows()][getCols()];
  60.  
  61. for (int row = 0; row < 10; row++)
  62. {
  63. for (int col = 0; col < 10; col++)
  64. {
  65. buttonBoard[row][col] = new JButton();
  66. buttonBoard[row][col].putClientProperty("row", row);
  67. buttonBoard[row][col].putClientProperty("col", col);
  68. buttonBoard[row][col].addActionListener(boardListener);
  69. }
  70. }
  71. }
  72. public String getUserName()
  73. {
  74. return userName;
  75. }
  76. public JButton[][] getBoard()
  77. {
  78. return buttonBoard;
  79. }
  80.  
  81. /**
  82. * @return the rows
  83. */
  84. public static int getRows()
  85. {
  86. return rows;
  87. }
  88.  
  89. /**
  90. * @return the cols
  91. */
  92. public static int getCols()
  93. {
  94. return cols;
  95. }
  96. public int getCurrentShipLength()
  97. {
  98. return currentShipLength;
  99. }
  100. public void setCurrentShip(int currentShip)
  101. {
  102. this.currentShip = currentShip;
  103.  
  104. if(currentShip == Constants.BATTLESHIP)
  105. currentShipLength = Constants.BATTLESHIP_LENGTH;
  106. else if(currentShip == Constants.CARRIER)
  107. currentShipLength = Constants.CARRIER_LENGTH;
  108. else if(currentShip == Constants.DESTROYER)
  109. currentShipLength = Constants.DESTROYER_LENGTH;
  110. else if(currentShip == Constants.SUBMARINE)
  111. currentShipLength = Constants.SUBMARINE_LENGTH;
  112. else if(currentShip == Constants.PATROL)
  113. currentShipLength = Constants.PATROL_BOAT_LENGTH;
  114. }
  115. public int getCurrentDirection()
  116. {
  117. return currentDirection;
  118. }
  119. public void setCurrentDirection(int currentDirection)
  120. {
  121. this.currentDirection = currentDirection;
  122. }
  123. public boolean isIsFirst()
  124. {
  125. return isFirst;
  126. }
  127. public void setIsfirst(boolean isFirst)
  128. {
  129. this.isFirst = isFirst;
  130. }
  131. public Color getShipColor()
  132. {
  133. return shipColor;
  134. }
  135. public void setShipColor(String shipColor)
  136. {
  137. //this.shipcolor = shipColor;
  138. }
  139. public static Battleship getBattleship()
  140. {
  141. return battleship;
  142. }
  143. public static Carrier getCarrier()
  144. {
  145. return carrier;
  146. }
  147. public static Destroyer getDestroyer()
  148. {
  149. return destroyer;
  150. }
  151. public static PatrolBoat getPatrolBoat()
  152. {
  153. return patrolBoat;
  154. }
  155. public static Submarine getSubmarine()
  156. {
  157. return submarine;
  158. }
  159.  
  160.  
  161.  
  162. private void placeShip(int rowClick, int colClick)
  163. {
  164. switch( getCurrentDirection())
  165. {
  166. case Constants.VERTICAL:
  167. {
  168. for(int row = rowClick; row < (rowClick + getCurrentShipLength());row++)
  169. {
  170. buttonBoard[row][colClick].setBackground(getShipColor());
  171.  
  172. if(currentShip == Constants.BATTLESHIP)
  173. {
  174. if(getBattleship().isShipPlaced())
  175. {
  176. //remove exisisting battleship to move it
  177. }
  178. getBattleship().setShipDirection(Constants.VERTICAL);
  179. getBattleship().setShipLocation(row, colClick);
  180. }
  181. else if(currentShip ==Constants.CARRIER)
  182. {
  183. if(getCarrier().isShipPlaced())
  184. {
  185. //remove exisisting battleship to move it
  186. }
  187. getCarrier().setShipDirection(Constants.VERTICAL);
  188. getCarrier().setShipLocation(row, colClick);
  189. }
  190. else if(currentShip ==Constants.DESTROYER)
  191. {
  192. if(getDestroyer().isShipPlaced())
  193. {
  194. //remove exisisting battleship to move it
  195. }
  196. getDestroyer().setShipDirection(Constants.VERTICAL);
  197. getDestroyer().setShipLocation(row, colClick);
  198. }
  199. else if(currentShip ==Constants.SUBMARINE)
  200. {
  201. if(getSubmarine().isShipPlaced())
  202. {
  203. //remove exisisting battleship to move it
  204. }
  205. getSubmarine().setShipDirection(Constants.VERTICAL);
  206. getSubmarine().setShipLocation(row, colClick);
  207. }
  208. else if(currentShip ==Constants.PATROL)
  209. {
  210. if(getPatrolBoat().isShipPlaced())
  211. {
  212. //remove exisisting battleship to move it
  213. }
  214. getPatrolBoat().setShipDirection(Constants.VERTICAL);
  215. getPatrolBoat().setShipLocation(row, colClick);
  216. }
  217. }
  218. break;
  219.  
  220. case Constants.HORIZONTAL:
  221. {
  222. for(int col = colClick; col < (colClick + getCurrentShipLength());col++)
  223. {
  224. buttonBoard[rowClick][col].setBackground(getShipColor());
  225.  
  226. if(currentShip == Constants.BATTLESHIP)
  227. {
  228. if(getBattleship().isShipPlaced())
  229. {
  230. //remove exisisting battleship to move it
  231. }
  232. getBattleship().setShipDirection(Constants.HORIZONTAL);
  233. getBattleship().setShipLocation(rowClick, col);
  234. }
  235. else if(currentShip ==Constants.CARRIER)
  236. {
  237. if(getCarrier().isShipPlaced())
  238. {
  239. //remove exisisting battleship to move it
  240. }
  241. getCarrier().setShipDirection(Constants.HORIZONTAL);
  242. getCarrier().setShipLocation(rowClick, col);
  243. }
  244. else if(currentShip ==Constants.DESTROYER)
  245. {
  246. if(getDestroyer().isShipPlaced())
  247. {
  248. //remove exisisting battleship to move it
  249. }
  250. getDestroyer().setShipDirection(Constants.HORIZONTAL);
  251. getDestroyer().setShipLocation(rowClick, col);
  252. }
  253. else if(currentShip ==Constants.SUBMARINE)
  254. {
  255. if(getSubmarine().isShipPlaced())
  256. {
  257. //remove exisisting battleship to move it
  258. }
  259. getSubmarine().setShipDirection(Constants.HORIZONTAL);
  260. getSubmarine().setShipLocation(rowClick, col);
  261. }
  262. else if(currentShip ==Constants.PATROL)
  263. {
  264. if(getPatrolBoat().isShipPlaced())
  265. {
  266. //remove exisisting battleship to move it
  267. }
  268. getPatrolBoat().setShipDirection(Constants.HORIZONTAL);
  269. getPatrolBoat().setShipLocation(rowClick, col);
  270. }
  271. }
  272. break;
  273. }
  274. }
  275. }}
  276. private boolean isValid(int rowClick, int colClick)
  277. {
  278. switch( getCurrentDirection())
  279. {
  280. case Constants.VERTICAL:
  281. {
  282. if( (rowClick + getCurrentShipLength()) > getRows())
  283. return false;
  284. break;
  285. }
  286. case Constants.HORIZONTAL:
  287. {
  288. if((colClick + getCurrentShipLength())>getCols())
  289. return false;
  290.  
  291. break;
  292. }
  293. }
  294. return true;
  295. }
  296. private void removeShip(Ship inShip)
  297. {
  298. //remove ship from location
  299. }
  300.  
  301. void setCurrentDirection() {
  302. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  303. }
  304. public class BoardListener implements ActionListener
  305. {
  306. public void actionPerformed(ActionEvent e)
  307. {
  308. if(e.getSource()instanceof JButton)
  309. {
  310. JButton button = (JButton)e.getSource();
  311. int rowClick = (int)button.getClientProperty("row");
  312. int colClick = (int)button.getClientProperty("col");
  313.  
  314. if (isValid(rowClick, colClick))
  315. {
  316. placeShip(rowClick, colClick);
  317. }
  318. else
  319. {
  320. JOptionPane.showMessageDialog(null, "Ship will not fit in selected location, select a different location",
  321. "Try Again", JOptionPane.ERROR_MESSAGE);
  322. }
  323. }
  324. }
  325. }
  326.  
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement