Darkrai1337

CheckChess

Nov 23rd, 2021 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.48 KB | None | 0 0
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7. var
  8.   figurePos: string;
  9.   xK,yK,xF,yF, figureIndex:integer;
  10.   figures: array[1..6] of string;
  11.  
  12. {function GetValidPos(typeF:string):string;
  13. procedure SetKingPos(position: string);
  14. Procedure SetFigurePos(position: string);
  15. procedure InitFigure();
  16. function ValidChoice():integer;
  17. function CheckAttack(xK,yK,xF,yF:integer; figure: string):boolean;  }
  18. procedure InitFigure();
  19. begin
  20. figures[1]:= 'Pawn';
  21. figures[2]:= 'Knight';
  22. figures[3]:= 'Rock';
  23. figures[4]:= 'Bishop';
  24. figures[5]:= 'Queen';
  25. figures[6]:= 'King';
  26. end;
  27.  
  28. function GetValidPos(typeF:string):string;
  29. begin
  30. while true do
  31.   begin
  32.   Write('Enter the ',typeF,''#39's position(etc. 5F, 4B):');
  33.   Readln(figurePos);
  34.   figurePos:=Trim(figurePos);
  35.   if (( Length(figurePos) = 2) and (ord(figurePos[1]) in [49..56]) and (ord(figurePos[2]) in [65..72])) then
  36.     break
  37.   end;
  38. Result:= figurePos;
  39. end;
  40.  
  41. function ValidChoice():integer;
  42. begin
  43. while true do
  44.   begin
  45.   Writeln('Select the number of opponent'#39's chess figure:');
  46.   Writeln('1.Pawn 2.Knight 3.Rock 4.Bishop 5.Queen 6.King');
  47.   Readln(figureIndex);
  48.   if figureIndex in [1..6] then
  49.     break
  50.   end;
  51.   Result:= figureIndex;
  52. end;
  53.  
  54. procedure SetKingPos(position: string);
  55. begin
  56. xK := ord(position[1])-ord('0');
  57. yK := ord(position[2])-ord('A') +1;
  58. end;
  59.  
  60. Procedure SetFigurePos(position: string);
  61. begin
  62. xF := ord(position[1])-ord('0');
  63. yF := ord(position[2])-ord('A') +1;
  64. end;
  65.  
  66. function CheckAttack(xK,yK,xF,yF:integer; var figure: string):boolean;
  67. begin
  68.  
  69.   if figure = 'Pawn' then
  70.     Result:= (abs(xK-xF) = 1) and (xF-xK = 1);
  71.   if figure = 'Knight' then
  72.     Result:= (((abs(xK-xF) = 2) and (abs(yK-yF) = 1)) xor ((abs(xK-xF) = 1) and (abs(yK-yF) = 2)));
  73.   if figure = 'Rock'then
  74.     Result:= ((xK-xF = 0) xor (yK-yF =0));
  75.   if figure = 'Bishop'then  //
  76.     Result:= (abs(xK-xF) = abs(yK-yF));
  77.   if figure = 'Queen'then
  78.     Result:= ((abs(xK-xF) = abs(yK-yF)) xor  ((xK-xF = 0) xor (yK-yF =0)));
  79.  if figure =  'King'then
  80.     Result:= (abs(xK-xF)<=1) or (abs(yK-yF)<=1);
  81.   end;
  82.  
  83.  
  84. begin
  85. InitFigure();
  86. Writeln('1A-1H - white side, 8A-8H - black side');
  87. SetKingPos(GetValidPos('future White ' + figures[6]));  //xK yK
  88. figureIndex := ValidChoice;
  89. SetFigurePos(GetValidPos('Black ' + figures[figureIndex]));  //xF yF
  90. if CheckAttack(xK, yK, xF, yF, figures[figureIndex]) then
  91. writeln('The move is impossible.')
  92. else
  93. writeln('The move is possible.');
  94. readln;
  95. end.
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
Add Comment
Please, Sign In to add comment