Board index » delphi » Simple programming help on game NIM

Simple programming help on game NIM

I'm creating a program (using only the basic pascal knowledge that i have)
which will replicate the game 'NIM' - i'm sure you've heard of it, if not an
explaination can be found here:http://home.pacbell.net/fransg/nim.htm

I've written the program however there seem to be various problems and i've
tried and tried to get it working but i can't!! At the moment when i run the
program a run-time error 202 message appears (215C:0381) and then it says on
the screen 'Stack overflow problem'

I'm not sure how to overcome this  - however i think (well, i HOPE!!) that my
program should work if this is solved.

Heres my program:

Program Nim;

USES crt;

CONST
     maxnum=8;

VAR
   num_array:array[1..maxnum]of integer;
   a,b,c,aa:array[1..maxnum]of integer;
   num,i,x,count,total,counter,choice2,a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4
:integer;
   pile,choice,key,again:char;

Procedure Welcome;
Begin
     clrscr;
     textcolor(yellow);
     writeln('**** WELCOME TO NIM ****');
     randomize;
End;

Procedure Pile_A; {randomize Pile A number}
     begin
     For i:=1 to maxnum do num_array [i]:=0;
          num:=random(maxnum+1);
          while num_array[num]<>0 do
          num:=random(maxnum+1);
          num_array[num]:=num;
          write('Pile A - ');
          write (num);
          write(' Stones');
          a[i]:=num;
     end;

Procedure Pile_B; {randomize Pile B number}
     Begin
     For i:=1 to maxnum do num_array [i]:=0;
          num:=random(maxnum+1);
          while num_array[num]<>0 do
          num:=random(maxnum+1);
          num_array[num]:=num;
          writeln;
          write('Pile B - ');
          write(num);
          write(' Stones');
          b[i]:=num;
      end;

Procedure Pile_C; {randomize Pile C number}
     Begin
     For i:=1 to maxnum do num_array [i]:=0;
          num:=random(maxnum+1);
          while num_array[num]<>0 do
          num:=random(maxnum+1);
          num_array[num]:=num;
          writeln;
          write('Pile C - ');
          write(num);
          write(' Stones');
          c[i]:=num;
     end;

Procedure You_Win;
Begin
writeln('You Win'); {Need to add opportunity to have another game}
End;

Procedure Which_Pile;FORWARD;Procedure ConvertA;FORWARD;
Procedure ConvertB;FORWARD;Procedure ConvertC;FORWARD;
Procedure Convert2A;FORWARD;Procedure TakenA;FORWARD;

Procedure Comp_TurnC; {Computer take away Stones from pile C}
Begin
writeln('C'); {Code needs to be written}
End;

Procedure Comp_TurnB; {Computer take away Stones from pile B}
Begin
writeln('B'); {Code needs to be written}
End;

Procedure Comp_TurnA; {Computer take away Stones from pile A}
Begin;
If ((a1=0)and(a2=0)and(a3=0)and(a4=0)and(counter=0))then Comp_TurnB;
If ((a1=0)and(a2=0)and(a3=0)and(a4=0)and(counter>0))then TakenA
Else Begin;
x:=0;Inc(x);
     If a1=1 then Begin;
     aa[i]:=a[i]-x;counter:=counter+1;
     Convert2A
     End;
     If a2=1 then Begin;
     aa[i]:=a[i]-x;counter:=counter+1;
     Convert2A
     End;
     If a3=1 then Begin;
     aa[i]:=a[i]-x;counter:=counter+1;
     Convert2A
     End;
     If a4=1 then Begin;
     aa[i]:=a[i]-x;counter:=counter+1;
     Convert2A
     End;
End;
End;

Procedure TakenA;
Begin
aa[i]:=a[i];
writeln('The computer has taken',counter,'Stones from Pile A');
Writeln('Pile A - ',a[i]);Writeln('Pile B - ',b[i]);Writeln('Pile C - ',c[i]);
Which_Pile
End;

Procedure Convert2A; {Convert a[x] to Binary}
Begin;
If aa[i]>=8 then Begin; a1:=1; aa[i]:=aa[i]-8; End;
If aa[i]>=4 then Begin; a2:=1; aa[i]:=aa[i]-4; End;
If aa[i]>=2 then Begin; a3:=1; aa[i]:=aa[i]-2; End;
If aa[i]=1 then a4:=1;
Comp_TurnA;
End;

Procedure ConvertA; {Convert a[i] to Binary}
Begin;
If a[i]>=8 then Begin; a1:=1; a[i]:=a[i]-8; End;
If a[i]>=4 then Begin; a2:=1; a[i]:=a[i]-4; End;
If a[i]>=2 then Begin; a3:=1; a[i]:=a[i]-2; End;
If a[i]=1 then a4:=1;
End;

