restgames/battleship-php

Battleship 游戏的 PHP 辅助组件

dev-master 2016-03-26 00:52 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:58:25 UTC


README

Build Status

这是一个 PHP 辅助库,它基于 Hasbro 原版 Battleship 游戏棋盘实现了一个 Battleship 网格,包括基本的规则,如放置船只、射击、获取射击结果以及检查所有船只是否已沉没。

查看 裁判 项目,该项目使两位玩家能够相互对战。您将在此处找到必须实现的 REST API 的详细信息。

什么是 REST Games?

欢迎来到 REST Games!我们的目标是提供一些超出 kata 的编码挑战。您将实现一个小型的 JSON REST API,该 API 将玩一个知名的游戏。酷的部分在于当两个伙伴开发相同的 JSON REST API 时,裁判可以让他们相互对战。酷,不是吗?

实体

  • 网格:10x10 空洞网格
  • 空洞:一个字母和一个数字(A,3)
  • 船只:您要放置并沉没的船只!
    • 1 x 航空母舰(5 个空洞和 id #1)
    • 1 x 战列舰(4 个空洞和 id #2)
    • 1 x 巡洋舰(3 个空洞和 id #3)
    • 1 x 潜艇(3 个空洞和 id #4)
    • 1 x 驱逐舰(2 个空洞和 id #5)
  • 位置:船只的位置
    • 水平
    • 垂直

测试

Battleship\Grid
 [x] Given an empty grid when no ships are placed then all ships are not placed
 [x] Given an empty grid when two ships are placed then all ships are not placed
 [x] Given an empty grid when one ship of every type is placed then all ships are placed
 [x] Given an empty grid when placing a ship out of bounds then an exception should be thrown
 [x] Given an empty grid when placing same ship type twice then an exception should be thrown
 [x] Given an empty grid when two ships of different type are overlapped then an exception should be thrown
 [x] Given an empty grid when placing a ship then a new grid should be returned aka grids are immutable
 [x] Given an empty grid when rendering grid then a 100 length string with 0s should be returned
 [x] Given an empty grid when placing all ships then render must math string
 [x] Given a valid grid string when building a grid from it then grid should be valid
 [x] Given a non valid grid string when building a grid from it then an exception should be thrown
 [x] Given an empty grid when shooting at any hole then an exception should be thrown
 [x] Given a hit ship when shooting again on the hit hole then hit should be returned again
 [x] Given a sunk ship when shooting again on the ship then sunk should be returned again
 [x] Given a valid grid string when building and shooting all the holes then all ships must be sunk aka a complete game

Battleship\Hole
 [x] Given a letter or a number out of bounds when creating a hole then an exception is thrown
 [x] Given a letter and a number in of bounds when creating a hole then hole is created
 [x] Given a number when asking for its letter then letter of this number order should be returned
 [x] Given a letter when asking for its number then number of this letter order should be returned

Battleship\Position
 [x] Given two positions with same value when comparing equality then must be true

Battleship\Ship\Ship
 [x] Given a ship when asking for size then size must match

示例

在以下示例中,我们将构建一个网格放置船只,并从左到右、从上到下射击。我们将检查射击结果(未命中、命中和沉没)是否正确。如果您想了解更多示例,请查看 tests 文件夹,特别是 GridTest.php 文件。

/*
Let's build the following grid
0300222200
0300000000
0310000000
0010005000
0010005000
0010044400
0010000000
0000000000
0000000000
0000000000
*/

$grid = (new Grid)
    ->placeShip(new Carrier(), new Hole('C', 3), Position::fromVertical())
    ->placeShip(new Battleship(), new Hole('A', 5), Position::fromHorizontal())
    ->placeShip(new Cruiser(), new Hole('A', 2), Position::fromVertical())
    ->placeShip(new Submarine(), new Hole('F', 6), Position::fromHorizontal())
    ->placeShip(new Destroyer(), new Hole('D', 7), Position::fromVertical())

$shotResults =
    '0100111200'.
    '0100000000'.
    '0210000000'.
    '0010001000'.
    '0010002000'.
    '0010011200'.
    '0020000000'.
    '0000000000'.
    '0000000000'.
    '0000000000';

$this->assertTrue($this->grid->areAllShipsPlaced());
$this->assertFalse($this->grid->areAllShipsSunk());

foreach(Grid::letters() as $l => $letter) {
    foreach(Grid::numbers() as $n => $number) {
        $this->assertSame(
            (int) $shotResults{$l * 10 + $n},
            $battleshipGrid->shot(new Hole($letter, $number))
        );
    }
}

$this->assertTrue($battleshipGrid->areAllShipsSunk());

提示

  • 所有无效条件都将抛出异常。例如,射击出界、在界外放置船只、构建无效的空洞等。

  • 您有一个可能有用的 ShipFactory