From 1a7d8cd7266cc184d4e4a3211a35c7648e99fda2 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 12:27:18 +0100 Subject: [PATCH 01/26] Czerwony i niebieski --- src/App.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/App.php b/src/App.php index 6c668e2..8a8e807 100644 --- a/src/App.php +++ b/src/App.php @@ -125,6 +125,11 @@ public static function StartGame() $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); if ($isHit) { + + } + + if ($isHit) { + self::$console->setForegroundColor(Color::RED); self::beep(); self::$console->println(" \\ . ./"); self::$console->println(" \\ .:\" \";'.:..\" \" /"); @@ -134,16 +139,20 @@ public static function StartGame() self::$console->println(" - (\\- | \\ / | /) -"); self::$console->println(" -\\ \\ / /-"); self::$console->println(" \\ \\ / /"); + self::$console->println("Yeah ! Nice hit !"); + } else { + self::$console->setForegroundColor(Color::CADET_BLUE); + self::$console->println("Miss"); } - - echo $isHit ? "Yeah ! Nice hit !" : "Miss"; - self::$console->println(); + self::$console->resetForegroundColor(); $position = self::getRandomPosition(); $isHit = GameController::checkIsHit(self::$myFleet, $position); self::$console->println(); - printf("Computer shoot in %s%s and %s", $position->getColumn(), $position->getRow(), $isHit ? "hit your ship !\n" : "miss"); + self::groupVisualy(); if ($isHit) { + self::$console->setForegroundColor(Color::CADET_BLUE); + self::$console->println(sprintf("Computer shoot in %s%s and hit your ship !", $position->getColumn(), $position->getRow())); self::beep(); self::$console->println(" \\ . ./"); @@ -155,12 +164,21 @@ public static function StartGame() self::$console->println(" -\\ \\ / /-"); self::$console->println(" \\ \\ / /"); + } else { + self::$console->setForegroundColor(Color::CADET_BLUE); + self::$console->println(sprintf("Computer shoot in %s%s and miss", $position->getColumn(), $position->getRow())); } + self::$console->resetForegroundColor(); + // exit(); } } + private static function groupVisualy() + { + self::$console->println("//" . str_repeat('-', 30) . "//"); + } public static function parsePosition($input) { $letter = substr($input, 0, 1); From e8e4e0d3c1976c568e188cc6c638f7cd4e6ac799 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 12:36:20 +0100 Subject: [PATCH 02/26] =?UTF-8?q?=C5=BB=C3=B3=C5=82t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 10 +++++----- src/Console.php | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/App.php b/src/App.php index 8a8e807..ebd879b 100644 --- a/src/App.php +++ b/src/App.php @@ -77,12 +77,12 @@ public static function InitializeMyFleet() { self::$myFleet = GameController::initializeShips(); - self::$console->println("Please position your fleet (Game board has size from A to H and 1 to 8) :"); + self::$console->printColoredLn("Please position your fleet (Game board has size from A to H and 1 to 8) :", Color::YELLOW); foreach (self::$myFleet as $ship) { self::$console->println(); - printf("Please enter the positions for the %s (size: %s)", $ship->getName(), $ship->getSize()); + self::$console->printColoredLn(sprintf("Please enter the positions for the %s (size: %s)", $ship->getName(), $ship->getSize()), Color::YELLOW); for ($i = 1; $i <= $ship->getSize(); $i++) { printf("\nEnter position %s of %s (i.e A3):", $i, $ship->getSize()); @@ -119,7 +119,7 @@ public static function StartGame() while (true) { self::$console->println(""); - self::$console->println("Player, it's your turn"); + self::$console->printColoredLn("Player, it's your turn", Color::YELLOW); self::$console->println("Enter coordinates for your shot :"); $position = readline(""); @@ -151,7 +151,7 @@ public static function StartGame() self::$console->println(); self::groupVisualy(); if ($isHit) { - self::$console->setForegroundColor(Color::CADET_BLUE); + self::$console->setForegroundColor(Color::RED); self::$console->println(sprintf("Computer shoot in %s%s and hit your ship !", $position->getColumn(), $position->getRow())); self::beep(); @@ -177,7 +177,7 @@ public static function StartGame() private static function groupVisualy() { - self::$console->println("//" . str_repeat('-', 30) . "//"); + self::$console->println("//" . str_repeat('-', 60) . "//"); } public static function parsePosition($input) { diff --git a/src/Console.php b/src/Console.php index 928b984..7ea1f71 100644 --- a/src/Console.php +++ b/src/Console.php @@ -18,4 +18,12 @@ function println($line = "") { echo "$line\n"; } + + function printColoredLn($line = "", $color = Battleship\Color::DEFAULT_GREY) + { + self::setForegroundColor($color); + self::println($line); + self::resetForegroundColor(); + } + } \ No newline at end of file From ac5a719eaf14e44cc4b026b7451a1b0be1111cd1 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 12:42:11 +0100 Subject: [PATCH 03/26] Poprawki --- src/App.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/App.php b/src/App.php index ebd879b..dd31c81 100644 --- a/src/App.php +++ b/src/App.php @@ -105,6 +105,7 @@ public static function InitializeGame() public static function StartGame() { + self::$console->setForegroundColor(Color::YELLOW); self::$console->println("\033[2J\033[;H"); self::$console->println(" __"); self::$console->println(" / \\"); @@ -116,9 +117,9 @@ public static function StartGame() self::$console->println(" | /_\'"); self::$console->println(" \\ \\_/"); self::$console->println(" \" \"\" \"\" \"\" \""); + self::$console->resetForegroundColor(); while (true) { - self::$console->println(""); self::$console->printColoredLn("Player, it's your turn", Color::YELLOW); self::$console->println("Enter coordinates for your shot :"); $position = readline(""); @@ -148,7 +149,6 @@ public static function StartGame() $position = self::getRandomPosition(); $isHit = GameController::checkIsHit(self::$myFleet, $position); - self::$console->println(); self::groupVisualy(); if ($isHit) { self::$console->setForegroundColor(Color::RED); @@ -170,14 +170,15 @@ public static function StartGame() } self::$console->resetForegroundColor(); - -// exit(); + self::groupVisualy(); } } private static function groupVisualy() { + self::$console->println(); self::$console->println("//" . str_repeat('-', 60) . "//"); + self::$console->println(); } public static function parsePosition($input) { From af88970446d35ef05e0ce199e8260b15e983336e Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 12:43:30 +0100 Subject: [PATCH 04/26] Poprawki --- src/App.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/App.php b/src/App.php index dd31c81..192f395 100644 --- a/src/App.php +++ b/src/App.php @@ -125,9 +125,6 @@ public static function StartGame() $position = readline(""); $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); - if ($isHit) { - - } if ($isHit) { self::$console->setForegroundColor(Color::RED); From f95f279c1deaf6dbf791a786159146b835033b23 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 12:49:38 +0100 Subject: [PATCH 05/26] Poprawiona kreska --- src/App.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/App.php b/src/App.php index 192f395..f5b12c1 100644 --- a/src/App.php +++ b/src/App.php @@ -120,6 +120,7 @@ public static function StartGame() self::$console->resetForegroundColor(); while (true) { + self::groupVisualy("Player turn"); self::$console->printColoredLn("Player, it's your turn", Color::YELLOW); self::$console->println("Enter coordinates for your shot :"); $position = readline(""); @@ -146,7 +147,7 @@ public static function StartGame() $position = self::getRandomPosition(); $isHit = GameController::checkIsHit(self::$myFleet, $position); - self::groupVisualy(); + self::groupVisualy("Computer turn"); if ($isHit) { self::$console->setForegroundColor(Color::RED); self::$console->println(sprintf("Computer shoot in %s%s and hit your ship !", $position->getColumn(), $position->getRow())); @@ -167,14 +168,17 @@ public static function StartGame() } self::$console->resetForegroundColor(); - self::groupVisualy(); } } - private static function groupVisualy() + private static function groupVisualy($text = "") { + $length = 60 - strlen($text) - 2; + + $length1 = ceil($length / 2); + $length2 = $length - $length1; self::$console->println(); - self::$console->println("//" . str_repeat('-', 60) . "//"); + self::$console->println("//" . str_repeat('-', $length1) . ' ' . $text . ' ' . str_repeat('-', $length2) . "//"); self::$console->println(); } public static function parsePosition($input) From c3d7ad89d7680ab13e22543d21f914e72b64cbd2 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 13:13:05 +0100 Subject: [PATCH 06/26] =?UTF-8?q?Umo=C5=BCliwienie=20wpisania=20jeszcze=20?= =?UTF-8?q?raz=20koordynat=20gdy=20niepoprawna=20litera?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 9 +++++++-- src/Batteship/Letter.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/App.php b/src/App.php index f5b12c1..301a1f8 100644 --- a/src/App.php +++ b/src/App.php @@ -86,8 +86,13 @@ public static function InitializeMyFleet() for ($i = 1; $i <= $ship->getSize(); $i++) { printf("\nEnter position %s of %s (i.e A3):", $i, $ship->getSize()); - $input = readline(""); - $ship->addPosition($input); + try { + $input = readline(""); + $ship->addPosition($input); + } catch (Exception $e) { + self::$console->printColoredln("\n".$e->getMessage() . ' Try again.', Color::RED); + $i--; + } } } } diff --git a/src/Batteship/Letter.php b/src/Batteship/Letter.php index 069bd79..cbac60e 100644 --- a/src/Batteship/Letter.php +++ b/src/Batteship/Letter.php @@ -18,7 +18,7 @@ public static function validate($letter) : string { if(!in_array($letter, self::$letters)) { - throw new InvalidArgumentException("Letter not exist"); + throw new InvalidArgumentException("Letter not exist."); } return $letter; From 374ae30f0337fcfcccc870263c8e7ac4243da8c8 Mon Sep 17 00:00:00 2001 From: Arkadiusz Janowski Date: Tue, 3 Dec 2024 13:15:14 +0100 Subject: [PATCH 07/26] MISIE-2 usuniecie pozycji statku --- src/Batteship/GameController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Batteship/GameController.php b/src/Batteship/GameController.php index b73455a..d3f11c6 100644 --- a/src/Batteship/GameController.php +++ b/src/Batteship/GameController.php @@ -7,7 +7,7 @@ class GameController { - public static function checkIsHit(array $fleet, $shot) + public static function checkIsHit(array &$fleet, $shot) { if ($fleet == null) { throw new InvalidArgumentException("ships is null"); @@ -20,6 +20,9 @@ public static function checkIsHit(array $fleet, $shot) foreach ($fleet as $ship) { foreach ($ship->getPositions() as $position) { if ($position == $shot) { + $index = array_search($position, $ship->getPositions()); + unset($ship->getPositions()[$index]); + return true; } } From 5affcddb1908a2e7fca1fd58b202ebd7d0658aaa Mon Sep 17 00:00:00 2001 From: Arkadiusz Janowski Date: Tue, 3 Dec 2024 13:30:30 +0100 Subject: [PATCH 08/26] MISIE-2 game over --- src/App.php | 10 ++++++++++ src/Batteship/GameController.php | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/App.php b/src/App.php index f5b12c1..6f1cccb 100644 --- a/src/App.php +++ b/src/App.php @@ -126,6 +126,10 @@ public static function StartGame() $position = readline(""); $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); + if (GameController::checkIsGameOver(self::$enemyFleet)) { + self::$console->println("You are the winner!"); + exit(); + } if ($isHit) { self::$console->setForegroundColor(Color::RED); @@ -147,6 +151,12 @@ public static function StartGame() $position = self::getRandomPosition(); $isHit = GameController::checkIsHit(self::$myFleet, $position); + + if (GameController::checkIsGameOver(self::$enemyFleet)) { + self::$console->println("You lost"); + exit(); + } + self::groupVisualy("Computer turn"); if ($isHit) { self::$console->setForegroundColor(Color::RED); diff --git a/src/Batteship/GameController.php b/src/Batteship/GameController.php index d3f11c6..d03a9a2 100644 --- a/src/Batteship/GameController.php +++ b/src/Batteship/GameController.php @@ -31,6 +31,17 @@ public static function checkIsHit(array &$fleet, $shot) return false; } + public static function checkIsGameOver(array $fleet) + { + foreach ($fleet as $ship) { + if (count($ship->getPositions()) > 0) { + return false; + } + } + + return true; + } + public static function initializeShips() { return Array( From 1a9dab0b62dc0c019ba27ff2860175264f33cc3a Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Tue, 3 Dec 2024 13:31:04 +0100 Subject: [PATCH 09/26] =?UTF-8?q?Umo=C5=BCliwienie=20wpisania=20jeszcze=20?= =?UTF-8?q?raz=20koordynat=20gdy=20niepoprawna=20cyfra?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Batteship/Digit.php | 21 +++++++++++++++++++++ src/Batteship/Position.php | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/Batteship/Digit.php diff --git a/src/Batteship/Digit.php b/src/Batteship/Digit.php new file mode 100644 index 0000000..f6ee142 --- /dev/null +++ b/src/Batteship/Digit.php @@ -0,0 +1,21 @@ + self::$to){ + throw new InvalidArgumentException("Digit not exist."); + } + + return $digit; + } +} \ No newline at end of file diff --git a/src/Batteship/Position.php b/src/Batteship/Position.php index dc4f9c7..b6d16bd 100644 --- a/src/Batteship/Position.php +++ b/src/Batteship/Position.php @@ -18,7 +18,9 @@ class Position public function __construct($letter, $number) { $this->column = Letter::validate(strtoupper($letter)); - $this->row = $number; +// $this->column = 1; + $this->row = Digit::validate($number); +// $this->row = $number; } public function getColumn() From 7fd7d5515bb0c0b343aa167685b7da4c509e7083 Mon Sep 17 00:00:00 2001 From: gprawdzik Date: Tue, 3 Dec 2024 13:48:53 +0100 Subject: [PATCH 10/26] =?UTF-8?q?Revert=20"Umo=C5=BCliwienie=20wpisania=20?= =?UTF-8?q?jeszcze=20raz=20koordynat=20gdy=20niepoprawna=20litera"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 9 ++------- src/Batteship/Digit.php | 21 --------------------- src/Batteship/Letter.php | 2 +- src/Batteship/Position.php | 4 +--- 4 files changed, 4 insertions(+), 32 deletions(-) delete mode 100644 src/Batteship/Digit.php diff --git a/src/App.php b/src/App.php index 4110198..6f1cccb 100644 --- a/src/App.php +++ b/src/App.php @@ -86,13 +86,8 @@ public static function InitializeMyFleet() for ($i = 1; $i <= $ship->getSize(); $i++) { printf("\nEnter position %s of %s (i.e A3):", $i, $ship->getSize()); - try { - $input = readline(""); - $ship->addPosition($input); - } catch (Exception $e) { - self::$console->printColoredln("\n".$e->getMessage() . ' Try again.', Color::RED); - $i--; - } + $input = readline(""); + $ship->addPosition($input); } } } diff --git a/src/Batteship/Digit.php b/src/Batteship/Digit.php deleted file mode 100644 index f6ee142..0000000 --- a/src/Batteship/Digit.php +++ /dev/null @@ -1,21 +0,0 @@ - self::$to){ - throw new InvalidArgumentException("Digit not exist."); - } - - return $digit; - } -} \ No newline at end of file diff --git a/src/Batteship/Letter.php b/src/Batteship/Letter.php index cbac60e..069bd79 100644 --- a/src/Batteship/Letter.php +++ b/src/Batteship/Letter.php @@ -18,7 +18,7 @@ public static function validate($letter) : string { if(!in_array($letter, self::$letters)) { - throw new InvalidArgumentException("Letter not exist."); + throw new InvalidArgumentException("Letter not exist"); } return $letter; diff --git a/src/Batteship/Position.php b/src/Batteship/Position.php index b6d16bd..dc4f9c7 100644 --- a/src/Batteship/Position.php +++ b/src/Batteship/Position.php @@ -18,9 +18,7 @@ class Position public function __construct($letter, $number) { $this->column = Letter::validate(strtoupper($letter)); -// $this->column = 1; - $this->row = Digit::validate($number); -// $this->row = $number; + $this->row = $number; } public function getColumn() From fee7c7f26e70681a31b2846e627694dc207c8dcd Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Wed, 4 Dec 2024 12:06:11 +0100 Subject: [PATCH 11/26] komenda help --- src/App.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/App.php b/src/App.php index 6f1cccb..1706d4f 100644 --- a/src/App.php +++ b/src/App.php @@ -125,6 +125,12 @@ public static function StartGame() self::$console->println("Enter coordinates for your shot :"); $position = readline(""); + if (strtolower(trim($position)) === "help") { + + self::$console->println("[TU MA SIĘ WYŚWIETLIĆ MAPA]"); + continue; + } + $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); if (GameController::checkIsGameOver(self::$enemyFleet)) { self::$console->println("You are the winner!"); From 0b86ab58a7f4814fe3567c6b225b2b4f31f44710 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Wed, 4 Dec 2024 12:07:26 +0100 Subject: [PATCH 12/26] komenda help, toto --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 1706d4f..b0078a9 100644 --- a/src/App.php +++ b/src/App.php @@ -126,7 +126,7 @@ public static function StartGame() $position = readline(""); if (strtolower(trim($position)) === "help") { - + // TODO: podpiąć wyświetlanie planszy self::$console->println("[TU MA SIĘ WYŚWIETLIĆ MAPA]"); continue; } From 90f576a2382ec0642d840bce4e8edcfc59747655 Mon Sep 17 00:00:00 2001 From: Arkadiusz Janowski Date: Wed, 4 Dec 2024 12:16:25 +0100 Subject: [PATCH 13/26] MISIE-6 random --- src/App.php | 175 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 152 insertions(+), 23 deletions(-) diff --git a/src/App.php b/src/App.php index 6f1cccb..5c94fe7 100644 --- a/src/App.php +++ b/src/App.php @@ -35,31 +35,29 @@ static function run() self::StartGame(); } - public static function InitializeEnemyFleet() + public static function InitializeEnemyFleet(int $fleetNumber) { self::$enemyFleet = GameController::initializeShips(); - array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 4)); - array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 5)); - array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 6)); - array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 7)); - array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 8)); - - array_push(self::$enemyFleet[1]->getPositions(), new Position('E', 6)); - array_push(self::$enemyFleet[1]->getPositions(), new Position('E', 7)); - array_push(self::$enemyFleet[1]->getPositions(), new Position('E', 8)); - array_push(self::$enemyFleet[1]->getPositions(), new Position('E', 9)); - - array_push(self::$enemyFleet[2]->getPositions(), new Position('A', 3)); - array_push(self::$enemyFleet[2]->getPositions(), new Position('B', 3)); - array_push(self::$enemyFleet[2]->getPositions(), new Position('C', 3)); - - array_push(self::$enemyFleet[3]->getPositions(), new Position('F', 8)); - array_push(self::$enemyFleet[3]->getPositions(), new Position('G', 8)); - array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 8)); - - array_push(self::$enemyFleet[4]->getPositions(), new Position('C', 5)); - array_push(self::$enemyFleet[4]->getPositions(), new Position('C', 6)); + switch ($fleetNumber) { + case 1: + self::initializeFleet1(); + break; + case 2: + self::initializeFleet2(); + break; + case 3: + self::initializeFleet3(); + break; + case 4: + self::initializeFleet4(); + break; + case 5: + self::initializeFleet5(); + break; + default: + throw new Exception("Invalid fleet number"); + } } public static function getRandomPosition() @@ -99,8 +97,10 @@ public static function beep() public static function InitializeGame() { + $fleetNumber = random_int(1, 5); + self::InitializeMyFleet(); - self::InitializeEnemyFleet(); + self::InitializeEnemyFleet($fleetNumber); } public static function StartGame() @@ -202,4 +202,133 @@ public static function parsePosition($input) return new Position($letter, $number); } + + private static function initializeFleet1() + { + array_push(self::$enemyFleet[0]->getPositions(), new Position('A', 1)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('A', 2)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('A', 3)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('A', 4)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('A', 5)); + + array_push(self::$enemyFleet[1]->getPositions(), new Position('C', 3)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('C', 4)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('C', 5)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('C', 6)); + + array_push(self::$enemyFleet[2]->getPositions(), new Position('E', 1)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('F', 1)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('G', 1)); + + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 4)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 5)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 6)); + + array_push(self::$enemyFleet[4]->getPositions(), new Position('F', 8)); + array_push(self::$enemyFleet[4]->getPositions(), new Position('G', 8)); + } + + private static function initializeFleet2() + { + array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 2)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 3)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 4)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 5)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('B', 6)); + + array_push(self::$enemyFleet[1]->getPositions(), new Position('D', 7)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('E', 7)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('F', 7)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('G', 7)); + + array_push(self::$enemyFleet[2]->getPositions(), new Position('A', 8)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('B', 8)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('C', 8)); + + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 3)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 4)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 5)); + + array_push(self::$enemyFleet[4]->getPositions(), new Position('F', 2)); + array_push(self::$enemyFleet[4]->getPositions(), new Position('F', 3)); + + } + + private static function initializeFleet3() + { + array_push(self::$enemyFleet[0]->getPositions(), new Position('C', 1)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('D', 1)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('E', 1)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('F', 1)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('G', 1)); + + array_push(self::$enemyFleet[1]->getPositions(), new Position('A', 4)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('B', 4)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('C', 4)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('D', 4)); + + array_push(self::$enemyFleet[2]->getPositions(), new Position('F', 5)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('F', 6)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('F', 7)); + + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 6)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 7)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('H', 8)); + + array_push(self::$enemyFleet[4]->getPositions(), new Position('B', 7)); + array_push(self::$enemyFleet[4]->getPositions(), new Position('C', 7)); + + + } + + private static function initializeFleet4() + { + array_push(self::$enemyFleet[0]->getPositions(), new Position('D', 3)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('D', 4)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('D', 5)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('D', 6)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('D', 7)); + + array_push(self::$enemyFleet[1]->getPositions(), new Position('F', 2)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('F', 3)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('F', 4)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('F', 5)); + + array_push(self::$enemyFleet[2]->getPositions(), new Position('B', 8)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('C', 8)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('D', 8)); + + array_push(self::$enemyFleet[3]->getPositions(), new Position('A', 6)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('A', 7)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('A', 8)); + + array_push(self::$enemyFleet[4]->getPositions(), new Position('H', 1)); + array_push(self::$enemyFleet[4]->getPositions(), new Position('H', 2)); + + } + + private static function initializeFleet5() + { + array_push(self::$enemyFleet[0]->getPositions(), new Position('G', 4)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('G', 5)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('G', 6)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('G', 7)); + array_push(self::$enemyFleet[0]->getPositions(), new Position('G', 8)); + + array_push(self::$enemyFleet[1]->getPositions(), new Position('B', 2)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('C', 2)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('D', 2)); + array_push(self::$enemyFleet[1]->getPositions(), new Position('E', 2)); + + array_push(self::$enemyFleet[2]->getPositions(), new Position('A', 1)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('A', 2)); + array_push(self::$enemyFleet[2]->getPositions(), new Position('A', 3)); + + array_push(self::$enemyFleet[3]->getPositions(), new Position('C', 7)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('C', 8)); + array_push(self::$enemyFleet[3]->getPositions(), new Position('D', 8)); + + array_push(self::$enemyFleet[4]->getPositions(), new Position('F', 5)); + array_push(self::$enemyFleet[4]->getPositions(), new Position('F', 6)); + } } \ No newline at end of file From 0a0f62a486d119b2e9bde12673f30a9d0864e1f8 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Wed, 4 Dec 2024 12:17:52 +0100 Subject: [PATCH 14/26] komenda help, help to map# --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index b0078a9..cc61f09 100644 --- a/src/App.php +++ b/src/App.php @@ -125,7 +125,7 @@ public static function StartGame() self::$console->println("Enter coordinates for your shot :"); $position = readline(""); - if (strtolower(trim($position)) === "help") { + if (strtolower(trim($position)) === "map#") { // TODO: podpiąć wyświetlanie planszy self::$console->println("[TU MA SIĘ WYŚWIETLIĆ MAPA]"); continue; From b859474c12c54fe2980abb2b3201d25317b627f5 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Wed, 4 Dec 2024 12:23:38 +0100 Subject: [PATCH 15/26] =?UTF-8?q?Poprawki=20kolor=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 11 +++++------ src/Batteship/Color.php | 1 + 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/App.php b/src/App.php index 6f1cccb..7425278 100644 --- a/src/App.php +++ b/src/App.php @@ -127,7 +127,7 @@ public static function StartGame() $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); if (GameController::checkIsGameOver(self::$enemyFleet)) { - self::$console->println("You are the winner!"); + self::$console->printColoredln("You are the winner!", Color::YELLOW); exit(); } @@ -144,8 +144,7 @@ public static function StartGame() self::$console->println(" \\ \\ / /"); self::$console->println("Yeah ! Nice hit !"); } else { - self::$console->setForegroundColor(Color::CADET_BLUE); - self::$console->println("Miss"); + self::$console->setForegroundColor(Color::DARK_CYAN); } self::$console->resetForegroundColor(); @@ -173,7 +172,7 @@ public static function StartGame() self::$console->println(" \\ \\ / /"); } else { - self::$console->setForegroundColor(Color::CADET_BLUE); + self::$console->setForegroundColor(Color::DARK_CYAN); self::$console->println(sprintf("Computer shoot in %s%s and miss", $position->getColumn(), $position->getRow())); } @@ -183,12 +182,12 @@ public static function StartGame() private static function groupVisualy($text = "") { - $length = 60 - strlen($text) - 2; + $length = 70 - strlen($text) - 2; $length1 = ceil($length / 2); $length2 = $length - $length1; self::$console->println(); - self::$console->println("//" . str_repeat('-', $length1) . ' ' . $text . ' ' . str_repeat('-', $length2) . "//"); + self::$console->println(str_repeat('-', $length1) . ' ' . $text . ' ' . str_repeat('-', $length2)); self::$console->println(); } public static function parsePosition($input) diff --git a/src/Batteship/Color.php b/src/Batteship/Color.php index e5e8502..42954d7 100644 --- a/src/Batteship/Color.php +++ b/src/Batteship/Color.php @@ -11,4 +11,5 @@ class Color const YELLOW = "\e[33m"; const ORANGE = "\e[33m"; const CHARTREUSE = "\e[32m"; + const DARK_CYAN = "\e[36m"; } \ No newline at end of file From 755f05347812e69dc9b58320c42a255df25f2014 Mon Sep 17 00:00:00 2001 From: Grzegorz Prawdzik Date: Wed, 4 Dec 2024 12:30:32 +0100 Subject: [PATCH 16/26] =?UTF-8?q?Kolor=20wiadomo=C5=9Bci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index cc61f09..a227f55 100644 --- a/src/App.php +++ b/src/App.php @@ -127,7 +127,7 @@ public static function StartGame() if (strtolower(trim($position)) === "map#") { // TODO: podpiąć wyświetlanie planszy - self::$console->println("[TU MA SIĘ WYŚWIETLIĆ MAPA]"); + self::$console->printColoredln("[TU MA SIĘ WYŚWIETLIĆ MAPA]", Color::YELLOW); continue; } From 1b183126b276971daaaff0ac3cd323dbeeee0e4f Mon Sep 17 00:00:00 2001 From: Arkadiusz Janowski Date: Wed, 4 Dec 2024 12:36:39 +0100 Subject: [PATCH 17/26] MISIE-6 random --- src/App.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index 5c94fe7..e77142d 100644 --- a/src/App.php +++ b/src/App.php @@ -9,6 +9,9 @@ class App { private static $myFleet = array(); private static $enemyFleet = array(); + + private static $fleetNumber; + private static $console; static function run() @@ -97,10 +100,10 @@ public static function beep() public static function InitializeGame() { - $fleetNumber = random_int(1, 5); + self::$fleetNumber = random_int(1, 5); self::InitializeMyFleet(); - self::InitializeEnemyFleet($fleetNumber); + self::InitializeEnemyFleet(self::$fleetNumber); } public static function StartGame() From 6deadc15808f3b43ed164930b201a9328bea5489 Mon Sep 17 00:00:00 2001 From: Arkadiusz Janowski Date: Wed, 4 Dec 2024 12:40:06 +0100 Subject: [PATCH 18/26] MISIE-6 random --- src/App.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index 4ee76d2..c3ceec5 100644 --- a/src/App.php +++ b/src/App.php @@ -129,8 +129,7 @@ public static function StartGame() $position = readline(""); if (strtolower(trim($position)) === "map#") { - // TODO: podpiąć wyświetlanie planszy - self::$console->printColoredln("[TU MA SIĘ WYŚWIETLIĆ MAPA]", Color::YELLOW); + self::$console->printColoredln("MAP: " . self::$fleetNumber, Color::YELLOW); continue; } From 2c0499df5f528f36e32d4905d9efc2c254e75663 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Wed, 4 Dec 2024 12:53:31 +0100 Subject: [PATCH 19/26] endgame any button --- src/App.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/App.php b/src/App.php index d5ad5c5..046ac37 100644 --- a/src/App.php +++ b/src/App.php @@ -136,6 +136,8 @@ public static function StartGame() $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); if (GameController::checkIsGameOver(self::$enemyFleet)) { self::$console->printColoredln("You are the winner!", Color::YELLOW); + self::$console->println("\nPress any button to quit game..."); + readline(); exit(); } From 27dbc7368c2e96a2755896f16ec522ce57534886 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Wed, 4 Dec 2024 12:55:49 +0100 Subject: [PATCH 20/26] endgame any button --- src/App.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/App.php b/src/App.php index 046ac37..fe895a3 100644 --- a/src/App.php +++ b/src/App.php @@ -161,13 +161,17 @@ public static function StartGame() $position = self::getRandomPosition(); $isHit = GameController::checkIsHit(self::$myFleet, $position); - if (GameController::checkIsGameOver(self::$enemyFleet)) { - self::$console->println("You lost"); - exit(); - } + self::groupVisualy("Computer turn"); if ($isHit) { + if (GameController::checkIsGameOver(self::$enemyFleet)) { + self::$console->println("You lost"); + self::$console->println("\nPress any button to quit game..."); + readline(); + exit(); + } + self::$console->setForegroundColor(Color::RED); self::$console->println(sprintf("Computer shoot in %s%s and hit your ship !", $position->getColumn(), $position->getRow())); self::beep(); From 774558b0ab457e8c6f0ac4cbb4513b53f958ca59 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Wed, 4 Dec 2024 13:03:40 +0100 Subject: [PATCH 21/26] endgame any button --- src/App.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index fe895a3..fbe205f 100644 --- a/src/App.php +++ b/src/App.php @@ -136,7 +136,7 @@ public static function StartGame() $isHit = GameController::checkIsHit(self::$enemyFleet, self::parsePosition($position)); if (GameController::checkIsGameOver(self::$enemyFleet)) { self::$console->printColoredln("You are the winner!", Color::YELLOW); - self::$console->println("\nPress any button to quit game..."); + self::$console->println("\nPress Enter to quit game..."); readline(); exit(); } @@ -167,7 +167,7 @@ public static function StartGame() if ($isHit) { if (GameController::checkIsGameOver(self::$enemyFleet)) { self::$console->println("You lost"); - self::$console->println("\nPress any button to quit game..."); + self::$console->println("\nPress Enter to quit game..."); readline(); exit(); } From 51a721c8eccba16ddbbd75ca6b298bfe8a4efe34 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Fri, 6 Dec 2024 10:49:38 +0100 Subject: [PATCH 22/26] walidacja --- src/App.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 7 deletions(-) diff --git a/src/App.php b/src/App.php index fbe205f..823f57e 100644 --- a/src/App.php +++ b/src/App.php @@ -78,19 +78,113 @@ public static function InitializeMyFleet() { self::$myFleet = GameController::initializeShips(); - self::$console->printColoredLn("Please position your fleet (Game board has size from A to H and 1 to 8) :", Color::YELLOW); + self::$console->printColoredLn("Ustaw swoją flotę (plansza ma wymiary od A do H i od 1 do 8):", Color::YELLOW); + self::$console->printColoredLn("Kierunki: N - północ, S - południe, E - wschód, W - zachód", Color::YELLOW); foreach (self::$myFleet as $ship) { + while (true) { + try { + self::$console->println(); + self::$console->printColoredLn( + sprintf("Wprowadź pozycje dla %s (rozmiar: %s)", $ship->getName(), $ship->getSize()), + Color::YELLOW + ); + + self::$console->println("Podaj pozycję początkową (np. A3):"); + $start = readline(""); + $startPos = self::parsePosition($start); + + self::$console->println("Podaj kierunek (N/S/E/W):"); + $direction = strtoupper(readline("")); + + if (!in_array($direction, ['N', 'S', 'E', 'W'])) { + throw new Exception("Nieprawidłowy kierunek! Użyj N, S, E lub W."); + } + + // Oblicz wszystkie pozycje statku + $shipPositions = self::calculateShipPositions($startPos, $direction, $ship->getSize()); + + // Sprawdź czy wszystkie pozycje są prawidłowe + foreach ($shipPositions as $position) { + $pos = self::parsePosition($position); + if (!self::isPositionValid($pos)) { + throw new Exception("Statek wychodzi poza planszę!"); + } + if (self::isCollisionWithOtherShips($pos, self::$myFleet)) { + throw new Exception("Statek nachodzi na inny statek!"); + } + } + + // Dodaj pozycje do statku + foreach ($shipPositions as $position) { + $ship->addPosition(self::parsePosition($position)); + } + + break; // Wyjdź z pętli jeśli wszystko się udało + + } catch (Exception $e) { + self::$console->printColoredLn("Błąd: " . $e->getMessage(), Color::RED); + self::$console->println("Spróbuj ponownie."); + } + } + } + } + + private static function isCollisionWithOtherShips(Position $position, array $fleet): bool + { + foreach ($fleet as $ship) { + foreach ($ship->getPositions() as $shipPosition) { + // Sprawdź bezpośrednią kolizję + if ($position->getColumn() === $shipPosition->getColumn() && + $position->getRow() === $shipPosition->getRow()) { + return true; + } + + // Sprawdź sąsiednie pola (włączając po skosie) + $colIndex = array_search($position->getColumn(), Letter::$letters); + $shipColIndex = array_search($shipPosition->getColumn(), Letter::$letters); + + if (abs($colIndex - $shipColIndex) <= 1 && + abs($position->getRow() - $shipPosition->getRow()) <= 1) { + return true; + } + } + } + return false; + } - self::$console->println(); - self::$console->printColoredLn(sprintf("Please enter the positions for the %s (size: %s)", $ship->getName(), $ship->getSize()), Color::YELLOW); + private static function isPositionValid(Position $position): bool + { + $col = array_search($position->getColumn(), Letter::$letters); + $row = $position->getRow(); + + return $col >= 0 && $col < 8 && $row >= 1 && $row <= 8; + } - for ($i = 1; $i <= $ship->getSize(); $i++) { - printf("\nEnter position %s of %s (i.e A3):", $i, $ship->getSize()); - $input = readline(""); - $ship->addPosition($input); + private static function calculateShipPositions(Position $start, string $direction, int $size): array + { + $positions = []; + $startCol = array_search($start->getColumn(), Letter::$letters); + $startRow = $start->getRow(); + + for ($i = 0; $i < $size; $i++) { + switch ($direction) { + case 'N': + $positions[] = $start->getColumn() . ($startRow - $i); + break; + case 'S': + $positions[] = $start->getColumn() . ($startRow + $i); + break; + case 'E': + $positions[] = Letter::$letters[$startCol + $i] . $startRow; + break; + case 'W': + $positions[] = Letter::$letters[$startCol - $i] . $startRow; + break; } } + + return $positions; } public static function beep() From 86eafedc02ecfd942985f327018bca3f00e091f3 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Fri, 6 Dec 2024 10:58:52 +0100 Subject: [PATCH 23/26] walidacja --- src/App.php | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/App.php b/src/App.php index 823f57e..a9d36e7 100644 --- a/src/App.php +++ b/src/App.php @@ -78,53 +78,53 @@ public static function InitializeMyFleet() { self::$myFleet = GameController::initializeShips(); - self::$console->printColoredLn("Ustaw swoją flotę (plansza ma wymiary od A do H i od 1 do 8):", Color::YELLOW); - self::$console->printColoredLn("Kierunki: N - północ, S - południe, E - wschód, W - zachód", Color::YELLOW); + self::$console->printColoredLn("Set up your fleet (board size is from A to H and 1 to 8):", Color::YELLOW); + self::$console->printColoredLn("Directions: N - North, S - South, E - East, W - West", Color::YELLOW); foreach (self::$myFleet as $ship) { while (true) { try { self::$console->println(); self::$console->printColoredLn( - sprintf("Wprowadź pozycje dla %s (rozmiar: %s)", $ship->getName(), $ship->getSize()), + sprintf("Enter positions for %s (size: %s)", $ship->getName(), $ship->getSize()), Color::YELLOW ); - self::$console->println("Podaj pozycję początkową (np. A3):"); + self::$console->println("Enter starting position (e.g. A3):"); $start = readline(""); $startPos = self::parsePosition($start); - self::$console->println("Podaj kierunek (N/S/E/W):"); + self::$console->println("Enter direction (N/S/E/W):"); $direction = strtoupper(readline("")); if (!in_array($direction, ['N', 'S', 'E', 'W'])) { - throw new Exception("Nieprawidłowy kierunek! Użyj N, S, E lub W."); + throw new Exception("Invalid direction! Use N, S, E or W."); } - // Oblicz wszystkie pozycje statku + // Calculate all ship positions $shipPositions = self::calculateShipPositions($startPos, $direction, $ship->getSize()); - // Sprawdź czy wszystkie pozycje są prawidłowe + // Validate all positions foreach ($shipPositions as $position) { $pos = self::parsePosition($position); if (!self::isPositionValid($pos)) { - throw new Exception("Statek wychodzi poza planszę!"); + throw new Exception("Ship position is out of bounds!"); } if (self::isCollisionWithOtherShips($pos, self::$myFleet)) { - throw new Exception("Statek nachodzi na inny statek!"); + throw new Exception("Ships cannot be placed adjacent to each other!"); } } - // Dodaj pozycje do statku + // Add positions to ship foreach ($shipPositions as $position) { $ship->addPosition(self::parsePosition($position)); } - break; // Wyjdź z pętli jeśli wszystko się udało + break; // Exit loop if successful } catch (Exception $e) { - self::$console->printColoredLn("Błąd: " . $e->getMessage(), Color::RED); - self::$console->println("Spróbuj ponownie."); + self::$console->printColoredLn("Error: " . $e->getMessage(), Color::RED); + self::$console->println("Please try again."); } } } @@ -134,13 +134,13 @@ private static function isCollisionWithOtherShips(Position $position, array $fle { foreach ($fleet as $ship) { foreach ($ship->getPositions() as $shipPosition) { - // Sprawdź bezpośrednią kolizję + // Check direct collision if ($position->getColumn() === $shipPosition->getColumn() && $position->getRow() === $shipPosition->getRow()) { return true; } - // Sprawdź sąsiednie pola (włączając po skosie) + // Check adjacent positions (including diagonals) $colIndex = array_search($position->getColumn(), Letter::$letters); $shipColIndex = array_search($shipPosition->getColumn(), Letter::$letters); From 4ad208df3b2f6469e506c0f9ff13e4811104786d Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Fri, 6 Dec 2024 11:05:44 +0100 Subject: [PATCH 24/26] walidacja --- src/App.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/App.php b/src/App.php index a9d36e7..86b1b14 100644 --- a/src/App.php +++ b/src/App.php @@ -79,7 +79,7 @@ public static function InitializeMyFleet() self::$myFleet = GameController::initializeShips(); self::$console->printColoredLn("Set up your fleet (board size is from A to H and 1 to 8):", Color::YELLOW); - self::$console->printColoredLn("Directions: N - North, S - South, E - East, W - West", Color::YELLOW); + self::$console->printColoredLn("Directions: U - Up, D - Down, L - Left, R - Right", Color::YELLOW); foreach (self::$myFleet as $ship) { while (true) { @@ -94,11 +94,11 @@ public static function InitializeMyFleet() $start = readline(""); $startPos = self::parsePosition($start); - self::$console->println("Enter direction (N/S/E/W):"); + self::$console->println("Enter direction (U/D/L/R):"); $direction = strtoupper(readline("")); - if (!in_array($direction, ['N', 'S', 'E', 'W'])) { - throw new Exception("Invalid direction! Use N, S, E or W."); + if (!in_array($direction, ['U', 'D', 'L', 'R'])) { + throw new Exception("Invalid direction! Use U (Up), D (Down), L (Left) or R (Right)."); } // Calculate all ship positions @@ -134,13 +134,13 @@ private static function isCollisionWithOtherShips(Position $position, array $fle { foreach ($fleet as $ship) { foreach ($ship->getPositions() as $shipPosition) { - // Check direct collision + // Sprawdź bezpośrednią kolizję if ($position->getColumn() === $shipPosition->getColumn() && $position->getRow() === $shipPosition->getRow()) { return true; } - // Check adjacent positions (including diagonals) + // Sprawdź sąsiednie pola (włączając po skosie) $colIndex = array_search($position->getColumn(), Letter::$letters); $shipColIndex = array_search($shipPosition->getColumn(), Letter::$letters); @@ -169,16 +169,16 @@ private static function calculateShipPositions(Position $start, string $directio for ($i = 0; $i < $size; $i++) { switch ($direction) { - case 'N': + case 'U': $positions[] = $start->getColumn() . ($startRow - $i); break; - case 'S': + case 'D': $positions[] = $start->getColumn() . ($startRow + $i); break; - case 'E': + case 'R': $positions[] = Letter::$letters[$startCol + $i] . $startRow; break; - case 'W': + case 'L': $positions[] = Letter::$letters[$startCol - $i] . $startRow; break; } From a1d1b6180174e429f81bcec351bb0133f3986291 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Fri, 6 Dec 2024 11:21:49 +0100 Subject: [PATCH 25/26] walidacja --- src/App.php | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/App.php b/src/App.php index 86b1b14..c7d223f 100644 --- a/src/App.php +++ b/src/App.php @@ -79,7 +79,7 @@ public static function InitializeMyFleet() self::$myFleet = GameController::initializeShips(); self::$console->printColoredLn("Set up your fleet (board size is from A to H and 1 to 8):", Color::YELLOW); - self::$console->printColoredLn("Directions: U - Up, D - Down, L - Left, R - Right", Color::YELLOW); + self::$console->printColoredLn("Directions: R - Right, D - Down", Color::YELLOW); foreach (self::$myFleet as $ship) { while (true) { @@ -94,22 +94,32 @@ public static function InitializeMyFleet() $start = readline(""); $startPos = self::parsePosition($start); - self::$console->println("Enter direction (U/D/L/R):"); + self::$console->println("Enter direction (R/D):"); $direction = strtoupper(readline("")); - if (!in_array($direction, ['U', 'D', 'L', 'R'])) { - throw new Exception("Invalid direction! Use U (Up), D (Down), L (Left) or R (Right)."); + if (!in_array($direction, ['R', 'D'])) { + throw new Exception("Invalid direction! Use R (Right) or D (Down)."); + } + + // Early validation for ship bounds + if ($direction === 'R') { + $endColIndex = array_search($startPos->getColumn(), Letter::$letters) + ($ship->getSize() - 1); + if ($endColIndex >= count(Letter::$letters)) { + throw new Exception("Ship cannot be placed here - it would go off the board to the right!"); + } + } else { // direction is 'D' + $endRow = $startPos->getRow() + ($ship->getSize() - 1); + if ($endRow > 8) { + throw new Exception("Ship cannot be placed here - it would go off the board downwards!"); + } } // Calculate all ship positions $shipPositions = self::calculateShipPositions($startPos, $direction, $ship->getSize()); - // Validate all positions + // Validate collisions with other ships foreach ($shipPositions as $position) { $pos = self::parsePosition($position); - if (!self::isPositionValid($pos)) { - throw new Exception("Ship position is out of bounds!"); - } if (self::isCollisionWithOtherShips($pos, self::$myFleet)) { throw new Exception("Ships cannot be placed adjacent to each other!"); } @@ -169,18 +179,12 @@ private static function calculateShipPositions(Position $start, string $directio for ($i = 0; $i < $size; $i++) { switch ($direction) { - case 'U': - $positions[] = $start->getColumn() . ($startRow - $i); - break; case 'D': $positions[] = $start->getColumn() . ($startRow + $i); break; case 'R': $positions[] = Letter::$letters[$startCol + $i] . $startRow; break; - case 'L': - $positions[] = Letter::$letters[$startCol - $i] . $startRow; - break; } } From 305a2f2a924645219b2d1b074ae2bdcf6e7793f9 Mon Sep 17 00:00:00 2001 From: Grzegorz Drzazga <> Date: Fri, 6 Dec 2024 11:50:21 +0100 Subject: [PATCH 26/26] walidacja --- src/App.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/App.php b/src/App.php index c7d223f..4713009 100644 --- a/src/App.php +++ b/src/App.php @@ -150,14 +150,6 @@ private static function isCollisionWithOtherShips(Position $position, array $fle return true; } - // Sprawdź sąsiednie pola (włączając po skosie) - $colIndex = array_search($position->getColumn(), Letter::$letters); - $shipColIndex = array_search($shipPosition->getColumn(), Letter::$letters); - - if (abs($colIndex - $shipColIndex) <= 1 && - abs($position->getRow() - $shipPosition->getRow()) <= 1) { - return true; - } } } return false;