-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a second type of player with a different strategy.
- Loading branch information
Thomas Baker
committed
May 12, 2016
1 parent
016fd99
commit 62ac7a2
Showing
4 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
|
||
class IllegalArgumentException extends Exception { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
class CautiousPlayer extends Player { | ||
public function evaluate(array $tiles, Move $move, Board $board, $bagIsEmpty) { | ||
$score = $board->score($move); | ||
if (count($move->placements()) === count($tiles) && $bagIsEmpty) { | ||
$score += Score::FINISHING_BONUS; | ||
} elseif ($this->enablesQwirkle($move, $board, $bagIsEmpty) && $score < 9) { | ||
$score -= 3.5; | ||
} | ||
return $score; | ||
} | ||
|
||
private function enablesQwirkle(Move $move, Board $board, $bagIsEmpty) { | ||
foreach ($move->lines($board) as $line) { | ||
if ($line->length() === count(Color::colors()) - 1) { | ||
$missingPiece = $this->missingPieces($line)[0]; | ||
if ($this->notAccountedFor($missingPiece, $board)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private function missingPieces($line) { | ||
if ($line->length() <= 1) { | ||
throw new IllegalArgumentException("Qwirkle this lines is part of is ambiguous ($line)."); | ||
} | ||
$sharedProperty = $line->sharedProperty(); | ||
if ($sharedProperty === null) { | ||
throw new IllegalArgumentException("This line does not have a shared property ($line)."); | ||
} | ||
$missing = []; | ||
if (get_class($sharedProperty) === Shape) { | ||
foreach (Color::colors() as $color) { | ||
$tile = Tile::getTile($color, $sharedProperty); | ||
if (!$line->contains($tile)) { | ||
$missing[] = $tile; | ||
} | ||
} | ||
} elseif (get_class($sharedProperty) === Color) { | ||
foreach (Shape::shapes() as $shape) { | ||
$tile = Tile::getTile($sharedProperty, $shape); | ||
if (!$line->contains($tile)) { | ||
$missing[] = $tile; | ||
} | ||
} | ||
} else { | ||
throw new IllegalArgumentException("Unrecognized property $sharedProperty"); | ||
} | ||
return $missing; | ||
} | ||
|
||
private function notAccountedFor($searchTile, Board $board) { | ||
$tiles = Tile::allTiles(); | ||
foreach ($board->tiles() as $tile) { | ||
if ($tile === $searchTile) { | ||
$pos = array_search($tile, $tiles); | ||
unset($tiles[$pos]); | ||
} | ||
} | ||
return in_array($tile, $tiles); | ||
} | ||
} |