-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPegSolitaireProblem.php
38 lines (32 loc) · 1.02 KB
/
PegSolitaireProblem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
/**
* PegSolitaireProblem Class
*
* Class for the representation of the peg solitaire problem
* @author Lucas Acosta <[email protected]>
* @version 0.1
* @package peg_solitaire
*/
include_once 'Problem.php';
include_once 'PegSolitaireState.php';
class PegSolitaireProblem extends Problem
{
function __construct(&$initial_state) {
if (! ($initial_state instanceof PegSolitaireState)) throw new Exception('The initial state must be an instance of PegSolitaireState class');
parent::__construct($initial_state);
}
function isSolution(&$state) {
# Problem is solved when there's only one peg remaining
return $state->remainingPegs() == 1;
}
function nextStates(&$state) {
$next_states = array();
foreach ($state->getRemainingPegs() as $peg) {
foreach(array('up', 'right', 'down', 'left') as $direction) {
if ($new_state = $state->makeMove($peg, $direction)) $next_states[] = $new_state;
}
}
return $next_states;
}
}
?>