blackbox.pp 4.6 KB

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