You get a bonus - 1 coin for daily activity. Now you have 1 coin

The arrangement of chess pieces

Lecture



The PROLOGUE program places chess pieces on a chessboard, so that the set piece is not beaten by other pieces or other pieces are not beaten by the put piece. There are no mathematical calculations to determine the location of the battle figures. Only facts and work with them are used. In other words, the program "sees" the chess field like a man. The program was made by me in 5 hours, including the development of the algorithm and interface on Visual Prolog 5.2. Due to the fact that instead of calculating the position there is an exhaustive search of the facts, the speed of the program is very low - 30 seconds on a processor with 800 MHz. However, it is much faster than a person “thinks”!

 
  Global domains 

 
  Fff = king;  queen;  elephant;  horse;  rook 
  Angle = obliquely;  straight 
  Range = line;  beside;  a horse 
  Beat = beat (Range, Angle);  a horse 
  String = string 
  Strings = String * 
  Field = Field (String, String) 

 
  Global predicates 
  put_figures (fff) 

 
  predicates 
  nondeterm rule (fff, byot) 
  markup1 (Strings) 
  markup2 (Strings) 

 
  clauses 
  % of the rules of combat figures 
  rule (king, byot (near, diagonally)). 
  rule (king, byot (next, right)). 
  rule (queen, byot (line, diagonally)). 
  rule (queen, byot (line, straight)). 
  rule (elephant, byot (line, diagonally)). 
  rule (horse, horse). 
  rule (rook, byot (line, straight)). 

 
  % of the size of the board vertically and horizontally 
  markup1 (["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]). 
  markup2 (["a", "b", "c", "d", "e", "f", "g", "i", "k", "l"]). 

 
  database - layouts 
  % fact - places where there are figures 
  worth (FFF, Field) 

 
  domains 
  Nap = left;  to the right;  up;  way down;  down_left;  down_right 
  up_left;  up_right 

 
  % 
  % service procedures for sorting cells 
  % to "understand" the rules of the computer 
  % note, here the "understanding" is the same as the person 
  % and there is no mathematical operation. 
  % everything is based on facts 
  % 
  predicates 
  nondeterm search_of_list_of_facts (Strings, String). 
  Element_left (Lines, Line Element, Line ElementLeft) 
  Right Element (Strings, String Element, String Element Right) 
  field_left (Field, Field Find) 
  field_sprava (Field, Field Find) 
  field_up (Field, Field Find) 
  field_below (Field, Field Find) 
  field_field (Angle, Field, Field Near) 
  nondeterm field_parking (Angle, Eg, Field, Field Beside) 
  field_line (Angle, Field, Field Line) 
  field_con (Angle, Field Of, Field) 

 
  clauses 
  % enumerates the elements of the list as the facts of the base of the PROLOGUE 
  % This allows you to enable the rollback mechanism. 
  search_of_facts ([String | _], String). 
  brute force_list_of_facts ([_ | Strings], String): - 
  search_of_list_of_facts (Strings, String). 

 
  % will find the item to the left of this in the list 
  Element_left ([ElementLeft, Element | _], Element, ElementLeft): - 
  ! 
  Element_left ([_ | List], Element, ElementLeft): - 
  Element_left (List, Element, ElementLeft), 
  ! 
  % will find the item to the right of this in the list 
  Right element ([Element, Right Element | _], Element, Right Element): - 
  ! 
  Right element ([_ | List], Element, Right Element): - 
  Right element (List, Element, Right Element), 
  ! 
  % takes a field next to 
  left field (field (Number, Letter), field (Number Left, Letter)): - 
  markup 1 (Numbers), 
  left-element (Numbers, Number, NumberLeft), 
  ! 
  right_field (field (Number, Letter), field (NumberNumber, Right)): - 
  markup 1 (Numbers), 
  right element (Numbers, Number, Right Number), 
  ! 
  top_field (field (Number, Letter), field (Number, Letter Top)): - 
  markup 2 (letters), 
  left element (Letters, Letter, Letter Above), 
  ! 
  bottom field (field (Number, Letter), field (Number, Letter B below)): - 
  markup 2 (letters), 
  right element (Letters, Letter, Letter Below), 
  ! 

 
  % enumerates all fields nearby in accordance with the angle of the battle 
  field_number (straight, Field, Field1): - 
  left_field (Field, Field1),!. 
  field_number (straight, Field, Field1): - 
  field_sprava (Field, Field1),!. 
  field_number (straight, Field, Field1): - 
  field_up (Field, Field1),!. 
  field_number (straight, Field, Field1): - 
  field_below (Field, Field1),!. 
  % the same, but the fields near the angle of the battle "diagonally" 
  field_on order (obliquely, field, field1): - 
  left_field (Field, Field0), 
  field_below (Pole0, Pole1),!. 
  field_on order (obliquely, field, field1): - 
  right_field (Field, Field0), 
  field_below (Pole0, Pole1),!. 
  field_on order (obliquely, field, field1): - 
  left_field (Field, Field0), 
  field_up (Field0, Field1),!. 
  field_on order (obliquely, field, field1): - 
  right_field (Field, Field0), 
  field_up (Field0, Field1),!. 

 
  % enumeration of all fields on the battle line 
  field_in order (straight, left, Field, Field1): - 
  left_field (Field, Field1). 
  field_in order (straight, left, Field, Field1): - 
  left_field (Field, Field0), 
  field_number (straight, left, Field0, Field1). 
  field_in order (straight, right, Field, Field1): - 
  right_field (Field, Field1). 
  field_in order (straight, right, Field, Field1): - 
  right_field (Field, Field0), 
  field_field (straight, right, Field0, Field1). 
  field_in order (straight, up, Field, Field1): - 
  top_field (Field, Field1). 
  field_in order (straight, up, Field, Field1): - 
  top_field (Field, Field0), 
  field_number (straight, up, Field0, Field1). 
  field_in order (straight, down, Field, Field1): - 
  field_below (Field, Field1). 
  field_in order (straight, down, Field, Field1): - 
  field_below (Field, Field0), 
  field_field (straight, down, Field0, Field1). 

 
  % enumeration of all fields along the line of battle “diagonally” 
  field_in order (obliquely, down_left, field, field1): - 
  left_field (Field, Field0), 
  bottom field (Field0, Field1). 
  field_in order (obliquely, down_left, field, field1): - 
  left_field (Field, Field0), 
  field_below (Pole0, Pole2), 
  field_in order (obliquely, down_left, field2, field1). 
  field_in order (obliquely, down_right, Field, Field1): - 
  right_field (Field, Field0), 
  bottom field (Field0, Field1). 
  field_in order (obliquely, down_right, Field, Field1): - 
  right_field (Field, Field0), 
  field_below (Pole0, Pole2), 
  field_number (obliquely, down_right, Field2, Field1). 
  field_in order (obliquely, upward, left, field, field1): - 
  left_field (Field, Field0), 
  top_field (Field0, Field1). 
  field_in order (obliquely, upward, left, field, field1): - 
  left_field (Field, Field0), 
  top_field (Field0, Field2), 
  field_in order (obliquely, upward, left2, field2, field1). 
  field_in order (obliquely, upward_right, Field, Field1): - 
  right_field (Field, Field0), 
  top_field (Field0, Field1). 
  field_in order (obliquely, upward_right, Field, Field1): - 
  right_field (Field, Field0), 
  top_field (Field0, Field2), 
  field_in order (obliquely, upward_right, Field2, Field1). 

 
  % if the fields are on the battle line 
  field_line (straight, field, field1): - 
  field_number (straight, left, Field, Field1),!. 
  field_line (straight, field, field1): - 
  field_number (straight, right, Field, Field1),!. 
  field_line (straight, field, field1): - 
  field_number (straight, down, Field, Field1),!. 
  field_line (straight, field, field1): - 
  field_number (straight, up, Field, Field1),!. 
  field_line (obliquely, field, field1): - 
  field_in order (obliquely, upward_right, Field, Field1),!. 
  field_line (obliquely, field, field1): - 
  field_number (obliquely, upward left, Field, Field1),!. 
  field_line (obliquely, field, field1): - 
  field_number (obliquely, down_right, Field, Field1),!. 
  field_line (obliquely, field, field1): - 
  field_number (obliquely, down_left, Field, Field1),!. 

 
  field_con (For example, FieldIz, Field): - 
  % skipped% 
  ! 

 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  % the field selection mechanism itself 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  predicates 
  figure_bob2 (Field Of, Byot, Field) 
  figure_bob1 (Field Of, Byot, Field) 
  figure_break (FFF, Field) 
  Shapes_Non_Boy (FFF, Field) 
  nondeterm select_field_to_dock (Field) 
  put_figures1 (FFF) 
  put_figure (FFF) 

 
  clauses 
  % takes a cell on a board that has not yet been placed 
  % figure 
  select_field_to_doske (field (Number, Letter)): - 
  markup 1 (Numbers), 
  markup 2 (letters), 
  search_of_list_of_facts (Numbers, Number) 
  search_of_facts (Letters, Letter). 

 
  % determines whether two figures are worth fighting 
  % 
  % if this place is already taken 
  figure_patcher2 (Field, _, Field): - !. 
  figure_sub_boy2 (PoleIz, byot (next, for example), Field): - 
  % if the figure is near and in the direction of the battle 
  field_field (for example, fieldof, field), 
  ! 
  figure_for_boy2 (FieldIz, byot (line, for example), Field): - 
  % if the figure is on the battle line 
  line_field (eg, field from, field), 
  ! 
  figure_for_boy2 (FieldFrom, horse, Field): - 
  % if the figure is on the battlefield of a horse 
  field_ (horse, field, out), field) 
  ! 

 
  % figure under the battle that we put 
  figure_bob1 (FieldIz, Byot, Field): - 
  figure_boys2 (FieldFrom, Byot, Field), 
  ! 
  % figure under battle that already stands 
  figure_bob1 (FieldIz, Byot, Field): - 
  figure_boys2 (Field, Byot, FieldIz), 
  ! 

 
  % figure under fight 
  figure_break (Figure, Field): - 
  % search rules for the figure 
  rule (Figure, Byot), 
  % brute force on the board 
  worth (_, fieldis), 
  % if the Figure with the Rule of battle beats a given cell from its coordinates 
  % or vice versa 
  figure_fighting1 (FieldFrom, Byot, Field), 
  % then figure under fight 
  ! 
  % make sure that the figure is not under attack 
  shapes_non_placing (Figure, Field): - 
  not (figure_stop (Figure, Field)), 
  ! 
  % puts one piece on the board 
  put_figure (Figure): - 
  % takes any field 
  select_field_to_doske (Field), 
  % if the figure on this field is not beaten 
  % and will not beat another figure 
  Shapes (figure, field), 
  % then put it on the board 
  assertz (standing (Figure, Field)), 
  ! 
  % puts the pieces on the board 
  put_figures1 (Figure): - 
  put_figure (Figure), 
  put_figures1 (Figure), 
  ! 
  put_figures1 (Figure): - 
  % output option with the arrangement 
  nl, write ("option for", Figure), 
  worth (Figure, Field), 
  write ("", field), 
  fail. 

 
  put_figures (Figure): - 
  % initial preparation 
  % enumeration of all fields 
  select_field_to_doske (Field), 
  % clear knowledge 
  retractall (_, layouts), 
  % put the first figure 
  assertz (standing (Figure, Field)), 
  put_figures1 (Figure), 
  ! 
  / * call install predicate * / 
  Goal put_figures (queen). 
  % 

The prologue gives the placement options for the given piece (queen). This program is not implemented search for the horse. Having examined the search for other figures - you can easily implement it yourself.

See also

created: 2014-09-23
updated: 2024-11-14
307



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Presentation and use of knowledge

Terms: Presentation and use of knowledge