Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IState{
- bool isGoal();
- }
- public interface IOperator{
- bool isApplicable(IState state);
- IState Apply(IState state);
- }
- public interface Problem{
- IState StartState();
- List<IOperator> Operators();
- }
- class Node{
- IState state;
- public Node(IState state){
- this.state = state;
- }
- }
- class Algorithm{
- private Problem problem;
- public Algorithm(Problem p){
- this.problem = p;
- }
- Node actualNode = null;
- public bool run(){
- actualNode = new Node(problem.StartState());
- Random rnd = new Random();
- while(true){
- if( actualNode.state.isGoal() ) break;
- List<IOperator> ops = new List<IOperator>;
- foreach( var o in problem.Operators() )
- if( o.isApplicable( actualNode.state ) )
- ops.Add(o);
- if( ops.count == 0 ) break;
- IOperator operator = ops[ rnd.Next(0, ops.Count - 1) ];
- IState newState = operator.Appy( actualNode.state );
- actualNode = new Node(newState);
- }
- if( actualNode.state.isGoal() ) Console.WriteLine( "Van megoldas" );
- else Console.WriteLine( "Nincs Megoldas" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement