Skip to content

Commit

Permalink
Turn更新メソッドGreen #55
Browse files Browse the repository at this point in the history
  • Loading branch information
ebinase committed May 19, 2022
1 parent 9139a9b commit d89833e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/OthelloCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function handle()
// スキップの場合はすぐにcontinue
if ($turn->mustSkip()) {
$this->confirm('置ける場所がないためスキップします。', true);
$turn = $turn->next();
$turn = $turn->advance();
continue;
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public function handle()
$action = Position::make([$row, $col]);
}

$turn = $turn->next($action);
$turn = $turn->advance($action);
} // while()

$this->error('=====================================');
Expand Down
10 changes: 8 additions & 2 deletions packages/Models/Othello/Board/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public function toArray(): array
return $this->board->toArray();
}

public function isFulfilled(): bool
{
return $this->getRest() === 0;
}


/**
* 何も置かれていない場所の数を取得
*
Expand Down Expand Up @@ -147,9 +153,9 @@ public function isValid(Position $position, Color $color): bool

public function update(Position $position, Color $color): Board
{
// 置けない場合
// 指定された場所にコマを置くことができるか確認
if (!$this->isValid($position, $color)) {
throw new \Exception();
throw new \Exception('指定された場所に置くことができません');
}

// 更新された盤面を返す
Expand Down
40 changes: 20 additions & 20 deletions packages/Models/Othello/Othello/Turn.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,14 @@ public static function make(int $turnNumber, Color $playableColor, Board $board)
* 次のターンへ
* ターン数:+1
* 色:反対の色へ
* 盤面:コマを置いて更新。スキップの場合は現在のものをそのまま設定
* 盤面:コマを置いて更新。
*/
public function next(?Position $position = null): Turn
public function advance(Position $position): Turn
{
// ゲームが終了している場合
if ($this->finishedLastTurn()) throw new \RuntimeException();
// ゲームが続行不能な場合
if (!$this->isContinuable()) throw new \RuntimeException();

if ($this->mustSkip()) {

}

// コマを置くことができる場合、盤面を更新してスキップカウントをリセット
// 必須チェック
if (!isset($position)) throw new \Exception('コマを置くことができるマスがある場合、スキップはできません。');
// 指定された場所にコマを置くことができるか確認
if (!$this->board->isValid($position, $this->playableColor)) throw new \Exception('指定された場所に置くことができません。');
// これ以上進めない場合
if (!$this->isAdvanceable()) throw new \RuntimeException();
// スキップするしかない場合はコマを置けない
if ($this->mustSkip()) throw new \RuntimeException('コマを置くことができるマスがある場合、スキップはできません。');

return new Turn(
$this->turnNumber + 1,
Expand All @@ -67,8 +57,19 @@ public function next(?Position $position = null): Turn
);
}

/**
* 次のターンへ
* ターン数:+1
* 色:反対の色へ
* 盤面:スキップの場合は現在のものをそのまま設定
*/
public function skip()
{
// これ以上進めない場合
if (!$this->isAdvanceable()) throw new \RuntimeException();
// コマを置くことができる場合はスキップできない
if ($this->mustSkip()) throw new \RuntimeException('コマを置くことができるマスがある場合、スキップはできません。');

return new Turn(
$this->turnNumber + 1,
$this->playableColor->opposite(),
Expand All @@ -80,15 +81,14 @@ public function skip()
* 最終ターンが終了しているか(=ゲームが正常終了しているか)判定
* @return bool
*/
public function finishedLastTurn(): bool
public function isAdvanceable(): bool
{
return $this->board->isFulfilled();
// 盤面がいっぱいになっていなかったら進行可能
return !$this->board->isFulfilled();
}

public function mustSkip(): bool
{
return !$this->board->hasPlayablePosition($this->playableColor);
}


}
14 changes: 7 additions & 7 deletions tests/Feature/Models/Othello/Turn/TurnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function ターン更新()
$turn = Turn::init(); // 1ターン目
$move = Position::make([4, 6]); // 先行プレイヤーの1ターン目の指した場所
// when:
$next = $turn->next($move); // ターンを進める
$next = $turn->advance($move); // ターンを進める
// then:
// 2ターン目の盤面
$boardAtSecondTurn = Board::init()->update($move, Color::white());
Expand All @@ -53,7 +53,7 @@ public function おけない場所を指定された時は例外を出す()
$move = Position::make([1, 1]); // おけない場所
// then:
$this->expectException(\Exception::class);
$turn->next($move); // ターンを進める
$turn->advance($move); // ターンを進める
}

// ---------------------------------------
Expand All @@ -70,8 +70,8 @@ public function スキップ()

// when:
$turn1 = Turn::make(1, Color::black(), $board, 0);
$turn2 = $turn1->next();
$turn3 = $turn2->next();
$turn2 = $turn1->advance();
$turn3 = $turn2->advance();

// then:
// 2ターン目
Expand All @@ -93,7 +93,7 @@ public function 置ける場所があるのにスキップしようとした場
// then:
$this->expectException(\Exception::class);
// 置ける場所があるのに場所指定なしで更新した場合
$turn->next();
$turn->advance();
}

// ---------------------------------------
Expand All @@ -109,8 +109,8 @@ public function 盤面に空いているマスがなくなった時が最後の
$firstTurn = Turn::init();
$lastTurn = Turn::make(20, Color::white(), $fullBoard, 0);
// then:
self::assertSame(false, $firstTurn->finishedLastTurn());
self::assertSame(true, $lastTurn->finishedLastTurn());
self::assertSame(false, !$firstTurn->isAdvanceable());
self::assertSame(true, !$lastTurn->isAdvanceable());
}


Expand Down

0 comments on commit d89833e

Please sign in to comment.