You should consider using some small arrays, and loops to scan
their content, rather than agonising over complicated if-statements
with a collection of separate variables.
So, somewhat as follows (you'll have to convert this to proper
pascal, with begin .. end, rather than "next i" for example).
const order = 4;
Var hand: array[1..order] of integer;
for i:=1:order do {Fill the hand.}
repeat {With a not previously-used digit.}
d:=random(10); {v:=Random(n) produces 0 <= v < n, not <= n}
until All(hand[1:i - 1] <> d);
hand[i]:=d;
next i;
But alas, Pascal does not offer an "All" or "Any" array-testing
construction whereby the test is applied to the specified range of
elements and the results combined.
for i:=1:order do
repeat
d:=Random(10);
new:=true; {Live in hope.}
for j:=1:i - 1 do if hand[j] = d then new:=false; next j;
until new;
hand[i]:=d;
next i;
Another way would be to prepare an array having a full set of
possible items, then as each item is selected from the array of
surviving possibilities, remove that item from the array. This is
better when the selection takes more than a few of the possibilities,
because the random generation of d will more and more often come up
with a value that has previously been selected and so that selection
must be discarded. The process of compacting the possibility array
(removing the selected item) is annoying in the absence of a "string"
organisation for integer arrays, but you could use character strings
where each symbol is one possibility. With characters, you must be
careful over the distinction between 1 and "1" even though when
printed, both forms manifest as 1.
Subsequent tests can be performed by suitable scanning loops,
rather than a tangle of compound if-statements that I presume is what
has dismayed you.
Onwards!
best wishes, Nicky.
--- In turbopascalprogramming2@yahoogroups.com, "Oya" <oya@b...> wrote:
>
> hello.i am new here.
> i have a homework. i'm using turbo pascal. and i'm trying to learn TP
> and make my homework.
> my homework is gueesing the random number which the computer keeps. i
> can get a random number which has 4 digits with this code:
>
>
> program randomnum;
> uses crt;
> var
> randomnumber:integer;
> k,l,m,n,i:integer;
> begin
> clrscr;
> while (1<>2) do
> begin
> k:=random(9)+1;
> l:=random(10);
> m:=random(10);
> n:=random(10);
> if ((k<>l) and (l<>m) and (m<>n)and (k<>m) and(k<>n)and(l<>n)) then
> begin
> i:=k*1000+l*100+m*10+n; break;
> end;
> end;
> randomsayi:=i;
> readkey;
> end.
>
>
> the player mustn't see this number and then the player has to start to
> guess. if 2 digits are true, my program must write +2,0.
> if some numbers are correct but not at the right digit,for example
> computer's number is 1234,and i wrote 6741, my program must write
> -2,0.because digits are not at the right place.or if random number is
> 1234 and i wrote 9231,it must write (+2,-1)
> some digits are true but 1 is not at it's right place.
> if all numbers and digits are wrong, it must write 0,and if every
> digits and numbers are true,it must write +4.
> have you sample codes about this program?
> sorry because of my english.
> thanks.
> oya the beatlefan
>