blackbox.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. Program blackbox;
  2. {
  3. (c) 1998 Michael Van Canneyt
  4. The object of the game is simple : You have a box of 9x9x9 cells.
  5. you can enter a number of atoms that will be put in the box.
  6. Then you can start shooting in the box with a laser beam.
  7. You enter the coordinates where the beam enters the box.
  8. (this must be on the edges, this means that one of the coordinates
  9. must be 1 or 9...)
  10. The beam will bounce off the atoms (using normal bouncing), and you
  11. will be told where the beam exits.
  12. From this you must guess where the atoms are...
  13. }
  14. Const MaxSize = 9;
  15. MaxAtom = 10;
  16. Type TRow = Array [0..MaxSize+1] of byte;
  17. TPlane = Array [0..MaxSize+1] of TRow;
  18. TCube = Array [0..MaxSize+1] of TPlane;
  19. Var
  20. Cube : TCube;
  21. Count,Guessed,x,y,z : Longint;
  22. ans : string;
  23. Procedure FillCube;
  24. var i,x,y,z : longint;
  25. begin
  26. randomize;
  27. for x:=0 to maxsize+1 do
  28. for y:=0 to maxsize+1 do
  29. for z:=0 to maxsize+1 do
  30. Cube[x,y,z]:=0;
  31. repeat
  32. Write ('Enter number of atoms (1-',maxatom,') : ');
  33. readln (count);
  34. if (count<1) or (count>MaxAtom) then
  35. writeln ('Invalid value entered. Please try again.');
  36. until (count>0) and (count<=MaxAtom);
  37. for I:=1 to count do
  38. begin
  39. repeat
  40. x:=Random(MaxSize)+1;
  41. y:=Random(MaxSize)+1;
  42. z:=Random(MaxSize)+1;
  43. until Cube[x,y,z]=0;
  44. Cube[x,y,z]:=1;
  45. end;
  46. end;
  47. Procedure GetCoords (Var X,y,z : longint);
  48. begin
  49. Write ('X : ');
  50. readln (x);
  51. write ('Y : ');
  52. readln (y);
  53. write ('z : ');
  54. readln (z);
  55. end;
  56. Procedure GetStart (Var x,y,z : longint);
  57. Var OK : boolean;
  58. begin
  59. Writeln ('Please enter beam start coordinates : ');
  60. Repeat
  61. GetCoords (x,y,z);
  62. OK:=((X=1) or (X=MaxSize)) or ((y=1) or (Y=MaxSize)) or
  63. ((Z=1) or (z=maxsize));
  64. if Not OK then
  65. writeln ('The beam should enter at an edge. Please try again');
  66. until OK;
  67. end;
  68. Function GetGuess : boolean;
  69. Var OK : boolean;
  70. x,y,z : longint;
  71. begin
  72. Writeln ('Please enter atom coordinates : ');
  73. Repeat
  74. getcoords (x,y,z);
  75. OK:=((X>=1) or (X<=MaxSize)) or ((y>=1) or (Y<=MaxSize)) or
  76. ((Z>=1) or (z<=maxsize));
  77. if Not OK then
  78. writeln ('These are not valid coordinates. Please try again');
  79. until OK;
  80. GetGuess:=False;
  81. If Cube[x,y,z]<0 then
  82. Writeln ('You already had this one ! Trying to be clever, eh ?')
  83. else if Cube[x,y,z]>0 then
  84. begin
  85. Writeln ('Correct guess !');
  86. Cube[x,y,z]:=-Cube[x,y,z];
  87. getguess:=true;
  88. end
  89. else
  90. Writeln ('Wrong guess !');
  91. end;
  92. Procedure CalcExit (X,Y,Z : longint);
  93. var tx,ty,tz,dx,dy,dz : longint;
  94. begin
  95. dx:=0;dy:=0;dz:=0;
  96. if x=1 then dx:=1 else if x=MaxSize then dx:=-1;
  97. if y=1 then dy:=1 else if y=MaxSize then dy:=-1;
  98. if z=1 then dz:=1 else if z=MaxSize then dz:=-1;
  99. writeln ('Direction : ',dx,',',dy,',',dz);
  100. repeat
  101. for tx:=-1 to 1 do
  102. for ty:=-1 to 1 do
  103. for tz:=-1 to 1 do
  104. if Cube [X+tx,y+ty,z+tz]<>0 then
  105. begin
  106. dx:=dx-tx;
  107. dy:=dy-ty;
  108. dz:=dz-tz;
  109. end;
  110. if dx<>0 then dx:=dx div abs(dx);
  111. if dz<>0 then dz:=dz div abs(dz);
  112. if dy<>0 then dy:=dy div abs(dy);
  113. x:=x+dx;y:=y+dy;z:=z+dz;
  114. until ((x=0) or (x=MaxSize+1)) or ((y=0) or (y=maxsize+1)) or
  115. ((z=0) or (z=maxsize+1));
  116. Writeln ('Beam exited at : (',x-dx,',',y-dy,',',z-dz,')');
  117. end;
  118. Procedure DumpCube ;
  119. Var x,y,z : longint;
  120. begin
  121. for x:=1 to MaxSize do
  122. for y:=1 to maxsize do
  123. for z:=1 to maxsize do
  124. if Cube[x,y,z]<>0 then
  125. writeln ('Atom at (',x,',',y,',',z,')');
  126. end;
  127. begin
  128. FillCube;
  129. Guessed:=0;
  130. Repeat
  131. repeat
  132. Write ('Shoot, guess or quit (s/g/q) : ');
  133. readln (ans);
  134. ans[1]:=Upcase(ans[1]);
  135. if not (ans[1] in ['S','G','Q']) then
  136. writeln ('Invalid entry. Please try again.');
  137. until ans[1] in ['S','G','Q'];
  138. Case ans[1] of
  139. 'S' : begin
  140. getstart (x,y,z);
  141. calcexit (x,y,z);
  142. end;
  143. 'G' : If GetGuess then Inc(Guessed);
  144. end;
  145. until (ans[1]='Q') or (guessed=count);
  146. If Guessed=count then
  147. Writeln ('Congratulations! All ',Count,' correct !')
  148. else
  149. Writeln ('Only ',guessed,' out of ',count,' correct...');
  150. end.