Procedure ConvertB; {Convert B to Binary}
Begin;
If b[i]>=8 then Begin; b1:=1; b[i]:=b[i]-8; End;
If b[i]>=4 then Begin; b2:=1; b[i]:=b[i]-4; End;
If b[i]>=2 then Begin; b3:=1; b[i]:=b[i]-2; End;
If b[i]=1 then b4:=1;
End;

Procedure ConvertC; {Convert C to Binary}
Begin;
If c[i]>=8 then Begin; c1:=1; c[i]:=c[i]-8; End;
If c[i]>=4 then Begin; c2:=1; c[i]:=c[i]-4; End;
If c[i]>=2 then Begin; c3:=1; c[i]:=c[i]-2; End;
If c[i]=1 then c4:=1;
Comp_TurnA;
End;

Procedure Left; {Show Stones left in each pile}
Begin;
writeln('Stones left in each pile:');
writeln;
If (a[i]<=0) then writeln('Pile A - No Stones left') else writeln('Pile A -
',a[i],' Stones');
If (b[i]<=0) then writeln('Pile B - No Stones left') else writeln('Pile B -
',b[i],' Stones');
If (c[i]<=0) then writeln('Pile C - No Stones left') else writeln('Pile C -
',c[i],' Stones');
writeln;writeln('Computers Turn');
ConvertA;ConvertB;ConvertC;
End;

Procedure Take_A; {Take away Stones from pile A}
Begin
If (choice2>a[i]) or (choice2<1) then
   Begin
   writeln('Invalid Number');
   writeln('Press ENTER to restart');
   readln(key);
   welcome;pile_a;pile_b;pile_c;which_pile;
   End
Else a[i]:=a[i]-choice2;
Left;
End;

Procedure Take_B; {Take away Stones from pile B}
Begin
If (choice2>b[i]) or (choice2<1) then
   Begin
   writeln ('Invalid Number');
   writeln('Press ENTER to restart');
   readln(key);
   welcome;pile_a;pile_b;pile_c;which_pile;
   End
Else b[i]:=b[i]-choice2;
Left;
End;

Procedure Take_C; {Take away Stones from pile C}
Begin
If (choice2>c[i]) or (choice2<1) then
   Begin
   writeln ('Invalid Number');
   writeln('Press ENTER to restart');
   readln(key);
   welcome;pile_a;pile_b;pile_c;which_pile;
   End
Else c[i]:=c[i]-choice2;
Left;
End;

Procedure Which_Pile; {Human Turn - take away first set of Stones}
Begin
     textcolor(cyan);
     writeln;writeln;
     writeln('Enter letter of pile (A,B or C) to take Stones away from and
press ENTER');
     writeln;
     read(choice);
     writeln('Enter amount of Stones to take away and press ENTER');
     writeln;
     read(choice2);
        Case choice of
       'A','a':Take_A;
       'B','b':Take_B;
       'C','c':Take_C;
       end;
end;

Begin; {Main Program}
Welcome;
Pile_A;
Pile_B;
Pile_C;
Which_Pile;
End.

I hope someone can find a solution to my problem - i would be very grateful!!!
Thanks,
ERich

 

Re:Simple programming help on game NIM


hi,

"Emmarich1" <emmari...@cs.com> schrieb im Newsbeitrag
news:20010323104635.19862.00000557@ng-cd1.news.cs.com...

Quote
> I'm creating a program (using only the basic pascal knowledge that i
have)
> which will replicate the game 'NIM' - i'm sure you've heard of it, if
not an
> explaination can be found here:http://home.pacbell.net/fransg/nim.htm

> I've written the program however there seem to be various problems and
i've
> tried and tried to get it working but i can't!! At the moment when i
run the
> program a run-time error 202 message appears (215C:0381) and then it
says on
> the screen 'Stack overflow problem'

> I'm not sure how to overcome this  - however i think (well, i HOPE!!)
that my
> program should work if this is solved.

[..code..]

imo you haven't realized the way procedures work.
in your program you use them like goto's (basic) - they work a little
different.

Your Code ...

procedure a;
begin
  [...]
  b;
end;

procedure b;
begin
  [...]
  a;
end;

because of this you must use forward.

neither a nor b will ever end - this causes your stack-overflow.

you should redesign your code ...

here is some pseudo-code .. you don't need any forward ...

program nim;

procedure user;
begin
  repeat
    pile?
    amount ?
  until valid;
  take coins;
end;

procedure compu;
begin
  evaluate pile,amount
  take coins;
end;

begin
  hello;
  repeat
    repeat
      user;
      if not done compu;
    until done;
    winner;
    again?
  until not again
end.

hth
jochen

Other Threads