summaryrefslogtreecommitdiff
path: root/demo/text/blackbox.pp
blob: 7fdb886d6d255c3907afb0c1ccbb53fc4e8061da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1993-98 by Michael Van Canneyt

    Blackbox Game Example

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
Program blackbox;

{
  The object of the game is simple : You have a box of 9x9x9 cells.
  you can enter a number of atoms that will be put in the box.
  Then you can start shooting in the box with a laser beam.
  You enter the coordinates where the beam enters the box.
  (this must be on the edges, this means that one of the coordinates
  must be 1 or 9...)
  The beam will bounce off the atoms (using normal bouncing), and you
  will be told where the beam exits.
  From this you must guess where the atoms are...
}

Const MaxSize = 9;
      MaxAtom = 10;

Type TRow   = Array [0..MaxSize+1] of byte;
     TPlane = Array [0..MaxSize+1] of TRow;
     TCube  = Array [0..MaxSize+1] of TPlane;

Var
  Cube                 : TCube;
  Count,Guessed,x,y,z  : Longint;
  ans : string;

Procedure FillCube;

var i,x,y,z : longint;

begin
  randomize;
  for x:=0 to maxsize+1 do
    for y:=0 to maxsize+1 do
      for z:=0 to maxsize+1 do
        Cube[x,y,z]:=0;
  repeat
    Write ('Enter number of atoms (1-',maxatom,') : ');
    readln (count);
    if (count<1) or (count>MaxAtom) then
      writeln ('Invalid value entered. Please try again.');
  until (count>0) and (count<=MaxAtom);
  for I:=1 to count do
     begin
     repeat
       x:=Random(MaxSize)+1;
       y:=Random(MaxSize)+1;
       z:=Random(MaxSize)+1;
     until Cube[x,y,z]=0;
     Cube[x,y,z]:=1;
     end;
end;

Procedure GetCoords (Var X,y,z : longint);

begin
  Write ('X : ');
  readln (x);
  write ('Y : ');
  readln (y);
  write ('z : ');
  readln (z);
end;

Procedure GetStart (Var x,y,z : longint);

Var OK : boolean;

begin
  Writeln ('Please enter beam start coordinates : ');
  Repeat
    GetCoords (x,y,z);
    OK:=((X=1) or (X=MaxSize)) and ((y=1) or (Y=MaxSize)) and
        ((Z=1) or (z=maxsize));
    if Not OK then
      writeln ('The beam should enter at an edge. Please try again');
  until OK;
end;

Function GetGuess : boolean;

Var OK : boolean;
    x,y,z : longint;

begin
  Writeln ('Please enter atom coordinates : ');
  Repeat
    getcoords (x,y,z);
    OK:=((X>=1) and (X<=MaxSize)) and ((y>=1) and (Y<=MaxSize)) and
        ((Z>=1) and (z<=maxsize));
    if Not OK then
      writeln ('These are not valid coordinates. Please try again');
  until OK;
  GetGuess:=False;
  If Cube[x,y,z]<0 then
    Writeln ('You already had this one ! Trying to be clever, eh ?')
  else if Cube[x,y,z]>0 then
    begin
    Writeln ('Correct guess !');
    Cube[x,y,z]:=-Cube[x,y,z];
    getguess:=true;
    end
  else
    Writeln ('Wrong guess !');
end;

Procedure CalcExit (X,Y,Z : longint);

var tx,ty,tz,dx,dy,dz : longint;

begin
  dx:=0;dy:=0;dz:=0;
  if x=1 then dx:=1 else if x=MaxSize then dx:=-1;
  if y=1 then dy:=1 else if y=MaxSize then dy:=-1;
  if z=1 then dz:=1 else if z=MaxSize then dz:=-1;
  writeln ('Direction : ',dx,',',dy,',',dz);
  repeat
  for tx:=-1 to 1 do
    for ty:=-1 to 1 do
      for tz:=-1 to 1 do
        if Cube [X+tx,y+ty,z+tz]<>0 then
          begin
          dx:=dx-tx;
          dy:=dy-ty;
          dz:=dz-tz;
          end;
  if dx<>0 then dx:=dx div abs(dx);
  if dz<>0 then dz:=dz div abs(dz);
  if dy<>0 then dy:=dy div abs(dy);
  x:=x+dx;y:=y+dy;z:=z+dz;
  until ((x=0) or (x=MaxSize+1)) or ((y=0) or (y=maxsize+1)) or
        ((z=0) or (z=maxsize+1));
  Writeln ('Beam exited at : (',x-dx,',',y-dy,',',z-dz,')');
end;

{
Procedure DumpCube ;

Var x,y,z : longint;

begin
  for x:=1 to MaxSize do
    for y:=1 to maxsize do
      for z:=1 to maxsize do
        if Cube[x,y,z]<>0 then
          writeln ('Atom at (',x,',',y,',',z,')');
end;
}

begin
  FillCube;
  Guessed:=0;
  Repeat
    repeat
      Write ('Shoot, guess or quit (s/g/q) : ');
      readln (ans);
      ans[1]:=Upcase(ans[1]);
      if not (ans[1] in ['S','G','Q']) then
        writeln ('Invalid entry. Please try again.');
    until ans[1] in ['S','G','Q'];
    Case ans[1] of
     'S' : begin
           getstart (x,y,z);
           calcexit (x,y,z);
           end;
     'G' : If GetGuess then Inc(Guessed);
    end;
  until (ans[1]='Q') or (guessed=count);
  If Guessed=count then
    Writeln ('Congratulations! All ',Count,' correct !')
  else
    Writeln ('Only ',guessed,' out of ',count,' correct...');
end.