-
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.
- Loading branch information
Thomas Baker
committed
May 12, 2016
0 parents
commit f6e765c
Showing
27 changed files
with
1,084 additions
and
0 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,2 @@ | ||
vendor | ||
|
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,8 @@ | ||
Copyright (c) 2016 Thomas David Baker <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
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,9 @@ | ||
(c) 2016 Thomas David Baker <[email protected]> | ||
|
||
See LICENSE. | ||
|
||
To run: | ||
|
||
$ php qwirkle.php | ||
|
||
If you don't like the display consider setting Tile::USE_ANSI to false. |
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,30 @@ | ||
Improvements | ||
============ | ||
|
||
- Instead of arbitrarily placing the first longest line when starting, instead decide which would be best in some way. | ||
- Oldest player goes first in the event of a tie in starting length is not implemented. Instead it is random. | ||
- We potentially need an untrusted Player class that makes decisions and a trusted Player class that holds hand and score information rather than storing in arrays. | ||
- Instead of placing lines in arbitrary order for starting move we should place them in a way that favors the rest of our hand. | ||
- We are currently very trusting of Player that he has the tiles he says he has. We should separate out the stragey and the admin. | ||
- Starting move is always color-based never shaped-based. | ||
- You can't use two of the same tile. | ||
- Algorithm just maximizes points for now, very naive. Need to look at giving away good scoring opportunities, changing tiles, dumping dupes and keeping potentially good tiles as well at least. | ||
- usefulSets should return pairs, triples, etc. not just single tiles and max tiles sharing a property. | ||
- When the computer changes letters it should be strategic about it. | ||
- Nonrandom mode that skips all three shuffles. | ||
- Don't pass board around inside board at all. Just clone it where necessary. | ||
- Board->score uses applyWithoutChecks rather than apply. Why are we scoring something that might be illegal? Can we speed up by cutting it earlier? | ||
- Do you return to the bag then draw? Or set aside and then draw? | ||
- interface or superclass for Line and Hand? | ||
- Board->isLegal doesn't check the following things (it relies on Player->move?) | ||
* Must have a placement on an adjoining square | ||
* Mustn't overlay a tile | ||
* All lines formed must be legal | ||
- A "show thinking" flag that shows the board of every legal move considered. | ||
//echo "For $direction $i $point ($tilesHand) ...\n"; | ||
// echo "Considering move: " . end($moves) . " with a score of " . $board->score(end($moves)) . "\n"; | ||
// $displayBoard = clone $board; | ||
// $displayBoard->applyWithoutChecks(end($moves)); | ||
// echo $displayBoard; | ||
// echo "----------\n"; | ||
//fgets(STDIN); |
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,11 @@ | ||
<?php | ||
|
||
class Assert { | ||
public static function type(array $a, $class) { | ||
foreach ($a as $item) { | ||
if (get_class($item) !== $class) { | ||
throw new TypeException("$item is not of type $class"); | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
class Autoloader { | ||
public function load($name) { | ||
foreach (['', 'exceptions/'] as $dir) { | ||
$path = __DIR__ . '/' . $dir . mb_strtolower($name) . '.php'; | ||
if (file_exists($path)) { | ||
require_once $path; | ||
} | ||
} | ||
} | ||
} | ||
|
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,43 @@ | ||
<?php | ||
|
||
class Bag { | ||
public function __construct() { | ||
$this->tiles = []; | ||
foreach (Color::colors() as $color) { | ||
foreach (Shape::shapes() as $shape) { | ||
$tile = new Tile($color, $shape); | ||
$this->tiles[] = $tile; | ||
$this->tiles[] = $tile; | ||
$this->tiles[] = $tile; | ||
} | ||
} | ||
$this->shuffle(); | ||
} | ||
|
||
public function draw($n) { | ||
$tiles = []; | ||
for ($i = 0; $i < $n; $i++) { | ||
$tile = array_shift($this->tiles); | ||
if ($tile === null) { | ||
return $tiles; | ||
} | ||
$tiles[$i] = $tile; | ||
} | ||
return $tiles; | ||
} | ||
|
||
public function discard($tiles) { | ||
$this->tiles = array_merge($this->tiles, $tiles); | ||
$this->shuffle(); | ||
} | ||
|
||
public function isEmpty() { | ||
return count($this->tiles) === 0; | ||
} | ||
|
||
private function shuffle() { | ||
if (Game::RANDOM) { | ||
shuffle($this->tiles); | ||
} | ||
} | ||
} |
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,189 @@ | ||
<?php | ||
|
||
class Board { | ||
const SIZE = 200; | ||
const MIN_VIEWPORT_SIZE = 0; | ||
|
||
private $board; | ||
|
||
public function __construct(Board $board = null) { | ||
if (!$board) { | ||
$this->board = array_fill(0, self::SIZE, []); | ||
for ($y = 0; $y <= self::SIZE; $y++) { | ||
$this->board[$y] = array_fill(0, self::SIZE, null); | ||
} | ||
} else { | ||
$this->board = $board; | ||
} | ||
} | ||
|
||
public function isLegal(Move $move) { | ||
foreach ($move->placements() as $placement) { | ||
if ($this->spotTaken($placement->point())) { | ||
return false; | ||
} | ||
} | ||
$board = clone $this; | ||
$board->applyWithoutChecks($move); | ||
foreach ($move->placements() as $placement) { | ||
if (!$this->isLegalPlacement($placement, $board)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private function isLegalPlacement(Placement $placement, Board $board) { | ||
$linesFromPlacement = $this->linesFromPlacement($placement, $board); | ||
foreach ($linesFromPlacement as $line) { | ||
if (!$line->isLegal($line)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private function linesFromPlacement(Placement $placement, Board $board) { | ||
list($x, $y) = [$placement->point()->x(), $placement->point()->y()]; | ||
$lines = []; | ||
$lines[] = new Line($this->getHorizontalLine($board, $x, $y)); | ||
$lines[] = new Line($this->getVerticalLine($board, $x, $y)); | ||
return $lines; | ||
} | ||
|
||
private function getHorizontalLine(Board $board, $x, $y) { | ||
return array_merge(array_reverse($this->getPartialLine($board, $x, $y, -1, 0)), $this->getPartialLine($board, $x - 1, $y, 1, 0)); | ||
} | ||
|
||
private function getVerticalline(Board $board, $x, $y) { | ||
return array_merge(array_reverse($this->getPartialLine($board, $x, $y, 0, -1)), $this->getPartialLine($board, $x, $y - 1, 0, 1)); | ||
} | ||
|
||
private function getPartialLine(Board $board, $x, $y, $xMod, $yMod) { | ||
$line = []; | ||
while (true) { | ||
$x += $xMod; | ||
$y += $yMod; | ||
$point = new Point($x, $y); | ||
if ($board->at($point) === null) { | ||
break; | ||
} | ||
$line[] = new Placement($point, $board->at($point)); | ||
} | ||
return $line; | ||
} | ||
|
||
public function apply(Move $move) { | ||
if (!$this->isLegal($move)) { | ||
throw new IllegalMoveException("Cannot place move `{$move}` on board `{$this}`"); | ||
} | ||
$this->applyWithoutChecks($move); | ||
return $this->score($move); | ||
} | ||
|
||
private function applyWithoutChecks(Move $move) { | ||
foreach ($move->placements as $placement) { | ||
list($x, $y) = [$placement->point()->x(), $placement->point()->y()]; | ||
$this->board[$y][$x] = $placement->tile(); | ||
} | ||
return $board; | ||
} | ||
|
||
public function at(Point $point) { | ||
if ($point->x() < 0 || $point->y() < 0 || $point->x() >= self::SIZE || $point->y() >= self::SIZE) { | ||
return null; | ||
} | ||
return $this->board[$point->y()][$point->x()]; | ||
} | ||
|
||
public function spotTaken(Point $point) { | ||
return isset($this->board[$point->y()][$point->x()]); | ||
} | ||
|
||
public function attachmentLocations() { | ||
$locations = []; | ||
for ($y = 0; $y < count($this->board); $y++) { | ||
for ($x = 0; $x < count($this->board[$y]); $x++) { | ||
$point = new Point($x, $y); | ||
if (!$this->spotTaken($point) && $this->adjacentIsOccupied($point)) { | ||
$locations[] = $point; | ||
} | ||
} | ||
} | ||
return $locations; | ||
} | ||
|
||
private function adjacentIsOccupied(Point $point) { | ||
return isset($this->board[$point->y() - 1][$point->x()]) | ||
|| isset($this->board[$point->y() + 1][$point->x()]) | ||
|| isset($this->board[$point->y()][$point->x() - 1]) | ||
|| isset($this->board[$point->y()][$point->x() + 1]); | ||
} | ||
|
||
public function isEmpty() { | ||
foreach ($this->board as $column) { | ||
foreach ($column as $tile) { | ||
if ($tile) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public function score(Move $move) { | ||
$board = clone $this; | ||
$board->applyWithoutChecks($move); | ||
$lines = []; | ||
foreach ($move->placements() as $placement) { | ||
$linesFromPlacement = $board->linesFromPlacement($placement, $board); | ||
$lines = array_merge($lines, $linesFromPlacement); | ||
} | ||
$lines = array_unique($lines); | ||
$points = 0; | ||
foreach ($lines as $line) { | ||
if ($line->length() === count(Color::colors())) { | ||
$score += Score::QWIRKLE_BONUS; | ||
} | ||
if ($line->length() > 1) { | ||
$score += $line->length(); | ||
} | ||
} | ||
return $score; | ||
} | ||
|
||
public function __toString() { | ||
$highest = self::SIZE / 2 - self::MIN_VIEWPORT_SIZE / 2 - 1; | ||
$lowest = self::SIZE / 2 + self::MIN_VIEWPORT_SIZE / 2 - 1; | ||
$leftmost = self::SIZE / 2 - self::MIN_VIEWPORT_SIZE / 2 - 1; | ||
$rightmost = self::SIZE / 2 + self::MIN_VIEWPORT_SIZE / 2 - 1; | ||
|
||
for ($y = 0; $y < count($this->board); $y++) { | ||
for ($x = 0; $x < count($this->board[$y]); $x++) { | ||
if ($this->board[$y][$x] !== null) { | ||
$highest = min($y, $highest); | ||
$lowest = max($y, $lowest); | ||
$leftmost = min($x, $leftmost); | ||
$rightmost = max($x, $rightmost); | ||
} | ||
} | ||
} | ||
$highest -= 1; | ||
$lowest += 2; | ||
$leftmost -= 1; | ||
$rightmost += 2; | ||
|
||
for ($y = $highest; $y < $lowest; $y++) { | ||
$a = []; | ||
for ($x = $leftmost; $x < $rightmost; $x++) { | ||
$tile = $this->board[$y][$x]; | ||
if ($tile === null) { | ||
$tile = Tile::USE_ANSI ? Color::ANSI_BACKGROUND . " \e[0m" : ' '; | ||
} | ||
$a[] = "{$tile}"; | ||
} | ||
$s .= '|' . implode("|", $a) . "|\n"; | ||
} | ||
return $s; | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
class Color { | ||
const ANSI_BACKGROUND = "\e[47m"; | ||
|
||
private static $blue; | ||
private static $green; | ||
private static $lilac; | ||
private static $orange; | ||
private static $red; | ||
private static $yellow; | ||
|
||
private static $ansi = [ | ||
'blue' => "\e[34m", | ||
'green' => "\e[1m", // 0 is bold black, not green but green wasn't very distinguishable. | ||
'lilac' => "\e[35m", | ||
'orange' => "\e[36m", // 36 is actually cyan but \e[48;2;255;160m is an (even more) unreliable orange. | ||
'red' => "\e[31m", | ||
'yellow' => "\e[33m" | ||
]; | ||
|
||
public static function colors() { | ||
return [self::blue(), self::green(), self::lilac(), self::orange(), self::red(), self::yellow()]; | ||
} | ||
|
||
public static function __callStatic($f, array $args) { | ||
if (!self::$$f) { | ||
self::$$f = new Color($f); | ||
} | ||
return self::$$f; | ||
} | ||
|
||
private function __construct($name) { | ||
$this->name = $name; | ||
} | ||
|
||
public function name() { | ||
return $this->name; | ||
} | ||
|
||
public function ansi() { | ||
return self::$ansi[$this->name()]; | ||
} | ||
|
||
public function __toString() { | ||
return mb_strtoupper(mb_substr($this->name(), 0, 1)); | ||
} | ||
} |
Oops, something went wrong.