Difference between revisions of "Tutorial Project. Release 7. Class Computer2"
From wiki.visual-prolog.com
Line 1: | Line 1: | ||
{{PolyLine7.Players}} | {{PolyLine7.Players}} | ||
+ | |||
+ | <vip> | ||
+ | /**************************************************** | ||
+ | Copyright (c) 2007,2008. Elena Efimova | ||
+ | Predicate successfulStep | ||
+ | |||
+ | Copyright (c) 2007,2008. Prolog Development Center | ||
+ | Written by: Victor Yukhtenko | ||
+ | |||
+ | Class computer2 | ||
+ | *****************************************************/ | ||
+ | class computer2:player | ||
+ | open core | ||
+ | |||
+ | predicates | ||
+ | getPlayerDescriptor:(game::language_D)->string Descriptor. | ||
+ | |||
+ | end class computer2 | ||
+ | |||
+ | implement computer2 | ||
+ | inherits genericComputer | ||
+ | open core | ||
+ | |||
+ | clauses | ||
+ | new():- | ||
+ | PolyLineBraneObj=polylineStrategy2::new(), | ||
+ | setpolylineStrategy(PolyLineBraneObj). | ||
+ | |||
+ | clauses | ||
+ | getPlayerDescriptor(game::en)=polylineStrategy2::playerDescriptorEn_C. | ||
+ | getPlayerDescriptor(game::ru)=polylineStrategy2::playerDescriptorRu_C. | ||
+ | |||
+ | end implement computer2 | ||
+ | |||
+ | /****************************************** | ||
+ | Class polylineStrategy2 | ||
+ | ******************************************/ | ||
+ | class polylineStrategy2:polylineStrategy | ||
+ | open core | ||
+ | predicates | ||
+ | classInfo : core::classInfo. | ||
+ | |||
+ | constants | ||
+ | playerDescriptorEn_C="Computer2: Limited Depth. Despair move - random". | ||
+ | playerDescriptorRu_C="Computer2: Ограниченная глубина. Ход очаяния - случайный". | ||
+ | |||
+ | end class polylineStrategy2 | ||
+ | |||
+ | implement polylineStrategy2 | ||
+ | open core, exception | ||
+ | |||
+ | constants | ||
+ | className = "polylineStrategy2". | ||
+ | classVersion = "1.0". | ||
+ | |||
+ | clauses | ||
+ | classInfo(className, classVersion). | ||
+ | |||
+ | facts | ||
+ | maxDepth_V:positive:=5. | ||
+ | genericComputer_V:genericComputer:=erroneous. | ||
+ | |||
+ | clauses | ||
+ | new():- | ||
+ | defineSearchDepth(). | ||
+ | |||
+ | clauses | ||
+ | setGenericComputer(GenericComputerObj):- | ||
+ | genericComputer_V:=GenericComputerObj. | ||
+ | |||
+ | predicates | ||
+ | defineSearchDepth:(). | ||
+ | clauses | ||
+ | defineSearchDepth():- | ||
+ | DepthStr=humanInterface::getInput(humanInterface::searchDepth_S,toString(maxDepth_V)), | ||
+ | not(DepthStr=""), | ||
+ | !, | ||
+ | try | ||
+ | maxDepth_V:=toTerm(DepthStr), | ||
+ | if maxDepth_V mod 2 = 0 then | ||
+ | maxDepth_V:=maxDepth_V+1 | ||
+ | end if | ||
+ | catch _TraceID1 do | ||
+ | humanInterface::announce(humanInterface::errorMustBeNumber_S,""), | ||
+ | defineSearchDepth() | ||
+ | end try. | ||
+ | defineSearchDepth(). | ||
+ | |||
+ | clauses | ||
+ | successfulStep(PolyLine)=BestMove:- | ||
+ | BestMove=successfulStep(maxDepth_V,PolyLine). | ||
+ | |||
+ | predicates | ||
+ | successfulStep: (integer Counter, juniourJudge::cell*)->juniourJudge::cell nondeterm. | ||
+ | clauses | ||
+ | successfulStep(Counter,PolyLine)=BestMove:- | ||
+ | genericComputer_V:stepCandidate(PolyLine,_PolyLine1,BestMove), | ||
+ | isStepSuccessful(Counter,PolyLine,BestMove), | ||
+ | !. | ||
+ | successfulStep(Counter,PolyLine)=Cell:- | ||
+ | genericComputer_V:stepCandidate(PolyLine, PolyLine1,Cell), | ||
+ | not(_=successfulStep(Counter-1,PolyLine1)). | ||
+ | |||
+ | class predicates | ||
+ | isStepSuccessful:(integer Counter,juniourJudge::cell* PolyLine,juniourJudge::cell BestMove) determ. | ||
+ | clauses | ||
+ | isStepSuccessful(_Counter,PolyLine,BestMove):- | ||
+ | list::isMember(BestMove, PolyLine), | ||
+ | !. | ||
+ | isStepSuccessful(Counter,_PolyLine,_BestMove):- | ||
+ | Counter<=1. | ||
+ | |||
+ | clauses | ||
+ | randomStep()=Cell:- | ||
+ | findAll(NewCell,genericComputer_V:stepCandidate(juniourJudge::polyline_P,_Polyline1, NewCell),CellCandidateListWithDuplicates), | ||
+ | CellCandidateList=list::removeDuplicates(CellCandidateListWithDuplicates), | ||
+ | not(CellCandidateList=[]), | ||
+ | NoOfVariants=list::length(CellCandidateList), | ||
+ | ChoiceNo=math::random(NoOfVariants-1), | ||
+ | Cell=list::nth(ChoiceNo+1,CellCandidateList). | ||
+ | |||
+ | end implement polylineStrategy2 | ||
+ | </vip> | ||
[[ru:Ползунок7. Класс Computer2]] | [[ru:Ползунок7. Класс Computer2]] | ||
[[Category:Examples]] | [[Category:Examples]] | ||
[[Category:Game 1+]] | [[Category:Game 1+]] |
Revision as of 21:43, 6 January 2008
Tutorial Project. Release 7 |
---|
/**************************************************** Copyright (c) 2007,2008. Elena Efimova Predicate successfulStep Copyright (c) 2007,2008. Prolog Development Center Written by: Victor Yukhtenko Class computer2 *****************************************************/ class computer2:player open core predicates getPlayerDescriptor:(game::language_D)->string Descriptor. end class computer2 implement computer2 inherits genericComputer open core clauses new():- PolyLineBraneObj=polylineStrategy2::new(), setpolylineStrategy(PolyLineBraneObj). clauses getPlayerDescriptor(game::en)=polylineStrategy2::playerDescriptorEn_C. getPlayerDescriptor(game::ru)=polylineStrategy2::playerDescriptorRu_C. end implement computer2 /****************************************** Class polylineStrategy2 ******************************************/ class polylineStrategy2:polylineStrategy open core predicates classInfo : core::classInfo. constants playerDescriptorEn_C="Computer2: Limited Depth. Despair move - random". playerDescriptorRu_C="Computer2: Ограниченная глубина. Ход очаяния - случайный". end class polylineStrategy2 implement polylineStrategy2 open core, exception constants className = "polylineStrategy2". classVersion = "1.0". clauses classInfo(className, classVersion). facts maxDepth_V:positive:=5. genericComputer_V:genericComputer:=erroneous. clauses new():- defineSearchDepth(). clauses setGenericComputer(GenericComputerObj):- genericComputer_V:=GenericComputerObj. predicates defineSearchDepth:(). clauses defineSearchDepth():- DepthStr=humanInterface::getInput(humanInterface::searchDepth_S,toString(maxDepth_V)), not(DepthStr=""), !, try maxDepth_V:=toTerm(DepthStr), if maxDepth_V mod 2 = 0 then maxDepth_V:=maxDepth_V+1 end if catch _TraceID1 do humanInterface::announce(humanInterface::errorMustBeNumber_S,""), defineSearchDepth() end try. defineSearchDepth(). clauses successfulStep(PolyLine)=BestMove:- BestMove=successfulStep(maxDepth_V,PolyLine). predicates successfulStep: (integer Counter, juniourJudge::cell*)->juniourJudge::cell nondeterm. clauses successfulStep(Counter,PolyLine)=BestMove:- genericComputer_V:stepCandidate(PolyLine,_PolyLine1,BestMove), isStepSuccessful(Counter,PolyLine,BestMove), !. successfulStep(Counter,PolyLine)=Cell:- genericComputer_V:stepCandidate(PolyLine, PolyLine1,Cell), not(_=successfulStep(Counter-1,PolyLine1)). class predicates isStepSuccessful:(integer Counter,juniourJudge::cell* PolyLine,juniourJudge::cell BestMove) determ. clauses isStepSuccessful(_Counter,PolyLine,BestMove):- list::isMember(BestMove, PolyLine), !. isStepSuccessful(Counter,_PolyLine,_BestMove):- Counter<=1. clauses randomStep()=Cell:- findAll(NewCell,genericComputer_V:stepCandidate(juniourJudge::polyline_P,_Polyline1, NewCell),CellCandidateListWithDuplicates), CellCandidateList=list::removeDuplicates(CellCandidateListWithDuplicates), not(CellCandidateList=[]), NoOfVariants=list::length(CellCandidateList), ChoiceNo=math::random(NoOfVariants-1), Cell=list::nth(ChoiceNo+1,CellCandidateList). end implement polylineStrategy2