blackbox.pp 4.7 KB

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