Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. package BattleShipUI.game;
  2.  
  3. import BattleShipUI.model.Cell;
  4. import BattleShipUI.player.ComputerPlayer;
  5. import BattleShipUI.player.Player;
  6. import BattleShipUI.util.Const;
  7. import BattleShipUI.util.GameMap;
  8. import javafx.scene.Parent;
  9. import javafx.scene.canvas.Canvas;
  10. import javafx.scene.canvas.GraphicsContext;
  11. import javafx.scene.paint.Color;
  12. import javafx.scene.paint.Paint;
  13.  
  14. public class GameWithComputer extends AbstractGame {
  15. private Player firstPlayer;
  16. private ComputerPlayer secondPlayer;
  17.  
  18. private Canvas firstMap;
  19. private Canvas secondMap;
  20.  
  21. private GameMap gameMap;
  22.  
  23. @Override
  24. protected void initialization(Parent root) {
  25. firstMap = (Canvas) root.lookup(Const.ID_CANVAS_FIRST);
  26. secondMap = (Canvas) root.lookup(Const.ID_CANVAS_SECOND);
  27.  
  28. gameMap = new GameMap();
  29.  
  30. firstPlayer = super.firstPlayer;
  31. secondPlayer = (ComputerPlayer) super.secondPlayer;
  32.  
  33. gameMap.setContext(firstMap.getGraphicsContext2D());
  34. gameMap.drawMap();
  35.  
  36. gameMap.setContext(secondMap.getGraphicsContext2D());
  37. gameMap.drawMap();
  38.  
  39. secondMap.setOnMouseClicked(event -> strike(
  40. secondMap.getGraphicsContext2D(),
  41. gameMap.defineCell(event.getX()),
  42. gameMap.defineCell(event.getY()),
  43. secondPlayer));
  44. }
  45.  
  46. @Override
  47. protected void strike(GraphicsContext context, int cellX, int cellY, Player player) {
  48. gameMap.setContext(context);
  49.  
  50. Paint paint = Color.GREY;
  51. boolean isHit = false;
  52. if (!gameMap.isFieldNotEmpty(cellX, cellY, 1, player.getShipList())) {
  53. paint = Color.RED;
  54. if (!firstPlayer.isCellExist(cellX, cellY)) {
  55. isHit = true;
  56. player.damage();
  57. }
  58. }
  59.  
  60. gameMap.drawField(cellX, cellY, paint);
  61.  
  62. if (!firstPlayer.isCellExist(cellX, cellY)) {
  63. firstPlayer.addCell(new Cell(cellX, cellY));
  64. if (!isHit) {
  65. nextStep(isHit);
  66. }
  67. }
  68.  
  69. super.isGameOver();
  70. }
  71.  
  72. private void nextStep(boolean isHit) {
  73. gameMap.setContext(firstMap.getGraphicsContext2D());
  74. Paint paint = Color.GREY;
  75. isHit = true;
  76.  
  77. while (isHit) {
  78. Cell cell = secondPlayer.getCell();
  79. isHit = false;
  80. if (!gameMap.isFieldNotEmpty(cell.getCellX(), cell.getCellY(), 1, firstPlayer.getShipList())) {
  81. paint = Color.RED;
  82. isHit = true;
  83. firstPlayer.damage();
  84. }
  85. secondPlayer.addCell(cell);
  86. secondPlayer.addHitList(isHit);
  87.  
  88. gameMap.drawField(cell.getCellX(), cell.getCellY(), paint);
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement