Schachdomino.dpr 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. program Schachdomino;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils;
  6. const BOARD_SIZE = 8;
  7. type TBoard = Array[1..BOARD_SIZE,1..BOARD_SIZE] of Integer;
  8. type TResult = record
  9. possible:Boolean;
  10. field:TBoard;
  11. tryCount:Integer;
  12. end;
  13. const
  14. sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF}
  15. {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};
  16. var LogFile:TextFile;
  17. // Write a board to console and log file
  18. procedure WriteBoard(field:TBoard);
  19. var
  20. S1: String;
  21. C1: Integer;
  22. C2: Integer;
  23. begin
  24. S1:='[';
  25. for C2 := 1 to BOARD_SIZE do begin
  26. S1:=S1 + sLineBreak;
  27. for C1 := 1 to BOARD_SIZE do begin
  28. S1:=S1 + Format('%2d',[field[C1,C2]])+ ', ';
  29. end;
  30. end;
  31. S1:=S1 + ']';
  32. Writeln(S1);
  33. Writeln(logFile,S1);
  34. end;
  35. // Write placing a brick at a given position and check if it is still possible
  36. // to fill the remaining free space of the board
  37. function tryBrick(posX,posY:Integer; field:TBoard; TryCount:Integer; BrickNum:Integer):TResult;
  38. var
  39. nextBrickX: Integer;
  40. nextBrickY: Integer;
  41. newField: TBoard;
  42. resultRight: TResult;
  43. resultBot: TResult;
  44. rightPossible: Boolean;
  45. botPossible: Boolean;
  46. begin
  47. if (posX=BOARD_SIZE) and (posY=BOARD_SIZE) then begin
  48. result.possible:=True;
  49. result.field:=field;
  50. result.tryCount:=TryCount+1;
  51. Exit;
  52. end;
  53. if posX = BOARD_SIZE then begin
  54. nextBrickX:=1;
  55. nextBrickY:=posY+1;
  56. end else begin
  57. nextBrickX:=posX+1;
  58. nextBrickY:=posY;
  59. end;
  60. Result.possible:=false;
  61. Result.tryCount:=TryCount;
  62. if field[posX,posY]<>0 then begin
  63. Result:=tryBrick(nextBrickX,nextBrickY,field,TryCount,BrickNum);
  64. Exit;
  65. end else begin
  66. //rechts
  67. rightPossible:=((posX+2<=BOARD_SIZE-1) or ((posY<=BOARD_SIZE-1) and (posX+2<=BOARD_SIZE)))
  68. and (field[posX+1,posY]=0)
  69. and (field[posX+2,posY]=0);
  70. if rightPossible then begin
  71. newField:=field;
  72. newField[posX,posY]:=BrickNum;
  73. newField[posX+1,posY]:=BrickNum;
  74. newField[posX+2,posY]:=BrickNum;
  75. resultRight:=tryBrick(nextBrickX,nextBrickY,newField,Result.tryCount,BrickNum+1);
  76. Result.tryCount:=resultRight.tryCount;
  77. if resultRight.possible then begin
  78. Result:=resultRight;
  79. Exit;
  80. end
  81. end;
  82. //unten
  83. botPossible:=((posY+2<=BOARD_SIZE-1) or ((posX<=BOARD_SIZE-1) and (posY+2<=BOARD_SIZE)))
  84. {and (field[posX,posY+1]=0) //always true
  85. and (field[posX,posY+2]=0)};
  86. if botPossible then begin
  87. newField:=field;
  88. newField[posX,posY]:=BrickNum;
  89. newField[posX,posY+1]:=BrickNum;
  90. newField[posX,posY+2]:=BrickNum;
  91. resultBot:=tryBrick(nextBrickX,nextBrickY,newField,Result.tryCount,BrickNum+1);
  92. Result.tryCount:=resultBot.tryCount;
  93. Result.possible:=resultBot.possible;
  94. if resultBot.possible then begin
  95. Result:=resultBot;
  96. Exit;
  97. end
  98. end;
  99. end;
  100. if not rightPossible and not botPossible then begin
  101. result.tryCount:=result.tryCount+1;
  102. Writeln('Try No. ' + IntToStr(result.TryCount) + ':');
  103. Writeln(logFile, 'Try No. ' + IntToStr(result.TryCount) + ':');
  104. WriteBoard(field);
  105. end;
  106. end;
  107. // Write a result to console and log file
  108. procedure WriteResult(result:TResult);
  109. var
  110. C1: Integer;
  111. C2: Integer;
  112. S1: string;
  113. begin
  114. if result.possible then begin
  115. Writeln('It is possible to fill this board.');
  116. Writeln(LogFile, 'It is possible to fill this board.');
  117. Writeln('This is the result:');
  118. Writeln(LogFile, 'This is the result:');
  119. WriteBoard(result.field);
  120. end else begin
  121. Writeln('It is not possible to fill this board.');
  122. Writeln(LogFile, 'It is not possible to fill this board.');
  123. end;
  124. Writeln('Program finished after '+IntToStr(result.tryCount) + ' tries. A log has been written to log.txt');
  125. Writeln(LogFile, 'Program finished after '+IntToStr(result.tryCount) + ' tries. A log has been written to log.txt');
  126. ReadLn;
  127. end;
  128. // main program
  129. var
  130. Field: TBoard;
  131. result:TResult;
  132. begin
  133. AssignFile(logFile, 'log.txt');
  134. ReWrite(logFile);
  135. result := tryBrick(1,1,Field,0,1);
  136. WriteResult(result);
  137. CloseFile(logFile);
  138. end.