You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ships.inc 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. class ship
  3. {
  4. //properties
  5. protected $lengte;
  6. protected $begin;
  7. protected $einde;
  8. protected $midden;
  9. protected $doklengte = 50;
  10. protected $hits = [];
  11. //methods
  12. function __construct($lengte)
  13. {
  14. $this->subPos($lengte);
  15. }
  16. protected function start($lengte)
  17. {
  18. $this->begin = $this->midden - floor($lengte / 2);
  19. }
  20. protected function end($lengte)
  21. {
  22. $this->einde = $this->begin + $lengte - 1;
  23. }
  24. //bepaald positie van het schip in het dok
  25. protected function randomPos($lengte)
  26. {
  27. $this->midden = rand(floor($lengte / 2), $this->doklengte - 1 - floor($lengte / 2));
  28. }
  29. // variable subPos() geeft de lengte aan de duikboot mee en legt eigenschappen duikboot vast.
  30. protected function subPos($lengte)
  31. {
  32. $this->lengte = $lengte;
  33. $this->randomPos($lengte);
  34. $this->start($lengte);
  35. $this->end($lengte);
  36. }
  37. //return array with beginning and end coordinates
  38. public function printCoord()
  39. {
  40. return [$this->begin, $this->einde];
  41. }
  42. //return int doklengte
  43. public function printDokLengte()
  44. {
  45. return $this->doklengte;
  46. }
  47. public function printLengte()
  48. {
  49. return $this->lengte;
  50. }
  51. // kijkt na of schot raak was
  52. // tabel begint Bij 1 dus let op bij weergave shot in tabel!
  53. // moet hiervoor nog corrigeren!!!
  54. public function compareShot($shot)
  55. {
  56. if ($this->begin <= $shot && $this->einde >= $shot) {
  57. if (!in_array($shot, $this->hits)) {
  58. //new part ship hit
  59. $this->hits[] = $shot;
  60. return 1;
  61. }
  62. } else {
  63. return 0;
  64. }
  65. }
  66. }
  67. class duikboot extends ship
  68. {
  69. function __construct()
  70. {
  71. $this->subPos(1);
  72. }
  73. }
  74. class torpedojager extends ship
  75. {
  76. function __construct()
  77. {
  78. $this->subPos(2);
  79. }
  80. }
  81. class kruiser extends ship
  82. {
  83. function __construct()
  84. {
  85. $this->subPos(3);
  86. }
  87. }
  88. class slagschip extends ship
  89. {
  90. function __construct()
  91. {
  92. $this->subPos(4);
  93. }
  94. }
  95. class vliegdekship extends ship
  96. {
  97. function __construct()
  98. {
  99. $this->subPos(5);
  100. }
  101. }