public function move(Move $new) { foreach ($this->moves as $move) { for ($row = 0; $row <= 2; $row++) { for ($col = 0; $col <= 2; $col++) { if ($move == $new) { throw new \RuntimeException('Already moved here'); } } } } $this->moves[] = $new; $currentName = $this->currentPlayer()->get('name'); $this->movesPerPlayer[$currentName][$new->get('row')][$new->get('col')] = true; return end($this->moves); }
<?php require_once 'vendor/autoload.php'; use Sensorario\Tris; $board = Tris\Board::box(['first_player' => Tris\Player::box(['name' => 'Simone']), 'second_player' => Tris\Player::box(['name' => 'Demo'])]); $currentMove = function ($move) use($board) { $player = $board->currentPlayer(); $lastMove = $board->move($move); echo "\nMove: "; echo $player->get('name'); echo " moved in "; echo " col " . $lastMove->get('col'); echo " row " . $lastMove->get('row'); if ($board->trisIsDone()) { echo "\n\nThe winner is: "; echo $board->lastPlayer()->get('name'); echo "\n\n"; die; } }; try { do { try { $currentMove(Tris\Move::createRandom()); } catch (Exception $e) { } } while ($board->matchIsNotFinished()); } catch (Tris\NoWinnerException $e) { echo "\n\nNobody wins\n\n"; die; }
public function testSimulation() { $countActions = 0; do { try { $move = Tris\Move::createRandom(); $this->board->move($move); } catch (\RuntimeException $e) { } $countActions++; } while ($this->board->countFreeTiles() !== 0); $this->assertTrue($this->board->boardIsFull()); }
public function move() { return Move::createRandom(); }